001: /*
002:
003: Derby - Class org.apache.derby.impl.tools.ij.ConnectionEnv
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.tools.ij;
023:
024: import java.io.File;
025: import java.io.FileNotFoundException;
026:
027: import java.util.Hashtable;
028: import java.util.Enumeration;
029: import java.util.Properties;
030:
031: import java.sql.Connection;
032: import java.sql.DriverManager;
033: import java.sql.SQLException;
034:
035: import org.apache.derby.tools.JDBCDisplayUtil;
036: import org.apache.derby.iapi.tools.i18n.LocalizedOutput;
037:
038: /**
039: To enable multi-user use of ij.Main2
040:
041: @author jerry
042: */
043: class ConnectionEnv {
044: Hashtable sessions = new Hashtable();
045: private Session currSession;
046: private String tag;
047: private boolean only;
048: private static final String CONNECTION_PROPERTY = "ij.connection";
049: private String protocol;
050:
051: ConnectionEnv(int userNumber, boolean printUserNumber,
052: boolean theOnly) {
053: if (printUserNumber)
054: tag = "(" + (userNumber + 1) + ")";
055: only = theOnly;
056: }
057:
058: /**
059: separate from the constructor so that connection
060: failure does not prevent object creation.
061: */
062: void init(LocalizedOutput out) throws SQLException,
063: ClassNotFoundException, InstantiationException,
064: IllegalAccessException {
065:
066: Connection c = util.startJBMS(null, null);
067:
068: // only load up ij.connection.* properties if there is
069: // only one ConnectionEnv in the system.
070: if (only) {
071: Properties p = System.getProperties();
072: protocol = p.getProperty(ij.PROTOCOL_PROPERTY);
073:
074: String prefix = CONNECTION_PROPERTY + ".";
075: for (Enumeration e = p.propertyNames(); e.hasMoreElements();) {
076: String key = (String) e.nextElement();
077: if (key.startsWith(prefix)) {
078: String name = key.substring(prefix.length());
079: installConnection(name
080: .toUpperCase(java.util.Locale.ENGLISH), p
081: .getProperty(key), out);
082: }
083: }
084: }
085:
086: if (c != null) // have a database from the startup?
087: {
088: String sname = Session.DEFAULT_NAME + sessions.size();
089: Session s = new Session(c, tag, sname);
090: sessions.put(sname, s);
091: currSession = s;
092: }
093:
094: }
095:
096: void doPrompt(boolean newStatement, LocalizedOutput out) {
097: if (currSession != null)
098: currSession
099: .doPrompt(newStatement, out, sessions.size() > 1);
100: else
101: utilMain.doPrompt(newStatement, out, tag);
102: }
103:
104: Connection getConnection() {
105: if (currSession == null)
106: return null;
107: return currSession.getConnection();
108: }
109:
110: /**
111: Making a new connection, add it to the pool, and make it current.
112: */
113: void addSession(Connection conn, String name) {
114: String aName;
115: if (name == null)
116: aName = getUniqueConnectionName();
117: else
118: aName = name;
119: Session s = new Session(conn, tag, aName);
120: sessions.put(aName, s);
121: currSession = s;
122: }
123:
124: //returns a unique Connection# name by going through existing sessions
125: public String getUniqueConnectionName() {
126: int newNum = 0;
127: boolean newConnectionNameOk = false;
128: String newConnectionName = "";
129: Enumeration e;
130: while (!newConnectionNameOk) {
131: newConnectionName = Session.DEFAULT_NAME + newNum;
132: newConnectionNameOk = true;
133: e = sessions.keys();
134: while (e.hasMoreElements() && newConnectionNameOk) {
135: if (((String) e.nextElement())
136: .equals(newConnectionName))
137: newConnectionNameOk = false;
138: }
139: newNum = newNum + 1;
140: }
141: return newConnectionName;
142: }
143:
144: Session getSession() {
145: return currSession;
146: }
147:
148: Hashtable getSessions() {
149: return sessions;
150: }
151:
152: Session setCurrentSession(String name) {
153: currSession = (Session) sessions.get(name);
154: return currSession;
155: }
156:
157: boolean haveSession(String name) {
158: return (name != null) && (sessions.size() > 0)
159: && (null != sessions.get(name));
160: }
161:
162: void removeCurrentSession() throws SQLException {
163: if (currSession == null)
164: return;
165: sessions.remove(currSession.getName());
166: currSession.close();
167: currSession = null;
168: }
169:
170: void removeSession(String name) throws SQLException {
171: Session s = (Session) sessions.remove(name);
172: s.close();
173: if (currSession == s)
174: currSession = null;
175: }
176:
177: void removeAllSessions() throws SQLException {
178: if (sessions == null || sessions.size() == 0)
179: return;
180: else
181: for (Enumeration e = sessions.keys(); e.hasMoreElements();) {
182: String n = (String) e.nextElement();
183: removeSession(n);
184: }
185: }
186:
187: private void installConnection(String name, String value,
188: LocalizedOutput out) throws SQLException {
189: // add protocol if no driver matches url
190: boolean noDriver = false;
191: try {
192: // if we have a full URL, make sure it's loaded first
193: try {
194: if (value.startsWith("jdbc:"))
195: util.loadDriverIfKnown(value);
196: } catch (Exception e) {
197: // want to continue with the attempt
198: }
199: DriverManager.getDriver(value);
200: } catch (SQLException se) {
201: noDriver = true;
202: }
203: if (noDriver && (protocol != null)) {
204: value = protocol + value;
205: }
206:
207: if (sessions.get(name) != null) {
208: throw ijException.alreadyHaveConnectionNamed(name);
209: }
210: try {
211:
212: String user = util.getSystemProperty("ij.user");
213: String password = util.getSystemProperty("ij.password");
214: Properties connInfo = util.updateConnInfo(user, password,
215: null);
216:
217: Connection theConnection = DriverManager.getConnection(
218: value, connInfo);
219:
220: addSession(theConnection, name);
221: } catch (Throwable t) {
222: JDBCDisplayUtil.ShowException(out, t);
223: }
224: }
225:
226: }
|