PageRenderTime 68ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib-fort/rules4jbi/src/main/org/openesb/components/rules4jbi/engine/component/Rules4JBIBootstrap.java

https://bitbucket.org/pymma/openesb-components
Java | 225 lines | 131 code | 62 blank | 32 comment | 5 complexity | f06ccbdfa3f4b74f80cdbf5a40558f52 MD5 | raw file
  1. /*
  2. * @(#)Rules4JBIBootstrap.java $Revision: 1.5 $ $Date: 2009/01/14 02:53:12 $
  3. *
  4. * Copyright (c) 2008 Milan Fort (http://www.milanfort.com/). All rights reserved.
  5. *
  6. * The contents of this file are subject to the terms of the Common Development
  7. * and Distribution License (the "License"). You may not use this file except
  8. * in compliance with the License.
  9. *
  10. * You can obtain a copy of the license at http://www.sun.com/cddl/cddl.html.
  11. * See the License for the specific language governing permissions and limitations
  12. * under the License.
  13. */
  14. package org.openesb.components.rules4jbi.engine.component;
  15. import java.io.File;
  16. import javax.jbi.JBIException;
  17. import javax.jbi.component.Bootstrap;
  18. import javax.jbi.component.ComponentContext;
  19. import javax.jbi.component.InstallationContext;
  20. import javax.management.MBeanServer;
  21. import javax.management.ObjectName;
  22. import org.w3c.dom.DocumentFragment;
  23. import nu.xom.Element;
  24. import nu.xom.Nodes;
  25. import nu.xom.XPathContext;
  26. import com.google.inject.Guice;
  27. import com.google.inject.Inject;
  28. import com.google.inject.Injector;
  29. import com.google.inject.Stage;
  30. import com.google.inject.name.Named;
  31. import org.openesb.components.rules4jbi.shared.logging.Logger;
  32. import org.openesb.components.rules4jbi.shared.util.XOMUtils;
  33. import org.openesb.components.rules4jbi.engine.guice.modules.LoggerModule;
  34. import org.openesb.components.rules4jbi.engine.util.DOMUtils;
  35. /**
  36. * Component bootstrap implementation. Note that according to the JBI spec,
  37. * there is no guarantee that the same instance of its Bootstrap implementation will be used
  38. * during both install and uninstall operations of the component. Data that need to be retained
  39. * between installation-time and uninstallation-time must be persisted in such a fashion that
  40. * a separate instance of the bootstrap class can find them, despite component or system shutdown.
  41. *
  42. * @author Milan Fort (http://www.milanfort.com/)
  43. * @version $Revision: 1.5 $ $Date: 2009/01/14 02:53:12 $
  44. *
  45. * @since 0.1
  46. */
  47. public class Rules4JBIBootstrap implements Bootstrap {
  48. private static final String JBI_CONFIGURATION_NAMESPACE = "http://www.sun.com/jbi/Configuration/V1.0";
  49. @Inject @Named("Bootstrap")
  50. private Logger logger = null;
  51. // @Inject
  52. // private Injector injector;
  53. @Inject
  54. private InstallationConfiguration installationConfiguration;
  55. private ObjectName extensionMBeanName;
  56. private InstallationContext installationContext;
  57. public void init(InstallationContext installationContext) throws JBIException {
  58. if (installationContext == null) {
  59. throw new JBIException("Null installation context received during bootstrap");
  60. }
  61. Injector injector =
  62. Guice.createInjector(Stage.PRODUCTION, new LoggerModule(installationContext.getContext()));
  63. injector.injectMembers(this);
  64. if (logger == null) {
  65. throw new JBIException("Unable to properly inject depencencies");
  66. }
  67. logger.entering(this.getClass(), "init");
  68. this.installationContext = installationContext;
  69. if (installationContext.isInstall()) {
  70. logger.fine("NEW INSTALLATION: %1$td.%1$tm.%1$tY at %1$tT", System.currentTimeMillis());
  71. }
  72. logInstallationContext(installationContext);
  73. parseInstallationDescriptorExtension(installationContext.getInstallationDescriptorExtension());
  74. registerExtensionMBean();
  75. logger.exiting(this.getClass(), "init");
  76. }
  77. private void registerExtensionMBean() throws JBIException {
  78. logger.fine("Registering installation configuration MBean");
  79. try {
  80. final ComponentContext componentContext = installationContext.getContext();
  81. // extensionMBeanName = new ObjectName("com.milanfort.rules4jbi", "type", "InstallationConfiguration");
  82. extensionMBeanName =
  83. componentContext.getMBeanNames().createCustomComponentMBeanName("Configuration");
  84. MBeanServer mbeanServer = componentContext.getMBeanServer();
  85. mbeanServer.registerMBean(installationConfiguration, extensionMBeanName);
  86. logger.fine("Installation configuration MBean registration sucessfull");
  87. } catch (Exception e) {
  88. logger.severe("Installation configuration MBean registration failed");
  89. throw new JBIException(e);
  90. }
  91. }
  92. public ObjectName getExtensionMBeanName() {
  93. logger.entering(this.getClass(), "getExtensionMBeanName");
  94. return extensionMBeanName;
  95. }
  96. public void onInstall() throws JBIException {
  97. logger.entering(this.getClass(), "onInstall");
  98. installationConfiguration.save(new File(installationContext.getContext().getWorkspaceRoot(),
  99. InstallationConfiguration.CONFIG_FILE_NAME));
  100. }
  101. public void onUninstall() throws JBIException {
  102. logger.entering(this.getClass(), "onUninstall");
  103. /*
  104. * No need to delete the configuration file here;
  105. * the JBI runtime will delete the whole workspace anyway.
  106. */
  107. }
  108. public void cleanUp() throws JBIException {
  109. logger.entering(this.getClass(), "cleanUp");
  110. unregisterExtensionMBean();
  111. }
  112. private void unregisterExtensionMBean() throws JBIException {
  113. logger.fine("Unregistering installation configuration MBean");
  114. try {
  115. MBeanServer mbeanServer = installationContext.getContext().getMBeanServer();
  116. mbeanServer.unregisterMBean(extensionMBeanName);
  117. logger.fine("Installation configuration MBean unregistration sucessfull");
  118. } catch (Exception e) {
  119. logger.severe("Installation configuration MBean unregistration failed");
  120. throw new JBIException(e);
  121. }
  122. }
  123. private void logInstallationContext(InstallationContext installationContext) {
  124. logger.config("--- Begin Installation Context Info ---");
  125. logger.config("Component_name: %s; %s",
  126. installationContext.getComponentName(),
  127. installationContext.isInstall() ? "installing" : "not installing");
  128. logger.config("Component_class_name: %s", installationContext.getComponentClassName());
  129. logger.config("Class_path_elements: %s", installationContext.getClassPathElements());
  130. logger.config("Installation_root_directory: %s", installationContext.getInstallRoot());
  131. logger.config("Workspace_root_directory: %s", installationContext.getContext().getWorkspaceRoot());
  132. org.w3c.dom.DocumentFragment doc = installationContext.getInstallationDescriptorExtension();
  133. String configString = DOMUtils.documentFragmentToString(doc);
  134. logger.config("Descriptor_extension: %s", configString);
  135. logger.config("--- End Installation Context Info ---");
  136. }
  137. private void parseInstallationDescriptorExtension(DocumentFragment extension) {
  138. logger.fine("Parsing installation descriptor extension");
  139. try {
  140. Element configuration = XOMUtils.toElement(DOMUtils.documentFragmentToString(extension));
  141. int poolSize = getConfigurationValue(
  142. configuration, "PoolSize", InstallationConfiguration.DEFAULT_POOL_SIZE);
  143. int maxServiceUnits = getConfigurationValue(
  144. configuration, "MaxServiceUnits", InstallationConfiguration.DEFAULT_MAX_SERVICE_UNITS);
  145. logger.config("Retrieved pool size: %d", poolSize);
  146. logger.config("Retrieved max service units: %d", maxServiceUnits);
  147. installationConfiguration.setPoolSize(poolSize);
  148. installationConfiguration.setMaxServiceUnits(maxServiceUnits);
  149. } catch (Exception e) {
  150. logger.severe("Unable to parse installation descriptor extension - using default values");
  151. }
  152. }
  153. static int getConfigurationValue(Element configuration, String propertyName, int defaultValue) {
  154. XPathContext context = new XPathContext("config", JBI_CONFIGURATION_NAMESPACE);
  155. try {
  156. Nodes query = configuration.query(
  157. "//config:Property[@name='" + propertyName + "']/@defaultValue", context);
  158. return Integer.parseInt(query.get(0).getValue());
  159. } catch (Exception e) {
  160. return defaultValue;
  161. }
  162. }
  163. }