Source Code Cross Referenced for ConnectionEnv.java in  » Database-DBMS » db-derby-10.2 » org » apache » derby » impl » tools » ij » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » db derby 10.2 » org.apache.derby.impl.tools.ij 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.