KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > jdbc > AutoloadedDriver


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

21
22 package org.apache.derby.jdbc;
23
24 import java.sql.DriverManager JavaDoc;
25 import java.sql.Driver JavaDoc;
26 import java.sql.Connection JavaDoc;
27 import java.sql.DriverPropertyInfo JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import java.io.PrintStream JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import org.apache.derby.iapi.reference.MessageId;
34 import org.apache.derby.iapi.reference.Attribute;
35 import org.apache.derby.iapi.services.i18n.MessageService;
36 import org.apache.derby.iapi.jdbc.JDBCBoot;
37
38
39 /**
40    This is the dummy driver which is autoloaded under JDBC4 and registered with
41    the DriverManager. Loading this class will NOT automatically boot the Derby engine.
42    Instead, the engine boots lazily when you ask for a
43    Connection. Alternatively, you can force the engine to boot as follows:
44
45      <PRE>
46      Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
47
48      // or
49
50      new org.apache.derby.jdbc.EmbeddedDriver();
51
52     
53     </PRE>
54 */

55 public class AutoloadedDriver implements Driver JavaDoc
56 {
57     // This flag is set if the engine is forcibly brought down.
58
private static boolean _engineForcedDown = false;
59     
60     //
61
// This is the driver that's specific to the JDBC level we're running at.
62
// It's the module which boots the whole Derby engine.
63
//
64
private static Driver JavaDoc _driverModule;
65     
66     static
67     {
68         try {
69             DriverManager.registerDriver( new AutoloadedDriver() );
70         }
71         catch (SQLException JavaDoc se)
72         {
73             String JavaDoc message = MessageService.getTextMessage
74                 (MessageId.JDBC_DRIVER_REGISTER_ERROR, se.getMessage() );
75
76             throw new IllegalStateException JavaDoc( message );
77         }
78     }
79
80     /*
81     ** Methods from java.sql.Driver.
82     */

83     /**
84         Accept anything that starts with <CODE>jdbc:derby:</CODE>.
85         @exception SQLException if a database-access error occurs.
86     @see java.sql.Driver
87     */

88     public boolean acceptsURL(String JavaDoc url) throws SQLException JavaDoc {
89
90         //
91
// We don't want to accidentally boot the engine just because
92
// the application is looking for a connection from some other
93
// driver.
94
//
95
return ( isBooted() && InternalDriver.embeddedDriverAcceptsURL(url) );
96     }
97
98    
99     /**
100         Connect to the URL if possible
101         @exception SQLException illegal url or problem with connectiong
102     @see java.sql.Driver
103   */

104     public Connection JavaDoc connect(String JavaDoc url, Properties JavaDoc info)
105         throws SQLException JavaDoc
106     {
107         //
108
// This pretty piece of logic compensates for the following behavior
109
// of the DriverManager: When asked to get a Connection, the
110
// DriverManager cycles through all of its autoloaded drivers, looking
111
// for one which will return a Connection. Without this pretty logic,
112
// the embedded driver module will be booted by any request for
113
// a connection which cannot be satisfied by drivers ahead of us
114
// in the list.
115
if (!InternalDriver.embeddedDriverAcceptsURL(url)) { return null; }
116
117         return getDriverModule().connect(url, info);
118     }
119
120   /**
121    * Returns an array of DriverPropertyInfo objects describing possible properties.
122     @exception SQLException if a database-access error occurs.
123     @see java.sql.Driver
124    */

125     public DriverPropertyInfo JavaDoc[] getPropertyInfo(String JavaDoc url, Properties JavaDoc info)
126         throws SQLException JavaDoc
127     {
128         return getDriverModule().getPropertyInfo(url, info);
129     }
130
131     /**
132      * Returns the driver's major version number.
133      @see java.sql.Driver
134      */

135     public int getMajorVersion() {
136         try {
137             return (getDriverModule().getMajorVersion());
138         }
139         catch (SQLException JavaDoc se) {
140             return 0;
141         }
142     }
143     /**
144      * Returns the driver's minor version number.
145      @see java.sql.Driver
146      */

147     public int getMinorVersion() {
148         try {
149             return (getDriverModule().getMinorVersion());
150         }
151         catch (SQLException JavaDoc se) {
152             return 0;
153         }
154     }
155
156   /**
157    * Report whether the Driver is a genuine JDBC COMPLIANT (tm) driver.
158      @see java.sql.Driver
159    */

160     public boolean jdbcCompliant() {
161         try {
162             return (getDriverModule().jdbcCompliant());
163         }
164         catch (SQLException JavaDoc se) {
165             return false;
166         }
167     }
168
169     ///////////////////////////////////////////////////////////////////////
170
//
171
// Support for booting and shutting down the engine.
172
//
173
///////////////////////////////////////////////////////////////////////
174

175     /*
176     ** Retrieve the driver which is specific to our JDBC level.
177     ** We defer real work to this specific driver.
178     */

179     public static Driver JavaDoc getDriverModule() throws SQLException JavaDoc {
180
181         if ( _engineForcedDown )
182         {
183             // Driver not registered
184
throw new SQLException JavaDoc
185                 (MessageService.getTextMessage(MessageId.CORE_JDBC_DRIVER_UNREGISTERED));
186         }
187
188         if ( !isBooted() ) { EmbeddedDriver.boot(); }
189
190         return _driverModule;
191     }
192     
193     /*
194     ** Record which driver module actually booted.
195     */

196     protected static void registerDriverModule( Driver JavaDoc driver )
197     {
198         _driverModule = driver;
199         _engineForcedDown = false;
200     }
201     
202     /*
203     ** Unregister the driver. This happens when the engine is
204     ** forcibly shut down.
205     */

206     protected static void unregisterDriverModule()
207     {
208         _driverModule = null;
209         _engineForcedDown = true;
210     }
211     
212
213     /*
214     ** Return true if the engine has been booted.
215     */

216     private static boolean isBooted()
217     {
218         return ( _driverModule != null );
219     }
220     
221 }
222
223
Popular Tags