KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ersatz > resourceadapter > ManagedConnectionImpl


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

7 package ersatz.resourceadapter;
8
9 import java.io.PrintWriter;
10 import java.util.Date;
11 import java.util.Vector; // <=========
12
import javax.security.auth.Subject;
13 import javax.resource.ResourceException;
14 import javax.resource.spi.ConnectionEvent;
15 import javax.transaction.xa.XAResource;
16 import javax.resource.spi.ManagedConnectionFactory;
17 import javax.resource.spi.ConnectionEventListener;
18 import javax.resource.spi.ConnectionManager;
19 import javax.resource.spi.ConnectionRequestInfo;
20 import javax.resource.spi.LocalTransaction;
21 import javax.resource.spi.ManagedConnection;
22 import javax.resource.spi.ManagedConnectionMetaData;
23
24 /**
25  * @author Bob Kruse
26  *
27  * used to test the J2EE Connector as implemented by JOnAS.
28  *
29  */

30 public class ManagedConnectionImpl
31     implements ManagedConnection
32 {
33     String Id = "";
34     private String userName="";
35     private String password="";
36     private ManagedConnectionFactory mcf; // loaded by ManagedConnectionFactory
37
public ConnectionManager cm; // loaded by ManagedConnectionFactory
38
private ConnectionRequestInfoImpl crii;
39     public LocalTransactionImpl loTx=null;
40     PrintWriter pw; // App Server sets to null by default
41
private Vector listeners;
42     //Managed environment
43
public String res_auth; // set by ConnectionFactory.getConnection()
44
// then put into ManagedConnection when created
45
public String re_authentication_UserName; // set by ConnectionFactory.getConnection()
46
public String re_authentication_Password;
47     public XAResource xar;
48     public XAResourceImpl xari;
49     boolean closed;
50     public ConnectionImpl cHandle; // ManagedConnection mapped 1:1 to a physical connection
51
private boolean destroyed; //set when destroyed
52
public boolean inXATrans;
53     public boolean inLocalTrans;
54     public boolean sendEvent;
55     String cName = "ManagedConnectionImpl";
56                                         
57     //
58
// constructor
59
public ManagedConnectionImpl(ConnectionRequestInfoImpl CRII)
60     {
61         crii = new ConnectionRequestInfoImpl(CRII);
62         userName=crii.userName;
63         password=crii.password;
64         closed = true;
65         listeners = new Vector();
66         pw = null;
67         xar = null;
68         xari = null;
69         loTx = null;
70     }
71     public void setUserName(String u) {
72         userName=u;
73         Utility.log(cName+".setUserName="+u);
74     }
75     public void setPassword(String p) {
76         password=p;
77         Utility.log(cName+".setPassword="+p);
78     }
79     public void setRes_Auth(String r) {
80         res_auth=r;
81         Utility.log(cName+".setRes_Auth="+r);
82     }
83     //
84
// Referenced classes of package javax.resource.spi:
85
// ConnectionRequestInfo, ConnectionEventListener, LocalTransaction,
86
// ManagedConnectionMetaData
87
//
88
// NOTE: Subject is instantiated by Application Server
89
//
90
public int cntListeners() {
91         Vector lst = (Vector) listeners.clone();
92         int len = lst.size();
93         Utility.log(cName+".cntListeners counted="+len);
94         return len;
95     }
96     public ConnectionImpl getCHandle() {
97         return cHandle;
98     }
99     public boolean isClosed() {
100         return closed;
101     }
102     public ConnectionRequestInfoImpl getCrii() {
103         return crii;
104     }
105         
106     public Object getConnection(Subject subject, ConnectionRequestInfo connectionrequestinfo)
107         throws ResourceException
108     {
109         Utility.log(cName+".getConnection *******************");
110         if (subject!=null) {
111             //
112
// for this code to execute, the following will be present in ra.xml
113
// <reauthentication-support>true</reauthentication-support>
114
//
115
Utility.log(cName+".getConnection via PasswordCredential");
116             Utility.log("<reauthentication-support>true</reauthentication-support>");
117             try {
118                 // TODO Find out about org.ietf.jgss.GSSCredential interface in J2SE 1.4
119
} catch (Exception e) {
120                 Utility.log(cName+".getConnection getPasswordCredential error: e="
121                              +e.toString());
122             }
123         }
124         // Code for reauthentication-support when Subject==null and ConnectionRequestInfo
125
// contains different password and username
126
//
127
if (connectionrequestinfo!=null) {
128             Utility.log(cName+".getConnection via ConnectionRequestInfo");
129             Utility.log("<reauthentication-support>true</reauthentication-support>");
130             ConnectionRequestInfoImpl cri = (ConnectionRequestInfoImpl)connectionrequestinfo;
131             String re_authentication_UserName = new String(cri.getUserName());
132             String re_authentication_Password = new String(cri.getPassword());
133             Utility.log(
134                   " re-authentication userName="+re_authentication_UserName+
135                   " compare to existing userName="+userName);
136             Utility.log(
137                   " re-authentication password="+re_authentication_Password+
138                   " compare to existing password="+password);
139         }
140         Object obj = new ConnectionImpl(this);
141         cHandle = (ConnectionImpl)obj;
142         closed=false;
143         destroyed=false;
144         return obj;
145     }
146     public void destroy()
147         throws ResourceException
148     {
149         Utility.log(cName+".destroy");
150         // the physical connection is closed
151
destroyed = true;
152         closed = true;
153         cHandle=null;
154     }
155
156     public void cleanup()
157         throws ResourceException
158     {
159         // the physical connection stays open
160
// but all connection handles invalidated
161
Utility.log(cName+".cleanup");
162         cHandle=null;
163         closed=true;
164         
165     }
166
167     public void associateConnection(Object obj)
168         throws ResourceException
169     {
170         Utility.log(cName+".associateConnection");
171         if (obj instanceof ConnectionImpl) {
172             ConnectionImpl conn = (ConnectionImpl) obj;
173             conn.associateConnection(this); // TODO may need more arg to associate
174
} else {
175             Utility.log(cName+".associateConnection "+
176             "error: obj not instanceof ConnectionImpl");
177         }
178     }
179
180     public void addConnectionEventListener(ConnectionEventListener listener)
181     {
182         listeners.addElement(listener);
183         Utility.log(cName+".addConnectionEventListener listener="
184             +listener);
185     }
186
187     public void removeConnectionEventListener(ConnectionEventListener listener)
188     {
189         Vector lst = (Vector) listeners.clone();
190         int len = lst.size();
191         Utility.log(cName+".removeConnectionEventListener "
192                  +"Number of listeners="+len);
193         try {
194             listeners.removeElement(listener);
195         } catch (Exception e) {
196             Utility.log(cName+".removeConnectionEventListener error: "
197                  +"unable to remove listener");
198         }
199         lst = (Vector) listeners.clone();
200         len = lst.size();
201         Utility.log(cName+".removeConnectionEventListener listener="
202             +listener+" was removed. Number listeners left="+len);
203     }
204
205     public XAResource getXAResource()
206         throws ResourceException
207     {
208         // TODO determine if <transaction-support>NoTransaction
209
// throw ResourceException
210
if (xar==null)
211             xar = (XAResource)new XAResourceImpl(this);
212         Utility.log(cName+".getXAResource xar="+xar);
213         return xar;
214     }
215     public void resetXar() {
216         // dissociate XAResource Xid from this ManagedConnection
217
Utility.log(cName+
218                   ".resetXar dissociate XAResource Xid from this ManagedConnection");
219         xar=null;
220         xari=null;
221     }
222     public XAResourceImpl getCurrentXar()
223         throws ResourceException
224     {
225         xari=(XAResourceImpl)xar;
226         Utility.log(cName+".getCurrentXar xari="+xari);
227         return xari;
228     }
229
230     public javax.resource.cci.LocalTransaction getLocalTransaction(boolean sendEvent)
231         throws ResourceException
232     {
233         LocalTransactionImpl lt = null;
234         Utility.log(cName+".getLocalTransaction(sendEvent)");
235         this.sendEvent=sendEvent;
236         try {
237             lt = (LocalTransactionImpl) getLocalTransaction();
238             Utility.log(cName+".getLocalTransaction(sendEvent) lt="+lt);
239         } catch (Exception e) {
240             Utility.log(cName+".getLocalTransaction(sendEvent) "
241             +"error: "+e.getMessage());
242         }
243         return (lt);
244     }
245
246     public LocalTransaction getLocalTransaction()
247         throws ResourceException
248     {
249         Utility.log(cName+".getLocalTransaction");
250         if (loTx==null) {
251             loTx = new LocalTransactionImpl(this, sendEvent);
252             Utility.log(cName+".getLocalTransaction new loTx="+loTx);
253             return loTx;
254         } else {
255             Utility.log(cName+".getLocalTransaction old loTx="+loTx);
256             loTx.setSendEvent(sendEvent);
257             return loTx;
258         }
259     }
260
261     public ManagedConnectionMetaData getMetaData()
262         throws ResourceException
263     {
264         Utility.log(cName+".getMetaData");
265         return new ManagedConnectionMetaDataImpl();
266     }
267     /** Send the specified event to all the EventListeners that have
268      * registered with this ManagedConnection instance.
269      *
270      * @param int Event type to send
271      * @param Exception if one is associated with this event
272      * @param ch Connection handle associated with this event
273      * @throws ResourceException generic exception if operation fails
274      * @throws IllegalArgumentException if an invalid event is specified
275      **/

276     public void sendEvent(int eType, Exception ex, Object ch) throws ResourceException
277     {
278         Vector lst = (Vector) listeners.clone();
279         ConnectionEvent ce = null;
280         if (ex==null) {
281             ce = new ConnectionEvent(this, eType);
282         }
283         else
284             ce = new ConnectionEvent(this, eType, ex);
285         
286         if (ch != null)
287             ce.setConnectionHandle(ch);
288         else
289             Utility.log(cName+".sendEvent ch==null");
290
291         int len = lst.size();
292         for (int i=0; i<len; i++)
293         {
294           ConnectionEventListener cel = (ConnectionEventListener) lst.elementAt(i);
295           switch (eType)
296           {
297             case ConnectionEvent.CONNECTION_CLOSED:
298               if (closed == false)
299               {
300                  closed = true;
301                  cel.connectionClosed(ce);
302                  Utility.log(cName
303                     +".sendEvent 'CONNECTION_CLOSED' to listener="+cel+" Num="+i);
304               }
305               break;
306             case ConnectionEvent.CONNECTION_ERROR_OCCURRED:
307               if (closed == false)
308               {
309                  closed=true;
310                  cel.connectionErrorOccurred(ce);
311                  Utility.log(cName
312                     +".sendEvent 'CONNECTION_ERROR_OCCURRED' to listener="+cel+" Num="+i);
313               }
314               break;
315             case ConnectionEvent.LOCAL_TRANSACTION_STARTED:
316               cel.localTransactionStarted(ce);
317               Utility.log(cName
318                  +".sendEvent 'LOCAL_TRANSACTION_STARTED' to listener="+cel+" Num="+i);
319               break;
320             case ConnectionEvent.LOCAL_TRANSACTION_COMMITTED:
321               cel.localTransactionCommitted(ce);
322               Utility.log(cName
323                  +".sendEvent 'LOCAL_TRANSACTION_COMMITTED' to listener="+cel+" Num="+i);
324               break;
325             case ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK:
326               cel.localTransactionRolledback(ce);
327               Utility.log(cName
328                  +".sendEvent 'LOCAL_TRANSACTION_ROLLEDBACK' to listener="+cel+" Num="+i);
329               break;
330             default:
331               IllegalArgumentException iae =
332                     new IllegalArgumentException("Illegal eventType: "+eType);
333               Utility.log(cName+".sendEvent error: "+iae.toString());
334               throw iae;
335           }
336         }
337
338     }
339     public void setMcf(ManagedConnectionFactory MCF) { // called by ManagedConnectionFactory
340
Utility.log(cName+".setMcf to "+MCF);
341         this.mcf=MCF;
342     }
343     public ManagedConnectionFactory getMcf() {
344         return this.mcf;
345     }
346     //
347
// The setLogWriter and getLogWriter are used in ManagedConnectionFactory
348
// and ManagedConnection
349
// the Application Server calls ManagedConnection.setLogWriter(pw)
350
//
351
public void setLogWriter(PrintWriter printwriter)
352         throws ResourceException
353     {
354         Utility.log(cName+".setLogWriter mci.pw="+pw+" ApplicationServer's PrintWriter="+printwriter);
355         if (printwriter!=null) pw = printwriter;
356     }
357     
358     public PrintWriter getLogWriter()
359         throws ResourceException
360     {
361         return pw;
362     }
363     public boolean equals(Object other)
364     {
365         boolean match = false;
366         if (other==null) {
367             return match;
368         }
369         if (other instanceof ManagedConnectionImpl) {
370             ManagedConnectionImpl otherMci = (ManagedConnectionImpl)other;
371             try {
372                 match = crii.equals(otherMci.crii);
373             } catch (Exception e) {
374                 Utility.log(cName+".equals("+otherMci+") error: false Exception="+e);
375                 return false;
376             }
377         }
378         return match;
379     }
380
381 }
Popular Tags