1 17 package org.apache.servicemix.geronimo; 18 19 import java.io.File ; 20 import java.util.Collection ; 21 22 import javax.jbi.JBIException; 23 import javax.resource.spi.work.WorkManager ; 24 import javax.transaction.TransactionManager ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.geronimo.gbean.GBeanInfo; 29 import org.apache.geronimo.gbean.GBeanInfoBuilder; 30 import org.apache.geronimo.gbean.GBeanLifecycle; 31 import org.apache.geronimo.kernel.Kernel; 32 import org.apache.geronimo.transaction.context.GeronimoTransactionManager; 33 import org.apache.geronimo.transaction.context.TransactionContextManager; 34 import org.apache.servicemix.jbi.container.ComponentEnvironment; 35 import org.apache.servicemix.jbi.container.JBIContainer; 36 import org.apache.servicemix.jbi.container.ServiceAssemblyEnvironment; 37 import org.apache.servicemix.jbi.framework.ComponentContextImpl; 38 import org.apache.servicemix.jbi.framework.ComponentMBeanImpl; 39 import org.apache.servicemix.jbi.framework.ComponentNameSpace; 40 import org.apache.servicemix.jbi.framework.ServiceAssemblyLifeCycle; 41 42 public class ServiceMixGBean implements GBeanLifecycle, Container { 43 44 private Log log = LogFactory.getLog(getClass().getName()); 45 46 private JBIContainer container; 47 private String name; 48 private String directory; 49 private TransactionContextManager transactionContextManager; 50 private WorkManager workManager; 51 private Kernel kernel; 52 private Collection jndiResources; 53 54 public static final GBeanInfo GBEAN_INFO; 55 56 static { 57 GBeanInfoBuilder infoFactory = new GBeanInfoBuilder("ServiceMix JBI Container", ServiceMixGBean.class, "JBIContainer"); 58 infoFactory.addInterface(Container.class); 59 infoFactory.addAttribute("name", String .class, true); 60 infoFactory.addAttribute("directory", String .class, true); 61 infoFactory.addReference("transactionContextManager", TransactionContextManager.class); 62 infoFactory.addReference("workManager", WorkManager .class); 63 infoFactory.addAttribute("kernel", Kernel.class, false); 64 infoFactory.setConstructor(new String []{"name", "directory", "transactionContextManager", "workManager", "kernel"}); 65 GBEAN_INFO = infoFactory.getBeanInfo(); 66 } 67 68 public static GBeanInfo getGBeanInfo() { 69 return GBEAN_INFO; 70 } 71 72 public ServiceMixGBean(String name, 73 String directory, 74 TransactionContextManager transactionContextManager, 75 WorkManager workManager, 76 Kernel kernel) { 77 this.name = name; 78 this.directory = directory; 79 this.transactionContextManager = transactionContextManager; 80 this.workManager = workManager; 81 this.kernel = kernel; 82 if (log.isDebugEnabled()) { 83 log.debug("ServiceMixGBean created"); 84 } 85 95 } 96 97 102 public void doStart() throws Exception { 103 if (log.isDebugEnabled()) { 104 log.debug("ServiceMixGBean doStart"); 105 } 106 ClassLoader old = Thread.currentThread().getContextClassLoader(); 107 Thread.currentThread().setContextClassLoader(ServiceMixGBean.class.getClassLoader()); 108 try { 109 if (container == null) { 110 container = createContainer(); 111 container.init(); 112 container.start(); 113 } 114 } finally { 115 Thread.currentThread().setContextClassLoader(old); 116 } 117 } 118 119 124 public void doStop() throws Exception { 125 if (log.isDebugEnabled()) { 126 log.debug("ServiceMixGBean doStop"); 127 } 128 try { 129 if (container != null) { 130 container.shutDown(); 131 } 132 } finally { 133 container = null; 134 } 135 } 136 137 140 public void doFail() { 141 if (log.isDebugEnabled()) { 142 log.debug("ServiceMixGBean doFail"); 143 } 144 try { 145 if (container != null) { 146 try { 147 container.shutDown(); 148 } 149 catch (JBIException e) { 150 log.info("Caught while closing due to failure: " + e, e); 151 } 152 } 153 } finally { 154 container = null; 155 } 156 } 157 158 private JBIContainer createContainer() { 159 JBIContainer container = new JBIContainer(); 160 container.setName(name); 161 container.setRootDir(directory); 162 container.setTransactionManager(getTransactionManager()); 163 container.setMonitorInstallationDirectory(false); 164 container.setMonitorDeploymentDirectory(false); 165 container.setWorkManager(workManager); 166 return container; 167 } 168 169 public TransactionManager getTransactionManager() { 170 if (transactionContextManager != null) { 171 return new GeronimoTransactionManager(transactionContextManager); 172 } 173 return null; 174 } 175 176 public void register(Component component) throws Exception { 177 ComponentNameSpace cns = new ComponentNameSpace(container.getName(), component.getName()); 178 ComponentContextImpl context = new ComponentContextImpl(container, cns); 179 ComponentEnvironment env = new ComponentEnvironment(); 180 env.setComponentRoot(new File (component.getRootDir())); 181 env.setInstallRoot(new File (component.getInstallDir())); 182 env.setWorkspaceRoot(new File (component.getWorkDir())); 183 context.setEnvironment(env); 184 185 container.activateComponent(null, 186 component.getComponent(), 187 component.getDescription(), 188 context, 189 component.getType().equals("binding-component"), 190 component.getType().equals("service-engine"), 191 null); 192 ComponentMBeanImpl cmb = container.getComponent(component.getName()); 193 File stateFile = cmb.getContext().getEnvironment().getStateFile(); 194 if (stateFile.isFile()) { 195 cmb.setInitialRunningState(); 196 } else { 197 cmb.start(); 198 } 199 } 200 201 public void unregister(Component component) throws Exception { 202 container.deactivateComponent(component.getName()); 203 } 204 205 public void register(ServiceAssembly assembly) throws Exception { 206 File rootDir = new File (assembly.getRootDir()); 207 ServiceAssemblyEnvironment env = new ServiceAssemblyEnvironment(); 208 env.setRootDir(rootDir); 209 env.setInstallDir(new File (rootDir, "install")); 210 env.setSusDir(new File (rootDir, "sus")); 211 env.setStateFile(new File (rootDir, "state.xml")); 212 ServiceAssemblyLifeCycle salc = container.getRegistry().registerServiceAssembly(assembly.getDescriptor().getServiceAssembly(), env); 213 if (env.getStateFile().isFile()) { 214 salc.restore(); 215 } else { 216 salc.start(); 217 } 218 } 219 220 public void unregister(ServiceAssembly assembly) throws Exception { 221 ServiceAssemblyLifeCycle salc = container.getRegistry().getServiceAssembly(assembly.getName()); 222 salc.shutDown(false); 223 container.getRegistry().unregisterServiceAssembly(assembly.getName()); 224 } 225 226 } 227 | Popular Tags |