1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.tools.ij.ConnectionEnv |
4 | |
5 | Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.impl.tools.ij; |
22 | |
23 | import java.io.File; |
24 | import java.io.FileNotFoundException; |
25 | |
26 | import java.util.Hashtable; |
27 | import java.util.Enumeration; |
28 | import java.util.Properties; |
29 | |
30 | import java.sql.Connection; |
31 | import java.sql.DriverManager; |
32 | import java.sql.SQLException; |
33 | |
34 | import org.apache.derby.tools.JDBCDisplayUtil; |
35 | import org.apache.derby.iapi.tools.i18n.LocalizedOutput; |
36 | |
37 | /** |
38 | To enable multi-user use of ij.Main2 |
39 | |
40 | @author jerry |
41 | */ |
42 | class ConnectionEnv { |
43 | Hashtable sessions = new Hashtable(); |
44 | private Session currSession; |
45 | private String tag; |
46 | private boolean only; |
47 | private static final String CONNECTION_PROPERTY = "ij.connection"; |
48 | private String protocol; |
49 | |
50 | ConnectionEnv(int userNumber, boolean printUserNumber, boolean theOnly) { |
51 | if (printUserNumber) |
52 | tag = "("+(userNumber+1)+")"; |
53 | only = theOnly; |
54 | } |
55 | |
56 | /** |
57 | separate from the constructor so that connection |
58 | failure does not prevent object creation. |
59 | */ |
60 | void init(LocalizedOutput out) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException { |
61 | |
62 | Connection c = util.startJBMS(null,null); |
63 | |
64 | // only load up ij.connection.* properties if there is |
65 | // only one ConnectionEnv in the system. |
66 | if (only) { |
67 | Properties p = System.getProperties(); |
68 | protocol = p.getProperty(ij.PROTOCOL_PROPERTY); |
69 | |
70 | String prefix = CONNECTION_PROPERTY + "."; |
71 | for (Enumeration e = p.propertyNames(); e.hasMoreElements(); ) |
72 | { |
73 | String key = (String)e.nextElement(); |
74 | if (key.startsWith(prefix)) { |
75 | String name = key.substring(prefix.length()); |
76 | installConnection(name.toUpperCase(java.util.Locale.ENGLISH), |
77 | p.getProperty(key), out); |
78 | } |
79 | } |
80 | } |
81 | |
82 | if (c!=null) // have a database from the startup? |
83 | { |
84 | String sname=Session.DEFAULT_NAME+sessions.size(); |
85 | Session s = new Session(c,tag,sname); |
86 | sessions.put(sname,s); |
87 | currSession = s; |
88 | } |
89 | |
90 | } |
91 | |
92 | void doPrompt(boolean newStatement, LocalizedOutput out) { |
93 | if (currSession != null) currSession.doPrompt(newStatement, out, sessions.size()>1); |
94 | else utilMain.doPrompt(newStatement, out, tag); |
95 | } |
96 | |
97 | Connection getConnection() { |
98 | if (currSession == null) return null; |
99 | return currSession.getConnection(); |
100 | } |
101 | |
102 | /** |
103 | Making a new connection, add it to the pool, and make it current. |
104 | */ |
105 | void addSession(Connection conn,String name) { |
106 | String aName; |
107 | if (name == null) aName = getUniqueConnectionName(); |
108 | else aName = name; |
109 | Session s = new Session(conn, tag, aName); |
110 | sessions.put(aName, s); |
111 | currSession = s; |
112 | } |
113 | |
114 | //returns a unique Connection# name by going through existing sessions |
115 | public String getUniqueConnectionName() { |
116 | int newNum = 0; |
117 | boolean newConnectionNameOk = false; |
118 | String newConnectionName = ""; |
119 | Enumeration e; |
120 | while (!newConnectionNameOk){ |
121 | newConnectionName = Session.DEFAULT_NAME + newNum; |
122 | newConnectionNameOk = true; |
123 | e = sessions.keys(); |
124 | while (e.hasMoreElements() && newConnectionNameOk){ |
125 | if (((String)e.nextElement()).equals(newConnectionName)) |
126 | newConnectionNameOk = false; |
127 | } |
128 | newNum = newNum + 1; |
129 | } |
130 | return newConnectionName; |
131 | } |
132 | |
133 | Session getSession() { |
134 | return currSession; |
135 | } |
136 | |
137 | Hashtable getSessions() { |
138 | return sessions; |
139 | } |
140 | |
141 | Session setCurrentSession(String name) { |
142 | currSession = (Session) sessions.get(name); |
143 | return currSession; |
144 | } |
145 | |
146 | boolean haveSession(String name) { |
147 | return (name != null) && (sessions.size()>0) && (null != sessions.get(name)); |
148 | } |
149 | |
150 | void removeCurrentSession() throws SQLException { |
151 | if (currSession ==null) return; |
152 | sessions.remove(currSession.getName()); |
153 | currSession.close(); |
154 | currSession = null; |
155 | } |
156 | |
157 | void removeSession(String name) throws SQLException { |
158 | Session s = (Session) sessions.remove(name); |
159 | s.close(); |
160 | if (currSession == s) |
161 | currSession = null; |
162 | } |
163 | |
164 | void removeAllSessions() throws SQLException { |
165 | if (sessions == null || sessions.size() == 0) |
166 | return; |
167 | else |
168 | for (Enumeration e = sessions.keys(); e.hasMoreElements(); ) { |
169 | String n = (String)e.nextElement(); |
170 | removeSession(n); |
171 | } |
172 | } |
173 | |
174 | private void installConnection(String name, String value, LocalizedOutput out) throws SQLException { |
175 | // add protocol if no driver matches url |
176 | boolean noDriver = false; |
177 | try { |
178 | // if we have a full URL, make sure it's loaded first |
179 | try { |
180 | if (value.startsWith("jdbc:")) |
181 | util.loadDriverIfKnown(value); |
182 | } catch (Exception e) { |
183 | // want to continue with the attempt |
184 | } |
185 | DriverManager.getDriver(value); |
186 | } catch (SQLException se) { |
187 | noDriver = true; |
188 | } |
189 | if (noDriver && (protocol != null)) { |
190 | value = protocol + value; |
191 | } |
192 | |
193 | if (sessions.get(name) != null) { |
194 | throw ijException.alreadyHaveConnectionNamed(name); |
195 | } |
196 | try { |
197 | |
198 | String user = util.getSystemProperty("ij.user"); |
199 | String password = util.getSystemProperty("ij.password"); |
200 | Properties connInfo = util.updateConnInfo(user, password,null); |
201 | |
202 | Connection theConnection = |
203 | DriverManager.getConnection(value, connInfo); |
204 | |
205 | addSession(theConnection,name); |
206 | } catch (Throwable t) { |
207 | JDBCDisplayUtil.ShowException(out,t); |
208 | } |
209 | } |
210 | |
211 | } |