1 21 22 package org.apache.derby.impl.tools.ij; 23 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 27 import java.util.Hashtable ; 28 import java.util.Enumeration ; 29 import java.util.Properties ; 30 31 import java.sql.Connection ; 32 import java.sql.DriverManager ; 33 import java.sql.SQLException ; 34 35 import org.apache.derby.tools.JDBCDisplayUtil; 36 import org.apache.derby.iapi.tools.i18n.LocalizedOutput; 37 38 43 class ConnectionEnv { 44 Hashtable sessions = new Hashtable (); 45 private Session currSession; 46 private String tag; 47 private boolean only; 48 private static final String CONNECTION_PROPERTY = "ij.connection"; 49 private String protocol; 50 51 ConnectionEnv(int userNumber, boolean printUserNumber, boolean theOnly) { 52 if (printUserNumber) 53 tag = "("+(userNumber+1)+")"; 54 only = theOnly; 55 } 56 57 61 void init(LocalizedOutput out) throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException { 62 63 Connection c = util.startJBMS(null,null); 64 65 if (only) { 68 Properties p = System.getProperties(); 69 protocol = p.getProperty(ij.PROTOCOL_PROPERTY); 70 71 String prefix = CONNECTION_PROPERTY + "."; 72 for (Enumeration e = p.propertyNames(); e.hasMoreElements(); ) 73 { 74 String key = (String )e.nextElement(); 75 if (key.startsWith(prefix)) { 76 String 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) { 85 String 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 getConnection() { 99 if (currSession == null) return null; 100 return currSession.getConnection(); 101 } 102 103 106 void addSession(Connection conn,String name) { 107 String 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 public String getUniqueConnectionName() { 117 int newNum = 0; 118 boolean newConnectionNameOk = false; 119 String newConnectionName = ""; 120 Enumeration 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 )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 getSessions() { 139 return sessions; 140 } 141 142 Session setCurrentSession(String name) { 143 currSession = (Session) sessions.get(name); 144 return currSession; 145 } 146 147 boolean haveSession(String name) { 148 return (name != null) && (sessions.size()>0) && (null != sessions.get(name)); 149 } 150 151 void removeCurrentSession() throws SQLException { 152 if (currSession ==null) return; 153 sessions.remove(currSession.getName()); 154 currSession.close(); 155 currSession = null; 156 } 157 158 void removeSession(String name) throws SQLException { 159 Session s = (Session) sessions.remove(name); 160 s.close(); 161 if (currSession == s) 162 currSession = null; 163 } 164 165 void removeAllSessions() throws SQLException { 166 if (sessions == null || sessions.size() == 0) 167 return; 168 else 169 for (Enumeration e = sessions.keys(); e.hasMoreElements(); ) { 170 String n = (String )e.nextElement(); 171 removeSession(n); 172 } 173 } 174 175 private void installConnection(String name, String value, LocalizedOutput out) throws SQLException { 176 boolean noDriver = false; 178 try { 179 try { 181 if (value.startsWith("jdbc:")) 182 util.loadDriverIfKnown(value); 183 } catch (Exception e) { 184 } 186 DriverManager.getDriver(value); 187 } catch (SQLException 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 user = util.getSystemProperty("ij.user"); 200 String password = util.getSystemProperty("ij.password"); 201 Properties connInfo = util.updateConnInfo(user, password,null); 202 203 Connection theConnection = 204 DriverManager.getConnection(value, connInfo); 205 206 addSession(theConnection,name); 207 } catch (Throwable t) { 208 JDBCDisplayUtil.ShowException(out,t); 209 } 210 } 211 212 } 213 | Popular Tags |