KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > jndi > JNDIConnector


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, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.jndi;
23
24 import java.util.*;
25 import java.sql.*;
26 import javax.naming.*;
27 import javax.sql.*;
28 import oracle.toplink.essentials.sessions.*;
29 import oracle.toplink.essentials.exceptions.*;
30 import oracle.toplink.essentials.internal.helper.*;
31 import oracle.toplink.essentials.internal.localization.*;
32
33 /**
34  * Specifies the J2EE DataSource lookup options.
35  * This connector is normally used with a login in a J2EE environment
36  * to connect to a server's connection pool defined by the DataSource name.
37  * The JNDI name that the DataSource is registered under must be specified,
38  * this must include any required prefix such as "java:comp/env/", (unless a DataSource object is given).
39  * A Context is only required if not running on the server, otheriwse default to a new InitialContext().
40  * @author Big Country
41  * @since TOPLink/Java 2.1
42  */

43 public class JNDIConnector implements Connector {
44     protected DataSource dataSource;
45     protected Context context;
46     protected String JavaDoc name;
47     public static final int STRING_LOOKUP = 1;
48     public static final int COMPOSITE_NAME_LOOKUP = 2;
49     public static final int COMPOUND_NAME_LOOKUP = 3;
50         //default setting is composite name to be consistent with previous TopLink versions
51
protected int lookupType = COMPOSITE_NAME_LOOKUP;
52
53     /**
54      * PUBLIC:
55      * Construct a Connector with no settings.
56      * The datasource name will still need to be set.
57      */

58     public JNDIConnector() {
59         super();
60     }
61
62     /**
63      * PUBLIC:
64      * Construct a Connector with the datasource name.
65      */

66     public JNDIConnector(Context context, String JavaDoc name) throws ValidationException {
67         this(name);
68         this.context = context;
69     }
70
71     /**
72      * PUBLIC:
73      * Construct a Connector with the datasource name.
74      */

75     public JNDIConnector(String JavaDoc name) {
76         this.name = name;
77     }
78
79     /**
80      * PUBLIC:
81      * Construct a Connector with the datasource object.
82      */

83     public JNDIConnector(DataSource dataSource) {
84         this.dataSource = dataSource;
85     }
86
87     /**
88      * INTERNAL:
89      * Clone the connector.
90      */

91     public Object JavaDoc clone() {
92         try {
93             return super.clone();
94         } catch (Exception JavaDoc exception) {
95             throw new InternalError JavaDoc("Clone failed");
96         }
97     }
98
99     /**
100      * INTERNAL:
101      * Connect with the specified properties and return the Connection.
102      */

103     public Connection connect(Properties properties) throws DatabaseException, ValidationException {
104         String JavaDoc user = properties.getProperty("user");
105         String JavaDoc password = properties.getProperty("password");
106
107         DataSource dataSource = getDataSource();
108         if (dataSource == null) {
109             try {
110                 //bug#2761428 and 4405389 JBoss needs to look up datasources based on a string not a composite or compound name
111
if (lookupType == STRING_LOOKUP) {
112                     dataSource = (DataSource)getContext().lookup(getName());
113                 } else if (lookupType == COMPOSITE_NAME_LOOKUP) {
114                     dataSource = (DataSource)getContext().lookup(new CompositeName(name));
115                 } else {
116                     dataSource = (DataSource)getContext().lookup(new CompoundName(name, new Properties()));
117                 }
118                 this.setDataSource(dataSource);
119             } catch (NamingException exception) {
120                 throw ValidationException.cannotAcquireDataSource(getName(), exception);
121             }
122         }
123
124         try {
125             // WebLogic connection pools do not require a user name and password.
126
// JDBCLogin usually initializes these values with an empty string.
127
// WebLogic data source does not support the getConnection() call with arguments
128
// it only supports the zero argument call. DM 26/07/2000
129
if ((user == null) || (user.equalsIgnoreCase(""))) {
130                 return dataSource.getConnection();
131             } else {
132                 return dataSource.getConnection(user, password);
133             }
134         } catch (SQLException exception) {
135             throw DatabaseException.sqlException(exception);
136         }
137     }
138
139     /**
140      * PUBLIC:
141      * Return the JNDI Context that can supplied the named DataSource.
142      */

143     public Context getContext() {
144         if (context == null) {
145             try {
146                 context = new InitialContext();
147             } catch (NamingException exception) {
148             }
149         }
150         return context;
151     }
152
153     /**
154      * PUBLIC:
155      * Return the javax.sql.DataSource.
156      */

157     public DataSource getDataSource() {
158         return dataSource;
159     }
160
161     /**
162      * PUBLIC:
163      * Return the name of the DataSource within the
164      * JNDI Context.
165      */

166     public String JavaDoc getName() {
167         return name;
168     }
169
170     /**
171      * PUBLIC:
172      * Provide the details of my connection information. This is primarily for JMX runtime services.
173      * @return java.lang.String
174      */

175     public String JavaDoc getConnectionDetails() {
176         return getName();
177     }
178
179     /**
180      * PUBLIC:
181      * Set the JNDI Context that can supply the named DataSource.
182      */

183     public void setContext(Context context) {
184         this.context = context;
185     }
186
187     /**
188      * PUBLIC:
189      * Set the javax.sql.DataSource.
190      */

191     public void setDataSource(DataSource dataSource) {
192         this.dataSource = dataSource;
193     }
194
195     /**
196      * PUBLIC:
197      * Set the name of the DataSource within the
198      * JNDI Context.
199      */

200     public void setName(String JavaDoc name) throws ValidationException {
201         this.name = name;
202     }
203
204     public void setLookupType(int lookupType) {
205         this.lookupType = lookupType;
206     }
207
208     public int getLookupType() {
209         return lookupType;
210     }
211
212     /**
213      * PUBLIC:
214      * Print data source info.
215      */

216     public String JavaDoc toString() {
217         return Helper.getShortClassName(getClass()) + ToStringLocalization.buildMessage("datasource_name", (Object JavaDoc[])null) + "=>" + getName();
218     }
219
220     /**
221      * INTERNAL:
222      * Print something useful on the log.
223      */

224     public void toString(java.io.PrintWriter JavaDoc writer) {
225         writer.print(ToStringLocalization.buildMessage("connector", (Object JavaDoc[])null) + "=>" + Helper.getShortClassName(getClass()));
226         writer.print(" ");
227         writer.println(ToStringLocalization.buildMessage("datasource_name", (Object JavaDoc[])null) + "=>" + getName());
228     }
229 }
230
Popular Tags