KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > gjc > spi > DMManagedConnectionFactory


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 in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
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 Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.gjc.spi;
25
26 import javax.resource.ResourceException JavaDoc;
27 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
28 import com.sun.gjc.util.SecurityUtils;
29 import javax.resource.spi.security.PasswordCredential JavaDoc;
30 import java.sql.DriverManager JavaDoc;
31 import com.sun.gjc.spi.ManagedConnectionFactory;
32 import com.sun.gjc.common.DataSourceSpec;
33 import java.util.Properties JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35 import java.util.NoSuchElementException JavaDoc;
36 import com.sun.logging.*;
37 import java.util.logging.Logger JavaDoc;
38 import java.util.logging.Level JavaDoc;
39
40 /**
41  * Driver Manager <code>ManagedConnectionFactory</code> implementation for Generic JDBC Connector.
42  *
43  * @version 1.0, 02/07/31
44  * @author Evani Sai Surya Kiran
45  */

46
47 public class DMManagedConnectionFactory extends ManagedConnectionFactory {
48     
49     Properties JavaDoc props;
50
51     private static Logger JavaDoc _logger;
52     static {
53         _logger = LogDomains.getLogger( LogDomains.RSR_LOGGER );
54     }
55     private boolean debug = false;
56
57     /**
58      * Creates a new physical connection to the underlying EIS resource
59      * manager.
60      *
61      * @param subject <code>Subject</code> instance passed by the application server
62      * @param cxRequestInfo <code>ConnectionRequestInfo</code> which may be created
63      * as a result of the invocation <code>getConnection(user, password)</code>
64      * on the <code>DataSource</code> object
65      * @return <code>ManagedConnection</code> object created
66      * @throws ResourceException if there is an error in instantiating the
67      * <code>DataSource</code> object used for the
68      * creation of the <code>ManagedConnection</code> object
69      * @throws SecurityException if there ino <code>PasswordCredential</code> object
70      * satisfying this request
71      * @throws ResourceAllocationException if there is an error in allocating the
72      * physical connection
73      */

74     public javax.resource.spi.ManagedConnection JavaDoc createManagedConnection(javax.security.auth.Subject JavaDoc subject,
75         ConnectionRequestInfo cxRequestInfo) throws ResourceException JavaDoc {
76         if(logWriter != null) {
77             logWriter.println("In createManagedConnection");
78         }
79         
80         PasswordCredential JavaDoc pc = SecurityUtils.getPasswordCredential(this, subject, cxRequestInfo);
81
82     try {
83         Thread.currentThread().getContextClassLoader().loadClass(spec.getDetail(DataSourceSpec.CLASSNAME));
84     } catch(ClassNotFoundException JavaDoc cnfe) {
85         _logger.log(Level.SEVERE, "jdbc.exc_cnfe");
86         throw new ResourceException JavaDoc("The driver could not be loaded: " + cnfe.getMessage());
87     }
88         
89         java.sql.Connection JavaDoc dsConn = null;
90         
91         Properties JavaDoc driverProps = getPropertiesObj();
92         
93         try {
94             if(cxRequestInfo != null) {
95                 driverProps.setProperty("user", pc.getUserName());
96                 driverProps.setProperty("password", new String JavaDoc(pc.getPassword()));
97             }
98             
99             dsConn = DriverManager.getConnection(spec.getDetail(DataSourceSpec.URL), driverProps);
100             
101         } catch(java.sql.SQLException JavaDoc sqle) {
102         _logger.log(Level.SEVERE, "jdbc.exc_create_mc", sqle);
103             throw new javax.resource.spi.ResourceAllocationException JavaDoc("The connection could not be allocated: " +
104                 sqle.getMessage());
105         }
106         
107         com.sun.gjc.spi.ManagedConnection mc = constructManagedConnection(
108             null, dsConn, pc, this);
109
110         //GJCINT
111
setIsolation(mc);
112         isValid(mc);
113         return mc;
114     }
115     
116     /**
117      * This method checks if the properties object is null or not.
118      * If the properties object is null, it creates a new Properties
119      * object and inserts the default "user" and "password" key value
120      * pairs. It checks if any other properties have been set or not
121      * and includes the key value pairs for those properties.
122      *
123      * @return props <code>Properties</code> object conatining properties for getting a connection
124      * @throws ResourceException if the driver properties string and delimiter are not proper
125      */

126     private Properties JavaDoc getPropertiesObj() throws ResourceException JavaDoc {
127         if(props != null) {
128             return props;
129         }
130         
131         props = new Properties JavaDoc();
132         props.setProperty("user", getuser());
133         props.setProperty("password", getpassword());
134         
135         String JavaDoc driverProps = spec.getDetail(DataSourceSpec.DRIVERPROPERTIES);
136         String JavaDoc delimiter = spec.getDetail(DataSourceSpec.DELIMITER);
137         
138         if(driverProps != null && driverProps.trim().equals("") == false) {
139             if(delimiter == null || delimiter.equals("")) {
140                 throw new ResourceException JavaDoc("Invalid driver properties string - " +
141                     "delimiter not properly set!!");
142             }
143             
144             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(driverProps, delimiter);
145             while (st.hasMoreTokens()) {
146                 String JavaDoc keyValuePair = null;
147                 try {
148                     keyValuePair = st.nextToken();
149                 } catch(NoSuchElementException JavaDoc nsee) {
150                     throw new ResourceException JavaDoc("Invalid driver properties string - " +
151                         "Key value pair not available: " + nsee.getMessage());
152                 }
153                 
154                 int indexOfEqualsSign = -1;
155                 try {
156                     indexOfEqualsSign = keyValuePair.indexOf("=");
157                     if(indexOfEqualsSign == -1) {
158                         throw new ResourceException JavaDoc("Invalid driver properties string - " +
159                             "Key value pair should be of the form key = value");
160                     }
161                 } catch(NullPointerException JavaDoc npe) {
162                 if (debug) {
163                     _logger.log(Level.FINE, "jdbc.exc_caught_ign", npe.getMessage() );
164                 }
165                     
166                 }
167                 
168                 String JavaDoc key = null;
169                 try {
170                     key = keyValuePair.substring(0, indexOfEqualsSign).trim();
171                 } catch(IndexOutOfBoundsException JavaDoc iobe) {
172                 if (debug) {
173                     _logger.log(Level.FINE, "jdbc.exc_caught_ign", iobe.getMessage() );
174                 }
175                 }
176                 if(key.equals("")) {
177                     throw new ResourceException JavaDoc("Invalid driver properties string - " +
178                         "Key cannot be an empty string");
179                 }
180                 
181                 String JavaDoc value = null;
182                 try {
183                     value = keyValuePair.substring(indexOfEqualsSign+1).trim();
184                 } catch(IndexOutOfBoundsException JavaDoc iobe) {
185                 if (debug) {
186                     _logger.log(Level.FINE, "jdbc.exc_caught_ign", iobe.getMessage() );
187                 }
188                 }
189                 
190                 props.setProperty(key, value);
191             }
192         }
193         
194         return props;
195     }
196     
197     /**
198      * Check if this <code>ManagedConnectionFactory</code> is equal to
199      * another <code>ManagedConnectionFactory</code>.
200      *
201      * @param other <code>ManagedConnectionFactory</code> object for checking equality with
202      * @return true if the property sets of both the
203      * <code>ManagedConnectionFactory</code> objects are the same
204      * false otherwise
205      */

206     public boolean equals(Object JavaDoc other) {
207         if(logWriter != null) {
208             logWriter.println("In equals");
209         }
210         
211         /**
212          * The check below means that two ManagedConnectionFactory objects are equal
213          * if and only if their properties are the same.
214          */

215         if(other instanceof com.sun.gjc.spi.DMManagedConnectionFactory) {
216             com.sun.gjc.spi.DMManagedConnectionFactory otherMCF =
217                 (com.sun.gjc.spi.DMManagedConnectionFactory) other;
218             return this.spec.equals(otherMCF.spec);
219         }
220         return false;
221     }
222     
223     /**
224      * Sets the login timeout.
225      *
226      * @param loginTimeOut <code>String</code>
227      * @see <code>getLoginTimeOut</code>
228      */

229     public void setloginTimeOut(String JavaDoc loginTimeOut) {
230         int timeOut = 0;
231         try {
232             timeOut = Integer.valueOf(loginTimeOut).intValue();
233             DriverManager.setLoginTimeout(timeOut);
234             spec.setDetail(DataSourceSpec.LOGINTIMEOUT, loginTimeOut);
235         } catch(Exception JavaDoc e) {
236         if (debug) {
237             _logger.log(Level.FINE, "jdbc.exc_caught_ign", e.getMessage() );
238         }
239         }
240     }
241     
242     /**
243      * Gets the login timeout.
244      *
245      * @return loginTimeout
246      * @see <code>setLoginTimeOut</code>
247      */

248     public String JavaDoc getloginTimeOut() {
249         return spec.getDetail(DataSourceSpec.LOGINTIMEOUT);
250     }
251     
252     /**
253      * Sets the login timeout.
254      *
255      * @param loginTimeOut <code>String</code>
256      * @see <code>getLoginTimeOut</code>
257      */

258     public void setLoginTimeOut(String JavaDoc loginTimeOut) {
259         int timeOut = 0;
260         try {
261             timeOut = Integer.valueOf(loginTimeOut).intValue();
262             DriverManager.setLoginTimeout(timeOut);
263             spec.setDetail(DataSourceSpec.LOGINTIMEOUT, loginTimeOut);
264         } catch(Exception JavaDoc e) {
265         if (debug) {
266             _logger.log(Level.FINE, "jdbc.exc_caught_ign", e.getMessage() );
267         }
268         }
269     }
270     
271     /**
272      * Gets the login timeout.
273      *
274      * @return loginTimeout
275      * @see <code>setLoginTimeOut</code>
276      */

277     public String JavaDoc getLoginTimeOut() {
278         return spec.getDetail(DataSourceSpec.LOGINTIMEOUT);
279     }
280     
281     /**
282      * Sets the connection url.
283      *
284      * @param url <code>String</code>
285      * @see <code>getConnectionURL</code>
286      */

287     public void setconnectionURL(String JavaDoc url) {
288         spec.setDetail(DataSourceSpec.URL, url);
289     }
290     
291     /**
292      * Gets the connection url.
293      *
294      * @return url
295      * @see <code>setConnectionURL</code>
296      */

297     public String JavaDoc getconnectionURL() {
298         return spec.getDetail(DataSourceSpec.URL);
299     }
300     
301     /**
302      * Sets the connection url.
303      *
304      * @param url <code>String</code>
305      * @see <code>getConnectionURL</code>
306      */

307     public void setConnectionURL(String JavaDoc url) {
308         spec.setDetail(DataSourceSpec.URL, url);
309     }
310     
311     /**
312      * Gets the connection url.
313      *
314      * @return url
315      * @see <code>setConnectionURL</code>
316      */

317     public String JavaDoc getConnectionURL() {
318         return spec.getDetail(DataSourceSpec.URL);
319     }
320     
321     /**
322      * Sets the class name of the driver.
323      *
324      * @param className <code>String</code>
325      * @see <code>getClassName</code>
326      */

327     public void setclassName(String JavaDoc className) {
328         spec.setDetail(DataSourceSpec.CLASSNAME, className);
329     }
330     
331     /**
332      * Gets the class name of the driver.
333      *
334      * @return class name of the driver
335      * @see <code>setClassName</code>
336      */

337     public String JavaDoc getclassName() {
338         return spec.getDetail(DataSourceSpec.CLASSNAME);
339     }
340     
341     /**
342      * Sets the class name of the driver.
343      *
344      * @param className <code>String</code>
345      * @see <code>getClassName</code>
346      */

347     public void setClassName(String JavaDoc className) {
348         spec.setDetail(DataSourceSpec.CLASSNAME, className);
349     }
350     
351     /**
352      * Gets the class name of the driver.
353      *
354      * @return class name of the driver
355      * @see <code>setClassName</code>
356      */

357     public String JavaDoc getClassName() {
358         return spec.getDetail(DataSourceSpec.CLASSNAME);
359     }
360     
361     /**
362      * Sets the driver properties.
363      *
364      * @param driverProps <code>String</code> of key = value pairs separated by a delimiter
365      * @see <code>getDriverProperties</code>
366      */

367     public void setdriverProperties(String JavaDoc driverProps) {
368         spec.setDetail(DataSourceSpec.DRIVERPROPERTIES, driverProps);
369     }
370     
371     /**
372      * Gets the driver properties.
373      *
374      * @return driverProps, which is a <code>String</code> of key = value pairs separated by a delimiter
375      * @see <code>setDriverProperties</code>
376      */

377     public String JavaDoc getdriverProperties() {
378         return spec.getDetail(DataSourceSpec.DRIVERPROPERTIES);
379     }
380     
381     /**
382      * Sets the driver properties.
383      *
384      * @param driverProps <code>String</code> of key = value pairs separated by a delimiter
385      * @see <code>getDriverProperties</code>
386      */

387     public void setDriverProperties(String JavaDoc driverProps) {
388         spec.setDetail(DataSourceSpec.DRIVERPROPERTIES, driverProps);
389     }
390     
391     /**
392      * Gets the driver properties.
393      *
394      * @return driverProps, which is a <code>String</code> of key = value pairs separated by a delimiter
395      * @see <code>setDriverProperties</code>
396      */

397     public String JavaDoc getDriverProperties() {
398         return spec.getDetail(DataSourceSpec.DRIVERPROPERTIES);
399     }
400     
401     /**
402      * Sets the delimiter which separates the key = value pairs
403      * in the driver properties string.
404      *
405      * @param delim <code>String</code>
406      * @see <code>getDelimiter</code>
407      */

408     public void setdelimiter(String JavaDoc delim) {
409         spec.setDetail(DataSourceSpec.DELIMITER, delim);
410     }
411     
412     /**
413      * Gets the delimiter.
414      *
415      * @return the delimiter
416      * @see <code>setDelimiter</code>
417      */

418     public String JavaDoc getdelimiter() {
419         return spec.getDetail(DataSourceSpec.DELIMITER);
420     }
421     
422     /**
423      * Sets the delimiter which separates the key = value pairs
424      * in the driver properties string.
425      *
426      * @param delim <code>String</code>
427      * @see <code>getDelimiter</code>
428      */

429     public void setDelimiter(String JavaDoc delim) {
430         spec.setDetail(DataSourceSpec.DELIMITER, delim);
431     }
432     
433     /**
434      * Gets the delimiter.
435      *
436      * @return the delimiter
437      * @see <code>setDelimiter</code>
438      */

439     public String JavaDoc getDelimiter() {
440         return spec.getDetail(DataSourceSpec.DELIMITER);
441     }
442 }
443
Popular Tags