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