Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / MyDerby-10.3 / java / tools / org / apache / derby / impl / tools / ij / ConnectionEnv.java
diff --git a/JMCR-Stable/real-world application/MyDerby-10.3/java/tools/org/apache/derby/impl/tools/ij/ConnectionEnv.java b/JMCR-Stable/real-world application/MyDerby-10.3/java/tools/org/apache/derby/impl/tools/ij/ConnectionEnv.java
new file mode 100644 (file)
index 0000000..377cc99
--- /dev/null
@@ -0,0 +1,217 @@
+/*\r
+\r
+   Derby - Class org.apache.derby.impl.tools.ij.ConnectionEnv\r
+\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to You under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+      http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.tools.ij;\r
+\r
+import java.io.File;\r
+import java.io.FileNotFoundException;\r
+\r
+import java.util.Hashtable;\r
+import java.util.Enumeration;\r
+import java.util.Properties;\r
+\r
+import java.security.AccessController;\r
+import java.security.PrivilegedAction;\r
+import java.sql.Connection;\r
+import java.sql.DriverManager;\r
+import java.sql.SQLException;\r
+\r
+import org.apache.derby.tools.JDBCDisplayUtil;\r
+import org.apache.derby.iapi.tools.i18n.LocalizedOutput;\r
+\r
+/**\r
+       To enable multi-user use of ij.Main2\r
+\r
+ */\r
+class ConnectionEnv {\r
+       Hashtable sessions = new Hashtable();\r
+       private Session currSession;\r
+       private String tag;\r
+       private boolean only;\r
+       private static final String CONNECTION_PROPERTY = "ij.connection";\r
+    private String protocol;\r
+\r
+       ConnectionEnv(int userNumber, boolean printUserNumber, boolean theOnly) {\r
+               if (printUserNumber)\r
+                       tag = "("+(userNumber+1)+")";\r
+               only = theOnly;\r
+       }\r
+\r
+       /**\r
+               separate from the constructor so that connection\r
+               failure does not prevent object creation.\r
+        */\r
+       void init(LocalizedOutput out) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {\r
+\r
+               Connection c = util.startJBMS(null,null);\r
+\r
+               // only load up ij.connection.* properties if there is\r
+               // only one ConnectionEnv in the system.\r
+               if (only) {\r
+            Properties p = (Properties) AccessController.doPrivileged(new PrivilegedAction() {\r
+               public Object run() {\r
+                       return System.getProperties();\r
+               }\r
+            });\r
+            protocol = p.getProperty(ij.PROTOCOL_PROPERTY);\r
+\r
+               String prefix = CONNECTION_PROPERTY + ".";\r
+                   for (Enumeration e = p.propertyNames(); e.hasMoreElements(); )\r
+                   {\r
+                       String key = (String)e.nextElement();\r
+                       if (key.startsWith(prefix)) {\r
+                               String name = key.substring(prefix.length());\r
+                               installConnection(name.toUpperCase(java.util.Locale.ENGLISH), \r
+                                               p.getProperty(key), out);\r
+                       }\r
+                   }\r
+               }\r
+\r
+               if (c!=null) // have a database from the startup?\r
+               {\r
+                       String sname=Session.DEFAULT_NAME+sessions.size();\r
+                       Session s = new Session(c,tag,sname);\r
+                       sessions.put(sname,s);\r
+                       currSession = s;\r
+               }\r
+\r
+       }\r
+\r
+       void doPrompt(boolean newStatement, LocalizedOutput out) {\r
+               if (currSession != null) currSession.doPrompt(newStatement, out, sessions.size()>1);\r
+               else utilMain.doPrompt(newStatement, out, tag);\r
+       }\r
+       \r
+       Connection getConnection() {\r
+               if (currSession == null) return null;\r
+               return currSession.getConnection();\r
+       }\r
+\r
+       /**\r
+               Making a new connection, add it to the pool, and make it current.\r
+        */\r
+       void addSession(Connection conn,String name) {\r
+               String aName;\r
+               if (name == null) aName = getUniqueConnectionName();\r
+               else aName = name;\r
+               Session s = new Session(conn, tag, aName);\r
+               sessions.put(aName, s);\r
+               currSession = s;\r
+       }\r
+\r
+  //returns a unique Connection# name by going through existing sessions\r
+  public String getUniqueConnectionName() {\r
+    int newNum = 0;\r
+    boolean newConnectionNameOk = false;\r
+    String newConnectionName = "";\r
+    Enumeration e;\r
+    while (!newConnectionNameOk){\r
+      newConnectionName = Session.DEFAULT_NAME + newNum;\r
+      newConnectionNameOk = true;\r
+      e = sessions.keys();\r
+      while (e.hasMoreElements() && newConnectionNameOk){\r
+        if (((String)e.nextElement()).equals(newConnectionName))\r
+           newConnectionNameOk = false;\r
+      }\r
+      newNum = newNum + 1;\r
+    }\r
+    return newConnectionName;\r
+  }\r
+\r
+       Session getSession() {\r
+               return currSession;\r
+       }\r
+\r
+       Hashtable getSessions() {\r
+               return sessions;\r
+       }\r
+\r
+       Session setCurrentSession(String name) {\r
+               currSession = (Session) sessions.get(name);\r
+               return currSession;\r
+       }\r
+\r
+       boolean haveSession(String name) {\r
+               return (name != null) && (sessions.size()>0) && (null != sessions.get(name));\r
+       }\r
+\r
+       void removeCurrentSession() throws SQLException {\r
+               if (currSession ==null) return;\r
+               sessions.remove(currSession.getName());\r
+               currSession.close();\r
+               currSession = null;\r
+       }\r
+\r
+       void removeSession(String name) throws SQLException {\r
+               Session s = (Session) sessions.remove(name);\r
+               s.close();\r
+               if (currSession == s)\r
+                       currSession = null;\r
+       }\r
+\r
+       void removeAllSessions() throws SQLException {\r
+               if (sessions == null || sessions.size() == 0)\r
+                       return;\r
+               else\r
+                       for (Enumeration e = sessions.keys(); e.hasMoreElements(); ) {\r
+                               String n = (String)e.nextElement();\r
+                               removeSession(n);\r
+                       }\r
+       }\r
+\r
+       private void installConnection(String name, String value, LocalizedOutput out) throws SQLException {\r
+               // add protocol if no driver matches url\r
+               boolean noDriver = false;\r
+               try {\r
+                       // if we have a full URL, make sure it's loaded first\r
+                       try {\r
+                               if (value.startsWith("jdbc:"))\r
+                                       util.loadDriverIfKnown(value);\r
+                       } catch (Exception e) {\r
+                               // want to continue with the attempt\r
+                       }\r
+                       DriverManager.getDriver(value);\r
+               } catch (SQLException se) {\r
+                       noDriver = true;\r
+               }\r
+               if (noDriver && (protocol != null)) {\r
+                       value = protocol + value;\r
+               }\r
+\r
+               if (sessions.get(name) != null) {\r
+                       throw ijException.alreadyHaveConnectionNamed(name);\r
+               }\r
+               try {\r
+                       \r
+                       String user = util.getSystemProperty("ij.user");\r
+                       String password = util.getSystemProperty("ij.password");\r
+                       Properties connInfo =  util.updateConnInfo(user, password,null);\r
+                                                                                                                  \r
+                       Connection theConnection = \r
+                               DriverManager.getConnection(value, connInfo);\r
+                                                                                                                                                          \r
+                   addSession(theConnection,name);\r
+               } catch (Throwable t) {\r
+                       JDBCDisplayUtil.ShowException(out,t);\r
+               }\r
+       }\r
+\r
+}\r