KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > tools > ij > ConnectionEnv


1 /*
2
3    Derby - Class org.apache.derby.impl.tools.ij.ConnectionEnv
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.tools.ij;
23
24 import java.io.File JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26
27 import java.util.Hashtable JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import java.sql.Connection JavaDoc;
32 import java.sql.DriverManager JavaDoc;
33 import java.sql.SQLException JavaDoc;
34
35 import org.apache.derby.tools.JDBCDisplayUtil;
36 import org.apache.derby.iapi.tools.i18n.LocalizedOutput;
37
38 /**
39     To enable multi-user use of ij.Main2
40
41     @author jerry
42  */

43 class ConnectionEnv {
44     Hashtable JavaDoc sessions = new Hashtable JavaDoc();
45     private Session currSession;
46     private String JavaDoc tag;
47     private boolean only;
48     private static final String JavaDoc CONNECTION_PROPERTY = "ij.connection";
49     private String JavaDoc protocol;
50
51     ConnectionEnv(int userNumber, boolean printUserNumber, boolean theOnly) {
52         if (printUserNumber)
53             tag = "("+(userNumber+1)+")";
54         only = theOnly;
55     }
56
57     /**
58         separate from the constructor so that connection
59         failure does not prevent object creation.
60      */

61     void init(LocalizedOutput out) throws SQLException JavaDoc, ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc {
62
63         Connection JavaDoc c = util.startJBMS(null,null);
64
65         // only load up ij.connection.* properties if there is
66
// only one ConnectionEnv in the system.
67
if (only) {
68             Properties JavaDoc p = System.getProperties();
69             protocol = p.getProperty(ij.PROTOCOL_PROPERTY);
70
71             String JavaDoc prefix = CONNECTION_PROPERTY + ".";
72             for (Enumeration JavaDoc e = p.propertyNames(); e.hasMoreElements(); )
73             {
74                 String JavaDoc key = (String JavaDoc)e.nextElement();
75                 if (key.startsWith(prefix)) {
76                     String JavaDoc name = key.substring(prefix.length());
77                     installConnection(name.toUpperCase(java.util.Locale.ENGLISH),
78                         p.getProperty(key), out);
79                 }
80             }
81         }
82
83         if (c!=null) // have a database from the startup?
84
{
85             String JavaDoc sname=Session.DEFAULT_NAME+sessions.size();
86             Session s = new Session(c,tag,sname);
87             sessions.put(sname,s);
88             currSession = s;
89         }
90
91     }
92
93     void doPrompt(boolean newStatement, LocalizedOutput out) {
94         if (currSession != null) currSession.doPrompt(newStatement, out, sessions.size()>1);
95         else utilMain.doPrompt(newStatement, out, tag);
96     }
97     
98     Connection JavaDoc getConnection() {
99         if (currSession == null) return null;
100         return currSession.getConnection();
101     }
102
103     /**
104         Making a new connection, add it to the pool, and make it current.
105      */

106     void addSession(Connection JavaDoc conn,String JavaDoc name) {
107         String JavaDoc aName;
108         if (name == null) aName = getUniqueConnectionName();
109         else aName = name;
110         Session s = new Session(conn, tag, aName);
111         sessions.put(aName, s);
112         currSession = s;
113     }
114
115   //returns a unique Connection# name by going through existing sessions
116
public String JavaDoc getUniqueConnectionName() {
117     int newNum = 0;
118     boolean newConnectionNameOk = false;
119     String JavaDoc newConnectionName = "";
120     Enumeration JavaDoc e;
121     while (!newConnectionNameOk){
122       newConnectionName = Session.DEFAULT_NAME + newNum;
123       newConnectionNameOk = true;
124       e = sessions.keys();
125       while (e.hasMoreElements() && newConnectionNameOk){
126         if (((String JavaDoc)e.nextElement()).equals(newConnectionName))
127            newConnectionNameOk = false;
128       }
129       newNum = newNum + 1;
130     }
131     return newConnectionName;
132   }
133
134     Session getSession() {
135         return currSession;
136     }
137
138     Hashtable JavaDoc getSessions() {
139         return sessions;
140     }
141
142     Session setCurrentSession(String JavaDoc name) {
143         currSession = (Session) sessions.get(name);
144         return currSession;
145     }
146
147     boolean haveSession(String JavaDoc name) {
148         return (name != null) && (sessions.size()>0) && (null != sessions.get(name));
149     }
150
151     void removeCurrentSession() throws SQLException JavaDoc {
152         if (currSession ==null) return;
153         sessions.remove(currSession.getName());
154         currSession.close();
155         currSession = null;
156     }
157
158     void removeSession(String JavaDoc name) throws SQLException JavaDoc {
159         Session s = (Session) sessions.remove(name);
160         s.close();
161         if (currSession == s)
162             currSession = null;
163     }
164
165     void removeAllSessions() throws SQLException JavaDoc {
166         if (sessions == null || sessions.size() == 0)
167             return;
168         else
169             for (Enumeration JavaDoc e = sessions.keys(); e.hasMoreElements(); ) {
170                 String JavaDoc n = (String JavaDoc)e.nextElement();
171                 removeSession(n);
172             }
173     }
174
175     private void installConnection(String JavaDoc name, String JavaDoc value, LocalizedOutput out) throws SQLException JavaDoc {
176         // add protocol if no driver matches url
177
boolean noDriver = false;
178         try {
179             // if we have a full URL, make sure it's loaded first
180
try {
181                 if (value.startsWith("jdbc:"))
182                     util.loadDriverIfKnown(value);
183             } catch (Exception JavaDoc e) {
184                 // want to continue with the attempt
185
}
186             DriverManager.getDriver(value);
187         } catch (SQLException JavaDoc se) {
188             noDriver = true;
189         }
190         if (noDriver && (protocol != null)) {
191             value = protocol + value;
192         }
193
194         if (sessions.get(name) != null) {
195             throw ijException.alreadyHaveConnectionNamed(name);
196         }
197         try {
198             
199             String JavaDoc user = util.getSystemProperty("ij.user");
200             String JavaDoc password = util.getSystemProperty("ij.password");
201             Properties JavaDoc connInfo = util.updateConnInfo(user, password,null);
202                                                            
203             Connection JavaDoc theConnection =
204                 DriverManager.getConnection(value, connInfo);
205                                                                                
206             addSession(theConnection,name);
207         } catch (Throwable JavaDoc t) {
208             JDBCDisplayUtil.ShowException(out,t);
209         }
210     }
211
212 }
213
Popular Tags