001: /*
002: * Created on December 11, 2003
003: *
004: * ManagedConnectionImpl.java is a Resource Adapter class to test the J2EE Connector
005: * as implemented by JOnAS.
006: */
007: package ersatz.resourceadapter;
008:
009: import java.io.PrintWriter;
010: import java.util.Date;
011: import java.util.Vector; // <=========
012: import javax.security.auth.Subject;
013: import javax.resource.ResourceException;
014: import javax.resource.spi.ConnectionEvent;
015: import javax.transaction.xa.XAResource;
016: import javax.resource.spi.ManagedConnectionFactory;
017: import javax.resource.spi.ConnectionEventListener;
018: import javax.resource.spi.ConnectionManager;
019: import javax.resource.spi.ConnectionRequestInfo;
020: import javax.resource.spi.LocalTransaction;
021: import javax.resource.spi.ManagedConnection;
022: import javax.resource.spi.ManagedConnectionMetaData;
023:
024: /**
025: * @author Bob Kruse
026: *
027: * used to test the J2EE Connector as implemented by JOnAS.
028: *
029: */
030: public class ManagedConnectionImpl implements ManagedConnection {
031: String Id = "";
032: private String userName = "";
033: private String password = "";
034: private ManagedConnectionFactory mcf; // loaded by ManagedConnectionFactory
035: public ConnectionManager cm; // loaded by ManagedConnectionFactory
036: private ConnectionRequestInfoImpl crii;
037: public LocalTransactionImpl loTx = null;
038: PrintWriter pw; // App Server sets to null by default
039: private Vector listeners;
040: //Managed environment
041: public String res_auth; // set by ConnectionFactory.getConnection()
042: // then put into ManagedConnection when created
043: public String re_authentication_UserName; // set by ConnectionFactory.getConnection()
044: public String re_authentication_Password;
045: public XAResource xar;
046: public XAResourceImpl xari;
047: boolean closed;
048: public ConnectionImpl cHandle; // ManagedConnection mapped 1:1 to a physical connection
049: private boolean destroyed; //set when destroyed
050: public boolean inXATrans;
051: public boolean inLocalTrans;
052: public boolean sendEvent;
053: String cName = "ManagedConnectionImpl";
054:
055: //
056: // constructor
057: public ManagedConnectionImpl(ConnectionRequestInfoImpl CRII) {
058: crii = new ConnectionRequestInfoImpl(CRII);
059: userName = crii.userName;
060: password = crii.password;
061: closed = true;
062: listeners = new Vector();
063: pw = null;
064: xar = null;
065: xari = null;
066: loTx = null;
067: }
068:
069: public void setUserName(String u) {
070: userName = u;
071: Utility.log(cName + ".setUserName=" + u);
072: }
073:
074: public void setPassword(String p) {
075: password = p;
076: Utility.log(cName + ".setPassword=" + p);
077: }
078:
079: public void setRes_Auth(String r) {
080: res_auth = r;
081: Utility.log(cName + ".setRes_Auth=" + r);
082: }
083:
084: //
085: // Referenced classes of package javax.resource.spi:
086: // ConnectionRequestInfo, ConnectionEventListener, LocalTransaction,
087: // ManagedConnectionMetaData
088: //
089: // NOTE: Subject is instantiated by Application Server
090: //
091: public int cntListeners() {
092: Vector lst = (Vector) listeners.clone();
093: int len = lst.size();
094: Utility.log(cName + ".cntListeners counted=" + len);
095: return len;
096: }
097:
098: public ConnectionImpl getCHandle() {
099: return cHandle;
100: }
101:
102: public boolean isClosed() {
103: return closed;
104: }
105:
106: public ConnectionRequestInfoImpl getCrii() {
107: return crii;
108: }
109:
110: public Object getConnection(Subject subject,
111: ConnectionRequestInfo connectionrequestinfo)
112: throws ResourceException {
113: Utility.log(cName + ".getConnection *******************");
114: if (subject != null) {
115: //
116: // for this code to execute, the following will be present in ra.xml
117: // <reauthentication-support>true</reauthentication-support>
118: //
119: Utility
120: .log(cName
121: + ".getConnection via PasswordCredential");
122: Utility
123: .log("<reauthentication-support>true</reauthentication-support>");
124: try {
125: // TODO Find out about org.ietf.jgss.GSSCredential interface in J2SE 1.4
126: } catch (Exception e) {
127: Utility
128: .log(cName
129: + ".getConnection getPasswordCredential error: e="
130: + e.toString());
131: }
132: }
133: // Code for reauthentication-support when Subject==null and ConnectionRequestInfo
134: // contains different password and username
135: //
136: if (connectionrequestinfo != null) {
137: Utility.log(cName
138: + ".getConnection via ConnectionRequestInfo");
139: Utility
140: .log("<reauthentication-support>true</reauthentication-support>");
141: ConnectionRequestInfoImpl cri = (ConnectionRequestInfoImpl) connectionrequestinfo;
142: String re_authentication_UserName = new String(cri
143: .getUserName());
144: String re_authentication_Password = new String(cri
145: .getPassword());
146: Utility.log(" re-authentication userName="
147: + re_authentication_UserName
148: + " compare to existing userName=" + userName);
149: Utility.log(" re-authentication password="
150: + re_authentication_Password
151: + " compare to existing password=" + password);
152: }
153: Object obj = new ConnectionImpl(this );
154: cHandle = (ConnectionImpl) obj;
155: closed = false;
156: destroyed = false;
157: return obj;
158: }
159:
160: public void destroy() throws ResourceException {
161: Utility.log(cName + ".destroy");
162: // the physical connection is closed
163: destroyed = true;
164: closed = true;
165: cHandle = null;
166: }
167:
168: public void cleanup() throws ResourceException {
169: // the physical connection stays open
170: // but all connection handles invalidated
171: Utility.log(cName + ".cleanup");
172: cHandle = null;
173: closed = true;
174:
175: }
176:
177: public void associateConnection(Object obj)
178: throws ResourceException {
179: Utility.log(cName + ".associateConnection");
180: if (obj instanceof ConnectionImpl) {
181: ConnectionImpl conn = (ConnectionImpl) obj;
182: conn.associateConnection(this ); // TODO may need more arg to associate
183: } else {
184: Utility.log(cName + ".associateConnection "
185: + "error: obj not instanceof ConnectionImpl");
186: }
187: }
188:
189: public void addConnectionEventListener(
190: ConnectionEventListener listener) {
191: listeners.addElement(listener);
192: Utility.log(cName + ".addConnectionEventListener listener="
193: + listener);
194: }
195:
196: public void removeConnectionEventListener(
197: ConnectionEventListener listener) {
198: Vector lst = (Vector) listeners.clone();
199: int len = lst.size();
200: Utility.log(cName + ".removeConnectionEventListener "
201: + "Number of listeners=" + len);
202: try {
203: listeners.removeElement(listener);
204: } catch (Exception e) {
205: Utility.log(cName
206: + ".removeConnectionEventListener error: "
207: + "unable to remove listener");
208: }
209: lst = (Vector) listeners.clone();
210: len = lst.size();
211: Utility.log(cName + ".removeConnectionEventListener listener="
212: + listener + " was removed. Number listeners left="
213: + len);
214: }
215:
216: public XAResource getXAResource() throws ResourceException {
217: // TODO determine if <transaction-support>NoTransaction
218: // throw ResourceException
219: if (xar == null)
220: xar = (XAResource) new XAResourceImpl(this );
221: Utility.log(cName + ".getXAResource xar=" + xar);
222: return xar;
223: }
224:
225: public void resetXar() {
226: // dissociate XAResource Xid from this ManagedConnection
227: Utility
228: .log(cName
229: + ".resetXar dissociate XAResource Xid from this ManagedConnection");
230: xar = null;
231: xari = null;
232: }
233:
234: public XAResourceImpl getCurrentXar() throws ResourceException {
235: xari = (XAResourceImpl) xar;
236: Utility.log(cName + ".getCurrentXar xari=" + xari);
237: return xari;
238: }
239:
240: public javax.resource.cci.LocalTransaction getLocalTransaction(
241: boolean sendEvent) throws ResourceException {
242: LocalTransactionImpl lt = null;
243: Utility.log(cName + ".getLocalTransaction(sendEvent)");
244: this .sendEvent = sendEvent;
245: try {
246: lt = (LocalTransactionImpl) getLocalTransaction();
247: Utility.log(cName + ".getLocalTransaction(sendEvent) lt="
248: + lt);
249: } catch (Exception e) {
250: Utility.log(cName + ".getLocalTransaction(sendEvent) "
251: + "error: " + e.getMessage());
252: }
253: return (lt);
254: }
255:
256: public LocalTransaction getLocalTransaction()
257: throws ResourceException {
258: Utility.log(cName + ".getLocalTransaction");
259: if (loTx == null) {
260: loTx = new LocalTransactionImpl(this , sendEvent);
261: Utility
262: .log(cName + ".getLocalTransaction new loTx="
263: + loTx);
264: return loTx;
265: } else {
266: Utility
267: .log(cName + ".getLocalTransaction old loTx="
268: + loTx);
269: loTx.setSendEvent(sendEvent);
270: return loTx;
271: }
272: }
273:
274: public ManagedConnectionMetaData getMetaData()
275: throws ResourceException {
276: Utility.log(cName + ".getMetaData");
277: return new ManagedConnectionMetaDataImpl();
278: }
279:
280: /** Send the specified event to all the EventListeners that have
281: * registered with this ManagedConnection instance.
282: *
283: * @param int Event type to send
284: * @param Exception if one is associated with this event
285: * @param ch Connection handle associated with this event
286: * @throws ResourceException generic exception if operation fails
287: * @throws IllegalArgumentException if an invalid event is specified
288: **/
289: public void sendEvent(int eType, Exception ex, Object ch)
290: throws ResourceException {
291: Vector lst = (Vector) listeners.clone();
292: ConnectionEvent ce = null;
293: if (ex == null) {
294: ce = new ConnectionEvent(this , eType);
295: } else
296: ce = new ConnectionEvent(this , eType, ex);
297:
298: if (ch != null)
299: ce.setConnectionHandle(ch);
300: else
301: Utility.log(cName + ".sendEvent ch==null");
302:
303: int len = lst.size();
304: for (int i = 0; i < len; i++) {
305: ConnectionEventListener cel = (ConnectionEventListener) lst
306: .elementAt(i);
307: switch (eType) {
308: case ConnectionEvent.CONNECTION_CLOSED:
309: if (closed == false) {
310: closed = true;
311: cel.connectionClosed(ce);
312: Utility
313: .log(cName
314: + ".sendEvent 'CONNECTION_CLOSED' to listener="
315: + cel + " Num=" + i);
316: }
317: break;
318: case ConnectionEvent.CONNECTION_ERROR_OCCURRED:
319: if (closed == false) {
320: closed = true;
321: cel.connectionErrorOccurred(ce);
322: Utility
323: .log(cName
324: + ".sendEvent 'CONNECTION_ERROR_OCCURRED' to listener="
325: + cel + " Num=" + i);
326: }
327: break;
328: case ConnectionEvent.LOCAL_TRANSACTION_STARTED:
329: cel.localTransactionStarted(ce);
330: Utility
331: .log(cName
332: + ".sendEvent 'LOCAL_TRANSACTION_STARTED' to listener="
333: + cel + " Num=" + i);
334: break;
335: case ConnectionEvent.LOCAL_TRANSACTION_COMMITTED:
336: cel.localTransactionCommitted(ce);
337: Utility
338: .log(cName
339: + ".sendEvent 'LOCAL_TRANSACTION_COMMITTED' to listener="
340: + cel + " Num=" + i);
341: break;
342: case ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK:
343: cel.localTransactionRolledback(ce);
344: Utility
345: .log(cName
346: + ".sendEvent 'LOCAL_TRANSACTION_ROLLEDBACK' to listener="
347: + cel + " Num=" + i);
348: break;
349: default:
350: IllegalArgumentException iae = new IllegalArgumentException(
351: "Illegal eventType: " + eType);
352: Utility.log(cName + ".sendEvent error: "
353: + iae.toString());
354: throw iae;
355: }
356: }
357:
358: }
359:
360: public void setMcf(ManagedConnectionFactory MCF) { // called by ManagedConnectionFactory
361: Utility.log(cName + ".setMcf to " + MCF);
362: this .mcf = MCF;
363: }
364:
365: public ManagedConnectionFactory getMcf() {
366: return this .mcf;
367: }
368:
369: //
370: // The setLogWriter and getLogWriter are used in ManagedConnectionFactory
371: // and ManagedConnection
372: // the Application Server calls ManagedConnection.setLogWriter(pw)
373: //
374: public void setLogWriter(PrintWriter printwriter)
375: throws ResourceException {
376: Utility.log(cName + ".setLogWriter mci.pw=" + pw
377: + " ApplicationServer's PrintWriter=" + printwriter);
378: if (printwriter != null)
379: pw = printwriter;
380: }
381:
382: public PrintWriter getLogWriter() throws ResourceException {
383: return pw;
384: }
385:
386: public boolean equals(Object other) {
387: boolean match = false;
388: if (other == null) {
389: return match;
390: }
391: if (other instanceof ManagedConnectionImpl) {
392: ManagedConnectionImpl otherMci = (ManagedConnectionImpl) other;
393: try {
394: match = crii.equals(otherMci.crii);
395: } catch (Exception e) {
396: Utility.log(cName + ".equals(" + otherMci
397: + ") error: false Exception=" + e);
398: return false;
399: }
400: }
401: return match;
402: }
403:
404: }
|