KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > internaldb > DerbyConnectionUtil


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

17
18 package org.apache.geronimo.console.internaldb;
19
20 import java.sql.Connection JavaDoc;
21 import java.sql.DriverManager JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import javax.sql.DataSource JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.geronimo.gbean.AbstractName;
31 import org.apache.geronimo.gbean.AbstractNameQuery;
32 import org.apache.geronimo.kernel.KernelRegistry;
33 import org.apache.geronimo.kernel.repository.Artifact;
34 import org.apache.geronimo.kernel.repository.Version;
35
36 /**
37  * A static class to handle retreiving connections. This class is built to
38  * handle lookups to the SystemDatabase as a special case. If a connection is
39  * requested for the SystemDatabase this class gets a DataSource from an admin
40  * object registered in the geronimo kernel otherwise the DataSource is looked
41  * up via JNDI.
42  *
43  * @version $Rev: 476321 $ $Date: 2006-11-17 16:18:49 -0500 (Fri, 17 Nov 2006) $
44  */

45 public class DerbyConnectionUtil {
46
47     private final static Log log = LogFactory.getLog(DerbyConnectionUtil.class);
48
49     public static final String JavaDoc CREATE_DB_PROP = ";create=true";
50
51     public static final String JavaDoc SHUTDOWN_DB_PROP = ";shutdown=true";
52
53     private static final int RDBMS_DERBY = 1;
54
55     private static final int RDBMS_MSSQL = 2;
56
57     private static final String JavaDoc SYSTEM_DB = "SYSTEMDATABASE";
58
59     private static final String JavaDoc DERBY_DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
60
61     private static final String JavaDoc PROTOCOL = "jdbc:derby:";
62
63     private static final String JavaDoc EMPTY_PROPS = "";
64     
65     private static AbstractName SYSTEM_DATASOURCE_NAME = null;
66     
67     static {
68         try {
69             log.debug("Looking up system datasource name...");
70             
71             // look up the system data source name without using the version number
72
HashMap JavaDoc props = new HashMap JavaDoc();
73             props.put("name","SystemDatasource");
74             props.put("j2eeType","JCAManagedConnectionFactory");
75             Artifact systemDB = new Artifact("org.apache.geronimo.configs", "system-database", (Version)null, "car");
76             AbstractNameQuery query = new AbstractNameQuery(systemDB,props);
77             Iterator JavaDoc iter = KernelRegistry.getSingleKernel().listGBeans(query).iterator();
78             
79             if (iter.hasNext()) {
80                 SYSTEM_DATASOURCE_NAME = (AbstractName)iter.next();
81                 log.debug("Using system datasource name: " + SYSTEM_DATASOURCE_NAME);
82             }
83             else {
84                 log.warn("Failed to lookup system datasource name");
85             }
86         }
87         catch (Throwable JavaDoc t) {
88             //
89
// HACK: Log any errors which occur when this is loading...
90
// the system is not logging the full detail, which it should
91
// but for now lets show the details here
92
//
93
log.error("Failed to initialize", t);
94             throw new Error JavaDoc(t);
95         }
96     }
97
98     /**
99      * Get database connection.
100      *
101      * @param dbName
102      * @return
103      * @throws SQLException
104      */

105     private static Connection JavaDoc getConnection(String JavaDoc dbName, String JavaDoc properties,
106             String JavaDoc protocol, String JavaDoc driver) throws SQLException JavaDoc {
107         try {
108             Class.forName(driver).newInstance();
109         } catch (Exception JavaDoc e) {
110             log.error("Problem loading driver class", e);
111         }
112         // If we are looking for the SystemDatabase get it from the kernel
113
// because it is not binded to our JNDI Context.
114
if (SYSTEM_DB.equalsIgnoreCase(dbName)) {
115             return getSystemDBConnection();
116         } else {
117             return DriverManager.getConnection(protocol + dbName + properties);
118         }
119     }
120
121     /**
122      * Get a connection to derby.
123      *
124      * @param dbName
125      * the name of the database to connect to.
126      * @param properties
127      * the properties to pass to the connection string.
128      * @return connection
129      */

130     public static Connection JavaDoc getDerbyConnection(String JavaDoc dbName, String JavaDoc properties)
131             throws SQLException JavaDoc {
132         return getConnection(dbName, properties, PROTOCOL, DERBY_DRIVER);
133     }
134
135     public static Connection JavaDoc getDerbyConnection(String JavaDoc dbName)
136             throws SQLException JavaDoc {
137         return getDerbyConnection(dbName, EMPTY_PROPS);
138     }
139
140     /**
141      * Get a connection to the SystemDatabase.
142      *
143      * @return
144      * @throws SQLException
145      */

146     public static Connection JavaDoc getSystemDBConnection() throws SQLException JavaDoc {
147         DataSource JavaDoc ds = null;
148         try {
149             ds = getDataSource(SYSTEM_DB);
150             return ds.getConnection();
151         } catch (Exception JavaDoc e) {
152             throw new SQLException JavaDoc(e.getMessage());
153         }
154     }
155
156     /**
157      * Get the datasource if dbName is == SYSTEM_DB, otherwise returns null.
158      *
159      * @param dbName
160      * @return datasource
161      */

162     public static DataSource JavaDoc getDataSource(String JavaDoc dbName) {
163         try {
164             if (SYSTEM_DATASOURCE_NAME!=null && SYSTEM_DB.equalsIgnoreCase(dbName)) {
165                 return (DataSource JavaDoc) KernelRegistry.getSingleKernel().invoke(
166                         SYSTEM_DATASOURCE_NAME, "$getResource");
167             }
168         } catch (Exception JavaDoc e) {
169             log.error("Problem getting datasource " + dbName, e);
170         }
171         return null;
172     }
173
174 }
175
Popular Tags