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