1 17 package org.apache.servicemix.common.xbean; 18 19 import java.io.File ; 20 import java.util.Collections ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import javax.jbi.management.DeploymentException; 25 26 import org.apache.servicemix.common.AbstractDeployer; 27 import org.apache.servicemix.common.BaseComponent; 28 import org.apache.servicemix.common.Endpoint; 29 import org.apache.servicemix.common.ServiceUnit; 30 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; 31 import org.springframework.core.io.FileSystemResource; 32 import org.apache.xbean.kernel.Kernel; 33 import org.apache.xbean.kernel.KernelFactory; 34 import org.apache.xbean.kernel.ServiceName; 35 import org.apache.xbean.server.repository.FileSystemRepository; 36 import org.apache.xbean.server.spring.configuration.ClassLoaderXmlPreprocessor; 37 import org.apache.xbean.server.spring.loader.SpringLoader; 38 39 public class AbstractXBeanDeployer extends AbstractDeployer { 40 41 public AbstractXBeanDeployer(BaseComponent component) { 42 super(component); 43 } 44 45 protected String getXBeanFile() { 46 return "xbean"; 47 } 48 49 52 public boolean canDeploy(String serviceUnitName, String serviceUnitRootPath) { 53 File xbean = new File (serviceUnitRootPath, getXBeanFile() + ".xml"); 54 if (logger.isDebugEnabled()) { 55 logger.debug("Looking for " + xbean + ": " + xbean.exists()); 56 } 57 return xbean.exists(); 58 } 59 60 63 public ServiceUnit deploy(String serviceUnitName, String serviceUnitRootPath) throws DeploymentException { 64 Kernel kernel = KernelFactory.newInstance().createKernel(component.getComponentName() + "/" + serviceUnitName); 65 try { 66 XBeanServiceUnit su = new XBeanServiceUnit(); 68 su.setKernel(kernel); 69 su.setComponent(component); 70 su.setName(serviceUnitName); 71 su.setRootPath(serviceUnitRootPath); 72 Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); 74 75 SpringLoader springLoader = new SpringLoader(); 76 springLoader.setKernel(kernel); 77 springLoader.setBaseDir(new File (serviceUnitRootPath)); 78 springLoader.setXmlPreprocessors(getXmlPreProcessors(serviceUnitRootPath)); 79 springLoader.setBeanFactoryPostProcessors(getBeanFactoryPostProcessors(serviceUnitRootPath)); 80 81 ServiceName configurationName = springLoader.load(getXBeanFile()); 82 kernel.startService(configurationName); 83 su.setConfiguration(configurationName); 84 List services = getServices(kernel); 86 if (services == null || services.size() == 0) { 87 throw failure("deploy", "No endpoints found", null); 88 } 89 for (Iterator iter = services.iterator(); iter.hasNext();) { 90 Endpoint endpoint = (Endpoint) iter.next(); 91 endpoint.setServiceUnit(su); 92 if (validate(endpoint)) { 93 su.addEndpoint(endpoint); 94 } else { 95 logger.warn("Endpoint " + endpoint + "has not been validated"); 96 } 97 } 98 if (su.getEndpoints().size() == 0) { 99 throw failure("deploy", "No endpoint found", null); 100 } 101 return su; 102 } catch (Throwable e) { 103 Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); 106 kernel.destroy(); 107 if (e instanceof DeploymentException) { 108 throw ((DeploymentException) e); 109 } else { 110 throw failure("deploy", "Could not deploy xbean service unit", e); 111 } 112 } finally { 113 Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); 114 } 115 } 116 117 protected List getServices(Kernel kernel) { 118 return kernel.getServices(Endpoint.class); 119 } 120 121 protected boolean validate(Endpoint endpoint) throws DeploymentException { 122 return true; 123 } 124 125 protected List getXmlPreProcessors(String serviceUnitRootPath) { 126 FileSystemRepository repository = new FileSystemRepository(new File (serviceUnitRootPath)); 127 ClassLoaderXmlPreprocessor classLoaderXmlPreprocessor = new ClassLoaderXmlPreprocessor(repository); 128 return Collections.singletonList(classLoaderXmlPreprocessor); 129 } 130 131 protected List getBeanFactoryPostProcessors(String serviceUnitRootPath) { 132 PropertyPlaceholderConfigurer propertyPlaceholder = new PropertyPlaceholderConfigurer(); 133 FileSystemResource propertiesFile = new FileSystemResource( 134 new File (serviceUnitRootPath) + "/" + getXBeanFile() 135 + ".properties"); 136 if (propertiesFile.getFile().exists()) { 137 propertyPlaceholder.setLocation(propertiesFile); 138 return Collections.singletonList(propertyPlaceholder); 139 } 140 return Collections.EMPTY_LIST; 141 } 142 143 } 144 | Popular Tags |