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