001: /*
002:
003: Derby - Class org.apache.derby.jdbc.AutoloadedDriver
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.jdbc;
023:
024: import java.sql.DriverManager;
025: import java.sql.Driver;
026: import java.sql.Connection;
027: import java.sql.DriverPropertyInfo;
028: import java.sql.SQLException;
029:
030: import java.io.PrintStream;
031: import java.util.Properties;
032:
033: import org.apache.derby.iapi.reference.MessageId;
034: import org.apache.derby.iapi.reference.Attribute;
035: import org.apache.derby.iapi.services.i18n.MessageService;
036: import org.apache.derby.iapi.jdbc.JDBCBoot;
037:
038: /**
039: This is the dummy driver which is autoloaded under JDBC4 and registered with
040: the DriverManager. Loading this class will NOT automatically boot the Derby engine.
041: Instead, the engine boots lazily when you ask for a
042: Connection. Alternatively, you can force the engine to boot as follows:
043:
044: <PRE>
045: Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
046:
047: // or
048:
049: new org.apache.derby.jdbc.EmbeddedDriver();
050:
051:
052: </PRE>
053: */
054: public class AutoloadedDriver implements Driver {
055: // This flag is set if the engine is forcibly brought down.
056: private static boolean _engineForcedDown = false;
057:
058: //
059: // This is the driver that's specific to the JDBC level we're running at.
060: // It's the module which boots the whole Derby engine.
061: //
062: private static Driver _driverModule;
063:
064: static {
065: try {
066: DriverManager.registerDriver(new AutoloadedDriver());
067: } catch (SQLException se) {
068: String message = MessageService.getTextMessage(
069: MessageId.JDBC_DRIVER_REGISTER_ERROR, se
070: .getMessage());
071:
072: throw new IllegalStateException(message);
073: }
074: }
075:
076: /*
077: ** Methods from java.sql.Driver.
078: */
079: /**
080: Accept anything that starts with <CODE>jdbc:derby:</CODE>.
081: @exception SQLException if a database-access error occurs.
082: @see java.sql.Driver
083: */
084: public boolean acceptsURL(String url) throws SQLException {
085:
086: //
087: // We don't want to accidentally boot the engine just because
088: // the application is looking for a connection from some other
089: // driver.
090: //
091: return (isBooted() && InternalDriver
092: .embeddedDriverAcceptsURL(url));
093: }
094:
095: /**
096: Connect to the URL if possible
097: @exception SQLException illegal url or problem with connectiong
098: @see java.sql.Driver
099: */
100: public Connection connect(String url, Properties info)
101: throws SQLException {
102: //
103: // This pretty piece of logic compensates for the following behavior
104: // of the DriverManager: When asked to get a Connection, the
105: // DriverManager cycles through all of its autoloaded drivers, looking
106: // for one which will return a Connection. Without this pretty logic,
107: // the embedded driver module will be booted by any request for
108: // a connection which cannot be satisfied by drivers ahead of us
109: // in the list.
110: if (!InternalDriver.embeddedDriverAcceptsURL(url)) {
111: return null;
112: }
113:
114: return getDriverModule().connect(url, info);
115: }
116:
117: /**
118: * Returns an array of DriverPropertyInfo objects describing possible properties.
119: @exception SQLException if a database-access error occurs.
120: @see java.sql.Driver
121: */
122: public DriverPropertyInfo[] getPropertyInfo(String url,
123: Properties info) throws SQLException {
124: return getDriverModule().getPropertyInfo(url, info);
125: }
126:
127: /**
128: * Returns the driver's major version number.
129: @see java.sql.Driver
130: */
131: public int getMajorVersion() {
132: try {
133: return (getDriverModule().getMajorVersion());
134: } catch (SQLException se) {
135: return 0;
136: }
137: }
138:
139: /**
140: * Returns the driver's minor version number.
141: @see java.sql.Driver
142: */
143: public int getMinorVersion() {
144: try {
145: return (getDriverModule().getMinorVersion());
146: } catch (SQLException se) {
147: return 0;
148: }
149: }
150:
151: /**
152: * Report whether the Driver is a genuine JDBC COMPLIANT (tm) driver.
153: @see java.sql.Driver
154: */
155: public boolean jdbcCompliant() {
156: try {
157: return (getDriverModule().jdbcCompliant());
158: } catch (SQLException se) {
159: return false;
160: }
161: }
162:
163: ///////////////////////////////////////////////////////////////////////
164: //
165: // Support for booting and shutting down the engine.
166: //
167: ///////////////////////////////////////////////////////////////////////
168:
169: /*
170: ** Retrieve the driver which is specific to our JDBC level.
171: ** We defer real work to this specific driver.
172: */
173: public static Driver getDriverModule() throws SQLException {
174:
175: if (_engineForcedDown) {
176: // Driver not registered
177: throw new SQLException(
178: MessageService
179: .getTextMessage(MessageId.CORE_JDBC_DRIVER_UNREGISTERED));
180: }
181:
182: if (!isBooted()) {
183: EmbeddedDriver.boot();
184: }
185:
186: return _driverModule;
187: }
188:
189: /*
190: ** Record which driver module actually booted.
191: */
192: protected static void registerDriverModule(Driver driver) {
193: _driverModule = driver;
194: _engineForcedDown = false;
195: }
196:
197: /*
198: ** Unregister the driver. This happens when the engine is
199: ** forcibly shut down.
200: */
201: protected static void unregisterDriverModule() {
202: _driverModule = null;
203: _engineForcedDown = true;
204: }
205:
206: /*
207: ** Return true if the engine has been booted.
208: */
209: private static boolean isBooted() {
210: return (_driverModule != null);
211: }
212:
213: }
|