KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ersatz > resourceadapter > ManagedConnectionFactoryImpl


1 /*
2  * Created on December 11, 2003
3  *
4  * ManagedConnectionFactoryImpl.java is part of a Resource Adapter to test
5  * J2EE Connector 1.5 as implemented by JOnAS.
6  */

7 package ersatz.resourceadapter;
8
9 import javax.naming.Reference;
10 import java.io.PrintWriter;
11 import java.util.Set;
12 import java.util.Timer;
13 import java.util.Iterator;
14 import java.util.Date;
15 import javax.security.auth.Subject;
16 import javax.resource.ResourceException;
17 import javax.resource.NotSupportedException;
18 import javax.resource.spi.ResourceAllocationException;
19 import javax.resource.spi.ManagedConnectionFactory;
20 import javax.resource.spi.ResourceAdapterAssociation;
21 import javax.resource.spi.ConnectionManager;
22 import javax.resource.spi.ConnectionRequestInfo;
23 import javax.resource.spi.ManagedConnection;
24 import javax.resource.spi.ResourceAdapter;
25 import javax.resource.spi.BootstrapContext;
26 import javax.resource.spi.security.PasswordCredential;
27 import java.io.Serializable;
28
29 /**
30  * @author Bob Kruse
31  *
32  * used to test the J2EE Connector as implemented by JOnAS.
33  *
34  */

35 public class ManagedConnectionFactoryImpl
36     implements ManagedConnectionFactory, ResourceAdapterAssociation,
37     Serializable
38 {
39     Reference reference;
40     public String defaultUserName=""; // used in ManagedConnectionImpl, ConnectionRequestInfo
41
public String defaultPassword=""; // set by "getter" method
42

43      // loaded by Application Server
44
ResourceAdapter ra=null;
45     String ServerName="";
46     String PortNumber="";
47     String userName="";
48     String password="";
49     String protocol="";
50
51     public ConnectionManager cm; // loaded by ManagedConnectionFactory
52
private ManagedConnectionFactoryImpl mcf;
53     private ConnectionRequestInfoImpl crii;
54     private ResourceAdapterImpl rai=null; // re-cast at the same time
55
private BootstrapContext bootstrapCtx; // used to test timer
56
private boolean raLoaded=false;
57     PrintWriter pw; // App Server sets to null by default
58
boolean forceMatchNull; // in matchManagedConnections() when debugging
59
public String res_auth; // set by ConnectionFactory.getConnection()
60
// then put into ManagedConnection when created
61
String mfId;
62     String cName="ManagedConnectionFactoryImpl";
63     //
64
public ManagedConnectionFactoryImpl()
65     {
66         crii = new ConnectionRequestInfoImpl();
67         pw = null;
68     }
69
70     public Object createConnectionFactory(ConnectionManager connectionmanager)
71         throws ResourceException
72     {
73         cm = connectionmanager;
74         Utility.log(cName+".createConnectionFactory cm="+cm);
75         Object obj;
76         obj = new ConnectionFactoryImpl(this, cm); // creates ConnectionFactory
77
return obj;
78     }
79     /**
80      *
81      * When the createConnectionFactory method takes no arguments, the resource adapter
82      * provides a default ConnectionManager instance. This case is used in a non-managed
83      * application scenario.
84      */

85     public Object createConnectionFactory()
86         throws ResourceException
87     {
88         Utility.log(cName+": error - non-managed detected");
89         throw new NotSupportedException(
90             "A non-managed two-tier application environment is not supported in J2EE testing.");
91     }
92     public ManagedConnection createManagedConnection(Subject subject,
93                                                       ConnectionRequestInfo CRI)
94         throws ResourceException
95     {
96         ConnectionRequestInfoImpl info = (ConnectionRequestInfoImpl)CRI;
97         Utility.log(cName+".createManagedConnection subject="+subject
98                     +", cri="+info);
99         mcf = this;
100         ManagedConnectionImpl mc;
101         if (subject!=null) {
102             //
103
// The application server user's password to the EIS is carried in Subject
104
//
105
try {
106                 PasswordCredential pc = Utility.getPasswordCredential(this, subject, info);
107                 if (pc==null) {
108                     Utility.log(cName
109                               +".createManagedConnection PasswordCredential = null, Subject not null.");
110                 } else {
111                     if (pc.getUserName()==null || pc.getUserName().length()==0) {
112                         Utility.log(cName+".createManagedConnection userName=empty");
113                     } else {
114                         userName = new String(pc.getUserName());
115                         Utility.log(cName+".createManagedConnection"
116                         +" PasswordCredential userName="+userName);
117                         password = new String(pc.getPassword()); // pc.password is char[]
118
Utility.log(cName+".createManagedConnection"
119                         +" PasswordCredential password="+password);
120                     }
121                 }
122             } catch (Exception e) {
123                 Utility.log(cName+".createManagedConnection getPasswordCredential "
124                        +"error: e="+e.toString());
125             }
126         }
127
128         if (info != null) {
129             //
130
// The client application component user's password is carried in info
131
// Use defaults if userName or password is empty
132
//
133
String s = info.getUserName();
134             if (s.length()>0) userName = s;
135             else
136                 Utility.log(cName+".createManagedConnection"
137                       +" ConnectionRequestInfo userName="+s+". Use default="+userName);
138             Utility.log(cName+".createManagedConnection"
139                       +" ConnectionRequestInfo userName="+userName);
140             s = info.getPassword();
141             if (s.length()>0) password = s;
142             else
143                 Utility.log(cName+".createManagedConnection"
144                       +" ConnectionRequestInfo password="+s+". Use default="+password);
145             Utility.log(cName+".createManagedConnection"
146                       +" ConnectionRequestInfo password="+password);
147         }
148
149         if ((subject==null && info==null) || userName=="") {
150             //
151
// The default user's password is carried in the ManagedConnectionFactory instance
152
//
153
userName=defaultUserName;
154             password=defaultPassword;
155             Utility.log(cName+".createManagedConnection default userName="+userName);
156             Utility.log(cName+".createManagedConnection default password="+password);
157         }
158         ConnectionRequestInfoImpl mcCrii = new ConnectionRequestInfoImpl(crii);
159         mcCrii.setPassword(password);
160         mcCrii.setUserName(userName);
161         try {
162             //
163
// Create the ManagedConnectionImpl Instance
164
//
165
mc = new ManagedConnectionImpl(mcCrii);
166             mc.setMcf(mcf);
167             mc.setRes_Auth(res_auth);
168             mc.setLogWriter(pw);
169             Utility.log(cName+".createManagedConnection for eis="+rai.getEIS_URL()
170                                   +" mc="+mc);
171             return mc;
172         } catch (Exception ex) {
173             Utility.log(cName+".createManagedConnection : error - Exception ex="
174                      +ex.getMessage());
175             throw new ResourceAllocationException(ex.getMessage());
176         }
177     }
178     public void setMatchNull(boolean truefalse) { // nobody calls this
179
forceMatchNull=truefalse;
180     }
181     public ManagedConnection matchManagedConnections(Set connectionSet,
182                                                      Subject subject,
183                                                      ConnectionRequestInfo info)
184         throws ResourceException
185     {
186         String mName=".matchManagedConnections";
187         Utility.log(cName+mName+" Set="+connectionSet+" Subject="+subject+" Crinfo="+info);
188         ManagedConnectionImpl mc = null;
189         if (forceMatchNull) {
190             Utility.log(cName+mName+" force new connection");
191             return mc; // forces new connection
192
}
193         
194         ConnectionRequestInfoImpl mcCrii = null;
195         ConnectionRequestInfoImpl curCrii = new ConnectionRequestInfoImpl(crii);
196         ManagedConnectionImpl wmc = null;
197         Iterator it = connectionSet.iterator();
198         int cnt=0;
199         while (it.hasNext()) {
200             Object obj = it.next();
201             Utility.log(cName+mName
202                +" find next ManagedConnectionImpl in Set cnt="+ (++cnt)+" obj="+obj);
203             if (obj instanceof ManagedConnectionImpl) {
204                 // see if ManagedConnectionImpl is available
205
// i.e., no connection handle exists for mc instance
206
// i.e., the connection is closed
207
// TODO choose one of the above
208
wmc = (ManagedConnectionImpl) obj;
209                 mcCrii=wmc.getCrii();
210                 if (curCrii.equals(mcCrii)) {
211                     if (wmc.getCHandle()==null || wmc.isClosed()) {
212                        mc=wmc;
213                        Utility.log(cName+mName
214                            +" connection handle == null. Set cnt="+cnt+" matched mc="+wmc);
215                 
216                     } else {
217                        // error: connection should not exist J2ee 1.0 pg. 32
218
String s = "connection handle should not exist";
219                        Utility.log(cName+mName
220                            +"error: "+s+". connectionSet cnt="+cnt);
221                     }
222                 }
223                 
224             }
225         }
226         if (mc==null)
227             Utility.log(cName+mName+" mc=null No match found. Set cnt="+cnt);
228         else
229             Utility.log(cName+mName+" match found mc="+mc);
230         return mc;
231     }
232         //
233
// The setLogWriter and getLogWriter
234
// the Application Server calls ManagedConnectionImpl.setLogWriter(pw)
235
//
236
public void setLogWriter(PrintWriter printwriter)
237         throws ResourceException
238     {
239         Utility.log(cName+".setLogWriter mcfi.pw="+pw+" ApplicationServer's PrintWriter="+printwriter);
240         if (printwriter!=null) pw = printwriter;
241     }
242
243     public PrintWriter getLogWriter()
244         throws ResourceException
245     {
246         return pw;
247     }
248     public ResourceAdapter getResourceAdapter()
249     {
250         return ra;
251     }
252     /** Set the ResourceAdapter association.
253      *
254      * @param ra ResourceAdapter passed in by Application Server (JOnAS)
255      * @exception javax.resource.ResourceException
256     **/

257     public void setResourceAdapter(ResourceAdapter ra) throws ResourceException
258     {
259         Utility.log(cName+".setResourceAdapter arg="+ra);
260         if (ra==null) {
261             String s = "Application Server cannot set ResourceAdapter to null";
262             Utility.log(cName+".setResourceAdapter error: "+s);
263             throw new ResourceException(s);
264         }
265         if (!raLoaded) {
266             this.ra = ra;
267             rai = (ResourceAdapterImpl)ra;
268             crii.setRaName(rai.getEIS_URL());
269             raLoaded=true; // load exactly once
270
} else {
271             if (this.ra==ra) {
272                 String s = "setResoruceAdapter called again by the Application "
273                           +"Server with same ResourceAdapter value.";
274                 Utility.log(cName+".setResourceAdapter error: "+s);
275                 throw new ResourceException(s);
276             } else {
277                 String s = "ResourceAdapter already set to "+this.ra
278                           +". Must not change association";
279                 Utility.log(cName+".setResourceAdapter error: "+s);
280                 throw new ResourceException(s);
281             }
282         }
283     }
284     //
285
// config-property processing "setter" properties
286
//
287
/** Set the JOnAS config-property.. Default value is "".
288      *
289      * @param c1 String of config-property.
290      * for testng RA.
291     **/

292     public void setServerName(String c1)
293     {
294       ServerName = c1;
295       crii.setServerName(ServerName);
296       //Utility.log(cName+".setServerName="+ServerName);
297
}
298     public String getServerName() {
299         return ServerName;
300     }
301     public void setPortNumber(String s)
302     {
303         PortNumber=s;
304         crii.setPortNumber(PortNumber);
305         //Utility.log(cName+".setPortNumber="+PortNumber);
306
}
307     public String getPortNumber()
308     {
309         return PortNumber;
310     }
311     public void setDUserName(String s)
312     {
313         defaultUserName=s;
314         crii.setUserName(s);
315         //Utility.log(cName+".setDUserName="+defaultUserName);
316
}
317     public String getDUserName()
318     {
319         return defaultUserName;
320     }
321     public void setDPassword(String s)
322     {
323         defaultPassword=s;
324         crii.setPassword(s);
325         //Utility.log(cName+".setDPassword="+defaultPassword);
326
}
327     public String getDPassword()
328     {
329         return defaultPassword;
330     }
331     public void setProtocol(String s)
332     {
333         protocol=s;
334         crii.setProtocol(protocol);
335         //Utility.log(cName+".setProtocol="+protocol);
336
}
337     public String getProtocol()
338     {
339         return protocol;
340     }
341     public ConnectionManager getCM()
342     {
343         return cm;
344     }
345     public String getRes_Auth() {
346         return res_auth;
347     }
348     public void setRes_Auth(String r) {
349         res_auth=r;
350     }
351     //
352
// hashCode and equals method used in ConnectionRequestInfo,
353
// ManagedConnectionFactory
354
public int hashCode()
355     {
356         if (mfId == null || mfId.length() == 0) {
357             crii.hashCode();
358             mfId = crii.getId();
359         }
360         int hash = mfId.hashCode();
361         return hash;
362     }
363     public boolean equals(Object other)
364     {
365         boolean match = false;
366         if (other==null) {
367             return match;
368         }
369         if (other instanceof ManagedConnectionFactoryImpl) {
370             ManagedConnectionFactoryImpl otherMcf = (ManagedConnectionFactoryImpl)other;
371             try {
372                 if (hashCode() != otherMcf.hashCode()) {
373                     Utility.log(cName+".equals("+otherMcf+") return="+match);
374                     return false;
375                 }
376                 match = crii.equals(otherMcf.crii);
377             } catch (Exception e) {
378                 Utility.log(cName+".equals("+otherMcf+") Exception="+e);
379                 return false;
380             }
381         }
382         return match;
383     }
384     /* 10.3.7
385     A resource adapter may not be able to directly create a Timer instance, if it does
386     not have adequate runtime permissions to create threads. This is because the Timer
387     instance starts a background thread. In such a case, the resource adapter can
388     instead use the BootstrapContext instance to obtain a Timer instance from the
389     application server.
390     */

391     public boolean isTimerWorking()
392     {
393         ResourceAdapterImpl rai = (ResourceAdapterImpl)ra;
394         try {
395             if (rai.bootstrapCtx==null) {
396                 throw new Exception("bootstrapCtx=null");
397             }
398             Timer timer = rai.bootstrapCtx.createTimer(); // get a Timer instance
399
Utility.log(cName+".isTimerWorking Timer was created. ");
400             return true;
401         } catch (Exception e) {
402             Utility.log(cName+".isTimerWorking Error: "+e);
403             return false;
404         }
405     }
406 }
407
Popular Tags