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: * @(#)BindingComponent.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package testcomponent;
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:
045: import javax.management.StandardMBean;
046: import javax.management.ObjectName;
047:
048: /**
049: * Dummy binding component used to test deployment.
050: *
051: * @author Sun Microsystems, Inc.
052: */
053: public class BindingComponent implements Component, ComponentLifeCycle,
054: ServiceUnitManager, Bootstrap {
055: /** Optional config file used to change behavior of component. */
056: private static final String CONFIG_FILE_NAME = "config.properties";
057:
058: /**
059: * Config property indicating that the component should throw an exception
060: * on start.
061: */
062: private static final String THROW_ON_COMP_START = "throw.on.comp.start";
063:
064: /** Config property indicating that the component should throw an exception
065: * on SU init.
066: */
067: private static final String THROW_ON_SU_INIT = "throw.on.su.init";
068:
069: /** Config property indicating that the component should throw an exception
070: * on shutdown.
071: */
072: private static final String THROW_ON_COMP_SHUTDOWN = "throw.on.comp.shutdown";
073:
074: /** Config property indicating that the component should throw an exception
075: * on service unit shutdown.
076: */
077: private static final String THROW_ON_SU_SHUTDOWN = "throw.on.su.shutdown";
078:
079: /** Config property indicating that the component should throw an exception
080: * on service unit start.
081: */
082: private static final String THROW_ON_SU_START = "throw.on.su.start";
083:
084: /** Config property indicating that the component should throw an exception
085: * on service unit undeploy.
086: */
087: private static final String THROW_ON_SU_UNDEPLOY = "throw.on.su.undeploy";
088:
089: /** Config property indicating that the component should throw an exception
090: * on service unit deploy.
091: */
092: private static final String THROW_ON_SU_DEPLOY = "throw.on.su.deploy";
093:
094: /** Config property indicating that the component should throw a plan-text exception
095: */
096: private static final String THROW_TEXT_EXCEPTION = "throw.text.exception";
097:
098: /** Config property indicating that the component should throw an exception
099: * on service unit undeploy.
100: */
101: private static final String REGISTER_CONFIG_MBEAN = "register.config.mbean";
102:
103: /**
104: * Local copy of the component name.
105: */
106: protected String mComponentName;
107:
108: /**
109: * Type of component.
110: */
111: protected String mComponentType = "Binding";
112:
113: /**
114: * Local handle to the ComponentContext.
115: */
116: protected ComponentContext mContext;
117:
118: /**
119: * Logger instance.
120: */
121: protected Logger mLog = Logger.getLogger("com.sun.jbi.management");
122:
123: /**
124: * Component configuration
125: */
126: private Properties mConfig = new Properties();
127:
128: //
129: // Component methods
130: //
131:
132: /**
133: * Get the ComponentLifeCycle implementation instance for this Binding
134: * Component.
135: * @return the life cycle implementation instance.
136: */
137: public ComponentLifeCycle getLifeCycle() {
138: mLog.info(mComponentType + " getLifeCycle called");
139: return this ;
140: }
141:
142: /**
143: * Get the ServiceUnitManager implementation instance for this Binding
144: * Component.
145: * @return the Service Unit manager implementation instance.
146: */
147: public ServiceUnitManager getServiceUnitManager() {
148: mLog.info(mComponentType + " " + mComponentName
149: + " getServiceUnitManager called");
150: return this ;
151: }
152:
153: /**
154: * Resolve descriptor details for the specified reference, which is for a
155: * service provided by this component.
156: * @param ref the endpoint reference to be resolved.
157: * @return the description for the specified reference.
158: */
159: public org.w3c.dom.Document getServiceDescription(
160: javax.jbi.servicedesc.ServiceEndpoint ref) {
161: mLog.info(mComponentType + " " + mComponentName
162: + " getServiceDescription called");
163: return null;
164: }
165:
166: /**
167: * This method is called by JBI to check if this component, in the role of
168: * provider of the service indicated by the given exchange, can actually
169: * perform the operation desired.
170: */
171: public boolean isExchangeWithConsumerOkay(
172: javax.jbi.servicedesc.ServiceEndpoint endpoint,
173: javax.jbi.messaging.MessageExchange exchange) {
174: mLog.info(mComponentType + " " + mComponentName
175: + " isExchangeWithConsumerOkay called");
176: return true;
177: }
178:
179: /**
180: * This method is called by JBI to check if this component, in the role of
181: * consumer of the service indicated by the given exchange, can actually
182: * interact with the the provider completely.
183: */
184: public boolean isExchangeWithProviderOkay(
185: javax.jbi.servicedesc.ServiceEndpoint endpoint,
186: javax.jbi.messaging.MessageExchange exchange) {
187: mLog.info(mComponentType + " " + mComponentName
188: + " isExchangeWithProviderOkay called");
189: return true;
190: }
191:
192: /**
193: * Resolve the given endpoint reference, given the capabilities of the
194: * given consumer. This is called by JBI when it is attempting to resolve
195: * the given endpoint reference on behalf of a component.
196: * @param epr the endpoint reference, in some XML dialect understood by the
197: * appropriate component (usually a Binding Component).
198: * @return the service endpoint for the endpoint reference;
199: * <code>null</code> if the endpoint reference cannot be resolved.
200: */
201: public javax.jbi.servicedesc.ServiceEndpoint resolveEndpointReference(
202: org.w3c.dom.DocumentFragment epr) {
203: mLog.info(mComponentType + " " + mComponentName
204: + " resolveEndpointReference called");
205: return null;
206: }
207:
208: //
209: // ComponentLifeCycle methods
210: //
211:
212: /**
213: * Initialize the Binding Component.
214: * @param context the JBI component context created by the JBI framework.
215: * @throws javax.jbi.JBIException if an error occurs.
216: */
217: public void init(ComponentContext context)
218: throws javax.jbi.JBIException {
219: mComponentName = context.getComponentName();
220: mContext = context;
221: File configFile;
222: InputStream inputStream;
223: try {
224: configFile = new File(context.getInstallRoot(),
225: CONFIG_FILE_NAME);
226: if (configFile.exists()) {
227: inputStream = new java.io.FileInputStream(configFile);
228: mConfig.load(inputStream);
229: inputStream.close();
230: }
231:
232: //write the timestamp in a properties file in workspace dir
233: try {
234: File settingsFile = new File(
235: context.getWorkspaceRoot(), "settings.txt");
236: FileWriter writer = new FileWriter(settingsFile);
237: SimpleDateFormat format = new SimpleDateFormat(
238: "yyyyMMddHHmm");
239: writer.write(format.format(new Date()));
240: writer.close();
241: } catch (Exception ex) {
242: }
243:
244: } catch (Exception ex) {
245: throw new javax.jbi.JBIException(ex.toString());
246: }
247: }
248:
249: /**
250: * Get the JMX ObjectName for any additional MBean for this BC. This
251: * implementation always returns null.
252: * @return javax.management.ObjectName is always null.
253: */
254: public javax.management.ObjectName getExtensionMBeanName() {
255: mLog.info(mComponentType + " "
256: + " getExtensionMBeanName called");
257: return null;
258: }
259:
260: /**
261: * Start the Binding Component.
262: * @throws javax.jbi.JBIException if an error occurs.
263: */
264: public void start() throws javax.jbi.JBIException {
265: mLog.info(mComponentType + " " + mComponentName
266: + " start called");
267:
268: if (mConfig.containsKey(THROW_ON_COMP_START)) {
269: throw new javax.jbi.JBIException(createErrorResult("start",
270: "Component start failed"));
271: }
272: if (mConfig.containsKey(REGISTER_CONFIG_MBEAN)) {
273: registerCustomMBeans();
274: registerLoggerMBean();
275: }
276: }
277:
278: /**
279: * Stop the Binding Component.
280: * @throws javax.jbi.JBIException if an error occurs.
281: */
282: public void stop() throws javax.jbi.JBIException {
283: mLog.info(mComponentType + " " + mComponentName
284: + " stop called");
285: }
286:
287: /**
288: * Shut down the Binding Component.
289: * @throws javax.jbi.JBIException if an error occurs.
290: */
291: public void shutDown() throws javax.jbi.JBIException {
292: mLog.info(mComponentType + " " + mComponentName
293: + " shutDown called");
294:
295: if (mConfig.containsKey(THROW_ON_COMP_SHUTDOWN)) {
296: throw new javax.jbi.JBIException(createErrorResult(
297: "shutDown", "Component shutDown failed"));
298: }
299:
300: if (mConfig.containsKey(REGISTER_CONFIG_MBEAN)) {
301: unregisterCustomMBeans();
302: }
303: }
304:
305: //
306: // ServiceUnitManager methods
307: //
308:
309: /**
310: * Deploy a Service Unit.
311: * @param serviceUnitName the name of the Service Unit being deployed.
312: * @param serviceUnitRootPath the full path to the Service Unit artifact
313: * root directory.
314: * @return a deployment status message.
315: * @throws javax.jbi.management.DeploymentException if the deployment
316: * operation is unsuccessful.
317: */
318: public String deploy(String serviceUnitName,
319: String serviceUnitRootPath)
320: throws javax.jbi.management.DeploymentException {
321: if (mConfig.containsKey(THROW_ON_SU_DEPLOY)) {
322: if (mConfig.containsKey(THROW_TEXT_EXCEPTION)) {
323: throw new javax.jbi.management.DeploymentException(
324: "Deployment of service unit " + serviceUnitName
325: + " FAILED.");
326: } else {
327: throw new javax.jbi.management.DeploymentException(
328: createErrorResult("init", "SU deploy failed"));
329: }
330: }
331:
332: mLog.info(mComponentType + " " + mComponentName
333: + " deployed Service Unit " + serviceUnitName
334: + " with serviceUnitRootPath " + serviceUnitRootPath);
335:
336: if (serviceUnitName.equals("esbadmin00089-su-3")
337: || serviceUnitName.equals("esbadmin00089-su-6")) {
338: return createDeployResult("deploy", false);
339: } else {
340: return createDeployResult("deploy", true);
341: }
342: }
343:
344: /**
345: * Initialize the deployment.
346: * @param serviceUnitName the name of the Service Unit being initialized.
347: * @param serviceUnitRootPath the full path to the Service Unit artifact
348: * root directory.
349: * @throws javax.jbi.management.DeploymentException if the Service Unit is
350: * not deployed, or is in an incorrect state.
351: */
352: public void init(String serviceUnitName, String serviceUnitRootPath)
353: throws javax.jbi.management.DeploymentException {
354: mLog.info(mComponentType + " " + mComponentName
355: + " initialized Service Unit " + serviceUnitName
356: + " with serviceUnitRootPath " + serviceUnitRootPath);
357:
358: if (mConfig.containsKey(THROW_ON_SU_INIT)) {
359: throw new javax.jbi.management.DeploymentException(
360: createErrorResult("init",
361: "SU initialization failed"));
362: }
363:
364: java.io.File suRoot = new java.io.File(serviceUnitRootPath);
365:
366: if (!suRoot.exists()) {
367: throw new javax.jbi.management.DeploymentException(
368: "Invalid service unit root " + serviceUnitRootPath
369: + " for service unit " + serviceUnitName);
370: }
371: }
372:
373: /**
374: * Shut down the deployment.
375: * @param serviceUnitName the name of the Service Unit being shut down.
376: * @throws javax.jbi.management.DeploymentException if the Service Unit
377: * is not deployed, or is in an incorrect state.
378: */
379: public void shutDown(String serviceUnitName)
380: throws javax.jbi.management.DeploymentException {
381: if (mConfig.containsKey(THROW_ON_SU_SHUTDOWN)) {
382: throw new javax.jbi.management.DeploymentException(
383: createErrorResult("shutDown", "Service unit "
384: + serviceUnitName + " shutDown failed"));
385: }
386:
387: mLog.info(mComponentType + " " + mComponentName
388: + " shut down Service Unit " + serviceUnitName);
389: }
390:
391: /**
392: * Start the deployment.
393: * @param serviceUnitName the name of the Service Unit being started.
394: * @throws javax.jbi.management.DeploymentException if the Service Unit
395: * is not deployed, or is in an incorrect state.
396: */
397: public void start(String serviceUnitName)
398: throws javax.jbi.management.DeploymentException {
399:
400: if (mConfig.containsKey(THROW_ON_SU_START)) {
401: throw new javax.jbi.management.DeploymentException(
402: createErrorResult("start", "Service unit "
403: + serviceUnitName + " start failed"));
404: }
405:
406: mLog.info(mComponentType + " " + mComponentName
407: + " started Service Unit " + serviceUnitName);
408: }
409:
410: /**
411: * Stop the deployment.
412: * @param serviceUnitName the name of the Service Unit being stopped.
413: * @throws javax.jbi.management.DeploymentException if the Service Unit
414: * is not deployed, or is in an incorrect state.
415: */
416: public void stop(String serviceUnitName)
417: throws javax.jbi.management.DeploymentException {
418: mLog.info(mComponentType + " " + mComponentName
419: + " stopped Service Unit " + serviceUnitName);
420: }
421:
422: /**
423: * Undeploy a Service Unit from the component.
424: * @param serviceUnitName the name of the Service Unit being undeployed.
425: * @param serviceUnitRootPath the full path to the Service Unit artifact
426: * root directory.
427: * @return an undeployment status message.
428: * @throws javax.jbi.management.DeploymentException if the undeployment
429: * operation is unsuccessful.
430: */
431: public String undeploy(String serviceUnitName,
432: String serviceUnitRootPath)
433: throws javax.jbi.management.DeploymentException {
434: mLog.info(mComponentType + " " + mComponentName
435: + " undeployed Service Unit " + serviceUnitName);
436:
437: if (mConfig.containsKey(THROW_ON_SU_UNDEPLOY)) {
438: throw new javax.jbi.management.DeploymentException(
439: createErrorResult("undeploy", "Service unit "
440: + serviceUnitName + " undeploy failed"));
441: } else if (serviceUnitName.equals("esbadmin00089-su-3")
442: || serviceUnitName.equals("esbadmin00089-su-6")) {
443: return createDeployResult("undeploy", false);
444: } else {
445: return createDeployResult("undeploy", true);
446: }
447: }
448:
449: public void onUninstall() throws javax.jbi.JBIException {
450: }
451:
452: public void onInstall() throws javax.jbi.JBIException {
453:
454: }
455:
456: public void init(
457: javax.jbi.component.InstallationContext installationContext)
458: throws javax.jbi.JBIException {
459:
460: }
461:
462: public void cleanUp() throws javax.jbi.JBIException {
463:
464: }
465:
466: /** Creates a (un)deployment result string.
467: * @param task 'deploy' or 'undeploy'
468: */
469: private String createDeployResult(String task, boolean isSuccess) {
470: return "<component-task-result xmlns=\"http://java.sun.com/xml/ns/jbi/management-message\">"
471: + "<component-name>"
472: + mComponentName
473: + "</component-name>"
474: + "<component-task-result-details>"
475: + "<task-result-details>"
476: + "<task-id>"
477: + task
478: + "</task-id>"
479: + "<task-result>"
480: + (isSuccess ? "SUCCESS" : "FAILED")
481: + "</task-result>"
482: + "</task-result-details>"
483: + "</component-task-result-details>"
484: + "</component-task-result>";
485: }
486:
487: /** Creates a component failure response with a configurable message.
488: * @param task 'deploy' or 'undeploy'
489: */
490: protected String createErrorResult(String task, String msg) {
491: return "<component-task-result xmlns=\"http://java.sun.com/xml/ns/jbi/management-message\">"
492: + "<component-name>"
493: + mComponentName
494: + "</component-name>"
495: + "<component-task-result-details>"
496: + "<task-result-details>"
497: + "<task-id>"
498: + task
499: + "</task-id>"
500: + "<task-result>"
501: + "FAILED"
502: + "</task-result>"
503: + "<message-type>ERROR</message-type>"
504: + "<task-status-msg>"
505: + "<msg-loc-info>"
506: + "<loc-token>JBIWHOOPS</loc-token>"
507: + "<loc-message>"
508: + msg
509: + "</loc-message>"
510: + "</msg-loc-info>"
511: + "</task-status-msg>"
512: + "</task-result-details>"
513: + "</component-task-result-details>"
514: + "</component-task-result>";
515: }
516:
517: /**
518: * Register custom MBeans
519: */
520: protected void registerCustomMBeans() throws javax.jbi.JBIException {
521: try {
522: ComponentConfigMBean mbean = new ComponentConfig(
523: mComponentName + " custom config MBean");
524:
525: StandardMBean compMBean = new StandardMBean(mbean,
526: ComponentConfigMBean.class);
527:
528: ObjectName compMBeanName = mContext.getMBeanNames()
529: .createCustomComponentMBeanName("Configuration");
530:
531: mContext.getMBeanServer().registerMBean(compMBean,
532: compMBeanName);
533: } catch (Exception exp) {
534: throw new javax.jbi.JBIException(exp.getMessage());
535: }
536: }
537:
538: /**
539: * Unregister custom MBeans
540: */
541: protected void unregisterCustomMBeans()
542: throws javax.jbi.JBIException {
543: try {
544: ObjectName compMBeanName = mContext.getMBeanNames()
545: .createCustomComponentMBeanName("Configuration");
546:
547: mContext.getMBeanServer().unregisterMBean(compMBeanName);
548: } catch (Exception exp) {
549: throw new javax.jbi.JBIException(exp.getMessage());
550: }
551: }
552:
553: /**
554: * ComponentContext.getLogger() registers a Logger MBean for the component.
555: * When the component is uninstalled the Logger MBean is unregistered.
556: */
557: protected void registerLoggerMBean() throws javax.jbi.JBIException {
558: mContext.getLogger("com.sun.jbi.test.binding.logger", null);
559: }
560: }
|