KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fictional > resourceadapter > CommonClient


1 /*
2  * Created on Jun 12, 2003
3  *
4  * CommonClient.java is used to test the J2EE Connector
5  * as implemented by JOnAS. This class implements some the Common Client Interface
6  * (cci) classes. Each cci method simulates actual functionality and returns test
7  * results to the caller which is a JUnit test program.
8  *
9  */

10 package fictional.resourceadapter;
11
12 import javax.naming.Reference;
13 import javax.naming.NamingException;
14 import javax.resource.ResourceException;
15 import javax.resource.NotSupportedException;
16 import javax.resource.spi.ConnectionManager;
17 import javax.resource.spi.ManagedConnectionFactory;
18 import javax.resource.spi.ConnectionRequestInfo;
19 // interfaces implemented in this class
20
import javax.resource.cci.ConnectionFactory;
21 import javax.resource.cci.Connection;
22 import javax.resource.cci.ConnectionSpec; // j2ee 1.5
23
import javax.resource.cci.RecordFactory;
24 import javax.resource.cci.ResourceAdapterMetaData;
25 import java.io.Serializable;
26 import javax.resource.Referenceable;
27 // logger imports
28
import org.objectweb.jonas.common.Log;
29 import org.objectweb.util.monolog.api.Logger;
30 import org.objectweb.util.monolog.api.BasicLevel;
31 /**
32  * @author Bob Kruse
33  *
34  * used to test the J2EE Connector as implemented by JOnAS.
35  *
36  */

37 public class CommonClient
38     implements
39     ConnectionFactory,
40     ConnectionSpec, // j2ee 1.5
41
ResourceAdapterMetaData,
42     Referenceable,
43     Serializable
44
45 {
46     Reference reference;
47     private ConnectionManager cm;
48     private ManagedConnectionFactory mcf; // used in cm.allocateConnection
49
private CommonClient cs; // ConnectionSpec
50
protected boolean managed = true;
51     private Logger logger = null;
52     private String userName = "";
53     private String passWord = "";
54     String cName = "";
55     
56     public CommonClient() {
57         if (logger == null) {
58             logger = Log.getLogger("fictional.resourceadapter");
59         }
60     }
61     //
62
// *****************
63
// ConnectionSpec methods
64
// *****************
65
//
66
public void setUserName(String u) {
67         userName=u;
68         cName = "ConnectionSpec";
69         logger.log(BasicLevel.DEBUG, cName+".setUserName="+u);
70     }
71     public void setPassword(String p) {
72         passWord=p;
73         cName = "ConnectionSpec";
74         logger.log(BasicLevel.DEBUG, cName+".setPassword="+p);
75     }
76     public String getUserName() {
77         cName = "ConnectionSpec";
78         logger.log(BasicLevel.DEBUG, cName+".getUserName="+userName);
79         return userName;
80     }
81     public String getPassword() {
82         cName = "ConnectionSpec";
83         logger.log(BasicLevel.DEBUG, cName+".getPassword="+passWord);
84         return passWord;
85     }
86     //
87
// *****************
88
// ConnectionFactory methods
89
// *****************
90
//
91
// Referenced classes of package javax.resource.cci:
92
// Connection, ConnectionSpec, RecordFactory, ResourceAdapterMetaData
93
// (below is constructor for ConnectionFactory
94
//
95
// ConnectionFactory instance created during lookup() by Application Server
96
//
97
public CommonClient(ManagedConnectionFactory mcf, ConnectionManager cm) {
98         if (logger == null) {
99             logger = Log.getLogger("fictional.resourceadapter");
100         }
101         this.mcf=mcf;
102         this.cm=cm;
103         
104     }
105     private String impl(Object obj) {
106         if (obj instanceof ConnectionFactory) {
107             return "ConnectionFactory";
108         } else if (obj instanceof ConnectionSpec) {
109             return "ConnectionSpec";
110         } else if (obj instanceof ResourceAdapterMetaData) {
111             return "ResourceAdapterMetaData";
112         } else
113             return "CommonClient. Is this an error";
114
115     }
116     //
117
// Container Managed Sign-on calls getConnection() from the Application Component
118
//
119
// Container-managed sign-on when file "connector.xml / secured.jar" contains line below
120
//
121
// <res-auth>Container</res-auth>
122
//
123
public Connection getConnection()
124         throws ResourceException
125     {
126         cName = "ConnectionFactory";
127         logger.log(BasicLevel.DEBUG, cName+".getConnection"+" (Container Managed Sign-on)");
128         Connection conn = null;
129         try {
130             conn = (Connection)getConnection(null);
131             return conn;
132         } catch (ResourceException ex) {
133             throw ex;
134         }
135     }
136     //
137
// Component Managed Sign-on calls getConnection(cs) directly from the Application Component
138
//
139
// Component-managed sign-on when file "connector.xml / secured.jar" contains line below
140
//
141
// <res-auth>Application</res-auth>
142
//
143
public Connection getConnection(ConnectionSpec connectionspec)
144         throws ResourceException
145     {
146         cName = "ConnectionFactory";
147         JtestResourceAdapter jmcf = (JtestResourceAdapter)mcf; // ManagedConnectionFactory
148
Connection conn = null;
149         CommonClient cs; // ConnectionSpec
150
JtestResourceAdapter jcri=null; // ConnectionRequestInfo
151

152         if (connectionspec==null) {
153             jmcf.setRes_Auth("Container");
154             logger.log(BasicLevel.DEBUG, cName+".getConnection detected res-auth='"+jmcf.getRes_Auth()+"'");
155             // TODO what now? anything?
156
// now pass null to cm.allocateConnection(mcf, null);
157
} else {
158             jmcf.setRes_Auth("Application");
159             logger.log(BasicLevel.DEBUG, cName+".getConnection detected res-auth='"+jmcf.getRes_Auth()+"'");
160             cs = (CommonClient)connectionspec;
161             // load user and password into ConnectionRequestInfo instance
162
jcri = new JtestResourceAdapter(); // ConnectionRequestInfo
163
jcri.setUserName(cs.getUserName());
164             jcri.setPassword(cs.getPassword());
165             
166         }
167         logger.log(BasicLevel.DEBUG, cName+".getConnection calling cm.allocateConnection");
168         try {
169             ConnectionRequestInfo cri = (ConnectionRequestInfo) jcri;
170             conn = (Connection)cm.allocateConnection(mcf, cri);
171             if (conn==null) {
172                 logger.log(BasicLevel.DEBUG, cName+". getConnection, cm.allocateConnection"
173                       +" error: Null connection object returned");
174                 throw new ResourceException("Null connection object returned by allocateConnection");
175             }
176             return conn;
177         } catch (IllegalStateException is) {
178             logger.log(BasicLevel.DEBUG, cName+".getConnection IllegalStateException"+is);
179             throw is;
180         } catch (ResourceException re) {
181             logger.log(BasicLevel.DEBUG, cName+".getConnection ResourceException="+re);
182             throw re;
183         }
184     }
185
186     public RecordFactory getRecordFactory()
187         throws ResourceException
188     {
189         cName = "ConnectionFactory";
190         logger.log(BasicLevel.DEBUG, cName+".getRecordFactory");
191         NotSupportedException nse = new NotSupportedException(
192             "RecordFactory is not currently supported");
193         throw nse;
194     }
195
196     public ResourceAdapterMetaData getMetaData()
197         throws ResourceException
198     {
199         cName = "ConnectionFactory";
200         logger.log(BasicLevel.DEBUG, cName+".getMetaData");
201         ResourceAdapterMetaData rd = null; // TODO do something
202
return rd;
203     }
204     public ManagedConnectionFactory getMcf() {
205         ManagedConnectionFactory MCF = (ManagedConnectionFactory)mcf;
206         return MCF;
207     }
208     //
209
// *****************
210
// Reference
211
// *****************
212
//
213
/** Required by the referencable attribute
214      * @param ref Reference object
215     **/

216     public void setReference(javax.naming.Reference ref)
217     {
218       this.reference = ref;
219     }
220
221     /** Required by the referencable attribute
222      * @return Reference object
223     **/

224     public Reference getReference()
225         throws NamingException
226     {
227       return reference;
228     }
229     //
230
// ***********************
231
// ResourceAdapterMetaData methods
232
// ***********************
233
//
234
public String getAdapterVersion() {
235         return "1.0";
236     }
237     public String getAdapterVendorName() {
238         return "Bull";
239     }
240     public String getAdapterName() {
241         return "JOnAS Test Resource Adapter";
242     }
243     public String getAdapterShortDescription() {
244         return "Test JOnAS Application Server compliance to J2EE Java Community Process (JSR112)";
245     }
246     public String getSpecVersion() {
247         return "J2EE Java Community Process (JSR112)";
248     }
249     public String[] getInteractionSpecsSupported() {
250         String[]s = {"JSR016","JSR112"};
251         return s;
252     }
253     public boolean supportsExecuteWithInputAndOutputRecord() {
254         return true;
255     }
256     public boolean supportsExecuteWithInputRecordOnly() {
257         return true;
258     }
259     public boolean supportsLocalTransactionDemarcation() {
260         return false;
261     }
262 }
263
Popular Tags