KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.*;
29 import oracle.toplink.essentials.internal.helper.*;
30 import oracle.toplink.essentials.exceptions.*;
31 import oracle.toplink.essentials.internal.localization.*;
32 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper;
33 import oracle.toplink.essentials.internal.security.PrivilegedClassForName;
34
35 /**
36  * <p>
37  * <b>Purpose</b>:Use this Connector to build a java.sql.Connection in the
38  * "standard" fashion, via the DriverManager.
39  *
40  * @author Big Country
41  * @since TOPLink/Java 2.1
42  */

43 public class DefaultConnector implements Connector {
44     protected String JavaDoc driverClassName;
45     protected String JavaDoc driverURLHeader;
46     protected String JavaDoc databaseURL;
47
48     /**
49      * PUBLIC:
50      * Construct a Connector with default settings (Sun JDBC-ODBC bridge).
51      * The database URL will still need to be set.
52      */

53     public DefaultConnector() {
54         this("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:", "");
55     }
56
57     /**
58      * PUBLIC:
59      * Construct a Connector with the specified settings.
60      */

61     public DefaultConnector(String JavaDoc driverClassName, String JavaDoc driverURLHeader, String JavaDoc databaseURL) {
62         this.initialize(driverClassName, driverURLHeader, databaseURL);
63     }
64
65     /**
66      * INTERNAL:
67      * Clone the connector.
68      */

69     public Object JavaDoc clone() {
70         try {
71             return super.clone();
72         } catch (Exception JavaDoc exception) {
73             throw new InternalError JavaDoc("Clone failed");
74         }
75     }
76
77     /**
78      * INTERNAL:
79      * Connect with the specified properties and return the Connection.
80      * @return java.sql.Connection
81      */

82     public Connection connect(Properties properties) throws DatabaseException {
83         this.loadDriver();// ensure the driver has been loaded and registered
84
try {
85             return DriverManager.getConnection(this.getConnectionString(), properties);
86         } catch (SQLException exception) {
87             throw DatabaseException.sqlException(exception);
88         }
89     }
90
91     /**
92      * PUBLIC:
93      * Return the JDBC connection string.
94      * This is a combination of the driver-specific URL header and the database URL.
95      */

96     public String JavaDoc getConnectionString() {
97         return this.getDriverURLHeader() + this.getDatabaseURL();
98     }
99
100     /**
101      * PUBLIC:
102      * Provide the details of my connection information. This is primarily for JMX runtime services.
103      * @return java.lang.String
104      */

105     public String JavaDoc getConnectionDetails() {
106         return this.getConnectionString();
107     }
108
109     /**
110      * PUBLIC:
111      * The database URL is the JDBC URL for the database server.
112      * The driver header is <i>not</i> be included in this URL
113      * (e.g. "dbase files"; not "jdbc:odbc:dbase files").
114      */

115     public String JavaDoc getDatabaseURL() {
116         return databaseURL;
117     }
118
119     /**
120      * PUBLIC:
121      * The driver class is the name of the Java class for the JDBC driver being used
122      * (e.g. "sun.jdbc.odbc.JdbcOdbcDriver").
123      */

124     public String JavaDoc getDriverClassName() {
125         return driverClassName;
126     }
127
128     /**
129      * PUBLIC:
130      * The driver URL header is the string predetermined by the JDBC driver to be
131      * part of the URL connection string, (e.g. "jdbc:odbc:").
132      * This is required to connect to the database.
133      */

134     public String JavaDoc getDriverURLHeader() {
135         return driverURLHeader;
136     }
137
138     /**
139      * INTERNAL:
140      * Initialize the connector with the specified settings.
141      */

142     protected void initialize(String JavaDoc driverClassName, String JavaDoc driverURLHeader, String JavaDoc databaseURL) {
143         this.setDriverClassName(driverClassName);
144         this.setDriverURLHeader(driverURLHeader);
145         this.setDatabaseURL(databaseURL);
146     }
147
148     /**
149      * INTERNAL:
150      * Ensure that the driver has been loaded and registered with the
151      * DriverManager. Just loading the class should cause the static
152      * initialization code to do the necessary registration.
153      * Return the loaded driver Class.
154      */

155     protected Class JavaDoc loadDriver() throws DatabaseException {
156         // CR#... The correct class loader must be used to load the class,
157
// not that Class.forName must be used to initialize the class a simple loadClass may not.
158
try {
159             if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
160                 try {
161                     return (Class JavaDoc)AccessController.doPrivileged(new PrivilegedClassForName(this.getDriverClassName(), true, ConversionManager.getDefaultManager().getLoader()));
162                 } catch (PrivilegedActionException JavaDoc exception) {
163                     throw DatabaseException.configurationErrorClassNotFound(this.getDriverClassName()); }
164             } else {
165                 return oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(this.getDriverClassName(), true, ConversionManager.getDefaultManager().getLoader());
166             }
167         } catch (ClassNotFoundException JavaDoc exception) {
168             throw DatabaseException.configurationErrorClassNotFound(this.getDriverClassName());
169         }
170     }
171
172     /**
173      * PUBLIC:
174      * The database URL is the JDBC URL for the database server.
175      * The driver header is <i>not</i> be included in this URL
176      * (e.g. "dbase files"; not "jdbc:odbc:dbase files").
177      */

178     public void setDatabaseURL(String JavaDoc databaseURL) {
179         this.databaseURL = databaseURL;
180     }
181
182     /**
183      * PUBLIC:
184      * The driver class is the name of the Java class for the JDBC driver being used
185      * (e.g. "sun.jdbc.odbc.JdbcOdbcDriver").
186      */

187     public void setDriverClassName(String JavaDoc driverClassName) {
188         this.driverClassName = driverClassName;
189     }
190
191     /**
192      * PUBLIC:
193      * The driver URL header is the string predetermined by the JDBC driver to be
194      * part of the URL connection string, (e.g. "jdbc:odbc:").
195      * This is required to connect to the database.
196      */

197     public void setDriverURLHeader(String JavaDoc driverURLHeader) {
198         this.driverURLHeader = driverURLHeader;
199     }
200
201     /**
202      * PUBLIC:
203      * Print connection string.
204      */

205     public String JavaDoc toString() {
206         return oracle.toplink.essentials.internal.helper.Helper.getShortClassName(getClass()) + "(" + getConnectionString() + ")";
207     }
208
209     /**
210      * INTERNAL:
211      * Print something useful on the log.
212      */

213     public void toString(PrintWriter writer) {
214         writer.println(ToStringLocalization.buildMessage("datasource_URL", (Object JavaDoc[])null) + "=> \"" + this.getConnectionString() + "\"");
215     }
216 }
217
Popular Tags