KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.jdbc.Driver20
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 org.apache.derby.iapi.reference.Attribute;
25 import org.apache.derby.iapi.reference.MessageId;
26 import org.apache.derby.iapi.reference.Property;
27 import org.apache.derby.iapi.reference.SQLState;
28
29 import org.apache.derby.impl.jdbc.EmbedConnection;
30
31 import org.apache.derby.iapi.services.sanity.SanityManager;
32 import org.apache.derby.iapi.error.StandardException;
33 import org.apache.derby.iapi.sql.ResultSet;
34 import org.apache.derby.iapi.jdbc.BrokeredConnection;
35 import org.apache.derby.iapi.jdbc.BrokeredConnectionControl;
36 import org.apache.derby.iapi.services.i18n.MessageService;
37 import org.apache.derby.iapi.services.monitor.Monitor;
38 import org.apache.derby.iapi.services.io.FormatableProperties;
39
40 import org.apache.derby.impl.jdbc.*;
41
42 import java.sql.Connection JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.sql.Driver JavaDoc;
45 import java.sql.DriverManager JavaDoc;
46 import java.sql.DriverPropertyInfo JavaDoc;
47
48 import java.util.Properties JavaDoc;
49
50 /**
51     This class extends the local JDBC driver in order to determine at JBMS
52     boot-up if the JVM that runs us does support JDBC 2.0. If it is the case
53     then we will load the appropriate class(es) that have JDBC 2.0 new public
54     methods and sql types.
55 */

56
57 public class Driver20 extends InternalDriver implements Driver JavaDoc {
58
59     private static final String JavaDoc[] BOOLEAN_CHOICES = {"false", "true"};
60
61     private Class JavaDoc antiGCDriverManager;
62
63     /*
64     ** Methods from ModuleControl
65     */

66
67     public void boot(boolean create, Properties properties) throws StandardException {
68
69         super.boot(create, properties);
70
71         // Register with the driver manager
72
AutoloadedDriver.registerDriverModule( this );
73
74         // hold onto the driver manager to avoid its being garbage collected.
75
// make sure the class is loaded by using .class
76
antiGCDriverManager = java.sql.DriverManager JavaDoc.class;
77     }
78
79     public void stop() {
80
81         super.stop();
82
83         AutoloadedDriver.unregisterDriverModule();
84     }
85
86     /**
87      * Get a new nested connection.
88      *
89      * @param conn The EmbedConnection.
90      *
91      * @return A nested connection object.
92      *
93      */

94     public Connection getNewNestedConnection(EmbedConnection conn)
95     {
96         return new EmbedConnection(conn);
97     }
98
99     /*
100         Methods to be overloaded in sub-implementations such as
101         a tracing driver.
102      */

103     public EmbedConnection getNewEmbedConnection(String JavaDoc url, Properties info)
104          throws SQLException JavaDoc
105     {
106         // make a new local connection with a new transaction resource
107
return new EmbedConnection(this, url, info);
108     }
109
110     /**
111         @exception SQLException if fails to create statement
112      */

113     public java.sql.PreparedStatement JavaDoc
114     newEmbedPreparedStatement (EmbedConnection conn,
115                                String JavaDoc stmt,
116                                boolean forMetaData,
117                                int resultSetType,
118                                int resultSetConcurrency,
119                                int resultSetHoldability,
120                                int autoGeneratedKeys,
121                                int[] columnIndexes,
122                                String JavaDoc[] columnNames)
123          throws SQLException JavaDoc
124     {
125         return new EmbedPreparedStatement20(conn, stmt, forMetaData,
126                                           resultSetType, resultSetConcurrency, resultSetHoldability,
127                                           autoGeneratedKeys, columnIndexes, columnNames);
128     }
129
130     /**
131         @exception SQLException if fails to create statement
132      */

133     public java.sql.CallableStatement JavaDoc newEmbedCallableStatement(
134                 EmbedConnection conn,
135                 String JavaDoc stmt,
136                 int resultSetType,
137                 int resultSetConcurrency,
138                 int resultSetHoldability)
139         throws SQLException JavaDoc
140     {
141         return new EmbedCallableStatement20(conn, stmt,
142                                           resultSetType, resultSetConcurrency, resultSetHoldability);
143     }
144   
145     public org.apache.derby.impl.jdbc.EmbedResultSet
146     newEmbedResultSet(EmbedConnection conn, ResultSet results, boolean forMetaData, org.apache.derby.impl.jdbc.EmbedStatement statement, boolean isAtomic)
147         throws SQLException JavaDoc
148     {
149         return new EmbedResultSet20(conn, results, forMetaData, statement,
150                                  isAtomic);
151     }
152     public BrokeredConnection newBrokeredConnection(BrokeredConnectionControl control) {
153
154         return new BrokeredConnection(control);
155     }
156     /**
157      * <p>The getPropertyInfo method is intended to allow a generic GUI tool to
158      * discover what properties it should prompt a human for in order to get
159      * enough information to connect to a database. Note that depending on
160      * the values the human has supplied so far, additional values may become
161      * necessary, so it may be necessary to iterate though several calls
162      * to getPropertyInfo.
163      *
164      * @param url The URL of the database to connect to.
165      * @param info A proposed list of tag/value pairs that will be sent on
166      * connect open.
167      * @return An array of DriverPropertyInfo objects describing possible
168      * properties. This array may be an empty array if no properties
169      * are required.
170      * @exception SQLException if a database-access error occurs.
171      */

172     public DriverPropertyInfo JavaDoc[] getPropertyInfo(String JavaDoc url, Properties info) throws SQLException JavaDoc {
173
174         // RESOLVE other properties should be added into this method in the future ...
175

176         if (info != null) {
177             if (Boolean.valueOf(info.getProperty(Attribute.SHUTDOWN_ATTR)).booleanValue()) {
178     
179                 // no other options possible when shutdown is set to be true
180
return new DriverPropertyInfo JavaDoc[0];
181             }
182         }
183
184         // at this point we have databaseName,
185

186         String JavaDoc dbname = InternalDriver.getDatabaseName(url, info);
187
188         // convert the ;name=value attributes in the URL into
189
// properties.
190
FormatableProperties finfo = getAttributes(url, info);
191         info = null; // ensure we don't use this reference directly again.
192
boolean encryptDB = Boolean.valueOf(finfo.getProperty(Attribute.DATA_ENCRYPTION)).booleanValue();
193         String JavaDoc encryptpassword = finfo.getProperty(Attribute.BOOT_PASSWORD);
194
195         if (dbname.length() == 0 || (encryptDB = true && encryptpassword == null)) {
196
197             // with no database name we can have shutdown or a database name
198

199             // In future, if any new attribute info needs to be included in this
200
// method, it just has to be added to either string or boolean or secret array
201
// depending on whether it accepts string or boolean or secret(ie passwords) value.
202

203             String JavaDoc[][] connStringAttributes = {
204                 {Attribute.DBNAME_ATTR, MessageId.CONN_DATABASE_IDENTITY},
205                 {Attribute.CRYPTO_PROVIDER, MessageId.CONN_CRYPTO_PROVIDER},
206                 {Attribute.CRYPTO_ALGORITHM, MessageId.CONN_CRYPTO_ALGORITHM},
207                 {Attribute.CRYPTO_KEY_LENGTH, MessageId.CONN_CRYPTO_KEY_LENGTH},
208                 {Attribute.CRYPTO_EXTERNAL_KEY, MessageId.CONN_CRYPTO_EXTERNAL_KEY},
209                 {Attribute.TERRITORY, MessageId.CONN_LOCALE},
210                 {Attribute.USERNAME_ATTR, MessageId.CONN_USERNAME_ATTR},
211                 {Attribute.LOG_DEVICE, MessageId.CONN_LOG_DEVICE},
212                 {Attribute.ROLL_FORWARD_RECOVERY_FROM, MessageId.CONN_ROLL_FORWARD_RECOVERY_FROM},
213                 {Attribute.CREATE_FROM, MessageId.CONN_CREATE_FROM},
214                 {Attribute.RESTORE_FROM, MessageId.CONN_RESTORE_FROM},
215             };
216
217             String JavaDoc[][] connBooleanAttributes = {
218                 {Attribute.SHUTDOWN_ATTR, MessageId.CONN_SHUT_DOWN_CLOUDSCAPE},
219                 {Attribute.CREATE_ATTR, MessageId.CONN_CREATE_DATABASE},
220                 {Attribute.DATA_ENCRYPTION, MessageId.CONN_DATA_ENCRYPTION},
221                 {Attribute.UPGRADE_ATTR, MessageId.CONN_UPGRADE_DATABASE},
222                 };
223
224             String JavaDoc[][] connStringSecretAttributes = {
225                 {Attribute.BOOT_PASSWORD, MessageId.CONN_BOOT_PASSWORD},
226                 {Attribute.PASSWORD_ATTR, MessageId.CONN_PASSWORD_ATTR},
227                 };
228
229             
230             DriverPropertyInfo JavaDoc[] optionsNoDB = new DriverPropertyInfo JavaDoc[connStringAttributes.length+
231                                                                       connBooleanAttributes.length+
232                                                                       connStringSecretAttributes.length];
233             
234             int attrIndex = 0;
235             for( int i = 0; i < connStringAttributes.length; i++, attrIndex++ )
236             {
237                 optionsNoDB[attrIndex] = new DriverPropertyInfo JavaDoc(connStringAttributes[i][0],
238                                       finfo.getProperty(connStringAttributes[i][0]));
239                 optionsNoDB[attrIndex].description = MessageService.getTextMessage(connStringAttributes[i][1]);
240             }
241
242             optionsNoDB[0].choices = Monitor.getMonitor().getServiceList(Property.DATABASE_MODULE);
243             // since database name is not stored in FormatableProperties, we
244
// assign here explicitly
245
optionsNoDB[0].value = dbname;
246
247             for( int i = 0; i < connStringSecretAttributes.length; i++, attrIndex++ )
248             {
249                 optionsNoDB[attrIndex] = new DriverPropertyInfo JavaDoc(connStringSecretAttributes[i][0],
250                                       (finfo.getProperty(connStringSecretAttributes[i][0]) == null? "" : "****"));
251                 optionsNoDB[attrIndex].description = MessageService.getTextMessage(connStringSecretAttributes[i][1]);
252             }
253
254             for( int i = 0; i < connBooleanAttributes.length; i++, attrIndex++ )
255             {
256                 optionsNoDB[attrIndex] = new DriverPropertyInfo JavaDoc(connBooleanAttributes[i][0],
257                     Boolean.valueOf(finfo == null? "" : finfo.getProperty(connBooleanAttributes[i][0])).toString());
258                 optionsNoDB[attrIndex].description = MessageService.getTextMessage(connBooleanAttributes[i][1]);
259                 optionsNoDB[attrIndex].choices = BOOLEAN_CHOICES;
260             }
261
262             return optionsNoDB;
263         }
264
265         return new DriverPropertyInfo JavaDoc[0];
266     }
267 }
268
Popular Tags