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: * @(#)SimpleTestEngine.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.ui.ant.test.statistics;
030:
031: import java.io.InputStream;
032: import java.io.File;
033: import java.io.FileWriter;
034: import java.text.SimpleDateFormat;
035: import java.util.logging.Logger;
036: import java.util.Date;
037: import java.util.Properties;
038:
039: import javax.jbi.component.Bootstrap;
040: import javax.jbi.component.Component;
041: import javax.jbi.component.ComponentContext;
042: import javax.jbi.component.ComponentLifeCycle;
043: import javax.jbi.component.ServiceUnitManager;
044: import javax.xml.namespace.QName;
045: import javax.management.StandardMBean;
046: import javax.management.ObjectName;
047: import javax.jbi.servicedesc.ServiceEndpoint;
048: import javax.jbi.messaging.MessageExchange;
049: import javax.jbi.messaging.MessageExchangeFactory;
050: import javax.jbi.messaging.MessagingException;
051: import javax.jbi.messaging.DeliveryChannel;
052: import javax.jbi.messaging.RobustInOnly;
053: import javax.jbi.messaging.ExchangeStatus;
054: import javax.jbi.messaging.NormalizedMessage;
055: import javax.xml.transform.Source;
056: import java.io.StringReader;
057: import org.xml.sax.InputSource;
058: import org.w3c.dom.Document;
059: import javax.xml.parsers.DocumentBuilder;
060: import javax.xml.parsers.DocumentBuilderFactory;
061: import javax.jbi.messaging.InOut;
062: import javax.xml.transform.dom.DOMSource;
063:
064: /**
065: * Simple Component used to test Statistics.
066: */
067: public class SimpleTestEngine implements Component, ComponentLifeCycle,
068: ServiceUnitManager, Bootstrap {
069:
070: /**
071: * Local copy of the component name.
072: */
073: protected String mComponentName;
074:
075: /**
076: * Type of component.
077: */
078: protected String mComponentType = "Engine";
079:
080: /**
081: * Local handle to the ComponentContext.
082: */
083: protected ComponentContext mContext;
084:
085: /**
086: * Logger instance.
087: */
088: protected Logger mLog = Logger.getLogger("com.sun.jbi.management");
089:
090: /**
091: * Component configuration
092: */
093: private Properties mConfig = new Properties();
094:
095: /**
096: * delivery channel
097: */
098: private DeliveryChannel mChannel;
099:
100: /**
101: * message exchange factory
102: */
103: private MessageExchangeFactory mFactory;
104:
105: /**
106: * mReq
107: */
108: private MessageExchange mReq;
109:
110: /**
111: * flag that controls this engine's lifecycle
112: */
113: private boolean mRunFlag = true;
114:
115: //
116: // Component methods
117: //
118:
119: /**
120: * Get the ComponentLifeCycle implementation instance for this Binding
121: * Component.
122: * @return the life cycle implementation instance.
123: */
124: public ComponentLifeCycle getLifeCycle() {
125: mLog.info(mComponentType + " getLifeCycle called");
126: return this ;
127: }
128:
129: /**
130: * Get the ServiceUnitManager implementation instance for this Binding
131: * Component.
132: * @return the Service Unit manager implementation instance.
133: */
134: public ServiceUnitManager getServiceUnitManager() {
135: mLog.info(mComponentType + " " + mComponentName
136: + " getServiceUnitManager called");
137: return this ;
138: }
139:
140: /**
141: * Resolve descriptor details for the specified reference, which is for a
142: * service provided by this component.
143: * @param ref the endpoint reference to be resolved.
144: * @return the description for the specified reference.
145: */
146: public org.w3c.dom.Document getServiceDescription(
147: javax.jbi.servicedesc.ServiceEndpoint ref) {
148: mLog.info(mComponentType + " " + mComponentName
149: + " getServiceDescription called");
150: return null;
151: }
152:
153: /**
154: * This method is called by JBI to check if this component, in the role of
155: * provider of the service indicated by the given exchange, can actually
156: * perform the operation desired.
157: */
158: public boolean isExchangeWithConsumerOkay(
159: javax.jbi.servicedesc.ServiceEndpoint endpoint,
160: javax.jbi.messaging.MessageExchange exchange) {
161: mLog.info(mComponentType + " " + mComponentName
162: + " isExchangeWithConsumerOkay called");
163: return true;
164: }
165:
166: /**
167: * This method is called by JBI to check if this component, in the role of
168: * consumer of the service indicated by the given exchange, can actually
169: * interact with the the provider completely.
170: */
171: public boolean isExchangeWithProviderOkay(
172: javax.jbi.servicedesc.ServiceEndpoint endpoint,
173: javax.jbi.messaging.MessageExchange exchange) {
174: mLog.info(mComponentType + " " + mComponentName
175: + " isExchangeWithProviderOkay called");
176: return true;
177: }
178:
179: /**
180: * Resolve the given endpoint reference, given the capabilities of the
181: * given consumer. This is called by JBI when it is attempting to resolve
182: * the given endpoint reference on behalf of a component.
183: * @param epr the endpoint reference, in some XML dialect understood by the
184: * appropriate component (usually a Binding Component).
185: * @return the service endpoint for the endpoint reference;
186: * <code>null</code> if the endpoint reference cannot be resolved.
187: */
188: public javax.jbi.servicedesc.ServiceEndpoint resolveEndpointReference(
189: org.w3c.dom.DocumentFragment epr) {
190: mLog.info(mComponentType + " " + mComponentName
191: + " resolveEndpointReference called");
192: return null;
193: }
194:
195: //
196: // ComponentLifeCycle methods
197: //
198:
199: /**
200: * Initialize the Binding Component.
201: * @param context the JBI component context created by the JBI framework.
202: * @throws javax.jbi.JBIException if an error occurs.
203: */
204: public void init(ComponentContext context)
205: throws javax.jbi.JBIException {
206: mComponentName = context.getComponentName();
207: mContext = context;
208: }
209:
210: /**
211: * Get the JMX ObjectName for any additional MBean for this BC. This
212: * implementation always returns null.
213: * @return javax.management.ObjectName is always null.
214: */
215: public javax.management.ObjectName getExtensionMBeanName() {
216: mLog.info(mComponentType + " "
217: + " getExtensionMBeanName called");
218: return null;
219: }
220:
221: /**
222: * Start the Binding Component.
223: * @throws javax.jbi.JBIException if an error occurs.
224: */
225: public void start() throws javax.jbi.JBIException {
226: mLog.info(mComponentType + " " + mComponentName
227: + " start called");
228:
229: /** send some test messages */
230: mLog.info("Test Engine message processing begins....");
231: try {
232:
233: String inputString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> "
234: + "<jbi:message version=\"1.0\" "
235: + "xmlns:jbi=\"http://java.sun.com/xml/ns/jbi/wsdl-11-wrapper\" "
236: + "xmlns:ping=\"http://ping\" "
237: + "name=\"pingIn\" "
238: + "type=\"ping:ping-message\"> "
239: + "<jbi:part> "
240: + "<ping:pinginput><ping:test-string>Hello World</ping:test-string></ping:pinginput>"
241: + "</jbi:part> " + "</jbi:message>";
242:
243: mChannel = mContext.getDeliveryChannel();
244: mFactory = mChannel.createExchangeFactory();
245:
246: String serviceName = "PingService";
247: String serviceNamespace = "http://ping";
248: String endpointName = "ConsumingPingPort";
249:
250: QName service = new QName(serviceNamespace, serviceName);
251:
252: ServiceEndpoint serviceEndpoint = mContext.getEndpoint(
253: service, endpointName);
254:
255: QName operation = new QName("http://ping", "ping");
256:
257: DeliveryChannel channel = mChannel;
258: MessageExchangeFactory factory = channel
259: .createExchangeFactory(serviceEndpoint);
260: InOut inOutME = factory.createInOutExchange();
261: inOutME.setOperation(operation);
262: NormalizedMessage inMsg = inOutME.createMessage();
263: inOutME.setInMessage(inMsg);
264:
265: // set the content of the IN normalized message ( Normalize the message )
266: StringReader xmlReader = new StringReader(inputString);
267: DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
268: .newInstance();
269: docBuilderFactory.setValidating(false);
270: DocumentBuilder docBuilder = docBuilderFactory
271: .newDocumentBuilder();
272: InputSource is = new InputSource(xmlReader);
273: Source inMsgSource = new DOMSource(docBuilder.parse(is));
274:
275: inMsg.setContent(inMsgSource);
276:
277: // send the message exchange and wait for response
278: boolean isSent = channel.sendSync(inOutME, 60000);
279: if (!isSent) {
280: throw new Exception(
281: "SimpleTestEngine:Timeout occurred ");
282: }
283: mLog.info("Sent message successfully");
284:
285: } catch (Exception me) {
286: me.printStackTrace();
287: }
288:
289: }
290:
291: /**
292: * Stop the Binding Component.
293: * @throws javax.jbi.JBIException if an error occurs.
294: */
295: public void stop() throws javax.jbi.JBIException {
296: mRunFlag = false;
297: mLog.info(mComponentType + " " + mComponentName
298: + " stop called");
299: }
300:
301: /**
302: * Shut down the Binding Component.
303: * @throws javax.jbi.JBIException if an error occurs.
304: */
305: public void shutDown() throws javax.jbi.JBIException {
306: mLog.info(mComponentType + " " + mComponentName
307: + " shutDown called");
308: }
309:
310: //
311: // ServiceUnitManager methods
312: //
313:
314: /**
315: * Deploy a Service Unit.
316: * @param serviceUnitName the name of the Service Unit being deployed.
317: * @param serviceUnitRootPath the full path to the Service Unit artifact
318: * root directory.
319: * @return a deployment status message.
320: * @throws javax.jbi.management.DeploymentException if the deployment
321: * operation is unsuccessful.
322: */
323: public String deploy(String serviceUnitName,
324: String serviceUnitRootPath)
325: throws javax.jbi.management.DeploymentException {
326:
327: mLog.info(mComponentType + " " + mComponentName
328: + " deployed Service Unit " + serviceUnitName
329: + " with serviceUnitRootPath " + serviceUnitRootPath);
330: return createDeployResult("deploy", true);
331: }
332:
333: /**
334: * Initialize the deployment.
335: * @param serviceUnitName the name of the Service Unit being initialized.
336: * @param serviceUnitRootPath the full path to the Service Unit artifact
337: * root directory.
338: * @throws javax.jbi.management.DeploymentException if the Service Unit is
339: * not deployed, or is in an incorrect state.
340: */
341: public void init(String serviceUnitName, String serviceUnitRootPath)
342: throws javax.jbi.management.DeploymentException {
343: mLog.info(mComponentType + " " + mComponentName
344: + " initialized Service Unit " + serviceUnitName
345: + " with serviceUnitRootPath " + serviceUnitRootPath);
346:
347: }
348:
349: /**
350: * Shut down the deployment.
351: * @param serviceUnitName the name of the Service Unit being shut down.
352: * @throws javax.jbi.management.DeploymentException if the Service Unit
353: * is not deployed, or is in an incorrect state.
354: */
355: public void shutDown(String serviceUnitName)
356: throws javax.jbi.management.DeploymentException {
357: mLog.info(mComponentType + " " + mComponentName
358: + " shut down Service Unit " + serviceUnitName);
359: }
360:
361: /**
362: * Start the deployment.
363: * @param serviceUnitName the name of the Service Unit being started.
364: * @throws javax.jbi.management.DeploymentException if the Service Unit
365: * is not deployed, or is in an incorrect state.
366: */
367: public void start(String serviceUnitName)
368: throws javax.jbi.management.DeploymentException {
369:
370: mLog.info(mComponentType + " " + mComponentName
371: + " started Service Unit " + serviceUnitName);
372: }
373:
374: /**
375: * Stop the deployment.
376: * @param serviceUnitName the name of the Service Unit being stopped.
377: * @throws javax.jbi.management.DeploymentException if the Service Unit
378: * is not deployed, or is in an incorrect state.
379: */
380: public void stop(String serviceUnitName)
381: throws javax.jbi.management.DeploymentException {
382: mLog.info(mComponentType + " " + mComponentName
383: + " stopped Service Unit " + serviceUnitName);
384: }
385:
386: /**
387: * Undeploy a Service Unit from the component.
388: * @param serviceUnitName the name of the Service Unit being undeployed.
389: * @param serviceUnitRootPath the full path to the Service Unit artifact
390: * root directory.
391: * @return an undeployment status message.
392: * @throws javax.jbi.management.DeploymentException if the undeployment
393: * operation is unsuccessful.
394: */
395: public String undeploy(String serviceUnitName,
396: String serviceUnitRootPath)
397: throws javax.jbi.management.DeploymentException {
398: mLog.info(mComponentType + " " + mComponentName
399: + " undeployed Service Unit " + serviceUnitName);
400: return createDeployResult("undeploy", true);
401: }
402:
403: public void onUninstall() throws javax.jbi.JBIException {
404: }
405:
406: public void onInstall() throws javax.jbi.JBIException {
407:
408: }
409:
410: public void init(
411: javax.jbi.component.InstallationContext installationContext)
412: throws javax.jbi.JBIException {
413:
414: }
415:
416: public void cleanUp() throws javax.jbi.JBIException {
417:
418: }
419:
420: /** Creates a (un)deployment result string.
421: * @param task 'deploy' or 'undeploy'
422: */
423: private String createDeployResult(String task, boolean isSuccess) {
424: return "<component-task-result xmlns=\"http://java.sun.com/xml/ns/jbi/management-message\">"
425: + "<component-name>"
426: + mComponentName
427: + "</component-name>"
428: + "<component-task-result-details>"
429: + "<task-result-details>"
430: + "<task-id>"
431: + task
432: + "</task-id>"
433: + "<task-result>"
434: + (isSuccess ? "SUCCESS" : "FAILED")
435: + "</task-result>"
436: + "</task-result-details>"
437: + "</component-task-result-details>"
438: + "</component-task-result>";
439: }
440:
441: }
|