KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ersatz > resourceadapter > ConnectionFactoryImpl


1 /*
2  * Created on December 12, 2003
3  *
4  * ConnectionFactoryImpl.java is used to test the J2EE Connector
5  * as implemented by JOnAS.
6  *
7  */

8 package ersatz.resourceadapter;
9
10 import javax.naming.Reference;
11 import javax.naming.NamingException;
12 import javax.resource.ResourceException;
13 import javax.resource.NotSupportedException;
14 import javax.resource.spi.ConnectionManager;
15 // interfaces implemented in this class
16
import javax.resource.cci.ConnectionFactory;
17 import javax.resource.cci.Connection;
18 import javax.resource.cci.ConnectionSpec; // j2ee 1.5
19
import javax.resource.cci.RecordFactory;
20 import javax.resource.cci.ResourceAdapterMetaData;
21 import java.io.Serializable;
22 import javax.resource.Referenceable;
23 /**
24  * @author Bob Kruse
25  *
26  * used to test the J2EE Connector as implemented by JOnAS.
27  *
28  */

29 public class ConnectionFactoryImpl implements ConnectionFactory,
30     Referenceable, Serializable
31
32 {
33     Reference reference;
34     private ConnectionManager cm;
35     private ManagedConnectionFactoryImpl mcf; // used in cm.allocateConnection
36
private ConnectionSpecImpl cs; // ConnectionSpec
37
protected boolean managed = true;
38     private String userName = "";
39     private String passWord = "";
40     String cName = "ConnectionFactoryImpl";
41     
42     //
43
// ConnectionFactory instance created during lookup() by Application Server
44
//
45
public ConnectionFactoryImpl() {
46     }
47     public ConnectionFactoryImpl(ManagedConnectionFactoryImpl mcf, ConnectionManager cm) {
48         this.mcf=mcf;
49         this.cm=cm;
50         
51     }
52     //
53
// Container Managed Sign-on calls getConnection() from the Application Component
54
//
55
// Container-managed sign-on when deployment file contains line below
56
//
57
// <res-auth>Container</res-auth>
58
//
59
public Connection getConnection()
60         throws ResourceException
61     {
62         Utility.log(cName+".getConnection"+" (Container Managed Sign-on)");
63         ConnectionImpl conn = null;
64         try {
65             conn = (ConnectionImpl)getConnection(null);
66             return conn;
67         } catch (ResourceException ex) {
68             throw ex;
69         }
70     }
71     //
72
// Component Managed Sign-on calls getConnection(cs) directly from the Application Component
73
//
74
// Component-managed sign-on when file "connector.xml / secured.jar" contains line below
75
//
76
// <res-auth>Application</res-auth>
77
//
78
public Connection getConnection(ConnectionSpec connectionspec)
79         throws ResourceException
80     {
81         ConnectionImpl conn = null;
82         ConnectionSpecImpl cs = null;
83         ConnectionRequestInfoImpl jcri=null; //
84

85         if (connectionspec==null) {
86             mcf.setRes_Auth("Container");
87             Utility.log(cName+".getConnection detected res-auth='"+mcf.getRes_Auth()+"'");
88             // TODO what now? anything?
89
// now pass null to cm.allocateConnection(mcf, null);
90
} else {
91             mcf.setRes_Auth("Application");
92             Utility.log(cName+".getConnection detected res-auth='"+mcf.getRes_Auth()+"'");
93             cs = (ConnectionSpecImpl)connectionspec;
94             // load user and password into ConnectionRequestInfo instance
95
jcri = new ConnectionRequestInfoImpl();
96             jcri.setUserName(cs.getUserName());
97             jcri.setPassword(cs.getPassword());
98             
99         }
100         Utility.log(cName+".getConnection calling cm.allocateConnection");
101         try {
102             conn = (ConnectionImpl)cm.allocateConnection(mcf, jcri);
103             if (conn==null) {
104                 Utility.log(cName+". getConnection, cm.allocateConnection"
105                       +" error: Null connection object returned");
106                 throw new ResourceException("Null connection object returned by allocateConnection");
107             }
108             conn.crii=jcri;
109             return conn;
110         } catch (IllegalStateException is) {
111             Utility.log(cName+".getConnection IllegalStateException"+is);
112             throw is;
113         } catch (ResourceException re) {
114             Utility.log(cName+".getConnection ResourceException="+re);
115             throw re;
116         }
117     }
118
119     public RecordFactory getRecordFactory()
120         throws ResourceException
121     {
122         NotSupportedException nse = new NotSupportedException(
123             "RecordFactory is not currently supported");
124         Utility.log(cName+".getRecordFactory "+nse);
125         throw nse;
126     }
127
128     public ResourceAdapterMetaData getMetaData()
129         throws ResourceException
130     {
131         Utility.log(cName+".getMetaData");
132         ResourceAdapterMetaData rd = null; // TODO do something
133
return rd;
134     }
135     public ConnectionManager getCM() {
136         return cm;
137     }
138     public ManagedConnectionFactoryImpl getMcf() {
139         return mcf;
140     }
141     //
142
// *****************
143
// Reference
144
// *****************
145
//
146
/** Required by the referencable attribute
147      * @param ref Reference object
148     **/

149     public void setReference(javax.naming.Reference ref)
150     {
151       this.reference = ref;
152     }
153
154     /** Required by the referencable attribute
155      * @return Reference object
156     **/

157     public Reference getReference()
158         throws NamingException
159     {
160       return reference;
161     }
162 }
163
Popular Tags