001: /*
002: * Created on January 10, 2004
003: *
004: * ResourceAdapterImpl.java is used to test JCA 1.5
005: * as implemented by JOnAS. This class implements the ResourceAdapter Interface
006: *
007: */
008: package ersatz.resourceadapter;
009:
010: import javax.transaction.xa.XAResource;
011: import javax.transaction.xa.Xid;
012: import javax.transaction.xa.XAException;
013: import javax.resource.NotSupportedException;
014: import javax.resource.spi.endpoint.MessageEndpointFactory;
015: import javax.resource.ResourceException;
016: import javax.resource.spi.work.WorkManager;
017: import javax.resource.spi.work.Work;
018: import javax.resource.spi.work.WorkException;
019: import javax.resource.spi.ActivationSpec;
020: import javax.resource.spi.BootstrapContext;
021: import javax.resource.spi.ResourceAdapterInternalException;
022: import java.util.HashMap;
023:
024: /**
025: * @author Bob Kruse
026: *
027: * JCA1.5 Resource Adapter
028: *
029: * used to test the J2EE Connector as implemented by JOnAS.
030: *
031: */
032: public class ResourceAdapterImpl implements
033: javax.resource.spi.ResourceAdapter, java.io.Serializable {
034: public String EIS_URL = ""; // should be the RA name when set, ie, ErsatzEIS
035: private boolean isReused = false;
036: public BootstrapContext bootstrapCtx = null; // set in start()
037: private WorkManager wm;
038: private WorkAdapterImpl wa;
039: private int countWork = 0;
040: private transient HashMap endpointFactories = new HashMap();
041: String cName = "ResourceAdapterImpl";
042: String reUseError = "Application Server attempted to reuse RA";
043: private MessageEndpointFactory factory;
044: private ActivationSpec spec;
045: private Work deliverWork;
046:
047: public ResourceAdapterImpl() { // constructor
048: }
049:
050: public String getEIS_URL() {
051: Utility.log(cName + ".getEIS_URL " + EIS_URL);
052: return EIS_URL;
053: }
054:
055: public void setEIS_URL(String EIS_URL) { // ra.xml deployment supplies value
056: Utility.log(cName + ".setEIS_URL " + EIS_URL);
057: this .EIS_URL = EIS_URL;
058: }
059:
060: public int isWorkStarted() {
061: return countWork;
062: }
063:
064: public void start(BootstrapContext bootstrapCtx)
065: throws ResourceAdapterInternalException {
066: this .bootstrapCtx = bootstrapCtx;
067: if (isReused) {
068: Utility.log(cName + ".start Error: " + reUseError);
069: throw new ResourceAdapterInternalException(reUseError);
070: }
071: Utility.log(cName + ".start "
072: + "Functional resource adapter instance created");
073: if (EIS_URL.equals("ErsatzEIS")) {
074: try {
075: engageWorkManager();
076: } catch (Exception e) {
077: // TODO is more logic needed here?
078: throw new ResourceAdapterInternalException(e.toString());
079: } catch (Throwable t) {
080: throw new ResourceAdapterInternalException(t.toString());
081: }
082: }
083: }
084:
085: private void engageWorkManager() throws Exception {
086: Utility.log(cName + ".engageWorkManager [enter]");
087: // get WorkManager reference
088: wm = bootstrapCtx.getWorkManager();
089: // TODO setup network endpoints
090: //
091: // provide 3 Work objects to WorkManager
092: //
093: Work work = null;
094: for (int i = 1; i < 4; i++) {
095: try {
096: work = new WorkImpl("work" + i);
097: } catch (Exception e) {
098: Utility.log(cName
099: + ".engageWorkManager error: new WorkImpl(work"
100: + i + ") failed. exception=" + e.toString());
101: throw e;
102: }
103: // create instance of WorkAdapter as the WorkListener
104: if (i == 1)
105: createWorkAdapter();
106:
107: if (i == 1) {
108: // test #1 "startWork()"
109: try {
110: wm.startWork(work);
111: ++countWork;
112: Utility.log(cName
113: + ".engageWorkManager startWork(work" + i
114: + ")");
115: } catch (Exception e) {
116: Utility
117: .log(cName
118: + ".engageWorkManager error: startWork(work"
119: + i + ") returned exception="
120: + e.toString());
121: throw e;
122: }
123: }
124: if (i == 2) {
125: // test #2 "scheduleWork()"
126: try {
127: wm.scheduleWork(work);
128: ++countWork;
129: Utility.log(cName
130: + ".engageWorkManager scheduleWork(work"
131: + i + ")");
132: } catch (Exception e) {
133: Utility
134: .log(cName
135: + ".engageWorkManager error: scheduleWork(work"
136: + i + ") returned exception="
137: + e.toString());
138: throw e;
139: }
140: }
141: if (i == 3) {
142: // test #3 "doWork()"
143: try {
144: wm.doWork(work);
145: ++countWork;
146: Utility.log(cName
147: + ".engageWorkManager doWork(work" + i
148: + ")");
149: } catch (Exception e) {
150: Utility.log(cName
151: + ".engageWorkManager error: doWork(work"
152: + i + ") returned exception="
153: + e.toString());
154: throw e;
155: }
156: }
157: }
158:
159: }
160:
161: private void createWorkAdapter() {
162: wa = new WorkAdapterImpl();
163: }
164:
165: public void stop() {
166: Utility.log(cName + ".stop " + EIS_URL);
167: isReused = true;
168: release();
169: }
170:
171: public void release() {
172: Utility.log(cName + ".release");
173: }
174:
175: /** Called by the application server when a message-driven bean
176: * (MessageEndpoint) is deployed.
177: *
178: */
179: public void endpointActivation(MessageEndpointFactory factory,
180: ActivationSpec spec) throws NotSupportedException,
181: ResourceAdapterInternalException {
182: Utility.log(cName
183: + ".endpointActivation. MessageEndpointFactory="
184: + factory);
185: Utility.log(cName + ".endpointActivation. ActivationSpec="
186: + spec);
187: Utility.log(cName + ".endpointActivation. RA name=" + EIS_URL);
188: this .factory = factory;
189: this .spec = spec;
190:
191: if (isReused) {
192: Utility.log(cName + ".endpointActivation Error: "
193: + reUseError);
194: throw new ResourceAdapterInternalException(reUseError);
195: }
196: if (!(spec instanceof ActivationSpecImpl)) {
197: Utility.log(cName
198: + ".endpointActivation Invalid spec error");
199: throw new NotSupportedException("invalid spec");
200: }
201: try {
202: endpointFactories.put(spec, factory);
203: } catch (Exception e) {
204: Utility.log(cName
205: + ".endpointActivation put(spec, factory) Error: "
206: + e);
207: throw new ResourceAdapterInternalException(e.toString());
208: }
209:
210: }
211:
212: public void endpointDeactivation(MessageEndpointFactory factory,
213: ActivationSpec spec) {
214: Utility.log(cName + ".endpointDeactivation for RA name="
215: + EIS_URL);
216: endpointFactories.remove(spec);
217: }
218:
219: public XAResource[] getXAResources(ActivationSpec[] specs)
220: throws ResourceException {
221: XAResource[] X = null;
222: Utility.log(cName + ".getXAResources only returns null");
223: /*
224: * Do nothing
225: */
226:
227: return X;
228: }
229:
230: //
231: // Deliver Message to Work Object called by InboundCASLR.java
232: //
233: public boolean deliverMsg(String msg) throws Exception {
234: Utility.log(cName + ".deliverMsg factory=" + factory
235: + " ActivationSpecImpl=" + spec);
236: if (factory == null) {
237: String s = "End point factory==null";
238: throw new Exception(cName + ".deliverMsg" + s);
239: }
240: try {
241: deliverWork = new WorkImpl(factory, msg);
242: Utility.log(cName + ".deliverMsg created WorkImpl object");
243: } catch (Exception e) {
244: Utility
245: .log(cName
246: + ".deliverMsg error: new WorkImpl() failed. exception="
247: + e.toString());
248: throw e;
249: }
250: try {
251: if (wa == null)
252: createWorkAdapter();
253: wm.startWork(deliverWork, wm.INDEFINITE, null, wa);
254: Utility
255: .log(cName
256: + ".deliverMsg executed startWork(). Examin trace for WorkAdapterImpl results.");
257: } catch (WorkException we) {
258: Utility
259: .log(cName
260: + ".deliverMsg error: wm.startWork failed. WorkException="
261: + we.toString());
262: throw we;
263: } catch (Exception ee) {
264: Utility
265: .log(cName
266: + ".deliverMsg error: wm.startWork failed. exception="
267: + ee.toString());
268: throw ee;
269: }
270: return true;
271: }
272: }
|