PageRenderTime 33ms CodeModel.GetById 26ms app.highlight 5ms 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
 15package org.openesb.components.rules4jbi.engine.component;
 16
 17import java.io.File;
 18
 19import javax.jbi.JBIException;
 20import javax.jbi.component.Bootstrap;
 21import javax.jbi.component.ComponentContext;
 22import javax.jbi.component.InstallationContext;
 23import javax.management.MBeanServer;
 24import javax.management.ObjectName;
 25
 26import org.w3c.dom.DocumentFragment;
 27
 28import nu.xom.Element;
 29import nu.xom.Nodes;
 30import nu.xom.XPathContext;
 31
 32import com.google.inject.Guice;
 33import com.google.inject.Inject;
 34import com.google.inject.Injector;
 35import com.google.inject.Stage;
 36import com.google.inject.name.Named;
 37
 38import org.openesb.components.rules4jbi.shared.logging.Logger;
 39import org.openesb.components.rules4jbi.shared.util.XOMUtils;
 40
 41import org.openesb.components.rules4jbi.engine.guice.modules.LoggerModule;
 42import org.openesb.components.rules4jbi.engine.util.DOMUtils;
 43
 44/**
 45 * Component bootstrap implementation. Note that according to the JBI spec,
 46 * there is no guarantee that the same instance of its Bootstrap implementation will be used
 47 * during both install and uninstall operations of the component. Data that need to be retained
 48 * between installation-time and uninstallation-time must be persisted in such a fashion that
 49 * a separate instance of the bootstrap class can find them, despite component or system shutdown.
 50 *
 51 * @author Milan Fort (http://www.milanfort.com/)
 52 * @version $Revision: 1.5 $ $Date: 2009/01/14 02:53:12 $
 53 * 
 54 * @since 0.1
 55 */
 56public class Rules4JBIBootstrap implements Bootstrap {
 57    
 58    private static final String JBI_CONFIGURATION_NAMESPACE = "http://www.sun.com/jbi/Configuration/V1.0";
 59    
 60    @Inject @Named("Bootstrap")
 61    private Logger logger = null;
 62    
 63//    @Inject
 64//    private Injector injector;
 65    
 66    @Inject
 67    private InstallationConfiguration installationConfiguration;
 68    
 69    private ObjectName extensionMBeanName;
 70    
 71    private InstallationContext installationContext;
 72    
 73    public void init(InstallationContext installationContext) throws JBIException {
 74        if (installationContext == null) {
 75            throw new JBIException("Null installation context received during bootstrap");
 76        }
 77        
 78        Injector injector =
 79                Guice.createInjector(Stage.PRODUCTION, new LoggerModule(installationContext.getContext()));
 80        
 81        injector.injectMembers(this);
 82        
 83        if (logger == null) {
 84            throw new JBIException("Unable to properly inject depencencies");
 85        }
 86
 87        logger.entering(this.getClass(), "init");
 88        
 89        this.installationContext = installationContext;
 90        
 91        if (installationContext.isInstall()) {
 92            logger.fine("NEW INSTALLATION: %1$td.%1$tm.%1$tY at %1$tT", System.currentTimeMillis());
 93        }
 94        
 95        logInstallationContext(installationContext);
 96        
 97        parseInstallationDescriptorExtension(installationContext.getInstallationDescriptorExtension());
 98        
 99        registerExtensionMBean();
100        
101        logger.exiting(this.getClass(), "init");
102    }
103
104    private void registerExtensionMBean() throws JBIException {
105        logger.fine("Registering installation configuration MBean");
106        try {
107            final ComponentContext componentContext = installationContext.getContext();
108            
109//            extensionMBeanName = new ObjectName("com.milanfort.rules4jbi", "type", "InstallationConfiguration");
110        
111            extensionMBeanName =
112                    componentContext.getMBeanNames().createCustomComponentMBeanName("Configuration");
113            
114            MBeanServer mbeanServer = componentContext.getMBeanServer();
115            mbeanServer.registerMBean(installationConfiguration, extensionMBeanName);
116            
117            logger.fine("Installation configuration MBean registration sucessfull");
118            
119        } catch (Exception e) {
120            logger.severe("Installation configuration MBean registration failed");
121            
122            throw new JBIException(e);
123        }
124    }
125
126    public ObjectName getExtensionMBeanName() {
127        logger.entering(this.getClass(), "getExtensionMBeanName");
128        
129        return extensionMBeanName;
130    }
131    
132    public void onInstall() throws JBIException {
133        logger.entering(this.getClass(), "onInstall");
134        
135        installationConfiguration.save(new File(installationContext.getContext().getWorkspaceRoot(),
136                                                InstallationConfiguration.CONFIG_FILE_NAME));
137    }
138
139    public void onUninstall() throws JBIException {
140        logger.entering(this.getClass(), "onUninstall");
141        
142        /* 
143         * No need to delete the configuration file here;
144         * the JBI runtime will delete the whole workspace anyway.
145         */
146    }
147
148    public void cleanUp() throws JBIException {
149        logger.entering(this.getClass(), "cleanUp");
150
151        unregisterExtensionMBean();
152    }
153
154    private void unregisterExtensionMBean() throws JBIException {
155        logger.fine("Unregistering installation configuration MBean");
156        try {
157            MBeanServer mbeanServer = installationContext.getContext().getMBeanServer();
158            mbeanServer.unregisterMBean(extensionMBeanName);
159
160            logger.fine("Installation configuration MBean unregistration sucessfull");
161
162        } catch (Exception e) {
163            logger.severe("Installation configuration MBean unregistration failed");
164
165            throw new JBIException(e);
166        }
167    }
168    
169    private void logInstallationContext(InstallationContext installationContext) {
170        logger.config("--- Begin Installation Context Info ---");
171        
172        logger.config("Component_name: %s; %s", 
173                installationContext.getComponentName(), 
174                installationContext.isInstall() ? "installing" : "not installing");
175        
176        logger.config("Component_class_name: %s", installationContext.getComponentClassName());
177        logger.config("Class_path_elements: %s", installationContext.getClassPathElements());
178        logger.config("Installation_root_directory: %s", installationContext.getInstallRoot());
179        logger.config("Workspace_root_directory: %s", installationContext.getContext().getWorkspaceRoot());
180
181        org.w3c.dom.DocumentFragment doc = installationContext.getInstallationDescriptorExtension();
182
183        String configString = DOMUtils.documentFragmentToString(doc);
184        logger.config("Descriptor_extension: %s", configString);
185        
186        logger.config("--- End Installation Context Info ---");
187    }
188    
189    private void parseInstallationDescriptorExtension(DocumentFragment extension) {
190        logger.fine("Parsing installation descriptor extension");
191        
192        try {
193            Element configuration = XOMUtils.toElement(DOMUtils.documentFragmentToString(extension));
194            
195            int poolSize = getConfigurationValue(
196                    configuration, "PoolSize", InstallationConfiguration.DEFAULT_POOL_SIZE);
197           
198            int maxServiceUnits = getConfigurationValue(
199                    configuration, "MaxServiceUnits", InstallationConfiguration.DEFAULT_MAX_SERVICE_UNITS);
200            
201            logger.config("Retrieved pool size: %d", poolSize);
202            logger.config("Retrieved max service units: %d", maxServiceUnits);
203            
204            installationConfiguration.setPoolSize(poolSize);
205            installationConfiguration.setMaxServiceUnits(maxServiceUnits);
206            
207        } catch (Exception e) {
208            logger.severe("Unable to parse installation descriptor extension - using default values");
209        }
210    }
211
212    static int getConfigurationValue(Element configuration, String propertyName, int defaultValue) {
213        XPathContext context = new XPathContext("config", JBI_CONFIGURATION_NAMESPACE);
214
215        try {
216            Nodes query = configuration.query(
217                    "//config:Property[@name='" + propertyName + "']/@defaultValue", context);
218            
219            return Integer.parseInt(query.get(0).getValue());    
220            
221        } catch (Exception e) {
222            return defaultValue;
223        }
224    }
225}