KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fictional > resourceadapter > JtestResourceAdapter


1 /*
2  * Created on Jun 11, 2003
3  *
4  * JtestResourceAdapter.java is a Resource Adapter to test the J2EE Connector
5  * as implemented by JOnAS. This class implements all the connector classes. Each
6  * connector method simulates actual functionality and returns test results to the
7  * caller which is a JUnit test program.
8  */

9 package fictional.resourceadapter;
10
11 import java.io.PrintWriter;
12 import java.io.Serializable;
13 import java.util.Date;
14 import java.util.Iterator;
15 import java.util.Set;
16 import java.util.Vector;
17
18 import javax.naming.NamingException;
19 import javax.naming.Reference;
20 import javax.resource.NotSupportedException;
21 import javax.resource.Referenceable;
22 import javax.resource.ResourceException;
23 import javax.resource.spi.ConnectionEvent;
24 import javax.resource.spi.ConnectionEventListener;
25 import javax.resource.spi.ConnectionManager;
26 import javax.resource.spi.ConnectionRequestInfo;
27 import javax.resource.spi.LocalTransaction;
28 import javax.resource.spi.ManagedConnection;
29 import javax.resource.spi.ManagedConnectionFactory;
30 import javax.resource.spi.ManagedConnectionMetaData;
31 import javax.resource.spi.ResourceAllocationException;
32 import javax.resource.spi.security.GenericCredential;
33 import javax.resource.spi.security.PasswordCredential;
34 import javax.security.auth.Subject;
35 import javax.transaction.xa.XAResource;
36
37 import org.objectweb.jonas.common.Log;
38 import org.objectweb.util.monolog.api.BasicLevel;
39 import org.objectweb.util.monolog.api.Logger;
40
41 /**
42  * @author Bob Kruse
43  *
44  * used to test the J2EE Connector as implemented by JOnAS.
45  *
46  */

47 public class JtestResourceAdapter
48     implements ManagedConnectionFactory,
49     ConnectionRequestInfo,
50     //LocalTransaction,
51
ManagedConnection,
52     ManagedConnectionMetaData,
53     GenericCredential,
54     Serializable, Referenceable
55 {
56     Reference reference;
57     String timeStamp = "";
58     private String defaultUserName=""; // used in ManagedConnection, ConnectionRequestInfo
59
private String defaultPassword=""; // set by "getter" method
60
private String userName="";
61     private String password="";
62     private ManagedConnectionFactory mcf; // loaded by ManagedConnectionFactory
63
public ConnectionManager cm; // loaded by ManagedConnectionFactory
64
public LocalTransactionImpl loTx=null;
65     PrintWriter pw; // App Server sets to null by default
66
private Vector listeners;
67     private Logger logger = null;
68     //Managed environment
69
public boolean managed; // see ConnectionRequestInfo
70
public String res_auth; // set by ConnectionFactory.getConnection()
71
// then put into ManagedConnection when created
72
public String re_authentication_UserName; // see by ConnectionFactory.getConnection()
73
public String re_authentication_Password;
74     public XAResource xar = null;
75     public XAResourceImpl xari = null;
76     String cName = "";
77     //
78
// constructor for all "implements" in this class
79
public JtestResourceAdapter()
80     {
81         if (logger == null) {
82             logger = Log.getLogger("fictional.resourceadapter");
83         }
84         timeStamp = Long.toString(System.currentTimeMillis()); // unique for this obj
85
forceMatchNull=false;
86         managed = true;
87         closed = true; // Managed Connection
88
connDate = null; // Managed Connection
89
listeners = new Vector(); // used in ManagedConnection
90
pw = null;
91         xar = null; // used in ManagedConnection
92
xari = null;
93         loTx = null; // used in ManagedConnection
94
}
95
96     private String impl(Object obj) {
97         if (obj instanceof ManagedConnectionFactory) {
98             return "ManagedConnectionFactory";
99         } else if (obj instanceof ConnectionRequestInfo) {
100             return "ConnectionRequestInfo";
101         } else if (obj instanceof ManagedConnection) {
102             return "ManagedConnection";
103         } else if (obj instanceof ManagedConnectionMetaData) {
104             return "ManagedConnectionMetaData";
105         } else if (obj instanceof GenericCredential) {
106             return "GenericCredential";
107         } else
108             return "JtestResourceAdapter. Is this an error";
109
110     }
111     // ************************
112
// ManagedConnectionFactory methods see 5.5.3
113
// ************************
114
// Referenced classes of package javax.resource.spi:
115
// ConnectionManager, ConnectionRequestInfo, ManagedConnection
116
public Object createConnectionFactory(ConnectionManager connectionmanager)
117         throws ResourceException
118     {
119         cm = connectionmanager;
120         cName="ManagedConnectionFactory";
121         logger.log(BasicLevel.DEBUG, cName+".createConnectionFactory cm="+cm);
122         Object obj;
123         obj = new CommonClient(this, connectionmanager); // creates ConnectionFactory
124
return obj;
125     }
126     /**
127      *
128      * When the createConnectionFactory method takes no arguments, the resource adapter
129      * provides a default ConnectionManager instance. This case is used in a non-managed
130      * application scenario.
131      * see 5.5.3
132      */

133     public Object createConnectionFactory()
134         throws ResourceException
135     {
136         cName="ManagedConnectionFactory";
137         logger.log(BasicLevel.DEBUG, cName+": error - non-managed detected");
138         throw new NotSupportedException(
139             "A non-managed two-tier application environment is not supported in J2EE testing.");
140     }
141     public ManagedConnection createManagedConnection(Subject subject,
142                                                     ConnectionRequestInfo connectionrequestinfo)
143         throws ResourceException
144     {
145         cName="ManagedConnectionFactory";
146         logger.log(BasicLevel.DEBUG, cName+".createManagedConnection subject="+subject
147                     +", cri="+connectionrequestinfo);
148         mcf = (ManagedConnectionFactory) this;
149         if (subject!=null) {
150             //
151
// The application server user's password to the EIS is carried in Subject
152
//
153
try {
154                 PasswordCredential pc = getPasswordCredential(mcf, subject, connectionrequestinfo);
155                 if (pc==null) {
156                     logger.log(BasicLevel.DEBUG, cName
157                               +".createManagedConnection PasswordCredential = null, Subject not null.");
158                 } else {
159                     if (pc.getUserName()==null || pc.getUserName().length()==0) {
160                         logger.log(BasicLevel.DEBUG, cName+".createManagedConnection userName=empty");
161                     } else {
162                         userName = new String(pc.getUserName());
163                         logger.log(BasicLevel.DEBUG, cName+".createManagedConnection"
164                         +" PasswordCredential userName="+userName);
165                         password = new String(pc.getPassword()); // pc.password is char[]
166
logger.log(BasicLevel.DEBUG, cName+".createManagedConnection"
167                         +" PasswordCredential password="+password);
168                     }
169                 }
170             } catch (Exception e) {
171                 logger.log(BasicLevel.DEBUG, cName+".createManagedConnection getPasswordCredential "
172                        +"error: e="+e.toString());
173             }
174         }
175
176         if (connectionrequestinfo != null) {
177             //
178
// The client application component user's password is carried in ConnectionRequestInfo
179
// Use defaults if userName or password is empty
180
//
181
JtestResourceAdapter jinfo = (JtestResourceAdapter) connectionrequestinfo;
182             String s = jinfo.getUserName();
183             if (s.length()>0) userName = s;
184             else
185                 logger.log(BasicLevel.DEBUG, cName+".createManagedConnection"
186                       +" ConnectionRequestInfo userName="+s+". Use default="+userName);
187             logger.log(BasicLevel.DEBUG, cName+".createManagedConnection"
188                       +" ConnectionRequestInfo userName="+userName);
189             s = jinfo.getPassword();
190             if (s.length()>0) password = s;
191             else
192                 logger.log(BasicLevel.DEBUG, cName+".createManagedConnection"
193                       +" ConnectionRequestInfo password="+s+". Use default="+password);
194             logger.log(BasicLevel.DEBUG, cName+".createManagedConnection"
195                       +" ConnectionRequestInfo password="+password);
196         }
197
198         if (subject==null && connectionrequestinfo==null) {
199             //
200
// The default user's password is carried in the ManagedConnectionFactory instance
201
//
202
userName=defaultUserName;
203             password=defaultPassword;
204             logger.log(BasicLevel.DEBUG, cName+".createManagedConnection default userName="+userName);
205             logger.log(BasicLevel.DEBUG, cName+".createManagedConnection default password="+password);
206         }
207         
208         Date cDate = new Date();
209         try {
210             //
211
// Create the ManagedConnection Instance
212
//
213
ManagedConnection mc = (ManagedConnection) new JtestResourceAdapter();
214             JtestResourceAdapter obj = (JtestResourceAdapter)mc;
215             obj.setConnDate(cDate);
216             obj.setMcf(mcf);
217             obj.setPassword(password);
218             obj.setUserName(userName);
219             obj.setRes_Auth(res_auth);
220             obj.setLogWriter(pw);
221             logger.log(BasicLevel.DEBUG, cName+".createManagedConnection mc="+mc
222                                    +" with connDate="+cDate);
223             return mc;
224         } catch (Exception ex) {
225             logger.log(BasicLevel.DEBUG, cName+".createManagedConnection : error - Exception ex="
226                      +ex.getMessage());
227             throw new ResourceAllocationException(ex.getMessage());
228         }
229     }
230     boolean forceMatchNull;
231     public void setMatchNull(boolean truefalse) {
232         forceMatchNull=truefalse;
233     }
234     public ManagedConnection matchManagedConnections(Set connectionSet,
235                                                      Subject subject,
236                                                      ConnectionRequestInfo connectionrequestinfo)
237         throws ResourceException
238     {
239         cName="ManagedConnectionFactory";
240         logger.log(BasicLevel.DEBUG, cName+".matchManagedConnections");
241         ManagedConnection mc = null;
242         if (forceMatchNull) {
243             logger.log(BasicLevel.DEBUG, cName+".matchManagedConnections force new connection");
244             return mc; // forces new connection
245
}
246         JtestResourceAdapter wmc = null;
247         Iterator it = connectionSet.iterator();
248         int cnt=0;
249         while (it.hasNext()) {
250             Object obj = it.next();
251             logger.log(BasicLevel.DEBUG, cName+".matchManagedConnections "
252                +"find next ManagedConnection in Set. connectionSet cnt="+ (++cnt));
253             if (obj instanceof ManagedConnection) {
254                 // see if ManagedConnection is available
255
// i.e., no connection handle exists for mc instance
256
// i.e., the connection is closed
257
// TODO choose one of the above
258
wmc = (JtestResourceAdapter) obj;
259                 if (wmc.getCHandle()==null || wmc.isClosed()) {
260                     mc=(ManagedConnection)wmc;
261                     logger.log(BasicLevel.DEBUG, cName+".matchManagedConnections "
262                         +"connection handle == null. connectionSet cnt="+cnt);
263                 } else {
264                     // error: connection should not exist J2ee 1.0 pg. 32
265
String s = "connection handle should not exist";
266                     logger.log(BasicLevel.DEBUG, cName+".matchManagedConnections "
267                         +"error: "+s+". connectionSet cnt="+cnt);
268                 }
269             }
270         }
271         if (mc==null)
272             logger.log(BasicLevel.DEBUG, cName+".matchManagedConnections mc=null connectionSet cnt="+cnt);
273         else
274             logger.log(BasicLevel.DEBUG, cName+".matchManagedConnections with connDate="
275                    +wmc.getConnDate());
276         return mc;
277     }
278         //
279
// The setLogWriter and getLogWriter are used in ManagedConnectionFactory
280
// and ManagedConnection
281
// the Application Server calls ManagedConnection.setLogWriter(pw)
282
//
283
public void setLogWriter(PrintWriter printwriter)
284         throws ResourceException
285     {
286         if (printwriter!=null) pw = printwriter;
287     }
288
289     public PrintWriter getLogWriter()
290         throws ResourceException
291     {
292         return pw;
293     }
294     //
295
// config-property processing "setter" properties
296
//
297
String ServerName="";
298     String PortNumber="";
299     String protocol="";
300     /** Set the JOnAS config-property.. Default value is "".
301      *
302      * @param c1 String of config-property.
303      * for testng RA.
304     **/

305     public void setServerName(String c1)
306     {
307       ServerName = c1;
308       //logger.log(BasicLevel.DEBUG, cName+".setServerName="+ServerName);
309
}
310     public String getServerName() {
311         return ServerName;
312     }
313     public void setPortNumber(String s)
314     {
315         PortNumber=s;
316         //logger.log(BasicLevel.DEBUG, cName+".setPortNumber="+PortNumber);
317
}
318     public void setDUserName(String s)
319     {
320         defaultUserName=s;
321         //logger.log(BasicLevel.DEBUG, cName+".setDUserName="+defaultUserName);
322
}
323     public void setDPassword(String s)
324     {
325         defaultPassword=s;
326         //logger.log(BasicLevel.DEBUG, cName+".setDPassword="+defaultPassword);
327
}
328     public void setProtocol(String s)
329     {
330         protocol=s;
331         //logger.log(BasicLevel.DEBUG, cName+".setProtocol="+protocol);
332
}
333     public String getProtocol()
334     {
335         return protocol;
336     }
337     public ConnectionManager getCM()
338     {
339         return cm;
340     }
341     public String getRes_Auth() {
342         return res_auth;
343     }
344     public void setRes_Auth(String r) {
345         res_auth=r;
346     }
347     //************************
348
// ConnectionRequestInfo
349
//************************
350
// module to add additional connection properties than
351
// the ones configured for the ConnectionFactory. This
352
// class is passed to the App Server and will be used
353
// on matchManagedConnections or createConnection
354
// methods by the App Server.
355
//
356
String Id=""; // used in hashcode() for ConnectionRequestInfo
357
//
358
public void setUserName(String u) {
359         userName=u;
360         if (cName.length() == 0) cName=impl(this);
361         logger.log(BasicLevel.DEBUG, cName+".setUserName="+u);
362     }
363     public void setPassword(String p) {
364         password=p;
365         if (cName.length() == 0) cName=impl(this);
366         logger.log(BasicLevel.DEBUG, cName+".setPassword="+p);
367     }
368     public String getUserName() {
369         if (cName.length() == 0) cName=impl(this);
370         logger.log(BasicLevel.DEBUG, cName+".getUserName="+userName);
371         return userName;
372     }
373     public String getPassword() {
374         if (cName.length() == 0) cName=impl(this);
375         logger.log(BasicLevel.DEBUG, cName+".getPassword="+password);
376         return password;
377     }
378     private void setId()
379     {
380         if (Id == null || Id.length() == 0)
381             Id=timeStamp;
382         
383     }
384     //
385
// hashCode and equals method used in ConnectionRequestInfo,
386
// ManagedConnectionFactory
387
// and GenericCredential
388
public int hashCode()
389     {
390         int hash = 1;
391         setId();
392         hash = Id.hashCode();
393         return hash;
394     }
395     public boolean equals(Object obj)
396     {
397         boolean match = false;
398         if (obj==null) {
399             return match;
400         }
401         JtestResourceAdapter other = (JtestResourceAdapter)obj;
402         if (this.timeStamp.equals(other.getTimeStamp())) match = true;
403         return match;
404     }
405     
406     // *****************
407
// GenericCredential methods
408
// *****************
409
//
410
String gcID =""; // used in hashcode() for GenericCredential
411
public String getName()
412     {
413         cName="GenericCredential";
414         logger.log(BasicLevel.DEBUG, cName+".getName");
415         return "";
416     }
417
418     public String getMechType()
419     {
420         cName="GenericCredential";
421         logger.log(BasicLevel.DEBUG, cName+".getMechType");
422         return "";
423     }
424
425     public byte[] getCredentialData()
426         throws SecurityException
427     {
428         cName="GenericCredential";
429         logger.log(BasicLevel.DEBUG, cName+".getCredentialData");
430         byte[] x = {0,0};
431         return x;
432     }
433
434     // *****************
435
// ManagedConnection methods
436
// *****************
437
//
438
// Referenced classes of package javax.resource.spi:
439
// ConnectionRequestInfo, ConnectionEventListener, LocalTransaction,
440
// ManagedConnectionMetaData
441
//
442
// NOTE: Subject is instantiated by Application Server
443
//
444
boolean closed;
445     public ConnectionImpl cHandle; // ManagedConnection mapped 1:1 to a physical connection
446
private boolean destroyed; //set when destroyed
447
private Date connDate; //save connection date/time for a ManagedConnection
448
public boolean inXATrans;
449     public boolean inLocalTrans;
450     public boolean sendEvent;
451                                         
452     public int cntListeners() {
453         cName="ManagedConnection";
454         Vector lst = (Vector) listeners.clone();
455         int len = lst.size();
456         logger.log(BasicLevel.DEBUG, cName+".cntListeners counted="+len);
457         return len;
458     }
459     public ConnectionImpl getCHandle() {
460         return cHandle;
461     }
462     public boolean isClosed() {
463         return closed;
464     }
465         
466     public Object getConnection(Subject subject, ConnectionRequestInfo connectionrequestinfo)
467         throws ResourceException
468     {
469         cName="ManagedConnection";
470         logger.log(BasicLevel.DEBUG, cName+".getConnection *******************");
471         if (subject!=null) {
472             //
473
// for this code to execute, the following will be present in ra.xml
474
// <reauthentication-support>true</reauthentication-support>
475
//
476
logger.log(BasicLevel.DEBUG, cName+".getConnection via PasswordCredential");
477             logger.log(BasicLevel.DEBUG, "<reauthentication-support>true</reauthentication-support>");
478             try {
479                 PasswordCredential pc = getPasswordCredential(mcf, subject, connectionrequestinfo);
480                 if (pc==null) {
481                     logger.log(BasicLevel.DEBUG, cName+".getConnection PasswordCredential = null");
482                 } else {
483                     if (pc.getUserName()==null || pc.getUserName().length()==0) {
484                         logger.log(BasicLevel.DEBUG, cName+".getConnection userName=empty");
485                     } else {
486                         String re_authentication_UserName = new String(pc.getUserName());
487                         String re_authentication_Password = new String(pc.getPassword());
488                         logger.log(BasicLevel.DEBUG, cName+".getConnection"
489                               +" re-authentication userName="+re_authentication_UserName);
490                         logger.log(BasicLevel.DEBUG, cName+".getConnection"
491                               +" compare to existing userName="+userName);
492                         logger.log(BasicLevel.DEBUG, cName+".getConnection"
493                               +" re-authentication password="+re_authentication_Password);
494                         logger.log(BasicLevel.DEBUG, cName+".getConnection"
495                               +" compare to existing password="+password);
496                     }
497                 }
498             } catch (Exception e) {
499                 logger.log(BasicLevel.DEBUG, cName+".getConnection getPasswordCredential error: e="
500                              +e.toString());
501             }
502         }
503         // Code for reauthentication-support when Subject==null and ConnectionRequestInfo
504
// contains different password and username
505
//
506
if (connectionrequestinfo!=null) {
507             logger.log(BasicLevel.DEBUG, impl(this)+".getConnection via ConnectionRequestInfo");
508             logger.log(BasicLevel.DEBUG, "<reauthentication-support>true</reauthentication-support>");
509             JtestResourceAdapter jcri = (JtestResourceAdapter) connectionrequestinfo;
510             String re_authentication_UserName = new String(jcri.getUserName());
511             String re_authentication_Password = new String(jcri.getPassword());
512             logger.log(BasicLevel.DEBUG,
513                   " re-authentication userName="+re_authentication_UserName+
514                   " compare to existing userName="+userName);
515             logger.log(BasicLevel.DEBUG,
516                   " re-authentication password="+re_authentication_Password+
517                   " compare to existing password="+password);
518         }
519         Object obj = new ConnectionImpl(this);
520         cHandle = (ConnectionImpl)obj;
521         closed=false;
522         destroyed=false;
523         return obj;
524     }
525     public void destroy()
526         throws ResourceException
527     {
528         cName="ManagedConnection";
529         logger.log(BasicLevel.DEBUG, cName+".destroy");
530         // the physical connection is closed
531
destroyed = true;
532         closed = true;
533         cHandle=null;
534     }
535
536     public void cleanup()
537         throws ResourceException
538     {
539         cName="ManagedConnection";
540         // the physical connection stays open
541
// but all connection handles invalidated
542
logger.log(BasicLevel.DEBUG, cName+".cleanup");
543         cHandle=null;
544         closed=true;
545         
546     }
547
548     public void associateConnection(Object obj)
549         throws ResourceException
550     {
551         cName="ManagedConnection";
552         logger.log(BasicLevel.DEBUG, cName+".associateConnection");
553         if (obj instanceof ConnectionImpl) {
554             ConnectionImpl conn = (ConnectionImpl) obj;
555             conn.associateConnection(this); // TODO may need more arg to associate
556
} else {
557             logger.log(BasicLevel.DEBUG, cName+".associateConnection "+
558             "error: obj not instanceof ConnectionImpl");
559         }
560     }
561
562     public void addConnectionEventListener(ConnectionEventListener listener)
563     {
564         cName="ManagedConnection";
565         listeners.addElement(listener);
566         logger.log(BasicLevel.DEBUG, cName+".addConnectionEventListener listener="
567             +listener);
568     }
569
570     public void removeConnectionEventListener(ConnectionEventListener listener)
571     {
572         cName="ManagedConnection";
573         Vector lst = (Vector) listeners.clone();
574         int len = lst.size();
575         logger.log(BasicLevel.DEBUG, cName+".removeConnectionEventListener "
576                  +"Number of listeners="+len);
577         try {
578             listeners.removeElement(listener);
579         } catch (Exception e) {
580             logger.log(BasicLevel.DEBUG, cName+".removeConnectionEventListener error: "
581                  +"unable to remove listener");
582         }
583         lst = (Vector) listeners.clone();
584         len = lst.size();
585         logger.log(BasicLevel.DEBUG, cName+".removeConnectionEventListener listener="
586             +listener+" was removed. Number listeners left="+len);
587     }
588
589     public XAResource getXAResource()
590         throws ResourceException
591     {
592         cName="ManagedConnection";
593         if (xar==null)
594             xar = (XAResource)new XAResourceImpl(this);
595         logger.log(BasicLevel.DEBUG, cName+".getXAResource xar="+xar);
596         return xar;
597     }
598     public void resetXar() {
599         // dissociate XAResource Xid from this ManagedConnection
600
xar=null;
601         xari=null;
602     }
603     public XAResourceImpl getCurrentXar()
604         throws ResourceException
605     {
606         xari=(XAResourceImpl)xar;
607         return xari;
608     }
609
610     public javax.resource.cci.LocalTransaction getLocalTransaction(boolean sendEvent)
611         throws ResourceException
612     {
613         cName="ManagedConnection";
614         LocalTransactionImpl lt = null;
615         logger.log(BasicLevel.DEBUG, cName+".getLocalTransaction(sendEvent)");
616         this.sendEvent=sendEvent;
617         try {
618             lt = (LocalTransactionImpl) getLocalTransaction();
619             logger.log(BasicLevel.DEBUG, cName+".getLocalTransaction(sendEvent) lt="+lt);
620         } catch (Exception e) {
621             logger.log(BasicLevel.DEBUG, cName+".getLocalTransaction(sendEvent) "
622             +"error: "+e.getMessage());
623         }
624         return (lt);
625     }
626
627     public LocalTransaction getLocalTransaction()
628         throws ResourceException
629     {
630         cName="ManagedConnection";
631         logger.log(BasicLevel.DEBUG, cName+".getLocalTransaction");
632         if (loTx==null) {
633             loTx = new LocalTransactionImpl(this, sendEvent);
634             logger.log(BasicLevel.DEBUG, cName+".getLocalTransaction new loTx="+loTx);
635             return loTx;
636         } else {
637             logger.log(BasicLevel.DEBUG, cName+".getLocalTransaction old loTx="+loTx);
638             loTx.setSendEvent(sendEvent);
639             return loTx;
640         }
641     }
642
643     public ManagedConnectionMetaData getMetaData()
644         throws ResourceException
645     {
646         cName="ManagedConnection";
647         logger.log(BasicLevel.DEBUG, cName+".getMetaData");
648         return (ManagedConnectionMetaData) new JtestResourceAdapter();
649     }
650     /** Send the specified event to all the EventListeners that have
651      * registered with this ManagedConnection instance.
652      *
653      * @param int Event type to send
654      * @param Exception if one is associated with this event
655      * @param ch Connection handle associated with this event
656      * @throws ResourceException generic exception if operation fails
657      * @throws IllegalArgumentException if an invalid event is specified
658      **/

659     public void sendEvent(int eType, Exception ex, Object ch) throws ResourceException
660     {
661         cName="ManagedConnection";
662         Vector lst = (Vector) listeners.clone();
663         ConnectionEvent ce = null;
664         if (ex==null) {
665             ce = new ConnectionEvent(this, eType);
666         }
667         else
668             ce = new ConnectionEvent(this, eType, ex);
669         
670         if (ch != null)
671             ce.setConnectionHandle(ch);
672         else
673             logger.log(BasicLevel.DEBUG, cName+".sendEvent ch==null");
674
675         int len = lst.size();
676         for (int i=0; i<len; i++)
677         {
678           ConnectionEventListener cel = (ConnectionEventListener) lst.elementAt(i);
679           switch (eType)
680           {
681             case ConnectionEvent.CONNECTION_CLOSED:
682               if (closed == false)
683               {
684                  closed = true;
685                  cel.connectionClosed(ce);
686                  cName="ManagedConnection"; // re-establish cName
687
logger.log(BasicLevel.DEBUG, cName
688                     +".sendEvent 'CONNECTION_CLOSED' to listener="+cel+" Num="+i);
689               }
690               break;
691             case ConnectionEvent.CONNECTION_ERROR_OCCURRED:
692               if (closed == false)
693               {
694                  closed=true;
695                  cel.connectionErrorOccurred(ce);
696                  cName="ManagedConnection"; // re-establish cName
697
logger.log(BasicLevel.DEBUG, cName
698                     +".sendEvent 'CONNECTION_ERROR_OCCURRED' to listener="+cel+" Num="+i);
699               }
700               break;
701             case ConnectionEvent.LOCAL_TRANSACTION_STARTED:
702               cel.localTransactionStarted(ce);
703               cName="ManagedConnection"; // re-establish cName
704
logger.log(BasicLevel.DEBUG, cName
705                  +".sendEvent 'LOCAL_TRANSACTION_STARTED' to listener="+cel+" Num="+i);
706               break;
707             case ConnectionEvent.LOCAL_TRANSACTION_COMMITTED:
708               cel.localTransactionCommitted(ce);
709               cName="ManagedConnection"; // re-establish cName
710
logger.log(BasicLevel.DEBUG, cName
711                  +".sendEvent 'LOCAL_TRANSACTION_COMMITTED' to listener="+cel+" Num="+i);
712               break;
713             case ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK:
714               cel.localTransactionRolledback(ce);
715               cName="ManagedConnection"; // re-establish cName
716
logger.log(BasicLevel.DEBUG, cName
717                  +".sendEvent 'LOCAL_TRANSACTION_ROLLEDBACK' to listener="+cel+" Num="+i);
718               break;
719             default:
720               IllegalArgumentException iae =
721                     new IllegalArgumentException("Illegal eventType: "+eType);
722               logger.log(BasicLevel.DEBUG, cName+".sendEvent error: "+iae.toString());
723               throw iae;
724           }
725         }
726
727     }
728     public void setConnDate(Date cDate) { // called by ManagedConnectionFactory
729
cName="ManagedConnection";
730         logger.log(BasicLevel.DEBUG, cName+".setConnDate to "+cDate);
731         connDate=cDate;
732     }
733     public void setMcf(ManagedConnectionFactory MCF) { // called by ManagedConnectionFactory
734
cName="ManagedConnection";
735         logger.log(BasicLevel.DEBUG, cName+".setMcf to "+MCF);
736         this.mcf=MCF;
737     }
738     public Date getConnDate() {
739         return connDate;
740     }
741     public ManagedConnectionFactory getMcf() {
742         return this.mcf;
743     }
744     public void setTimeStamp(String ts) {
745         cName=impl(this);
746         timeStamp=ts;
747         logger.log(BasicLevel.DEBUG, cName+".setTimeStamp to "+ts);
748     }
749     public String getTimeStamp() {
750         return timeStamp;
751     }
752     // *************************
753
// ManagedConnectionMetaData methods
754
// *************************
755
//
756
String product="Fictional EIS";
757     String version="1.1";
758     public String getEISProductName()
759         throws ResourceException
760     {
761         cName="ManagedConnectionMetaData";
762         logger.log(BasicLevel.DEBUG, cName+".getEISProductName");
763         return product;
764     }
765
766     public String getEISProductVersion()
767         throws ResourceException
768     {
769         cName="ManagedConnectionMetaData";
770         logger.log(BasicLevel.DEBUG, cName+".getEISProductVersion");
771         return version;
772     }
773
774     public int getMaxConnections()
775         throws ResourceException
776     {
777         cName="ManagedConnectionMetaData";
778         logger.log(BasicLevel.DEBUG, cName+".getMaxConnections");
779         return 0;
780     }
781
782     /** Required by the referencable attribute
783      * @param ref Reference object
784     **/

785     public void setReference(javax.naming.Reference ref)
786     {
787       this.reference = ref;
788     }
789
790     /** Required by the referencable attribute
791      * @return Reference object
792     **/

793     public Reference getReference()
794         throws NamingException
795     {
796       return reference;
797     }
798     /** Returns the Password credential
799     *
800     * @param mcf ManagedConnectionFactory currently being used
801     * @param subject Subject associated with this call
802     * @param info ConnectionRequestInfo
803     *
804     * @return PasswordCredential for this user
805     */

806     public PasswordCredential getPasswordCredential
807            (ManagedConnectionFactory mcf,
808             Subject subject,
809             ConnectionRequestInfo info
810             )
811                                         throws ResourceException
812     {
813
814         logger.log(BasicLevel.DEBUG, cName+".getPasswordCredential subject="+subject);
815         PasswordCredential pc = null;
816         try {
817             if (subject == null)
818             {
819                if (info == null) return null;
820                JtestResourceAdapter crii = (JtestResourceAdapter) info; // ConnectionRequestInfo
821
pc = new PasswordCredential(crii.userName,crii.password.toCharArray());
822                pc.setManagedConnectionFactory(mcf);
823                return pc;
824             }
825             Set cred = subject.getPrivateCredentials(PasswordCredential.class);
826             pc = null;
827             for (Iterator iter = cred.iterator(); iter.hasNext();)
828             {
829                 PasswordCredential tmpPc = (PasswordCredential) iter.next();
830                 if (mcf.equals(tmpPc.getManagedConnectionFactory()))
831                 {
832                     logger.log(BasicLevel.DEBUG, cName+".getPasswordCredential PasswordCredential "
833                              +"found mcf="+mcf);
834                     pc = tmpPc;
835                     break;
836                 }
837             }
838         } catch (Exception e) {
839             logger.log(BasicLevel.DEBUG, cName+".getPasswordCredential error: "+e.toString());
840             pc=null;
841         }
842         if (pc == null)
843         {
844             SecurityException se = new SecurityException("No PasswordCredential found");
845             logger.log(BasicLevel.DEBUG, cName+".getPasswordCredential error: "+se);
846             throw se;
847         }
848         return pc;
849     }
850
851 }
852
Popular Tags