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: * @(#)PBTestBinding.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package TestBinding;
030:
031: import javax.activation.DataHandler;
032: import javax.activation.FileDataSource;
033:
034: import java.io.IOException;
035: import java.io.PrintWriter;
036: import java.io.OutputStreamWriter;
037: import java.io.StringReader;
038: import java.io.StringWriter;
039:
040: import java.util.logging.Logger;
041:
042: import javax.jbi.component.Component;
043: import javax.jbi.component.ComponentContext;
044: import javax.jbi.component.ComponentLifeCycle;
045: import javax.jbi.component.ServiceUnitManager;
046:
047: import javax.jbi.messaging.DeliveryChannel;
048: import javax.jbi.messaging.ExchangeStatus;
049: import javax.jbi.messaging.Fault;
050: import javax.jbi.messaging.MessageExchangeFactory;
051: import javax.jbi.messaging.InOptionalOut;
052: import javax.jbi.messaging.InOut;
053: import javax.jbi.messaging.InOnly;
054: import javax.jbi.messaging.MessageExchange;
055: import javax.jbi.messaging.NormalizedMessage;
056:
057: import javax.jbi.servicedesc.ServiceEndpoint;
058:
059: import javax.naming.InitialContext;
060:
061: import javax.xml.namespace.QName;
062: import javax.xml.parsers.DocumentBuilder;
063: import javax.xml.parsers.DocumentBuilderFactory;
064: import javax.xml.transform.TransformerFactory;
065: import javax.xml.transform.stream.StreamResult;
066: import javax.xml.transform.Source;
067: import javax.xml.transform.Transformer;
068: import javax.xml.transform.TransformerFactory;
069:
070: import javax.xml.transform.stream.StreamSource;
071:
072: import javax.xml.transform.dom.DOMSource;
073: import javax.xml.transform.dom.DOMResult;
074:
075: import org.w3c.dom.Document;
076: import org.w3c.dom.DocumentFragment;
077: import org.w3c.dom.Element;
078: import org.w3c.dom.NodeList;
079:
080: /**
081: * This is a servlet that acts as a Binding Component and tests the dynamic
082: * registration support provided by JBI. It registers itself as a binding and
083: * then performs a fixed set of message exchanges with another servlet that
084: * acts as a Service Engine. That servlet is EngineServlet.
085: *
086: * @author Sun Microsystems, Inc.
087: */
088: public class PBTestBinding implements Component, ComponentLifeCycle {
089:
090: /**
091: * Logger for the servlet.
092: */
093: private java.util.logging.Logger mLog;
094:
095: /**
096: * Handle to the XML Document Builder.
097: */
098: private DocumentBuilder mDocumentBuilder;
099:
100: /**
101: * Binding channel for NMS communications.
102: */
103: private DeliveryChannel mChannel;
104:
105: /**
106: * Component context from JBI.
107: */
108: private ComponentContext mContext;
109:
110: /**
111: * MessageExchangeFactory.
112: */
113: private MessageExchangeFactory mFactory;
114:
115: /**
116: * The action for an initialize request.
117: */
118: private static final String ACTION_INIT = "init";
119:
120: /**
121: * The action for an acceptping request.
122: */
123:
124: private static final String ACTION_SENDINOUT = "sendinout";
125: private static final String ACTION_SENDINOPTIONALOUT = "sendinoptionalout";
126: private static final String ACTION_SENDINONLY = "sendinonly";
127: private static final String ACTION_SENDSYNCHINONLY = "sendsynchinonly";
128: private static final String ACTION_SERVICEDESCRIPTION = "servicedescription";
129: private static final String ACTION_RESOLVEEPR = "resolveendpoint";
130: private static final String ACTION_RESOLVESERVICE = "resolveservice";
131:
132: /**
133: * The engine's service name.
134: */
135: private static final QName ENGINE_SERVICE = new QName(
136: "pb-test-engine_service");
137:
138: /**
139: * Qualified name for the binding's service.
140: */
141: private static final QName BINDING_SERVICE = new QName(
142: "pb-test-binding_service");
143:
144: /**
145: * The binding's endpoint name.
146: */
147: private static final String BINDING_ENDPOINT = "pb-test-binding_endpoint";
148:
149: /**
150: * JBI component name
151: */
152: private static final String JBI_COMPONENT_NAME = "PBTestBinding";
153:
154: /**
155: * JBI component description
156: */
157: private static final String JBI_COMPONENT_DESC = "ProxyBinding TestBinding";
158:
159: /**
160: * Runner Thread.
161: */
162: private Thread mRunnerThread;
163: private boolean mRunning;
164: private StringBuffer mSB;
165:
166: public PBTestBinding() {
167: mLog = Logger.getLogger(this .getClass().getPackage().getName());
168: }
169:
170: //---------------------------- Component Methods ------------------------------
171:
172: /**
173: * Get the ComponentLifeCycle instance.
174: * @return The ComponentLifeCycle instance.
175: */
176: public ComponentLifeCycle getLifeCycle() {
177: return this ;
178: }
179:
180: /**
181: * Get the ServiceUnitManager instance.
182: * @return The ServiceUnitManager instance, which is always null for
183: * this test.
184: */
185: public ServiceUnitManager getServiceUnitManager() {
186: mLog
187: .info("PBTestBinding: Component getServiceUnitManager() called");
188: return null;
189: }
190:
191: /**
192: * This method is called by JBI to check if this component, in the role of
193: * provider of the service indicated by the given exchange, can actually
194: * perform the operation desired.
195: *
196: * @param endpoint
197: * @param exchange
198: *
199: * @return true if its OK.
200: */
201: public boolean isExchangeWithConsumerOkay(
202: javax.jbi.servicedesc.ServiceEndpoint endpoint,
203: javax.jbi.messaging.MessageExchange exchange) {
204: /* Should not always return true
205: * The capabilities of this component has to be checked first.
206: * Not implemented right now
207: */
208: return true;
209: }
210:
211: /**
212: * This method is called by JBI to check if this component, in the role of
213: * consumer of the service indicated by the given exchange, can actually
214: * interact with the the provider completely.
215: *
216: * @param endpoint
217: * @param exchange
218: *
219: * @return true if OK.
220: */
221: public boolean isExchangeWithProviderOkay(
222: javax.jbi.servicedesc.ServiceEndpoint endpoint,
223: javax.jbi.messaging.MessageExchange exchange) {
224: /* Should not always return true
225: * The capabilities of this component has to be checked first.
226: * Not implemented right now
227: */
228: return true;
229: }
230:
231: /**
232: * Returns a resolver for meta-data.
233: *
234: * @param ServiceEndpoint endpoint reference object.
235: *
236: * @return Descriptor for file binding.
237: */
238: public org.w3c.dom.Document getServiceDescription(
239: javax.jbi.servicedesc.ServiceEndpoint ServiceEndpoint) {
240: org.w3c.dom.Document desc = null;
241:
242: try {
243: } catch (Exception e) {
244: e.printStackTrace();
245: }
246:
247: return desc;
248: }
249:
250: /**
251: * Resolve the endpoint reference using the given capabilities of the
252: * consumer. This is called by JBI when its trying to resove the given EPR
253: * on behalf of the component.
254: *
255: * @param epr endpoint reference.
256: *
257: * @return Service enpoint.
258: */
259: public ServiceEndpoint resolveEndpointReference(DocumentFragment epr) {
260: return null;
261: }
262:
263: //------------------------ ComponentLifeCycle Methods -------------------------
264:
265: /**
266: * Initializes the JBI LifeCycle. Because this servlet specified the
267: * "self-started" option when it registered, this method does nothing.
268: * @param context - the component context provided by the JBI framework.
269: * @throws javax.jbi.JBIException if any error occurs.
270: */
271: public void init(ComponentContext context)
272: throws javax.jbi.JBIException {
273: mLog.info("PBTestBinding: LifeCycle init() called");
274: mContext = context;
275: DocumentBuilderFactory dbf = DocumentBuilderFactory
276: .newInstance();
277: dbf.setNamespaceAware(true);
278: try {
279: mDocumentBuilder = dbf.newDocumentBuilder();
280: } catch (javax.xml.parsers.ParserConfigurationException pcEx) {
281: throw new javax.jbi.JBIException(
282: "Unable to create new DocumentBuilder instance: "
283: + pcEx, pcEx);
284: }
285: try {
286: mChannel = mContext.getDeliveryChannel();
287: mFactory = mChannel.createExchangeFactory();
288: } catch (javax.jbi.messaging.MessagingException mEx) {
289: throw new javax.jbi.JBIException(
290: "Unable to obtain DeliveryChannel: " + mEx, mEx);
291: }
292: }
293:
294: /**
295: * Get the Extension MBean name.
296: * @return The JMX object name of the Extension MBean for this BC,
297: * which is always null in this test.
298: */
299: public javax.management.ObjectName getExtensionMBeanName() {
300: return null;
301: }
302:
303: /**
304: * Starts the JBI LifeCycle. Because this servlet specified the
305: * "self-started" option when it registered, this method does nothing.
306: * @throws javax.jbi.JBIException if any error occurs.
307: */
308: public void start() throws javax.jbi.JBIException {
309: mLog.info("PBTestBinding: LifeCycle start() entered");
310: mRunning = true;
311: mRunnerThread = new Thread(new BindingRunner());
312: mRunnerThread.setName("PBTestBinding-Runner");
313: mRunnerThread.setDaemon(true);
314: mRunnerThread.start();
315: mLog.info("PBTestBinding: LifeCycle start() exited");
316: }
317:
318: /**
319: * Stops the JBI LifeCycle. Because this servlet specified the
320: * "self-started" option when it registered, this method does nothing.
321: * @throws javax.jbi.JBIException if any error occurs.
322: */
323: public void stop() throws javax.jbi.JBIException {
324: mLog.info("PBTestBinding: LifeCycle stop() called");
325: mRunning = false;
326: mRunnerThread.interrupt();
327: mLog.info("PBTestBinding: Results =====================\n"
328: + mSB.toString());
329: mLog.info("PBTestBinding: END Results =================\n");
330: }
331:
332: /**
333: * Shuts down the JBI LifeCycle. Because this servlet specified the
334: * "self-started" option when it registered, this method does nothing.
335: * @throws javax.jbi.JBIException if any error occurs.
336: */
337: public void shutDown() throws javax.jbi.JBIException {
338: mLog.info("PBTestBinding: LifeCycle shutDown() called");
339: }
340:
341: class BindingRunner implements java.lang.Runnable {
342: public void run() {
343: mLog.info("PBTestBinding: Start processing requests");
344: StringBuffer sb = mSB = new StringBuffer();
345:
346: try {
347: if (mRunning)
348: sb.append(sendInOut() + "\n");
349: if (mRunning)
350: sb.append(sendInOptionalOut() + "\n");
351: if (mRunning)
352: sb.append(sendInOnly() + "\n");
353: if (mRunning)
354: sb.append(sendSynchInOnly() + "\n");
355: if (mRunning)
356: sb.append(serviceDescription() + "\n");
357: if (mRunning)
358: sb.append(resolveEPR() + "\n");
359: if (mRunning)
360: sb.append(resolveService() + "\n");
361: } catch (Exception ex) {
362: mLog.warning("PBTestBinding:Result: Exception: " + ex);
363: }
364:
365: mLog.info("PBTestBinding: Processing stopping");
366: }
367: }
368:
369: //----------------------------- Private Methods ------------------------------
370:
371: /**
372: * Process an "SendInOut" request.
373: */
374: private String sendInOut() {
375: mLog.info("PBTestBinding: Processing sendInOut request");
376: String respMsg = null;
377:
378: InOut io;
379: try {
380: io = mFactory.createInOutExchange();
381: io.setEndpoint(mContext
382: .getEndpointsForService(ENGINE_SERVICE)[0]);
383: io.setOperation(new QName("InOut"));
384: io.setInMessage(createMessage(io,
385: "PBTestBInding to InOut service"));
386: mChannel.send(io);
387: io = (InOut) mChannel.accept();
388: if (io.getStatus() == ExchangeStatus.ACTIVE) {
389: respMsg = "PBTestBinding:Result: sendInOut Successful";
390: } else {
391: respMsg = "PBTestBinding:Result: sendInOut FAILED";
392: }
393: io.setStatus(ExchangeStatus.DONE);
394: mChannel.send(io);
395: } catch (javax.jbi.messaging.MessagingException mEx) {
396: respMsg = "PBTestBinding:Result: sendInOut: Messaging error: "
397: + mEx;
398: }
399: mLog.info("PBTestBinding: Finished sendInOut request");
400:
401: return (respMsg);
402: }
403:
404: /**
405: * Process a "sendInOnly" request.
406: */
407: private String sendInOnly() {
408: mLog.info("PBTestBinding: Processing sendInOnly request");
409:
410: String respMsg = null;
411:
412: InOnly io;
413: try {
414: io = mFactory.createInOnlyExchange();
415: io.setEndpoint(mContext
416: .getEndpointsForService(ENGINE_SERVICE)[0]);
417: io.setOperation(new QName("InOnly"));
418: io.setInMessage(createMessage(io,
419: "PBTestBinding: message to InOnly service"));
420: io.setProperty("Property1", "PropertyValue");
421: io.setProperty("TestProp", new Integer(5));
422: mChannel.send(io);
423: io = (InOnly) mChannel.accept();
424: if (io.getStatus() == ExchangeStatus.DONE) {
425: respMsg = "PBTestBinding:Result: sendInOnly Successful";
426: } else {
427: respMsg = "PBTestBinding:Result: sendInOnly FAILED";
428: }
429: } catch (javax.jbi.messaging.MessagingException mEx) {
430: respMsg = "PBTestBinding:Result: sendInOnly: Messaging error: "
431: + mEx;
432: }
433: mLog.info("PBTestBinding: Finished sendInOnly request");
434: return (respMsg);
435: }
436:
437: /**
438: * Process a "sendSynchInOnly" request.
439: */
440: private String sendSynchInOnly() {
441: mLog.info("PBTEstBinding: Processing sendsynchInOnly request");
442:
443: String respMsg = null;
444:
445: InOnly io;
446: try {
447: io = mFactory.createInOnlyExchange();
448: io.setEndpoint(mContext
449: .getEndpointsForService(ENGINE_SERVICE)[0]);
450: io.setOperation(new QName("InOnlyTimeout"));
451: io.setInMessage(createMessage(io,
452: "PBTestBInding message to InOnly service"));
453: io.setProperty("Property1", "PropertyValue2");
454: io.setProperty("TestProp", new Integer(6));
455: mChannel.sendSync(io, 3000);
456: if (io.getStatus() == ExchangeStatus.ERROR) {
457: respMsg = "PBTestBinding:Result: sendSynchInOnly Successful";
458: } else {
459: respMsg = "PBTestBinding:Result: sendSynchInOnly FAILED";
460: }
461: } catch (javax.jbi.messaging.MessagingException mEx) {
462: respMsg = "PBTestBinding:Result: sendSynchInOnly: Messaging error: "
463: + mEx;
464: }
465: mLog.info("PBTestBinding: Finished sendSynchInOnly request");
466: return (respMsg);
467: }
468:
469: /**
470: * Process an "SendInOptionalOut" request.
471: */
472: private String sendInOptionalOut() {
473: mLog
474: .info("PBTEstBinding: Processing sendInOptionalOut request");
475:
476: String respMsg = null;
477:
478: InOptionalOut io;
479: try {
480: io = mFactory.createInOptionalOutExchange();
481: io.setEndpoint(mContext
482: .getEndpointsForService(ENGINE_SERVICE)[0]);
483: io.setOperation(new QName("InOptionalOut"));
484: io.setInMessage(createMessage(io,
485: "PBTestBInding message to InOptionalOut service"));
486: io.setProperty("Property1", "PropertyValue3");
487: io.setProperty("TestProp", new Integer(7));
488: mChannel.send(io);
489: io = (InOptionalOut) mChannel.accept();
490: if (io.getStatus() == ExchangeStatus.ACTIVE) {
491: respMsg = "PBTestBinding:Result: sendInOptionalOut Successful";
492: } else {
493: respMsg = "PBTestBinding:Result: sendInOptinalOut FAILED";
494: }
495: io.setStatus(ExchangeStatus.DONE);
496: mChannel.send(io);
497: } catch (javax.jbi.messaging.MessagingException mEx) {
498: respMsg = "PBTestBinding:Result: sendInOptionalOut: Messaging error: "
499: + mEx;
500: }
501: mLog.info("PBTestBinding: Finished sendInOptionalOut request");
502: return (respMsg);
503: }
504:
505: /**
506: * Process an "ResolveService" request.
507: */
508: private String resolveService() {
509: mLog.info("PBTEstBinding: Processing resolveService");
510:
511: String respMsg = null;
512:
513: InOut io;
514: try {
515: io = mFactory.createInOutExchange();
516: io.setService(ENGINE_SERVICE);
517: io.setOperation(new QName("InOut"));
518: io
519: .setInMessage(createMessage(io,
520: "PBTestBinding message InOut with resolve service"));
521: mChannel.send(io);
522: io = (InOut) mChannel.accept();
523: if (io.getStatus() == ExchangeStatus.ACTIVE) {
524: respMsg = "PBTestBinding:Result: resolveService Successful";
525: } else {
526: respMsg = "PBTestBinding:Result: resolveService FAILED";
527: }
528: io.setStatus(ExchangeStatus.DONE);
529: mChannel.send(io);
530: } catch (javax.jbi.messaging.MessagingException mEx) {
531: respMsg = "PBTestBinding:Result: resolveService: Messaging error: "
532: + mEx;
533: }
534: mLog.info("PBTestBinding: Finished resolveService request");
535: return (respMsg);
536: }
537:
538: /**
539: * Create a Normalized Message.
540: * @param exchange - a MessageExchange for which a message is to be created.
541: * @param content - a String containing the message content.
542: * @return The NormalizedMessage instance with the provided content.
543: */
544: private NormalizedMessage createMessage(MessageExchange exchange,
545: String content) {
546: NormalizedMessage msg = null;
547: try {
548: StreamDataSource fds = new StreamDataSource("Test",
549: "text/plain");
550: OutputStreamWriter osw = new OutputStreamWriter(fds
551: .getOutputStream());
552: osw.write("This is the contents of the attachment.");
553: osw.close();
554:
555: /*
556: FileDataSource fds = new FileDataSource("C:/temp/junk");
557: */
558: Document doc;
559: Element elem;
560: msg = exchange.createMessage();
561: doc = mDocumentBuilder.newDocument();
562: elem = doc.createElement("message");
563: elem.appendChild(doc.createTextNode(content));
564: doc.appendChild(elem);
565: msg.setContent(new DOMSource(doc));
566: msg.addAttachment("Attachment1", new DataHandler(fds));
567: } catch (java.io.IOException ioEx) {
568: mLog.warning("IOException " + ioEx.toString());
569: } catch (javax.jbi.messaging.MessagingException ex) {
570: mLog.warning("MessagingException " + ex.toString());
571: }
572: return msg;
573: }
574:
575: /**
576: * Create a Normalized Message.
577: * @param exchange - a MessageExchange for which a message is to be created.
578: * @param content - a String containing the message content.
579: * @return The NormalizedMessage instance with the provided content.
580: */
581: private Fault createFault(MessageExchange exchange, String content) {
582: Fault fault = null;
583: try {
584: Document doc;
585: Element elem;
586: fault = exchange.createFault();
587: doc = mDocumentBuilder.newDocument();
588: elem = doc.createElement("message");
589: elem.appendChild(doc.createTextNode(content));
590: doc.appendChild(elem);
591: fault.setContent(new DOMSource(doc));
592: } catch (javax.jbi.messaging.MessagingException ex) {
593: mLog.warning("MessagingException " + ex.toString());
594: }
595: return fault;
596: }
597:
598: /**
599: * Process a "servicedescription" request.
600: */
601: private String serviceDescription() {
602: mLog
603: .info("PBTestBinding: Processing servicedescription request");
604:
605: String respMsg = null;
606: Document document;
607:
608: try {
609: document = mContext.getEndpointDescriptor(mContext
610: .getEndpointsForService(ENGINE_SERVICE)[0]);
611: StringWriter sw;
612: TransformerFactory.newInstance().newTransformer()
613: .transform(new DOMSource(document),
614: new StreamResult(sw = new StringWriter()));
615: respMsg = "PBTestBinding:Result: serviceDescription Successful: "
616: + sw.toString();
617: } catch (javax.jbi.JBIException mEx) {
618: respMsg = "PBTestBinding:Result: resolveService: Messaging Exception: "
619: + mEx;
620: } catch (Exception e) {
621: respMsg = "PBTestBinding:Result: resolveService: Exception: "
622: + e;
623: }
624: mLog.info("PBTestBinding: Finished serviceDescription request");
625: return (respMsg);
626: }
627:
628: /**
629: * Process a "resolveepr" request.
630: */
631: private String resolveEPR() {
632: mLog.info("PBTestBInding: Processing resolveEPR request");
633:
634: String respMsg = null;
635: DocumentFragment df = null;
636:
637: try {
638: org.w3c.dom.Document desc = null;
639: Source source;
640: DOMResult dr;
641: Transformer transform;
642: String wsdl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
643: + "<myepr>"
644: + "<service name=\"testService\">"
645: + "<correlation id=\"ABCD-GHI-JKLMNOP-QRST-UVWXYZ\"/>"
646: + "</service>" + "</myepr>";
647: source = new StreamSource(new StringReader(wsdl));
648: transform = TransformerFactory.newInstance()
649: .newTransformer();
650: transform.transform(source, dr = new DOMResult());
651: desc = (Document) dr.getNode();
652: df = desc.createDocumentFragment();
653: NodeList nodes = desc.getChildNodes();
654: for (int i = 0; i < nodes.getLength();) {
655: df.appendChild(nodes.item(i));
656: }
657: } catch (Exception e) {
658: respMsg = "PBTestBinding:Result: resolveEPR: Exception: "
659: + e;
660: }
661: try {
662: ServiceEndpoint se = mContext.resolveEndpointReference(df);
663: respMsg = "PBTestBinding:Result: resolveEPR Successful: "
664: + se.toString();
665: } catch (Exception e) {
666: respMsg = "PBTestBinding:Result: resolveEPR2: Exception: "
667: + e;
668: }
669: mLog.info("PBTestBinding: Finished serviceDescription request");
670: return (respMsg);
671: }
672: }
|