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