001: /*
002: * Created on Jun 11, 2003
003: *
004: * JtestResourceAdapter.java is a Resource Adapter to test the J2EE Connector
005: * as implemented by JOnAS. This class implements all the connector classes. Each
006: * connector method simulates actual functionality and returns test results to the
007: * caller which is a JUnit test program.
008: */
009: package fictional.resourceadapter;
010:
011: import java.io.PrintWriter;
012: import java.io.Serializable;
013: import java.util.Date;
014: import java.util.Iterator;
015: import java.util.Set;
016: import java.util.Vector;
017:
018: import javax.naming.NamingException;
019: import javax.naming.Reference;
020: import javax.resource.NotSupportedException;
021: import javax.resource.Referenceable;
022: import javax.resource.ResourceException;
023: import javax.resource.spi.ConnectionEvent;
024: import javax.resource.spi.ConnectionEventListener;
025: import javax.resource.spi.ConnectionManager;
026: import javax.resource.spi.ConnectionRequestInfo;
027: import javax.resource.spi.LocalTransaction;
028: import javax.resource.spi.ManagedConnection;
029: import javax.resource.spi.ManagedConnectionFactory;
030: import javax.resource.spi.ManagedConnectionMetaData;
031: import javax.resource.spi.ResourceAllocationException;
032: import javax.resource.spi.security.GenericCredential;
033: import javax.resource.spi.security.PasswordCredential;
034: import javax.security.auth.Subject;
035: import javax.transaction.xa.XAResource;
036:
037: import org.objectweb.jonas.common.Log;
038: import org.objectweb.util.monolog.api.BasicLevel;
039: import org.objectweb.util.monolog.api.Logger;
040:
041: /**
042: * @author Bob Kruse
043: *
044: * used to test the J2EE Connector as implemented by JOnAS.
045: *
046: */
047: public class JtestResourceAdapter implements ManagedConnectionFactory,
048: ConnectionRequestInfo,
049: //LocalTransaction,
050: ManagedConnection, ManagedConnectionMetaData,
051: GenericCredential, Serializable, Referenceable {
052: Reference reference;
053: String timeStamp = "";
054: private String defaultUserName = ""; // used in ManagedConnection, ConnectionRequestInfo
055: private String defaultPassword = ""; // set by "getter" method
056: private String userName = "";
057: private String password = "";
058: private ManagedConnectionFactory mcf; // loaded by ManagedConnectionFactory
059: public ConnectionManager cm; // loaded by ManagedConnectionFactory
060: public LocalTransactionImpl loTx = null;
061: PrintWriter pw; // App Server sets to null by default
062: private Vector listeners;
063: private Logger logger = null;
064: //Managed environment
065: public boolean managed; // see ConnectionRequestInfo
066: public String res_auth; // set by ConnectionFactory.getConnection()
067: // then put into ManagedConnection when created
068: public String re_authentication_UserName; // see by ConnectionFactory.getConnection()
069: public String re_authentication_Password;
070: public XAResource xar = null;
071: public XAResourceImpl xari = null;
072: String cName = "";
073:
074: //
075: // constructor for all "implements" in this class
076: public JtestResourceAdapter() {
077: if (logger == null) {
078: logger = Log.getLogger("fictional.resourceadapter");
079: }
080: timeStamp = Long.toString(System.currentTimeMillis()); // unique for this obj
081: forceMatchNull = false;
082: managed = true;
083: closed = true; // Managed Connection
084: connDate = null; // Managed Connection
085: listeners = new Vector(); // used in ManagedConnection
086: pw = null;
087: xar = null; // used in ManagedConnection
088: xari = null;
089: loTx = null; // used in ManagedConnection
090: }
091:
092: private String impl(Object obj) {
093: if (obj instanceof ManagedConnectionFactory) {
094: return "ManagedConnectionFactory";
095: } else if (obj instanceof ConnectionRequestInfo) {
096: return "ConnectionRequestInfo";
097: } else if (obj instanceof ManagedConnection) {
098: return "ManagedConnection";
099: } else if (obj instanceof ManagedConnectionMetaData) {
100: return "ManagedConnectionMetaData";
101: } else if (obj instanceof GenericCredential) {
102: return "GenericCredential";
103: } else
104: return "JtestResourceAdapter. Is this an error";
105:
106: }
107:
108: // ************************
109: // ManagedConnectionFactory methods see 5.5.3
110: // ************************
111: // Referenced classes of package javax.resource.spi:
112: // ConnectionManager, ConnectionRequestInfo, ManagedConnection
113: public Object createConnectionFactory(
114: ConnectionManager connectionmanager)
115: throws ResourceException {
116: cm = connectionmanager;
117: cName = "ManagedConnectionFactory";
118: logger.log(BasicLevel.DEBUG, cName
119: + ".createConnectionFactory cm=" + cm);
120: Object obj;
121: obj = new CommonClient(this , connectionmanager); // creates ConnectionFactory
122: return obj;
123: }
124:
125: /**
126: *
127: * When the createConnectionFactory method takes no arguments, the resource adapter
128: * provides a default ConnectionManager instance. This case is used in a non-managed
129: * application scenario.
130: * see 5.5.3
131: */
132: public Object createConnectionFactory() throws ResourceException {
133: cName = "ManagedConnectionFactory";
134: logger.log(BasicLevel.DEBUG, cName
135: + ": error - non-managed detected");
136: throw new NotSupportedException(
137: "A non-managed two-tier application environment is not supported in J2EE testing.");
138: }
139:
140: public ManagedConnection createManagedConnection(Subject subject,
141: ConnectionRequestInfo connectionrequestinfo)
142: throws ResourceException {
143: cName = "ManagedConnectionFactory";
144: logger.log(BasicLevel.DEBUG, cName
145: + ".createManagedConnection subject=" + subject
146: + ", cri=" + connectionrequestinfo);
147: mcf = (ManagedConnectionFactory) this ;
148: if (subject != null) {
149: //
150: // The application server user's password to the EIS is carried in Subject
151: //
152: try {
153: PasswordCredential pc = getPasswordCredential(mcf,
154: subject, connectionrequestinfo);
155: if (pc == null) {
156: logger
157: .log(
158: BasicLevel.DEBUG,
159: cName
160: + ".createManagedConnection PasswordCredential = null, Subject not null.");
161: } else {
162: if (pc.getUserName() == null
163: || pc.getUserName().length() == 0) {
164: logger
165: .log(
166: BasicLevel.DEBUG,
167: cName
168: + ".createManagedConnection userName=empty");
169: } else {
170: userName = new String(pc.getUserName());
171: logger.log(BasicLevel.DEBUG, cName
172: + ".createManagedConnection"
173: + " PasswordCredential userName="
174: + userName);
175: password = new String(pc.getPassword()); // pc.password is char[]
176: logger.log(BasicLevel.DEBUG, cName
177: + ".createManagedConnection"
178: + " PasswordCredential password="
179: + password);
180: }
181: }
182: } catch (Exception e) {
183: logger
184: .log(
185: BasicLevel.DEBUG,
186: cName
187: + ".createManagedConnection getPasswordCredential "
188: + "error: e=" + e.toString());
189: }
190: }
191:
192: if (connectionrequestinfo != null) {
193: //
194: // The client application component user's password is carried in ConnectionRequestInfo
195: // Use defaults if userName or password is empty
196: //
197: JtestResourceAdapter jinfo = (JtestResourceAdapter) connectionrequestinfo;
198: String s = jinfo.getUserName();
199: if (s.length() > 0)
200: userName = s;
201: else
202: logger.log(BasicLevel.DEBUG, cName
203: + ".createManagedConnection"
204: + " ConnectionRequestInfo userName=" + s
205: + ". Use default=" + userName);
206: logger.log(BasicLevel.DEBUG, cName
207: + ".createManagedConnection"
208: + " ConnectionRequestInfo userName=" + userName);
209: s = jinfo.getPassword();
210: if (s.length() > 0)
211: password = s;
212: else
213: logger.log(BasicLevel.DEBUG, cName
214: + ".createManagedConnection"
215: + " ConnectionRequestInfo password=" + s
216: + ". Use default=" + password);
217: logger.log(BasicLevel.DEBUG, cName
218: + ".createManagedConnection"
219: + " ConnectionRequestInfo password=" + password);
220: }
221:
222: if (subject == null && connectionrequestinfo == null) {
223: //
224: // The default user's password is carried in the ManagedConnectionFactory instance
225: //
226: userName = defaultUserName;
227: password = defaultPassword;
228: logger.log(BasicLevel.DEBUG, cName
229: + ".createManagedConnection default userName="
230: + userName);
231: logger.log(BasicLevel.DEBUG, cName
232: + ".createManagedConnection default password="
233: + password);
234: }
235:
236: Date cDate = new Date();
237: try {
238: //
239: // Create the ManagedConnection Instance
240: //
241: ManagedConnection mc = (ManagedConnection) new JtestResourceAdapter();
242: JtestResourceAdapter obj = (JtestResourceAdapter) mc;
243: obj.setConnDate(cDate);
244: obj.setMcf(mcf);
245: obj.setPassword(password);
246: obj.setUserName(userName);
247: obj.setRes_Auth(res_auth);
248: obj.setLogWriter(pw);
249: logger.log(BasicLevel.DEBUG, cName
250: + ".createManagedConnection mc=" + mc
251: + " with connDate=" + cDate);
252: return mc;
253: } catch (Exception ex) {
254: logger
255: .log(
256: BasicLevel.DEBUG,
257: cName
258: + ".createManagedConnection : error - Exception ex="
259: + ex.getMessage());
260: throw new ResourceAllocationException(ex.getMessage());
261: }
262: }
263:
264: boolean forceMatchNull;
265:
266: public void setMatchNull(boolean truefalse) {
267: forceMatchNull = truefalse;
268: }
269:
270: public ManagedConnection matchManagedConnections(Set connectionSet,
271: Subject subject, ConnectionRequestInfo connectionrequestinfo)
272: throws ResourceException {
273: cName = "ManagedConnectionFactory";
274: logger
275: .log(BasicLevel.DEBUG, cName
276: + ".matchManagedConnections");
277: ManagedConnection mc = null;
278: if (forceMatchNull) {
279: logger.log(BasicLevel.DEBUG, cName
280: + ".matchManagedConnections force new connection");
281: return mc; // forces new connection
282: }
283: JtestResourceAdapter wmc = null;
284: Iterator it = connectionSet.iterator();
285: int cnt = 0;
286: while (it.hasNext()) {
287: Object obj = it.next();
288: logger
289: .log(
290: BasicLevel.DEBUG,
291: cName
292: + ".matchManagedConnections "
293: + "find next ManagedConnection in Set. connectionSet cnt="
294: + (++cnt));
295: if (obj instanceof ManagedConnection) {
296: // see if ManagedConnection is available
297: // i.e., no connection handle exists for mc instance
298: // i.e., the connection is closed
299: // TODO choose one of the above
300: wmc = (JtestResourceAdapter) obj;
301: if (wmc.getCHandle() == null || wmc.isClosed()) {
302: mc = (ManagedConnection) wmc;
303: logger
304: .log(
305: BasicLevel.DEBUG,
306: cName
307: + ".matchManagedConnections "
308: + "connection handle == null. connectionSet cnt="
309: + cnt);
310: } else {
311: // error: connection should not exist J2ee 1.0 pg. 32
312: String s = "connection handle should not exist";
313: logger.log(BasicLevel.DEBUG, cName
314: + ".matchManagedConnections " + "error: "
315: + s + ". connectionSet cnt=" + cnt);
316: }
317: }
318: }
319: if (mc == null)
320: logger
321: .log(
322: BasicLevel.DEBUG,
323: cName
324: + ".matchManagedConnections mc=null connectionSet cnt="
325: + cnt);
326: else
327: logger.log(BasicLevel.DEBUG, cName
328: + ".matchManagedConnections with connDate="
329: + wmc.getConnDate());
330: return mc;
331: }
332:
333: //
334: // The setLogWriter and getLogWriter are used in ManagedConnectionFactory
335: // and ManagedConnection
336: // the Application Server calls ManagedConnection.setLogWriter(pw)
337: //
338: public void setLogWriter(PrintWriter printwriter)
339: throws ResourceException {
340: if (printwriter != null)
341: pw = printwriter;
342: }
343:
344: public PrintWriter getLogWriter() throws ResourceException {
345: return pw;
346: }
347:
348: //
349: // config-property processing "setter" properties
350: //
351: String ServerName = "";
352: String PortNumber = "";
353: String protocol = "";
354:
355: /** Set the JOnAS config-property.. Default value is "".
356: *
357: * @param c1 String of config-property.
358: * for testng RA.
359: **/
360: public void setServerName(String c1) {
361: ServerName = c1;
362: //logger.log(BasicLevel.DEBUG, cName+".setServerName="+ServerName);
363: }
364:
365: public String getServerName() {
366: return ServerName;
367: }
368:
369: public void setPortNumber(String s) {
370: PortNumber = s;
371: //logger.log(BasicLevel.DEBUG, cName+".setPortNumber="+PortNumber);
372: }
373:
374: public void setDUserName(String s) {
375: defaultUserName = s;
376: //logger.log(BasicLevel.DEBUG, cName+".setDUserName="+defaultUserName);
377: }
378:
379: public void setDPassword(String s) {
380: defaultPassword = s;
381: //logger.log(BasicLevel.DEBUG, cName+".setDPassword="+defaultPassword);
382: }
383:
384: public void setProtocol(String s) {
385: protocol = s;
386: //logger.log(BasicLevel.DEBUG, cName+".setProtocol="+protocol);
387: }
388:
389: public String getProtocol() {
390: return protocol;
391: }
392:
393: public ConnectionManager getCM() {
394: return cm;
395: }
396:
397: public String getRes_Auth() {
398: return res_auth;
399: }
400:
401: public void setRes_Auth(String r) {
402: res_auth = r;
403: }
404:
405: //************************
406: // ConnectionRequestInfo
407: //************************
408: // module to add additional connection properties than
409: // the ones configured for the ConnectionFactory. This
410: // class is passed to the App Server and will be used
411: // on matchManagedConnections or createConnection
412: // methods by the App Server.
413: //
414: String Id = ""; // used in hashcode() for ConnectionRequestInfo
415:
416: //
417: public void setUserName(String u) {
418: userName = u;
419: if (cName.length() == 0)
420: cName = impl(this );
421: logger.log(BasicLevel.DEBUG, cName + ".setUserName=" + u);
422: }
423:
424: public void setPassword(String p) {
425: password = p;
426: if (cName.length() == 0)
427: cName = impl(this );
428: logger.log(BasicLevel.DEBUG, cName + ".setPassword=" + p);
429: }
430:
431: public String getUserName() {
432: if (cName.length() == 0)
433: cName = impl(this );
434: logger
435: .log(BasicLevel.DEBUG, cName + ".getUserName="
436: + userName);
437: return userName;
438: }
439:
440: public String getPassword() {
441: if (cName.length() == 0)
442: cName = impl(this );
443: logger
444: .log(BasicLevel.DEBUG, cName + ".getPassword="
445: + password);
446: return password;
447: }
448:
449: private void setId() {
450: if (Id == null || Id.length() == 0)
451: Id = timeStamp;
452:
453: }
454:
455: //
456: // hashCode and equals method used in ConnectionRequestInfo,
457: // ManagedConnectionFactory
458: // and GenericCredential
459: public int hashCode() {
460: int hash = 1;
461: setId();
462: hash = Id.hashCode();
463: return hash;
464: }
465:
466: public boolean equals(Object obj) {
467: boolean match = false;
468: if (obj == null) {
469: return match;
470: }
471: JtestResourceAdapter other = (JtestResourceAdapter) obj;
472: if (this .timeStamp.equals(other.getTimeStamp()))
473: match = true;
474: return match;
475: }
476:
477: // *****************
478: // GenericCredential methods
479: // *****************
480: //
481: String gcID = ""; // used in hashcode() for GenericCredential
482:
483: public String getName() {
484: cName = "GenericCredential";
485: logger.log(BasicLevel.DEBUG, cName + ".getName");
486: return "";
487: }
488:
489: public String getMechType() {
490: cName = "GenericCredential";
491: logger.log(BasicLevel.DEBUG, cName + ".getMechType");
492: return "";
493: }
494:
495: public byte[] getCredentialData() throws SecurityException {
496: cName = "GenericCredential";
497: logger.log(BasicLevel.DEBUG, cName + ".getCredentialData");
498: byte[] x = { 0, 0 };
499: return x;
500: }
501:
502: // *****************
503: // ManagedConnection methods
504: // *****************
505: //
506: // Referenced classes of package javax.resource.spi:
507: // ConnectionRequestInfo, ConnectionEventListener, LocalTransaction,
508: // ManagedConnectionMetaData
509: //
510: // NOTE: Subject is instantiated by Application Server
511: //
512: boolean closed;
513: public ConnectionImpl cHandle; // ManagedConnection mapped 1:1 to a physical connection
514: private boolean destroyed; //set when destroyed
515: private Date connDate; //save connection date/time for a ManagedConnection
516: public boolean inXATrans;
517: public boolean inLocalTrans;
518: public boolean sendEvent;
519:
520: public int cntListeners() {
521: cName = "ManagedConnection";
522: Vector lst = (Vector) listeners.clone();
523: int len = lst.size();
524: logger.log(BasicLevel.DEBUG, cName + ".cntListeners counted="
525: + len);
526: return len;
527: }
528:
529: public ConnectionImpl getCHandle() {
530: return cHandle;
531: }
532:
533: public boolean isClosed() {
534: return closed;
535: }
536:
537: public Object getConnection(Subject subject,
538: ConnectionRequestInfo connectionrequestinfo)
539: throws ResourceException {
540: cName = "ManagedConnection";
541: logger.log(BasicLevel.DEBUG, cName
542: + ".getConnection *******************");
543: if (subject != null) {
544: //
545: // for this code to execute, the following will be present in ra.xml
546: // <reauthentication-support>true</reauthentication-support>
547: //
548: logger.log(BasicLevel.DEBUG, cName
549: + ".getConnection via PasswordCredential");
550: logger
551: .log(BasicLevel.DEBUG,
552: "<reauthentication-support>true</reauthentication-support>");
553: try {
554: PasswordCredential pc = getPasswordCredential(mcf,
555: subject, connectionrequestinfo);
556: if (pc == null) {
557: logger
558: .log(
559: BasicLevel.DEBUG,
560: cName
561: + ".getConnection PasswordCredential = null");
562: } else {
563: if (pc.getUserName() == null
564: || pc.getUserName().length() == 0) {
565: logger.log(BasicLevel.DEBUG, cName
566: + ".getConnection userName=empty");
567: } else {
568: String re_authentication_UserName = new String(
569: pc.getUserName());
570: String re_authentication_Password = new String(
571: pc.getPassword());
572: logger.log(BasicLevel.DEBUG, cName
573: + ".getConnection"
574: + " re-authentication userName="
575: + re_authentication_UserName);
576: logger.log(BasicLevel.DEBUG, cName
577: + ".getConnection"
578: + " compare to existing userName="
579: + userName);
580: logger.log(BasicLevel.DEBUG, cName
581: + ".getConnection"
582: + " re-authentication password="
583: + re_authentication_Password);
584: logger.log(BasicLevel.DEBUG, cName
585: + ".getConnection"
586: + " compare to existing password="
587: + password);
588: }
589: }
590: } catch (Exception e) {
591: logger
592: .log(
593: BasicLevel.DEBUG,
594: cName
595: + ".getConnection getPasswordCredential error: e="
596: + e.toString());
597: }
598: }
599: // Code for reauthentication-support when Subject==null and ConnectionRequestInfo
600: // contains different password and username
601: //
602: if (connectionrequestinfo != null) {
603: logger.log(BasicLevel.DEBUG, impl(this )
604: + ".getConnection via ConnectionRequestInfo");
605: logger
606: .log(BasicLevel.DEBUG,
607: "<reauthentication-support>true</reauthentication-support>");
608: JtestResourceAdapter jcri = (JtestResourceAdapter) connectionrequestinfo;
609: String re_authentication_UserName = new String(jcri
610: .getUserName());
611: String re_authentication_Password = new String(jcri
612: .getPassword());
613: logger.log(BasicLevel.DEBUG, " re-authentication userName="
614: + re_authentication_UserName
615: + " compare to existing userName=" + userName);
616: logger.log(BasicLevel.DEBUG, " re-authentication password="
617: + re_authentication_Password
618: + " compare to existing password=" + password);
619: }
620: Object obj = new ConnectionImpl(this );
621: cHandle = (ConnectionImpl) obj;
622: closed = false;
623: destroyed = false;
624: return obj;
625: }
626:
627: public void destroy() throws ResourceException {
628: cName = "ManagedConnection";
629: logger.log(BasicLevel.DEBUG, cName + ".destroy");
630: // the physical connection is closed
631: destroyed = true;
632: closed = true;
633: cHandle = null;
634: }
635:
636: public void cleanup() throws ResourceException {
637: cName = "ManagedConnection";
638: // the physical connection stays open
639: // but all connection handles invalidated
640: logger.log(BasicLevel.DEBUG, cName + ".cleanup");
641: cHandle = null;
642: closed = true;
643:
644: }
645:
646: public void associateConnection(Object obj)
647: throws ResourceException {
648: cName = "ManagedConnection";
649: logger.log(BasicLevel.DEBUG, cName + ".associateConnection");
650: if (obj instanceof ConnectionImpl) {
651: ConnectionImpl conn = (ConnectionImpl) obj;
652: conn.associateConnection(this ); // TODO may need more arg to associate
653: } else {
654: logger.log(BasicLevel.DEBUG, cName
655: + ".associateConnection "
656: + "error: obj not instanceof ConnectionImpl");
657: }
658: }
659:
660: public void addConnectionEventListener(
661: ConnectionEventListener listener) {
662: cName = "ManagedConnection";
663: listeners.addElement(listener);
664: logger.log(BasicLevel.DEBUG, cName
665: + ".addConnectionEventListener listener=" + listener);
666: }
667:
668: public void removeConnectionEventListener(
669: ConnectionEventListener listener) {
670: cName = "ManagedConnection";
671: Vector lst = (Vector) listeners.clone();
672: int len = lst.size();
673: logger.log(BasicLevel.DEBUG, cName
674: + ".removeConnectionEventListener "
675: + "Number of listeners=" + len);
676: try {
677: listeners.removeElement(listener);
678: } catch (Exception e) {
679: logger.log(BasicLevel.DEBUG, cName
680: + ".removeConnectionEventListener error: "
681: + "unable to remove listener");
682: }
683: lst = (Vector) listeners.clone();
684: len = lst.size();
685: logger.log(BasicLevel.DEBUG, cName
686: + ".removeConnectionEventListener listener=" + listener
687: + " was removed. Number listeners left=" + len);
688: }
689:
690: public XAResource getXAResource() throws ResourceException {
691: cName = "ManagedConnection";
692: if (xar == null)
693: xar = (XAResource) new XAResourceImpl(this );
694: logger.log(BasicLevel.DEBUG, cName + ".getXAResource xar="
695: + xar);
696: return xar;
697: }
698:
699: public void resetXar() {
700: // dissociate XAResource Xid from this ManagedConnection
701: xar = null;
702: xari = null;
703: }
704:
705: public XAResourceImpl getCurrentXar() throws ResourceException {
706: xari = (XAResourceImpl) xar;
707: return xari;
708: }
709:
710: public javax.resource.cci.LocalTransaction getLocalTransaction(
711: boolean sendEvent) throws ResourceException {
712: cName = "ManagedConnection";
713: LocalTransactionImpl lt = null;
714: logger.log(BasicLevel.DEBUG, cName
715: + ".getLocalTransaction(sendEvent)");
716: this .sendEvent = sendEvent;
717: try {
718: lt = (LocalTransactionImpl) getLocalTransaction();
719: logger.log(BasicLevel.DEBUG, cName
720: + ".getLocalTransaction(sendEvent) lt=" + lt);
721: } catch (Exception e) {
722: logger.log(BasicLevel.DEBUG, cName
723: + ".getLocalTransaction(sendEvent) " + "error: "
724: + e.getMessage());
725: }
726: return (lt);
727: }
728:
729: public LocalTransaction getLocalTransaction()
730: throws ResourceException {
731: cName = "ManagedConnection";
732: logger.log(BasicLevel.DEBUG, cName + ".getLocalTransaction");
733: if (loTx == null) {
734: loTx = new LocalTransactionImpl(this , sendEvent);
735: logger.log(BasicLevel.DEBUG, cName
736: + ".getLocalTransaction new loTx=" + loTx);
737: return loTx;
738: } else {
739: logger.log(BasicLevel.DEBUG, cName
740: + ".getLocalTransaction old loTx=" + loTx);
741: loTx.setSendEvent(sendEvent);
742: return loTx;
743: }
744: }
745:
746: public ManagedConnectionMetaData getMetaData()
747: throws ResourceException {
748: cName = "ManagedConnection";
749: logger.log(BasicLevel.DEBUG, cName + ".getMetaData");
750: return (ManagedConnectionMetaData) new JtestResourceAdapter();
751: }
752:
753: /** Send the specified event to all the EventListeners that have
754: * registered with this ManagedConnection instance.
755: *
756: * @param int Event type to send
757: * @param Exception if one is associated with this event
758: * @param ch Connection handle associated with this event
759: * @throws ResourceException generic exception if operation fails
760: * @throws IllegalArgumentException if an invalid event is specified
761: **/
762: public void sendEvent(int eType, Exception ex, Object ch)
763: throws ResourceException {
764: cName = "ManagedConnection";
765: Vector lst = (Vector) listeners.clone();
766: ConnectionEvent ce = null;
767: if (ex == null) {
768: ce = new ConnectionEvent(this , eType);
769: } else
770: ce = new ConnectionEvent(this , eType, ex);
771:
772: if (ch != null)
773: ce.setConnectionHandle(ch);
774: else
775: logger.log(BasicLevel.DEBUG, cName + ".sendEvent ch==null");
776:
777: int len = lst.size();
778: for (int i = 0; i < len; i++) {
779: ConnectionEventListener cel = (ConnectionEventListener) lst
780: .elementAt(i);
781: switch (eType) {
782: case ConnectionEvent.CONNECTION_CLOSED:
783: if (closed == false) {
784: closed = true;
785: cel.connectionClosed(ce);
786: cName = "ManagedConnection"; // re-establish cName
787: logger
788: .log(
789: BasicLevel.DEBUG,
790: cName
791: + ".sendEvent 'CONNECTION_CLOSED' to listener="
792: + cel + " Num=" + i);
793: }
794: break;
795: case ConnectionEvent.CONNECTION_ERROR_OCCURRED:
796: if (closed == false) {
797: closed = true;
798: cel.connectionErrorOccurred(ce);
799: cName = "ManagedConnection"; // re-establish cName
800: logger
801: .log(
802: BasicLevel.DEBUG,
803: cName
804: + ".sendEvent 'CONNECTION_ERROR_OCCURRED' to listener="
805: + cel + " Num=" + i);
806: }
807: break;
808: case ConnectionEvent.LOCAL_TRANSACTION_STARTED:
809: cel.localTransactionStarted(ce);
810: cName = "ManagedConnection"; // re-establish cName
811: logger
812: .log(
813: BasicLevel.DEBUG,
814: cName
815: + ".sendEvent 'LOCAL_TRANSACTION_STARTED' to listener="
816: + cel + " Num=" + i);
817: break;
818: case ConnectionEvent.LOCAL_TRANSACTION_COMMITTED:
819: cel.localTransactionCommitted(ce);
820: cName = "ManagedConnection"; // re-establish cName
821: logger
822: .log(
823: BasicLevel.DEBUG,
824: cName
825: + ".sendEvent 'LOCAL_TRANSACTION_COMMITTED' to listener="
826: + cel + " Num=" + i);
827: break;
828: case ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK:
829: cel.localTransactionRolledback(ce);
830: cName = "ManagedConnection"; // re-establish cName
831: logger
832: .log(
833: BasicLevel.DEBUG,
834: cName
835: + ".sendEvent 'LOCAL_TRANSACTION_ROLLEDBACK' to listener="
836: + cel + " Num=" + i);
837: break;
838: default:
839: IllegalArgumentException iae = new IllegalArgumentException(
840: "Illegal eventType: " + eType);
841: logger.log(BasicLevel.DEBUG, cName
842: + ".sendEvent error: " + iae.toString());
843: throw iae;
844: }
845: }
846:
847: }
848:
849: public void setConnDate(Date cDate) { // called by ManagedConnectionFactory
850: cName = "ManagedConnection";
851: logger
852: .log(BasicLevel.DEBUG, cName + ".setConnDate to "
853: + cDate);
854: connDate = cDate;
855: }
856:
857: public void setMcf(ManagedConnectionFactory MCF) { // called by ManagedConnectionFactory
858: cName = "ManagedConnection";
859: logger.log(BasicLevel.DEBUG, cName + ".setMcf to " + MCF);
860: this .mcf = MCF;
861: }
862:
863: public Date getConnDate() {
864: return connDate;
865: }
866:
867: public ManagedConnectionFactory getMcf() {
868: return this .mcf;
869: }
870:
871: public void setTimeStamp(String ts) {
872: cName = impl(this );
873: timeStamp = ts;
874: logger.log(BasicLevel.DEBUG, cName + ".setTimeStamp to " + ts);
875: }
876:
877: public String getTimeStamp() {
878: return timeStamp;
879: }
880:
881: // *************************
882: // ManagedConnectionMetaData methods
883: // *************************
884: //
885: String product = "Fictional EIS";
886: String version = "1.1";
887:
888: public String getEISProductName() throws ResourceException {
889: cName = "ManagedConnectionMetaData";
890: logger.log(BasicLevel.DEBUG, cName + ".getEISProductName");
891: return product;
892: }
893:
894: public String getEISProductVersion() throws ResourceException {
895: cName = "ManagedConnectionMetaData";
896: logger.log(BasicLevel.DEBUG, cName + ".getEISProductVersion");
897: return version;
898: }
899:
900: public int getMaxConnections() throws ResourceException {
901: cName = "ManagedConnectionMetaData";
902: logger.log(BasicLevel.DEBUG, cName + ".getMaxConnections");
903: return 0;
904: }
905:
906: /** Required by the referencable attribute
907: * @param ref Reference object
908: **/
909: public void setReference(javax.naming.Reference ref) {
910: this .reference = ref;
911: }
912:
913: /** Required by the referencable attribute
914: * @return Reference object
915: **/
916: public Reference getReference() throws NamingException {
917: return reference;
918: }
919:
920: /** Returns the Password credential
921: *
922: * @param mcf ManagedConnectionFactory currently being used
923: * @param subject Subject associated with this call
924: * @param info ConnectionRequestInfo
925: *
926: * @return PasswordCredential for this user
927: */
928: public PasswordCredential getPasswordCredential(
929: ManagedConnectionFactory mcf, Subject subject,
930: ConnectionRequestInfo info) throws ResourceException {
931:
932: logger.log(BasicLevel.DEBUG, cName
933: + ".getPasswordCredential subject=" + subject);
934: PasswordCredential pc = null;
935: try {
936: if (subject == null) {
937: if (info == null)
938: return null;
939: JtestResourceAdapter crii = (JtestResourceAdapter) info; // ConnectionRequestInfo
940: pc = new PasswordCredential(crii.userName,
941: crii.password.toCharArray());
942: pc.setManagedConnectionFactory(mcf);
943: return pc;
944: }
945: Set cred = subject
946: .getPrivateCredentials(PasswordCredential.class);
947: pc = null;
948: for (Iterator iter = cred.iterator(); iter.hasNext();) {
949: PasswordCredential tmpPc = (PasswordCredential) iter
950: .next();
951: if (mcf.equals(tmpPc.getManagedConnectionFactory())) {
952: logger
953: .log(
954: BasicLevel.DEBUG,
955: cName
956: + ".getPasswordCredential PasswordCredential "
957: + "found mcf=" + mcf);
958: pc = tmpPc;
959: break;
960: }
961: }
962: } catch (Exception e) {
963: logger.log(BasicLevel.DEBUG, cName
964: + ".getPasswordCredential error: " + e.toString());
965: pc = null;
966: }
967: if (pc == null) {
968: SecurityException se = new SecurityException(
969: "No PasswordCredential found");
970: logger.log(BasicLevel.DEBUG, cName
971: + ".getPasswordCredential error: " + se);
972: throw se;
973: }
974: return pc;
975: }
976:
977: }
|