1 package net.argius.stew;
3 import static net.argius.stew.Bootstrap.getSystemFile;
8 import net.argius.stew.ui.*;
13 public final class Environment {
15 static final String ALIAS_PROPERTIES_NAME = "alias.properties";
17 private static final Logger log = Logger.getLogger(Environment.class);
18 static ResourceManager res = ResourceManager.getInstance(Environment.class);
20 private OutputProcessor outputProcessor;
21 private ConnectorMap connectorMap;
22 private Connector connector;
23 private Connection conn;
25 private int timeoutSeconds;
26 private File currentDirectory;
27 private long connectorTimestamp;
28 private AliasMap aliasMap;
33 public Environment() {
34 initializeQueryTimeout();
36 this.connectorMap = new ConnectorMap();
39 this.currentDirectory = getInitialCurrentDirectory();
41 final File aliasPropFile = getSystemFile(ALIAS_PROPERTIES_NAME);
42 this.aliasMap = new AliasMap(aliasPropFile);
43 if (aliasPropFile.exists()) {
46 } catch (IOException ex) {
53 * A constructor (for copy).
56 public Environment(Environment src) {
57 // never copy coconnector,conn,op into this
58 this.connectorMap = new ConnectorMap(src.connectorMap);
59 this.timeoutSeconds = src.timeoutSeconds;
60 this.currentDirectory = src.currentDirectory;
64 * Releases resouces it keeps.
66 public void release() {
69 log.debug("released connection");
70 } catch (SQLException ex) {
71 log.error(ex, "release error");
73 outputProcessor = null;
77 currentDirectory = null;
79 log.debug("released internal state of Environment");
83 * Establishes a connection.
85 * @throws SQLException
87 void establishConnection(Connector connector) throws SQLException {
88 Connection conn = connector.getConnection();
90 if (connector.isReadOnly()) {
91 conn.setReadOnly(true);
93 } catch (RuntimeException ex) {
96 boolean isAutoCommitAvailable;
98 conn.setAutoCommit(false);
99 isAutoCommitAvailable = conn.getAutoCommit();
100 } catch (RuntimeException ex) {
102 isAutoCommitAvailable = false;
104 if (isAutoCommitAvailable) {
105 outputMessage("w.auto-commit-not-available");
107 setCurrentConnection(conn);
108 setCurrentConnector(connector);
109 outputMessage("i.connected");
110 log.debug("connected %s (conn=%08x, env=%08x)", connector.getId(), conn.hashCode(), hashCode());
111 if (Bootstrap.getPropertyAsBoolean("net.argius.stew.print-connected-time")) {
112 outputMessage("i.now", System.currentTimeMillis());
117 * Releases the connection.
118 * @throws SQLException
120 void releaseConnection() throws SQLException {
122 log.debug("not connected");
126 if (connector != null && connector.usesAutoRollback()) {
129 outputMessage("i.rollbacked");
130 log.debug("rollbacked %s (%s)", connector.getId(), conn);
131 } catch (SQLException ex) {
133 Writer w = new StringWriter();
134 PrintWriter out = new PrintWriter(w);
135 ex.printStackTrace(out);
136 outputMessage("e.database", w);
137 outputMessage("w.error-occurred-on-auto-rollback", ex);
142 log.debug("disconnected %s (conn=%08x, env=%08x)", connector.getId(), conn.hashCode(), hashCode());
143 if (Bootstrap.getPropertyAsBoolean("net.argius.stew.print-disconnected-time")) {
144 outputMessage("i.now", System.currentTimeMillis());
146 } catch (SQLException ex) {
156 private void outputMessage(String id, Object... args) throws CommandException {
157 if (outputProcessor != null) {
158 outputProcessor.output(res.get(id, args));
162 private static File getInitialCurrentDirectory() {
163 final String propkey = "net.argius.stew.directory";
164 if (Bootstrap.hasProperty(propkey)) {
165 File directory = new File(Bootstrap.getProperty(propkey, ""));
166 if (directory.isDirectory()) {
170 return new File(".");
173 private void initializeQueryTimeout() {
174 this.timeoutSeconds = Bootstrap.getPropertyAsInt("net.argius.stew.query.timeout", -1);
175 if (log.isDebugEnabled()) {
176 log.debug("timeout: " + this.timeoutSeconds);
181 * Loads and refreshes connector map.
183 public void loadConnectorMap() {
186 m = ConnectorConfiguration.load();
187 } catch (IOException ex) {
188 m = new ConnectorMap();
190 synchronized (connectorMap) {
191 if (connectorMap.size() > 0) {
192 connectorMap.clear();
194 connectorMap.putAll(m);
195 connectorTimestamp = ConnectorConfiguration.lastModified();
200 * Updates connector map.
201 * When file was updated, it calls loadConnectorMap().
202 * @return whether updated or not
204 public boolean updateConnectorMap() {
205 if (ConnectorConfiguration.lastModified() > connectorTimestamp) {
212 public OutputProcessor getOutputProcessor() {
213 return outputProcessor;
216 public void setOutputProcessor(OutputProcessor outputProcessor) {
217 this.outputProcessor = outputProcessor;
220 public ConnectorMap getConnectorMap() {
224 public Connector getCurrentConnector() {
228 void setCurrentConnector(Connector connector) {
229 this.connector = connector;
232 public Connection getCurrentConnection() {
236 void setCurrentConnection(Connection conn) {
240 public int getTimeoutSeconds() {
241 return timeoutSeconds;
244 public File getCurrentDirectory() {
245 return currentDirectory;
248 public void setCurrentDirectory(File currentDirectory) {
249 this.currentDirectory = currentDirectory;
253 * @return system directory
254 * @deprecated use Bootstrap.getSystemDirectory() instead
257 public File getSystemDirectory() {
258 return Bootstrap.getSystemDirectory();
261 public AliasMap getAliasMap() {