KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.resource.spi.security.PasswordCredential JavaDoc;
29 import com.sun.gjc.common.DataSourceObjectBuilder;
30 import com.sun.gjc.util.SecurityUtils;
31 import com.sun.logging.*;
32 import java.util.logging.Logger JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import com.sun.enterprise.util.i18n.StringManager;
35 import javax.resource.spi.ResourceAllocationException JavaDoc;
36
37 /**
38  * Data Source <code>ManagedConnectionFactory</code> implementation for Generic JDBC Connector.
39  *
40  * @version 1.0, 02/07/30
41  * @author Evani Sai Surya Kiran
42  */

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

70     public javax.resource.spi.ManagedConnection JavaDoc createManagedConnection(javax.security.auth.Subject JavaDoc subject,
71         ConnectionRequestInfo cxRequestInfo) throws ResourceException JavaDoc {
72         if(logWriter != null) {
73                 logWriter.println("In createManagedConnection");
74         }
75         PasswordCredential JavaDoc pc = SecurityUtils.getPasswordCredential(this, subject, cxRequestInfo);
76         
77         if(dataSourceObj == null) {
78             if(dsObjBuilder == null) {
79                 dsObjBuilder = new DataSourceObjectBuilder(spec);
80             }
81         
82             try {
83                 dataSourceObj = (javax.sql.DataSource JavaDoc) dsObjBuilder.constructDataSourceObject();
84             } catch(ClassCastException JavaDoc cce) {
85             _logger.log(Level.SEVERE, "jdbc.exc_cce", cce);
86                 throw new javax.resource.ResourceException JavaDoc(cce.getMessage());
87             }
88         }
89         
90         java.sql.Connection JavaDoc dsConn = null;
91         
92         try {
93         /* For the case where the user/passwd of the connection pool is
94          * equal to the PasswordCredential for the connection request
95          * get a connection from this pool directly.
96          * for all other conditions go create a new connection
97          */

98
99         if ( isEqual( pc, getUser(), getPassword() ) ) {
100             dsConn = dataSourceObj.getConnection();
101         } else {
102             dsConn = dataSourceObj.getConnection(pc.getUserName(),
103             new String JavaDoc(pc.getPassword()));
104         }
105         } catch(java.sql.SQLException JavaDoc sqle) {
106         //_logger.log(Level.WARNING, "jdbc.exc_create_conn", sqle.getMessage());
107
_logger.log(Level.FINE, "jdbc.exc_create_conn", sqle.getMessage());
108             StringManager localStrings =
109                 StringManager.getManager( DataSourceObjectBuilder.class );
110         String JavaDoc msg = localStrings.getString( "jdbc.cannot_allocate_connection"
111              ,sqle.getMessage());
112             ResourceAllocationException JavaDoc rae = new ResourceAllocationException JavaDoc( msg );
113         rae.initCause( sqle );
114             throw rae;
115         }
116         
117         com.sun.gjc.spi.ManagedConnection mc = constructManagedConnection(
118              null, dsConn, pc, this);
119
120         //GJCINT
121
validateAndSetIsolation( mc );
122
123         return mc;
124     }
125     
126     /**
127      * Check if this <code>ManagedConnectionFactory</code> is equal to
128      * another <code>ManagedConnectionFactory</code>.
129      *
130      * @param other <code>ManagedConnectionFactory</code> object for checking equality with
131      * @return true if the property sets of both the
132      * <code>ManagedConnectionFactory</code> objects are the same
133      * false otherwise
134      */

135     public boolean equals(Object JavaDoc other) {
136         if(logWriter != null) {
137                 logWriter.println("In equals");
138         }
139         /**
140          * The check below means that two ManagedConnectionFactory objects are equal
141          * if and only if their properties are the same.
142          */

143         if(other instanceof com.sun.gjc.spi.DSManagedConnectionFactory) {
144             com.sun.gjc.spi.DSManagedConnectionFactory otherMCF =
145                 (com.sun.gjc.spi.DSManagedConnectionFactory) other;
146             return this.spec.equals(otherMCF.spec);
147         }
148         return false;
149     }
150 }
151
Popular Tags