KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ersatz > resourceadapter > ResourceAdapterImpl


1 /*
2  * Created on January 10, 2004
3  *
4  * ResourceAdapterImpl.java is used to test JCA 1.5
5  * as implemented by JOnAS. This class implements the ResourceAdapter Interface
6  *
7  */

8 package ersatz.resourceadapter;
9
10 import javax.transaction.xa.XAResource;
11 import javax.transaction.xa.Xid;
12 import javax.transaction.xa.XAException;
13 import javax.resource.NotSupportedException;
14 import javax.resource.spi.endpoint.MessageEndpointFactory;
15 import javax.resource.ResourceException;
16 import javax.resource.spi.work.WorkManager;
17 import javax.resource.spi.work.Work;
18 import javax.resource.spi.work.WorkException;
19 import javax.resource.spi.ActivationSpec;
20 import javax.resource.spi.BootstrapContext;
21 import javax.resource.spi.ResourceAdapterInternalException;
22 import java.util.HashMap;
23
24 /**
25  * @author Bob Kruse
26  *
27  * JCA1.5 Resource Adapter
28  *
29  * used to test the J2EE Connector as implemented by JOnAS.
30  *
31  */

32 public class ResourceAdapterImpl
33         implements javax.resource.spi.ResourceAdapter,
34                    java.io.Serializable
35 {
36     public String EIS_URL = ""; // should be the RA name when set, ie, ErsatzEIS
37
private boolean isReused = false;
38     public BootstrapContext bootstrapCtx = null; // set in start()
39
private WorkManager wm;
40     private WorkAdapterImpl wa;
41     private int countWork = 0;
42     private transient HashMap endpointFactories = new HashMap();
43     String cName = "ResourceAdapterImpl";
44     String reUseError="Application Server attempted to reuse RA";
45     private MessageEndpointFactory factory;
46     private ActivationSpec spec;
47     private Work deliverWork;
48
49     public ResourceAdapterImpl() { // constructor
50
}
51
52     public String getEIS_URL() {
53         Utility.log(cName+".getEIS_URL "+EIS_URL);
54         return EIS_URL;
55     }
56     public void setEIS_URL(String EIS_URL) { // ra.xml deployment supplies value
57
Utility.log(cName+".setEIS_URL "+EIS_URL);
58         this.EIS_URL = EIS_URL;
59     }
60     public int isWorkStarted()
61     {
62         return countWork;
63     }
64     public void start(BootstrapContext bootstrapCtx)
65         throws ResourceAdapterInternalException
66     {
67         this.bootstrapCtx=bootstrapCtx;
68         if (isReused) {
69             Utility.log(cName+".start Error: "+reUseError);
70             throw new ResourceAdapterInternalException(reUseError);
71         }
72         Utility.log(cName+".start "
73                   +"Functional resource adapter instance created");
74         if (EIS_URL.equals("ErsatzEIS")) {
75             try {
76                 engageWorkManager();
77             } catch (Exception e) {
78                 // TODO is more logic needed here?
79
throw new ResourceAdapterInternalException(e.toString());
80             } catch (Throwable t) {
81                 throw new ResourceAdapterInternalException(t.toString());
82             }
83         }
84     }
85     private void engageWorkManager() throws Exception
86     {
87         Utility.log(cName+".engageWorkManager [enter]");
88         // get WorkManager reference
89
wm = bootstrapCtx.getWorkManager();
90         // TODO setup network endpoints
91
//
92
// provide 3 Work objects to WorkManager
93
//
94
Work work=null;
95         for (int i=1; i<4; i++) {
96             try {
97                 work = new WorkImpl("work"+i);
98             } catch (Exception e) {
99                 Utility.log(cName
100                 +".engageWorkManager error: new WorkImpl(work"+i+") failed. exception="+e.toString());
101                 throw e;
102             }
103             // create instance of WorkAdapter as the WorkListener
104
if (i==1) createWorkAdapter();
105
106             if (i==1) {
107                 // test #1 "startWork()"
108
try {
109                     wm.startWork(work);
110                     ++countWork;
111                     Utility.log(cName+".engageWorkManager startWork(work"+i+")");
112                 } catch (Exception e) {
113                     Utility.log(cName
114                     +".engageWorkManager error: startWork(work"+i+") returned exception="+e.toString());
115                     throw e;
116                 }
117             }
118             if (i==2) {
119                 // test #2 "scheduleWork()"
120
try {
121                     wm.scheduleWork(work);
122                     ++countWork;
123                     Utility.log(cName+".engageWorkManager scheduleWork(work"+i+")");
124                 } catch (Exception e) {
125                     Utility.log(cName
126                     +".engageWorkManager error: scheduleWork(work"+i+") returned exception="+e.toString());
127                     throw e;
128                 }
129             }
130             if (i==3) {
131                 // test #3 "doWork()"
132
try {
133                     wm.doWork(work);
134                     ++countWork;
135                     Utility.log(cName+".engageWorkManager doWork(work"+i+")");
136                 } catch (Exception e) {
137                     Utility.log(cName
138                     +".engageWorkManager error: doWork(work"+i+") returned exception="+e.toString());
139                     throw e;
140                 }
141             }
142         }
143     
144     }
145     private void createWorkAdapter()
146     {
147         wa=new WorkAdapterImpl();
148     }
149     public void stop()
150     {
151         Utility.log(cName+".stop "+EIS_URL);
152         isReused=true;
153         release();
154     }
155
156     public void release()
157     {
158         Utility.log(cName+".release");
159     }
160
161     /** Called by the application server when a message-driven bean
162      * (MessageEndpoint) is deployed.
163      *
164      */

165     public void endpointActivation(MessageEndpointFactory factory, ActivationSpec spec)
166         throws NotSupportedException, ResourceAdapterInternalException
167     {
168         Utility.log(cName+".endpointActivation. MessageEndpointFactory="+factory);
169         Utility.log(cName+".endpointActivation. ActivationSpec="+spec);
170         Utility.log(cName+".endpointActivation. RA name="+EIS_URL);
171         this.factory=factory;
172         this.spec=spec;
173         
174         if (isReused) {
175             Utility.log(cName+".endpointActivation Error: "+reUseError);
176             throw new ResourceAdapterInternalException(reUseError);
177         }
178         if ( ! (spec instanceof ActivationSpecImpl))
179         {
180             Utility.log(cName+".endpointActivation Invalid spec error");
181             throw new NotSupportedException("invalid spec");
182         }
183         try {
184             endpointFactories.put(spec, factory);
185         } catch (Exception e) {
186             Utility.log(cName+".endpointActivation put(spec, factory) Error: "+e);
187             throw new ResourceAdapterInternalException(e.toString());
188         }
189
190     }
191
192     public void endpointDeactivation(MessageEndpointFactory factory, ActivationSpec spec)
193     {
194         Utility.log(cName+".endpointDeactivation for RA name="+EIS_URL);
195         endpointFactories.remove(spec);
196     }
197
198     public XAResource[] getXAResources(ActivationSpec[] specs)
199         throws ResourceException
200     {
201         XAResource[] X = null;
202         Utility.log(cName+".getXAResources only returns null");
203         /*
204          * Do nothing
205          */

206         
207         return X;
208     }
209     //
210
// Deliver Message to Work Object called by InboundCASLR.java
211
//
212
public boolean deliverMsg(String msg) throws Exception
213     {
214         Utility.log(cName+".deliverMsg factory="+factory+" ActivationSpecImpl="+spec);
215         if (factory==null) {
216             String s="End point factory==null";
217             throw new Exception(cName+".deliverMsg"+s);
218         }
219         try {
220             deliverWork = new WorkImpl(factory, msg);
221             Utility.log(cName+".deliverMsg created WorkImpl object");
222         } catch (Exception e) {
223             Utility.log(cName
224             +".deliverMsg error: new WorkImpl() failed. exception="+e.toString());
225             throw e;
226         }
227         try {
228             if (wa==null) createWorkAdapter();
229             wm.startWork(deliverWork,wm.INDEFINITE,null,wa);
230             Utility.log(cName+".deliverMsg executed startWork(). Examin trace for WorkAdapterImpl results.");
231         } catch (WorkException we) {
232             Utility.log(cName
233             +".deliverMsg error: wm.startWork failed. WorkException="+we.toString());
234             throw we;
235         } catch (Exception ee) {
236             Utility.log(cName
237             +".deliverMsg error: wm.startWork failed. exception="+ee.toString());
238             throw ee;
239         }
240         return true;
241     }
242 }
Popular Tags