001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)FooEngine.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package testengine;
030:
031: import java.io.File;
032:
033: import java.util.ArrayList;
034: import java.util.HashMap;
035: import java.util.Iterator;
036: import java.util.LinkedList;
037: import java.util.Set;
038: import java.util.logging.Logger;
039:
040: import javax.jbi.component.Component;
041: import javax.jbi.component.ComponentContext;
042: import javax.jbi.component.ComponentLifeCycle;
043: import javax.jbi.messaging.DeliveryChannel;
044: import javax.jbi.messaging.ExchangeStatus;
045: import javax.jbi.messaging.Fault;
046: import javax.jbi.messaging.InOnly;
047: import javax.jbi.messaging.InOut;
048: import javax.jbi.messaging.InOptionalOut;
049: import javax.jbi.messaging.MessageExchange;
050: import javax.jbi.messaging.MessageExchangeFactory;
051: import javax.jbi.messaging.MessagingException;
052: import javax.jbi.messaging.NormalizedMessage;
053: import javax.jbi.messaging.RobustInOnly;
054:
055: import javax.jbi.servicedesc.ServiceEndpoint;
056:
057: import javax.xml.namespace.QName;
058:
059: import org.w3c.dom.Document;
060: import org.w3c.dom.DocumentFragment;
061:
062: /**
063: * This class implements EngineLifeCycle. The JBI framework will start this
064: * engine class automatically when JBI framework starts up.
065: *
066: * @author Sun Microsystems, Inc.
067: */
068: public class FooEngine implements ComponentLifeCycle, Component {
069: /**
070: * In Only MEP.
071: */
072: public static final String IN_ONLY = "http://www.w3.org/2004/08/wsdl/in-only";
073:
074: /**
075: * In Out MEP.
076: */
077: public static final String IN_OUT = "http://www.w3.org/2004/08/wsdl/in-out";
078:
079: /**
080: * In Optional Out MEP.
081: */
082: public static final String IN_OPTIONAL_OUT = "http://www.w3.org/2004/08/wsdl/in-opt-out";
083:
084: /**
085: * Robust In Only MEP.
086: */
087: public static final String ROBUST_IN_ONLY = "http://www.w3.org/2004/08/wsdl/robust-in-only";
088:
089: /**
090: *
091: */
092: private static final String SERVICE_NAMESPACE = "http://sun.com/transform-file.wsdl";
093:
094: /**
095: *
096: */
097: private static final String INBOUND_SERVICE_NAME = "transformService";
098:
099: private static final String INBOUND_ENDPOINT_NAME = "filetxEP";
100:
101: /**
102: *
103: */
104: private static final String OUTBOUND_SERVICE_NAME = "outboundService";
105:
106: /**
107: *
108: */
109: public Thread mRecv;
110:
111: /**
112: *
113: */
114: private ServiceEndpoint mEndpoint;
115:
116: /**
117: *
118: */
119:
120: /**
121: *
122: */
123: private DeliveryChannel mChannel;
124:
125: /**
126: *
127: */
128: private MessageExchangeFactory mFactory;
129:
130: /**
131: * Environment conext passed down from framework to this stockquote engine.
132: */
133: private ComponentContext mContext = null;
134:
135: /**
136: * Refernce to logger.
137: */
138: private Logger mLog = null;
139:
140: /**
141: *
142: */
143: private MessageExchange mReq;
144:
145: /**
146: *
147: */
148: private ServiceEndpoint mInboundReference;
149:
150: /**
151: *
152: */
153: private QName mOutbound;
154:
155: /**
156: *
157: */
158: private boolean mRunFlag = true;
159:
160: /**
161: * Get the JMX ObjectName for the DeploymentMBean for this BPE. If there is
162: * none, return null.
163: *
164: * @return ObjectName the JMX object name of the DeploymentMBean or null if
165: * there is no DeploymentMBean.
166: */
167: public javax.management.ObjectName getDeploymentMBeanName() {
168: return null;
169: }
170:
171: /**
172: * Get the JMX ObjectName for any additional MBean for this BC. If there is
173: * none, return null.
174: *
175: * @return ObjectName the JMX object name of the additional MBean or null
176: * if there is no additional MBean.
177: */
178: public javax.management.ObjectName getExtensionMBeanName() {
179: return null;
180: }
181:
182: /**
183: * Initialize the test engine. This performs initialization required
184: *
185: * @param jbiContext the JBI environment mContext
186: *
187: * @throws javax.jbi.JBIException if the stockquote engine is unable to
188: * initialize.
189: */
190: public void init(javax.jbi.component.ComponentContext jbiContext)
191: throws javax.jbi.JBIException {
192: mLog = Logger.getLogger(this .getClass().getPackage().getName());
193: mLog.info("test engine INIT invoked");
194:
195: if (jbiContext == null) {
196: throw (new javax.jbi.JBIException("Context is null",
197: new NullPointerException()));
198: }
199:
200: /* Assign values */
201: mContext = jbiContext;
202: Payload.mDefaultPath = mContext.getInstallRoot()
203: + File.separatorChar + "testengine"
204: + File.separatorChar + "payload.xml";
205: Payload.mDefaultOutPath = mContext.getInstallRoot()
206: + File.separatorChar + "testengine"
207: + File.separatorChar + "response.xml";
208: mLog.info(" INIT METHOD FINISHED ");
209: }
210:
211: /**
212: * Shutdown the SE. This performs cleanup before the SE is terminated.
213: * Once this has been called, init() must be called before the SE can be
214: * started again with a call to start().
215: *
216: * @throws javax.jbi.JBIException if the SE is unable to shut down.
217: */
218: public void shutDown() throws javax.jbi.JBIException {
219: mLog.info("test Engine Shutdown-Start");
220: mLog.info("test Engine Shutdown-End");
221: }
222:
223: /**
224: */
225: public void start() throws javax.jbi.JBIException {
226: mLog.info(" Test ENGINE INVOKED ");
227:
228: try {
229: mChannel = mContext.getDeliveryChannel();
230: mFactory = mChannel.createExchangeFactory();
231:
232: mOutbound = new QName(SERVICE_NAMESPACE,
233: OUTBOUND_SERVICE_NAME);
234:
235: QName inbound = new QName(SERVICE_NAMESPACE,
236: INBOUND_SERVICE_NAME);
237: mInboundReference = mContext.activateEndpoint(inbound,
238: INBOUND_ENDPOINT_NAME);
239: mLog.info("Activated endpoint "
240: + mInboundReference.toString());
241: } catch (MessagingException me) {
242: me.printStackTrace();
243: }
244:
245: if (mChannel == null) {
246: mLog.severe("Cannot get Engine Channel from context ");
247:
248: return;
249: }
250:
251: doTestGoodOutOnly();
252:
253: doTestBadOutOnly();
254:
255: doTestGoodRobustOutOnly();
256:
257: doTestBadRobustOutOnly();
258:
259: mRecv = new Thread(new Receiver());
260: mRecv.start();
261:
262: String compRoot = mContext.getInstallRoot();
263:
264: mLog.info(" START FINISHED ");
265: }
266:
267: /**
268: */
269: public void stop() throws javax.jbi.JBIException {
270: mLog.info("STOP INVOKED ");
271: mRunFlag = false;
272:
273: try {
274: mContext.deactivateEndpoint(mInboundReference);
275: mRecv.interrupt();
276: mRecv.join();
277: } catch (Exception e) {
278: ;
279: }
280:
281: mLog.info(" STOP FINISHED ");
282: }
283:
284: /**
285: *
286: */
287: private void doTestBadOutOnly() {
288: mLog
289: .info("Test engine sending bad out-only message to binding");
290:
291: NormalizedMessage outMsg;
292: InOnly inOnly;
293:
294: try {
295: // create the exchange
296: inOnly = mFactory.createInOnlyExchange();
297: outMsg = inOnly.createMessage();
298:
299: // set the stuff we know
300: inOnly.setService(mOutbound);
301: inOnly.setOperation(new QName(SERVICE_NAMESPACE,
302: "transforminonly"));
303:
304: // lookup the endpoint reference and set on exchange
305: mEndpoint = mContext.getEndpointsForService(mOutbound)[0];
306: inOnly.setEndpoint(mEndpoint);
307:
308: // set the payload
309: Payload.setBadPayload(outMsg);
310:
311: // set the message on the exchange
312: inOnly.setInMessage(outMsg);
313:
314: // send the exchange
315: mChannel.send(inOnly);
316:
317: // receive the response
318: inOnly = (InOnly) mChannel.accept();
319: mLog.info("Received status "
320: + inOnly.getStatus().toString());
321:
322: if (inOnly.getStatus() == ExchangeStatus.ERROR) {
323: mLog.info("Test-BADOUTONLY-Passed");
324: } else {
325: mLog.info("Test-BADOUTONLY-Failed");
326: }
327: } catch (Exception e) {
328: mLog.severe("Test-BADOUTONLY-threw-Exception");
329: e.printStackTrace();
330: }
331: }
332:
333: /**
334: *
335: */
336: private void doTestBadRobustOutOnly() {
337: NormalizedMessage outMsg;
338: Exception error;
339: RobustInOnly robInOnly;
340:
341: try {
342: // create the exchange
343: robInOnly = mFactory.createRobustInOnlyExchange();
344: outMsg = robInOnly.createMessage();
345:
346: // set the stuff we know
347: robInOnly.setService(mOutbound);
348: robInOnly.setOperation(new QName(SERVICE_NAMESPACE,
349: "transformrobustinonly"));
350:
351: // lookup the endpoint reference and set on exchange
352: mEndpoint = mContext.getEndpointsForService(mOutbound)[0];
353: robInOnly.setEndpoint(mEndpoint);
354:
355: // set the payload
356: Payload.setBadPayload(outMsg);
357: robInOnly.setInMessage(outMsg);
358:
359: // Now we can proceed normally
360: mChannel.send(robInOnly);
361:
362: // receive the response
363: robInOnly = (RobustInOnly) mChannel.accept();
364:
365: // check for a fault or error
366: error = robInOnly.getError();
367:
368: if (error != null) {
369: mLog.info("Test-BADROBUSTOUTONLY-Passed");
370: }
371: } catch (Exception e) {
372: mLog.severe("Test-BADROBUSTOUTONLY-threw-Exception");
373: e.printStackTrace();
374: }
375: }
376:
377: /**
378: *
379: */
380: private void doTestGoodInOnly() {
381: /* start accpeting the in-only, in-out and robust in-only
382: */
383: try {
384: InOnly inonly = (InOnly) mReq;
385:
386: if (inonly.getInMessage() == null) {
387: mLog.info("Test-GOODINONLY-Failed");
388:
389: return;
390: }
391:
392: if (inonly.getInMessage().getContent() == null) {
393: mLog.info("Test-GOODINONLY-Failed");
394:
395: return;
396: }
397:
398: inonly.setStatus(ExchangeStatus.DONE);
399: mChannel.send(inonly);
400: mLog.info("Test-GOODINONLY-Passed");
401: } catch (Exception e) {
402: mLog.severe("Test-GOODINONLY-threw-Exception"
403: + e.getMessage());
404: e.printStackTrace();
405: }
406: }
407:
408: /**
409: *
410: */
411: private void doTestGoodInOut() {
412: /* start accpeting the in-only, in-out and robust in-only
413: */
414: try {
415: InOut inout = (InOut) mReq;
416:
417: if (inout.getStatus() == ExchangeStatus.DONE) {
418: mLog.info("Test-GOODINOUT-Passed");
419:
420: return;
421: }
422:
423: if (inout.getStatus() == ExchangeStatus.ERROR) {
424: mLog.info("Test-GOODINOUT-Failed");
425:
426: return;
427: }
428:
429: if (inout.getInMessage() == null) {
430: mLog.info("Test-GOODINOUT-Failed");
431:
432: return;
433: }
434: NormalizedMessage outmsg = inout.createMessage();
435: Payload.setResponse(outmsg);
436: inout.setOutMessage(outmsg);
437: mChannel.send(inout);
438: } catch (Exception e) {
439: mLog.severe("Test-GOODINOUT-threw-Exception"
440: + e.getMessage());
441: e.printStackTrace();
442: }
443: }
444:
445: /**
446: *
447: */
448: private void doTestGoodOutOnly() {
449: mLog
450: .info("Test engine sending good out-only message to binding");
451:
452: NormalizedMessage outMsg;
453: InOnly inOnly;
454:
455: try {
456: // create the exchange
457: inOnly = mFactory.createInOnlyExchange();
458: outMsg = inOnly.createMessage();
459:
460: // set the stuff we know
461: inOnly.setService(mOutbound);
462: inOnly.setOperation(new QName(SERVICE_NAMESPACE,
463: "transforminonly"));
464:
465: ServiceEndpoint mEndpoint;
466:
467: // lookup the endpoint reference and set on exchange
468: mEndpoint = mContext.getEndpointsForService(mOutbound)[0];
469: inOnly.setEndpoint(mEndpoint);
470:
471: // set the payload
472: Payload.setPayload(outMsg);
473:
474: // set the message on the exchange
475: inOnly.setInMessage(outMsg);
476:
477: // send the exchange
478: mChannel.send(inOnly);
479:
480: // receive the response
481: inOnly = (InOnly) mChannel.accept();
482:
483: if (inOnly.getStatus() == ExchangeStatus.DONE) {
484: mLog.info("Test-GOODOUTONLY-Passed");
485: } else {
486: mLog.info("Test-GOODOUTONLY-Failed");
487: }
488: } catch (Exception e) {
489: mLog.severe("Test-GOODOUTONLY-threw-Exception");
490: e.printStackTrace();
491: }
492: }
493:
494: /**
495: *
496: */
497: private void doTestGoodRobustInOnly() {
498: /* start accpeting the in-only, in-out and robust in-only
499: */
500: try {
501: RobustInOnly robinonly = (RobustInOnly) mReq;
502:
503: if (robinonly.getInMessage() == null) {
504: mLog.info("Test-GOODROBUSTINONLY-Failed");
505: robinonly.setError(new Exception("Message is null"));
506: mChannel.send(robinonly);
507:
508: return;
509: }
510:
511: if (robinonly.getInMessage().getContent() == null) {
512: mLog.info("Test-GOODROBUSTINONLY-Failed");
513:
514: return;
515: }
516:
517: robinonly.setStatus(ExchangeStatus.DONE);
518: mChannel.send(robinonly);
519: mLog.info("Test-GOODROBUSTINONLY-Passed");
520: } catch (Exception e) {
521: mLog.severe("Test-GOODROBUSTINONLY-threw-Exception"
522: + e.getMessage());
523: e.printStackTrace();
524: }
525: }
526:
527: /**
528: *
529: */
530: private void doTestGoodRobustOutOnly() {
531: NormalizedMessage outMsg;
532: Fault fault;
533: RobustInOnly robInOnly;
534:
535: try {
536: // create the exchange
537: robInOnly = mFactory.createRobustInOnlyExchange();
538: outMsg = robInOnly.createMessage();
539:
540: // set the stuff we know
541: robInOnly.setService(mOutbound);
542: robInOnly.setOperation(new QName(SERVICE_NAMESPACE,
543: "transformrobustinonly"));
544:
545: // lookup the endpoint reference and set on exchange
546: mEndpoint = mContext.getEndpointsForService(mOutbound)[0];
547: robInOnly.setEndpoint(mEndpoint);
548:
549: // set the payload
550: Payload.setPayload(outMsg);
551: robInOnly.setInMessage(outMsg);
552:
553: // Now we can proceed normally
554: mChannel.send(robInOnly);
555:
556: // receive the response
557: robInOnly = (RobustInOnly) mChannel.accept();
558:
559: // check for a fault
560: fault = robInOnly.getFault();
561:
562: if ((fault == null)
563: && (robInOnly.getStatus() == ExchangeStatus.DONE)) {
564: mLog.info("Test-GOODROBUSTOUTONLY-Passed");
565: }
566: } catch (Exception e) {
567: mLog.severe("Test-GOODROBUSTOUTONLY-threw-Exception");
568: e.printStackTrace();
569: }
570: }
571:
572: /**
573: *
574: *
575: * @author Sun Microsystems, Inc.
576: *
577: */
578: public class Receiver implements Runnable {
579: /**
580: *
581: */
582: public void run() {
583: mLog.info("Test engine receiver started");
584:
585: try {
586: while (mRunFlag) {
587: mReq = mChannel.accept();
588: mLog.info("Got message i test ebgine");
589:
590: /* start test for resolver
591: Descriptor d =
592: mChannel.getEndpointDescriptor(mReq.getEndpoint());
593:
594: mLog.info("Got a descriptor with "
595: + d.getDescription().getDocumentElement().getLocalName());
596: */
597:
598: if (mReq instanceof InOut) {
599: doTestGoodInOut();
600: } else if (mReq instanceof InOnly) {
601: doTestGoodInOnly();
602: } else if (mReq instanceof RobustInOnly) {
603: doTestGoodRobustInOnly();
604: }
605: }
606: } catch (Exception e) {
607: e.printStackTrace(System.out);
608: mLog.severe("Exception at receiver");
609: mLog.info("Test engine receiver ended");
610: }
611: }
612: }
613:
614: public javax.jbi.component.ComponentLifeCycle getLifeCycle() {
615: return this ;
616: }
617:
618: public javax.jbi.component.ServiceUnitManager getServiceUnitManager() {
619: return null;
620: }
621:
622: public Document getServiceDescription(
623: javax.jbi.servicedesc.ServiceEndpoint ServiceEndpoint) {
624: return null;
625: }
626:
627: public boolean isExchangeWithConsumerOkay(
628: javax.jbi.servicedesc.ServiceEndpoint endpoint,
629: javax.jbi.messaging.MessageExchange exchange) {
630: return true;
631: }
632:
633: public boolean isExchangeWithProviderOkay(
634: javax.jbi.servicedesc.ServiceEndpoint endpoint,
635: javax.jbi.messaging.MessageExchange exchange) {
636: return true;
637: }
638:
639: public ServiceEndpoint resolveEndpointReference(DocumentFragment epr) {
640: return null;
641: }
642: }
|