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