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: * @(#)CSVFeederEngineLifeCycle.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: import org.w3c.dom.Document;
030: import org.w3c.dom.DocumentFragment;
031:
032: import java.io.File;
033:
034: import java.util.ArrayList;
035: import java.util.HashMap;
036: import java.util.Iterator;
037: import java.util.LinkedList;
038: import java.util.Set;
039: import java.util.logging.Logger;
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.MessageExchange;
049: import javax.jbi.messaging.MessagingException;
050: import javax.jbi.messaging.NormalizedMessage;
051: import javax.jbi.messaging.MessageExchangeFactory;
052:
053: import javax.jbi.servicedesc.ServiceEndpoint;
054:
055: import javax.xml.namespace.QName;
056:
057: /**
058: * This class implements ComponentLifeCycle. The JBI framework will start this
059: * engine class automatically when JBI framework starts up.
060: *
061: * @author Nilesh
062: */
063: public class CSVFeederEngineLifeCycle implements ComponentLifeCycle,
064: Component {
065: /**
066: * In Only MEP.
067: */
068: public static final String IN_ONLY = "http://www.w3.org/2004/08/wsdl/in-only";
069:
070: /**
071: * In Out MEP.
072: */
073: public static final String IN_OUT = "http://www.w3.org/2004/08/wsdl/in-out";
074:
075: /**
076: * In Optional Out MEP.
077: */
078: public static final String IN_OPTIONAL_OUT = "http://www.w3.org/2004/08/wsdl/in-opt-out";
079:
080: /**
081: * Robust In Only MEP.
082: */
083: public static final String ROBUST_IN_ONLY = "http://www.w3.org/2004/08/wsdl/robust-in-only";
084:
085: /**
086: *
087: */
088: private MessageExchangeFactory mFactory;
089:
090: /**
091: *
092: */
093: private static final String INOUT_SERVICE_NAME = "transformService2";
094:
095: /**
096: *
097: */
098: private static final String CSV_INOUT_SERVICE_NAME = "csvTransformService";
099:
100: /**
101: *
102: */
103: private static final String INOUT_SERVICE_NAMESPACE = "http://www.sun.com/ns/jbi/binding/engines/transformation/deploy/service-config";
104:
105: /**
106: *
107: */
108: private static final String CSV_INOUT_SERVICE_NAMESPACE = "http://www.sun.com/ns/jbi/engines/transformation/deploy/service-config";
109:
110: /**
111: *
112: */
113: private static final String ENDPOINT = "JBI";
114:
115: /**
116: *
117: */
118: public Thread mRecv;
119:
120: /**
121: *
122: */
123: private ServiceEndpoint mEndpoint;
124:
125: /**
126: *
127: */
128: private DeliveryChannel mChannel;
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 mInonlyReference;
149:
150: /**
151: *
152: */
153: private ServiceEndpoint mInoutReference;
154:
155: /**
156: *
157: */
158: private ServiceEndpoint mOutboundReference;
159:
160: /**
161: *
162: */
163: private ServiceEndpoint mRobustinonlyReference;
164:
165: /**
166: *
167: */
168: private boolean mRunFlag = true;
169:
170: /**
171: * Get the JMX ObjectName for the DeploymentMBean for this BPE. If there is
172: * none, return null.
173: *
174: * @return ObjectName the JMX object name of the DeploymentMBean or null if
175: * there is no DeploymentMBean.
176: */
177: public javax.management.ObjectName getDeploymentMBeanName() {
178: return null;
179: }
180:
181: /**
182: * Get the JMX ObjectName for any additional MBean for this BC. If there is
183: * none, return null.
184: *
185: * @return ObjectName the JMX object name of the additional MBean or null
186: * if there is no additional MBean.
187: */
188: public javax.management.ObjectName getExtensionMBeanName() {
189: return null;
190: }
191:
192: /**
193: * Initialize the Data Feeder Engine. This performs initialization required
194: *
195: * @param jbiContext the JBI environment mContext
196: *
197: * @throws javax.jbi.JBIException if the stockquote engine is unable to
198: * initialize.
199: */
200: public void init(javax.jbi.component.ComponentContext jbiContext)
201: throws javax.jbi.JBIException {
202: mLog = Logger.getLogger("DataFeederEngine");
203: mLog.info("Data Feeder Engine INIT invoked");
204:
205: if (jbiContext == null) {
206: throw (new javax.jbi.JBIException("Context is null",
207: new NullPointerException()));
208: }
209:
210: /* Assign values */
211: mContext = jbiContext;
212: Payload.mXMLFile = mContext.getInstallRoot()
213: + File.separatorChar + "te_input.xml";
214:
215: Payload.mCSVFileName = mContext.getInstallRoot()
216: + File.separatorChar + "csvinput.txt";
217:
218: mLog.info(" INIT METHOD FINISHED ");
219: }
220:
221: /**
222: * Shutdown the SE. This performs cleanup before the SE is terminated.
223: * Once this has been called, init() must be called before the SE can be
224: * started again with a call to start().
225: *
226: * @throws javax.jbi.JBIException if the SE is unable to shut down.
227: */
228: public void shutDown() throws javax.jbi.JBIException {
229: mLog.info("Data Feeder Engine Shutdown-Start");
230: mLog.info("Data Feeder Engine Shutdown-End");
231: }
232:
233: /**
234: */
235: public void start() throws javax.jbi.JBIException {
236: mLog.info(" Data Feeder Engine INVOKED ");
237:
238: try {
239: mChannel = mContext.getDeliveryChannel();
240: mFactory = mChannel.createExchangeFactory();
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: /*mRecv = new Thread(new Receiver());
252: mRecv.start();*/
253:
254: testXMLTrasnformation();
255:
256: testCSVTrasnformation();
257:
258: mLog.info(" START FINISHED ");
259: }
260:
261: /**
262: */
263: public void stop() throws javax.jbi.JBIException {
264: mLog.info("STOP INVOKED ");
265: mRunFlag = false;
266:
267: try {
268: if (mChannel != null) {
269: mChannel.close();
270: }
271: mRecv.interrupt();
272: mRecv.join();
273: } catch (Exception e) {
274: ;
275: }
276:
277: mLog.info(" STOP FINISHED ");
278: }
279:
280: /**
281: *
282: *
283: */
284: public void testCSVTrasnformation() {
285: mLog.info("Data Feeder engine sending CSV Input to TE");
286:
287: NormalizedMessage aMsg;
288: InOut inout;
289:
290: try {
291: // create the exchange
292: inout = mFactory.createInOutExchange();
293: aMsg = inout.createMessage();
294:
295: // set the stuff we know
296: inout.setOperation(new QName(CSV_INOUT_SERVICE_NAMESPACE,
297: "transform"));
298:
299: //ServiceEndpoint [] mEndpoint;
300: ServiceEndpoint mEndpoint;
301:
302: // lookup the endpoint reference and set on exchange
303: mEndpoint = mContext
304: .getEndpointsForService(new QName(
305: CSV_INOUT_SERVICE_NAMESPACE,
306: CSV_INOUT_SERVICE_NAME))[0];
307:
308: // set the payload
309: Payload.setCSVPayload(aMsg);
310: inout.setService(new QName(CSV_INOUT_SERVICE_NAMESPACE,
311: CSV_INOUT_SERVICE_NAME));
312: inout.setEndpoint(mEndpoint);
313: // set the message on the exchange
314: inout.setInMessage(aMsg);
315:
316: // send the exchange
317: mChannel.send(inout);
318:
319: // receive the response
320: inout = (InOut) mChannel.accept();
321:
322: if (inout.getStatus() == ExchangeStatus.ERROR) {
323: mLog.info("CSV to XML Trasnformation request Failed");
324:
325: return;
326: } else if (inout.getStatus() == ExchangeStatus.ACTIVE) {
327: if (inout.getOutMessage() != null) {
328: mLog.info("XML Transformation SUCCESS");
329: } else {
330: mLog.info("XML Transformation FAILED");
331: }
332: }
333:
334: inout.setStatus(ExchangeStatus.DONE);
335: mChannel.send(inout);
336: } catch (Exception e) {
337: mLog.severe("XML Transformation threw-Exception");
338: e.printStackTrace();
339: }
340: }
341:
342: /**
343: *
344: *
345: */
346: public void testXMLTrasnformation() {
347: mLog.info("Data Feeder engine sending XML Input to TE");
348:
349: NormalizedMessage aMsg;
350: InOut inout;
351:
352: try {
353: // create the exchange
354: inout = mFactory.createInOutExchange();
355: aMsg = inout.createMessage();
356:
357: // set the stuff we know
358: //outIn.setService(mOutboundReference);
359: inout.setOperation(new QName(INOUT_SERVICE_NAMESPACE,
360: "transform"));
361:
362: //ServiceEndpoint [] mEndpoint;
363: ServiceEndpoint mEndpoint;
364:
365: // lookup the endpoint reference and set on exchange
366: mEndpoint = mContext.getEndpointsForService(new QName(
367: INOUT_SERVICE_NAMESPACE, INOUT_SERVICE_NAME))[0];
368:
369: // set the payload
370: Payload.setXMLPayload(aMsg);
371: inout.setService(new QName(INOUT_SERVICE_NAMESPACE,
372: INOUT_SERVICE_NAME));
373: inout.setEndpoint(mEndpoint);
374: // set the message on the exchange
375: inout.setInMessage(aMsg);
376:
377: // send the exchange
378: mChannel.send(inout);
379:
380: // receive the response
381: inout = (InOut) mChannel.accept();
382:
383: if (inout.getStatus() == ExchangeStatus.ERROR) {
384: mLog.info("XML Trasnformation request Failed");
385:
386: return;
387: } else if (inout.getStatus() == ExchangeStatus.ACTIVE) {
388: if (inout.getOutMessage() != null) {
389: mLog.info("XML Transformation SUCCESS");
390: } else {
391: mLog.info("XML Transformation FAILED");
392: }
393: }
394:
395: inout.setStatus(ExchangeStatus.DONE);
396: mChannel.send(inout);
397: } catch (Exception e) {
398: mLog.severe("XML Transformation threw-Exception");
399: e.printStackTrace();
400: }
401: }
402:
403: /**
404: *
405: *
406: */
407: public class Receiver implements Runnable {
408: /**
409: *
410: */
411: public void run() {
412: mLog.info("Data Feeder Engine receiver started");
413:
414: try {
415: while (mRunFlag) {
416: mReq = mChannel.accept();
417:
418: mLog.info("Got message in feeder engine");
419: if (mReq != null) {
420: mLog
421: .info("Message ID: "
422: + mReq.getExchangeId());
423: mLog.info("Message Pattern: "
424: + mReq.getPattern());
425: mLog.info("Status: " + mReq.getStatus());
426: mLog.info("Role: " + mReq.getRole());
427:
428: /*if (mReq.getPattern().toString().equals(IN_OUT))
429: {
430: doTestGoodInOut();
431: }
432: else if (mReq.getPattern().toString().equals(IN_ONLY))
433: {
434: doTestGoodInOnly();
435: }
436: else if (mReq.getPattern().toString().equals(ROBUST_IN_ONLY))
437: {
438: doTestGoodRobustInOnly();
439: }*/
440: } //end if
441: }
442: } catch (Exception e) {
443: mLog.severe("Exception at receiver");
444: mLog.info("Data Feeder Engine receiver ended");
445: }
446: }
447: }
448:
449: public javax.jbi.component.ComponentLifeCycle getLifeCycle() {
450: return this ;
451: }
452:
453: public javax.jbi.component.ServiceUnitManager getServiceUnitManager() {
454: return null;
455: }
456:
457: public org.w3c.dom.Document getServiceDescription(
458: javax.jbi.servicedesc.ServiceEndpoint ServiceEndpoint) {
459: return null;
460: }
461:
462: /** This method is called by JBI to check if this component, in the role of
463: * provider of the service indicated by the given exchange, can actually
464: * perform the operation desired.
465: */
466: public boolean isExchangeWithConsumerOkay(
467: javax.jbi.servicedesc.ServiceEndpoint endpoint,
468: javax.jbi.messaging.MessageExchange exchange) {
469: return true;
470: }
471:
472: /** This method is called by JBI to check if this component, in the role of
473: * consumer of the service indicated by the given exchange, can actually
474: * interact with the the provider completely.
475: */
476: public boolean isExchangeWithProviderOkay(
477: javax.jbi.servicedesc.ServiceEndpoint endpoint,
478: javax.jbi.messaging.MessageExchange exchange) {
479: return true;
480: }
481:
482: /**
483: * Resolve the given endpoint reference, given the capabilities of the
484: * given consumer. This is called by JBI when it is attempting to resolve
485: * the given endpoint reference on behalf of a component.
486: * @param epr the endpoint reference, in some XML dialect understood by the
487: * appropriate component (usually a Binding Component).
488: * @return the service endpoint for the endpoint reference;
489: * <code>null</code> if the endpoint reference cannot be resolved.
490: */
491: public ServiceEndpoint resolveEndpointReference(DocumentFragment epr) {
492: return null;
493: }
494: }
|