Source Code Cross Referenced for DerbyConnectionUtil.java in  » EJB-Server-geronimo » plugins » org » apache » geronimo » console » internaldb » 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 » EJB Server geronimo » plugins » org.apache.geronimo.console.internaldb 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */package org.apache.geronimo.console.internaldb;
017:
018:        import java.sql.Connection;
019:        import java.sql.DriverManager;
020:        import java.sql.SQLException;
021:        import java.util.Set;
022:
023:        import javax.sql.DataSource;
024:
025:        import org.apache.commons.logging.Log;
026:        import org.apache.commons.logging.LogFactory;
027:        import org.apache.geronimo.derby.DerbySystemGBean;
028:        import org.apache.geronimo.gbean.AbstractName;
029:        import org.apache.geronimo.gbean.AbstractNameQuery;
030:        import org.apache.geronimo.kernel.KernelRegistry;
031:        import org.apache.geronimo.management.JCAManagedConnectionFactory;
032:
033:        /**
034:         * A static class to handle retreiving connections. This class is built to
035:         * handle lookups to the SystemDatabase as a special case. If a connection is
036:         * requested for the SystemDatabase this class gets a DataSource from an admin
037:         * object registered in the geronimo kernel otherwise the DataSource is looked
038:         * up via JNDI.
039:         *
040:         * @version $Rev: 570429 $ $Date: 2007-08-28 06:55:02 -0700 (Tue, 28 Aug 2007) $
041:         */
042:        public class DerbyConnectionUtil {
043:
044:            private final static Log log = LogFactory
045:                    .getLog(DerbyConnectionUtil.class);
046:
047:            public static final String CREATE_DB_PROP = ";create=true";
048:
049:            public static final String SHUTDOWN_DB_PROP = ";shutdown=true";
050:
051:            private static final int RDBMS_DERBY = 1;
052:
053:            private static final int RDBMS_MSSQL = 2;
054:
055:            private static final String SYSTEM_DB = "SYSTEMDATABASE";
056:
057:            private static final String DERBY_DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
058:
059:            private static final String PROTOCOL = "jdbc:derby:";
060:
061:            private static final String EMPTY_PROPS = "";
062:
063:            private static AbstractName SYSTEM_DATASOURCE_NAME = null;
064:
065:            static {
066:                try {
067:                    log.debug("Looking up system datasource name...");
068:
069:                    // cache the name for the system data source
070:                    AbstractNameQuery query = new AbstractNameQuery(
071:                            JCAManagedConnectionFactory.class.getName());
072:                    Set<AbstractName> names = KernelRegistry.getSingleKernel()
073:                            .listGBeans(query);
074:                    for (AbstractName name : names) {
075:                        String nameProperty = name.getNameProperty("name");
076:                        if ("SystemDatasource".equals(nameProperty)) {
077:                            SYSTEM_DATASOURCE_NAME = name;
078:                            log.debug("Using system datasource name: "
079:                                    + SYSTEM_DATASOURCE_NAME);
080:                        }
081:                    }
082:
083:                    if (SYSTEM_DATASOURCE_NAME == null) {
084:                        log.warn("Failed to lookup system datasource name");
085:                    }
086:                } catch (Throwable t) {
087:                    //
088:                    // HACK: Log any errors which occur when this is loading...
089:                    //       the system is not logging the full detail, which it should
090:                    //       but for now lets show the details here
091:                    //
092:                    log.error("Failed to initialize", t);
093:                    throw new Error(t);
094:                }
095:            }
096:
097:            private static String derbyHome = null;
098:
099:            /**
100:             * Get the Derby home directory path.
101:             */
102:            public static String getDerbyHome() {
103:                if (derbyHome == null) {
104:                    try {
105:                        derbyHome = (String) KernelRegistry.getSingleKernel()
106:                                .getAttribute(DerbySystemGBean.class,
107:                                        "derbyHome");
108:                    } catch (Exception e) {
109:                        throw new RuntimeException("Failed to query derbyHome",
110:                                e);
111:                    }
112:                }
113:                return derbyHome;
114:            }
115:
116:            /**
117:             * Get database connection.
118:             *
119:             * @param dbName
120:             * @return
121:             * @throws SQLException
122:             */
123:            private static Connection getConnection(String dbName,
124:                    String properties, String protocol, String driver)
125:                    throws SQLException {
126:                try {
127:                    Class.forName(driver).newInstance();
128:                } catch (Exception e) {
129:                    log.error("Problem loading driver class", e);
130:                }
131:                // If we are looking for the SystemDatabase get it from the kernel
132:                // because it is not binded to our JNDI Context.
133:                if (SYSTEM_DB.equalsIgnoreCase(dbName)) {
134:                    return getSystemDBConnection();
135:                } else {
136:                    return DriverManager.getConnection(protocol + dbName
137:                            + properties);
138:                }
139:            }
140:
141:            /**
142:             * Get a connection to derby.
143:             *
144:             * @param dbName
145:             *            the name of the database to connect to.
146:             * @param properties
147:             *            the properties to pass to the connection string.
148:             * @return connection
149:             */
150:            public static Connection getDerbyConnection(String dbName,
151:                    String properties) throws SQLException {
152:                return getConnection(dbName, properties, PROTOCOL, DERBY_DRIVER);
153:            }
154:
155:            public static Connection getDerbyConnection(String dbName)
156:                    throws SQLException {
157:                return getDerbyConnection(dbName, EMPTY_PROPS);
158:            }
159:
160:            /**
161:             * Get a connection to the SystemDatabase.
162:             *
163:             * @return
164:             * @throws SQLException
165:             */
166:            public static Connection getSystemDBConnection()
167:                    throws SQLException {
168:                DataSource ds = null;
169:                try {
170:                    ds = getDataSource(SYSTEM_DB);
171:                    return ds.getConnection();
172:                } catch (Exception e) {
173:                    throw new SQLException(e.getMessage());
174:                }
175:            }
176:
177:            /**
178:             * Get the datasource if dbName is == SYSTEM_DB, otherwise returns null.
179:             *
180:             * @param dbName
181:             * @return datasource
182:             */
183:            public static DataSource getDataSource(String dbName) {
184:                try {
185:                    if (SYSTEM_DATASOURCE_NAME != null
186:                            && SYSTEM_DB.equalsIgnoreCase(dbName)) {
187:                        return (DataSource) KernelRegistry.getSingleKernel()
188:                                .invoke(SYSTEM_DATASOURCE_NAME, "$getResource");
189:                    }
190:                } catch (Exception e) {
191:                    log.error("Problem getting datasource " + dbName, e);
192:                }
193:                return null;
194:            }
195:
196:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.