KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > sessions > DirectConnector


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.sessions;
23
24 import java.util.*;
25 import java.security.AccessController JavaDoc;
26 import java.security.PrivilegedActionException JavaDoc;
27 import java.sql.*;
28 import oracle.toplink.essentials.exceptions.*;
29 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper;
30 import oracle.toplink.essentials.internal.security.PrivilegedNewInstanceFromClass;
31
32 /**
33  * <p>
34  * <b>Purpose</b>:Use this Connector to build a java.sql.Connection by
35  * directly instantiating the Driver, as opposed to using the DriverManager.
36  *
37  * @author Big Country
38  * @since TOPLink/Java 2.1
39  */

40 public class DirectConnector extends DefaultConnector {
41
42     /** cache up the instantiated Driver to speed up reconnects */
43     protected Driver cachedInstance;
44
45     /**
46      * PUBLIC:
47      * Construct a Connector with default settings (Sun JDBC-ODBC bridge).
48      * Although this does not really make sense for a "direct" Connector -
49      * the Sun JdbcOdbcDriver works fine with the DriverManager.
50      */

51     public DirectConnector() {
52         super();
53     }
54
55     /**
56      * PUBLIC:
57      * Construct a Connector with the specified settings.
58      */

59     public DirectConnector(String JavaDoc driverClassName, String JavaDoc driverURLHeader, String JavaDoc databaseURL) {
60         super(driverClassName, driverURLHeader, databaseURL);
61     }
62
63     /**
64      * INTERNAL:
65      * Connect with the specified properties and return the Connection.
66      * @return java.sql.Connection
67      */

68     public Connection connect(Properties properties) throws DatabaseException {
69         try {
70             return this.instantiateDriver(this.loadDriver()).connect(this.getConnectionString(), properties);
71         } catch (SQLException exception) {
72             throw DatabaseException.sqlException(exception);
73         }
74     }
75
76     /**
77      * INTERNAL:
78      * Instantiate the Driver if necessary.
79      * @return java.sql.Driver
80      */

81     protected Driver instantiateDriver(Class JavaDoc driverClass) throws DatabaseException {
82         if (cachedInstance != null) {
83             return cachedInstance;
84         }
85
86         try {
87             if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
88                 try {
89                     cachedInstance = (Driver)AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(driverClass));
90                 } catch (PrivilegedActionException JavaDoc exception) {
91                     Exception JavaDoc throwableException = exception.getException();
92                     if (throwableException instanceof InstantiationException JavaDoc) {
93                         throw DatabaseException.configurationErrorNewInstanceInstantiationException((InstantiationException JavaDoc)throwableException, driverClass);
94                     } else {
95                         throw DatabaseException.configurationErrorNewInstanceIllegalAccessException((IllegalAccessException JavaDoc)throwableException, driverClass);
96                     }
97                 }
98             } else {
99                 cachedInstance = (Driver)PrivilegedAccessHelper.newInstanceFromClass(driverClass);
100             }
101             return cachedInstance;
102         } catch (InstantiationException JavaDoc ie) {
103             throw DatabaseException.configurationErrorNewInstanceInstantiationException(ie, driverClass);
104         } catch (IllegalAccessException JavaDoc iae) {
105             throw DatabaseException.configurationErrorNewInstanceIllegalAccessException(iae, driverClass);
106         }
107     }
108 }
109
Popular Tags