1 | /* |
2 | |
3 | Derby - Class org.apache.derby.jdbc.EmbeddedDriver |
4 | |
5 | Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.jdbc; |
22 | |
23 | import java.sql.DriverManager; |
24 | import java.sql.Driver; |
25 | import java.sql.Connection; |
26 | import java.sql.DriverPropertyInfo; |
27 | import java.sql.SQLException; |
28 | |
29 | import java.io.PrintStream; |
30 | import java.util.Properties; |
31 | |
32 | import org.apache.derby.iapi.reference.MessageId; |
33 | import org.apache.derby.iapi.reference.Attribute; |
34 | import org.apache.derby.iapi.services.i18n.MessageService; |
35 | import org.apache.derby.iapi.jdbc.JDBCBoot; |
36 | |
37 | |
38 | /** |
39 | The embedded JDBC driver (Type 4) for Derby. |
40 | <P> |
41 | The driver automatically supports the correct JDBC specification version |
42 | for the Java Virtual Machine's environment. |
43 | <UL> |
44 | <LI> JDBC 3.0 - Java 2 - JDK 1.4, J2SE 5.0 |
45 | <LI> JDBC 2.0 - Java 2 - JDK 1.2,1.3 |
46 | </UL> |
47 | |
48 | <P> |
49 | Loading this JDBC driver boots the database engine |
50 | within the same Java virtual machine. |
51 | <P> |
52 | The correct code to load the Derby engine using this driver is |
53 | (with approriate try/catch blocks): |
54 | <PRE> |
55 | Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); |
56 | |
57 | // or |
58 | |
59 | new org.apache.derby.jdbc.EmbeddedDriver(); |
60 | |
61 | |
62 | </PRE> |
63 | When loaded in this way, the class boots the actual JDBC driver indirectly. |
64 | The JDBC specification recommends the Class.ForName method without the .newInstance() |
65 | method call, but adding the newInstance() guarantees |
66 | that Derby will be booted on any Java Virtual Machine. |
67 | |
68 | <P> |
69 | Any initial error messages are placed in the PrintStream |
70 | supplied by the DriverManager. If the PrintStream is null error messages are |
71 | sent to System.err. Once the Derby engine has set up an error |
72 | logging facility (by default to derby.log) all subsequent messages are sent to it. |
73 | <P> |
74 | By convention, the class used in the Class.forName() method to |
75 | boot a JDBC driver implements java.sql.Driver. |
76 | |
77 | This class is not the actual JDBC driver that gets registered with |
78 | the Driver Manager. It proxies requests to the registered Derby JDBC driver. |
79 | |
80 | @see java.sql.DriverManager |
81 | @see java.sql.DriverManager#getLogStream |
82 | @see java.sql.Driver |
83 | @see java.sql.SQLException |
84 | */ |
85 | |
86 | public class EmbeddedDriver implements Driver { |
87 | |
88 | static { |
89 | |
90 | EmbeddedDriver.boot(); |
91 | } |
92 | |
93 | // Boot from the constructor as well to ensure that |
94 | // Class.forName(...).newInstance() reboots Derby |
95 | // after a shutdown inside the same JVM. |
96 | public EmbeddedDriver() { |
97 | EmbeddedDriver.boot(); |
98 | } |
99 | |
100 | /* |
101 | ** Methods from java.sql.Driver. |
102 | */ |
103 | /** |
104 | Accept anything that starts with <CODE>jdbc:derby:</CODE>. |
105 | @exception SQLException if a database-access error occurs. |
106 | @see java.sql.Driver |
107 | */ |
108 | public boolean acceptsURL(String url) throws SQLException { |
109 | return getRegisteredDriver().acceptsURL(url); |
110 | } |
111 | |
112 | /** |
113 | Connect to the URL if possible |
114 | @exception SQLException illegal url or problem with connectiong |
115 | @see java.sql.Driver |
116 | */ |
117 | public Connection connect(String url, Properties info) |
118 | throws SQLException |
119 | { |
120 | return getRegisteredDriver().connect(url, info); |
121 | } |
122 | |
123 | /** |
124 | * Returns an array of DriverPropertyInfo objects describing possible properties. |
125 | @exception SQLException if a database-access error occurs. |
126 | @see java.sql.Driver |
127 | */ |
128 | public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) |
129 | throws SQLException |
130 | { |
131 | return getRegisteredDriver().getPropertyInfo(url, info); |
132 | } |
133 | |
134 | /** |
135 | * Returns the driver's major version number. |
136 | @see java.sql.Driver |
137 | */ |
138 | public int getMajorVersion() { |
139 | try { |
140 | return (getRegisteredDriver().getMajorVersion()); |
141 | } |
142 | catch (SQLException se) { |
143 | return 0; |
144 | } |
145 | } |
146 | /** |
147 | * Returns the driver's minor version number. |
148 | @see java.sql.Driver |
149 | */ |
150 | public int getMinorVersion() { |
151 | try { |
152 | return (getRegisteredDriver().getMinorVersion()); |
153 | } |
154 | catch (SQLException se) { |
155 | return 0; |
156 | } |
157 | } |
158 | |
159 | /** |
160 | * Report whether the Driver is a genuine JDBC COMPLIANT (tm) driver. |
161 | @see java.sql.Driver |
162 | */ |
163 | public boolean jdbcCompliant() { |
164 | try { |
165 | return (getRegisteredDriver().jdbcCompliant()); |
166 | } |
167 | catch (SQLException se) { |
168 | return false; |
169 | } |
170 | } |
171 | |
172 | private static void boot() { |
173 | PrintStream ps = DriverManager.getLogStream(); |
174 | |
175 | if (ps == null) |
176 | ps = System.err; |
177 | |
178 | new JDBCBoot().boot(Attribute.PROTOCOL, ps); |
179 | } |
180 | |
181 | /* |
182 | ** Retrieve the actual Registered Driver, |
183 | ** probe the DriverManager in order to get it. |
184 | */ |
185 | private Driver getRegisteredDriver() throws SQLException { |
186 | |
187 | try { |
188 | return DriverManager.getDriver(Attribute.PROTOCOL); |
189 | } |
190 | catch (SQLException se) { |
191 | // Driver not registered |
192 | throw new SQLException(MessageService.getTextMessage(MessageId.CORE_JDBC_DRIVER_UNREGISTERED)); |
193 | } |
194 | } |
195 | } |