1 17 package org.apache.servicemix.jbi.container; 18 19 import javax.jbi.JBIException; 20 import javax.jbi.component.ServiceUnitManager; 21 import javax.resource.spi.work.WorkManager ; 22 23 import org.apache.servicemix.components.util.ComponentAdaptor; 24 import org.apache.servicemix.jbi.framework.ComponentMBeanImpl; 25 import org.jencks.factory.WorkManagerFactoryBean; 26 import org.springframework.beans.BeansException; 27 import org.springframework.beans.factory.BeanFactory; 28 import org.springframework.beans.factory.BeanFactoryAware; 29 import org.springframework.beans.factory.DisposableBean; 30 import org.springframework.beans.factory.InitializingBean; 31 import org.springframework.context.ApplicationContext; 32 import org.springframework.context.ApplicationContextAware; 33 34 35 44 public class SpringJBIContainer extends JBIContainer 45 implements InitializingBean, DisposableBean, BeanFactoryAware, ApplicationContextAware { 46 47 private String [] componentNames; 48 private ActivationSpec[] activationSpecs; 49 private BeanFactory beanFactory; 50 private ApplicationContext applicationContext; 51 private String [] deployArchives; 52 private Object shutdownLock; 53 54 public void afterPropertiesSet() throws Exception { 55 init(); 56 57 if (componentNames != null) { 59 for (int i = 0; i < componentNames.length; i++) { 60 String componentName = componentNames[i]; 61 activateComponent(new ActivationSpec(componentName, lookupBean(componentName))); 62 } 63 } 64 65 if (activationSpecs != null) { 66 for (int i = 0; i < activationSpecs.length; i++) { 67 ActivationSpec activationSpec = activationSpecs[i]; 68 activateComponent(activationSpec); 69 } 70 } 71 72 if (deployArchives != null) { 73 for (int i = 0; i < deployArchives.length; i++) { 74 String archive = deployArchives[i]; 75 installArchive(archive); 76 } 77 } 78 79 start(); 80 } 81 82 public void stop() throws JBIException { 83 if (beanFactory instanceof DisposableBean) { 84 DisposableBean disposable = (DisposableBean) beanFactory; 85 try { 86 disposable.destroy(); 87 } 88 catch (Exception e) { 89 throw new JBIException("Failed to dispose of the Spring BeanFactory due to: " + e, e); 90 } 91 } 92 super.stop(); 93 } 94 95 101 public Object getBean(String id) { 102 ComponentMBeanImpl component = getComponent(id); 103 Object bean = component != null ? component.getComponent() : null; 104 if (bean instanceof ComponentAdaptor) { 105 bean = ((ComponentAdaptor) bean).getLifeCycle(); 106 } 107 return bean; 108 } 109 110 111 public BeanFactory getBeanFactory() { 114 return beanFactory; 115 } 116 117 public void setBeanFactory(BeanFactory beanFactory) throws BeansException { 118 this.beanFactory = beanFactory; 119 } 120 121 public String [] getComponentNames() { 122 return componentNames; 123 } 124 125 public void setComponentNames(String [] componentNames) { 126 this.componentNames = componentNames; 127 } 128 129 public ServiceUnitManager getServiceManager() { 130 return serviceManager; 131 } 132 133 public void setServiceManager(ServiceUnitManager serviceManager) { 134 this.serviceManager = serviceManager; 135 } 136 137 public ActivationSpec[] getActivationSpecs() { 138 return activationSpecs; 139 } 140 141 public void setActivationSpecs(ActivationSpec[] activationSpecs) throws JBIException { 142 this.activationSpecs = activationSpecs; 143 } 144 145 public String [] getDeployArchives() { 146 return deployArchives; 147 } 148 149 public void setDeployArchives(String [] deployArchives) { 150 this.deployArchives = deployArchives; 151 } 152 153 protected Object lookupBean(String componentName) { 156 Object bean = beanFactory.getBean(componentName); 157 if (bean == null) { 158 throw new IllegalArgumentException ("Component name: " + componentName 159 + " is not found in the Spring BeanFactory"); 160 } 161 return bean; 162 } 163 164 public ApplicationContext getApplicationContext() { 165 return applicationContext; 166 } 167 168 public void setApplicationContext(ApplicationContext applicationContext) { 169 this.applicationContext = applicationContext; 170 } 171 172 protected WorkManager createWorkManager() throws JBIException { 173 WorkManagerFactoryBean factory = new WorkManagerFactoryBean(); 174 factory.setApplicationContext(applicationContext); 175 try { 176 return factory.getWorkManager(); 177 } 178 catch (Exception e) { 179 throw new JBIException("Failed to start WorkManager: " + e, e); 180 } 181 } 182 183 public void destroy() throws Exception { 184 super.shutDown(); 185 } 186 187 public void shutDown() throws JBIException { 188 if (shutdownLock != null) { 189 synchronized (shutdownLock) { 190 shutdownLock.notify(); 191 } 192 } 193 } 194 195 public void setShutdownLock(Object lock) { 196 this.shutdownLock = lock; 197 } 198 199 } 200 | Popular Tags |