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: }
|