001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.common.xbean;
018:
019: import org.apache.servicemix.common.AbstractDeployer;
020: import org.apache.servicemix.common.Endpoint;
021: import org.apache.servicemix.common.ServiceMixComponent;
022: import org.apache.servicemix.common.ServiceUnit;
023: import org.apache.servicemix.jbi.container.JBIContainer;
024: import org.apache.servicemix.jbi.framework.ComponentContextImpl;
025: import org.apache.xbean.kernel.Kernel;
026: import org.apache.xbean.kernel.KernelFactory;
027: import org.apache.xbean.kernel.ServiceName;
028: import org.apache.xbean.server.repository.FileSystemRepository;
029: import org.apache.xbean.server.spring.loader.SpringLoader;
030: import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
031: import org.springframework.core.io.FileSystemResource;
032:
033: import javax.jbi.component.ComponentContext;
034: import javax.jbi.management.DeploymentException;
035: import java.io.File;
036: import java.util.Collections;
037: import java.util.Iterator;
038: import java.util.List;
039:
040: public class AbstractXBeanDeployer extends AbstractDeployer {
041:
042: public AbstractXBeanDeployer(ServiceMixComponent component) {
043: super (component);
044: }
045:
046: protected String getXBeanFile() {
047: return "xbean";
048: }
049:
050: /* (non-Javadoc)
051: * @see org.apache.servicemix.common.Deployer#canDeploy(java.lang.String, java.lang.String)
052: */
053: public boolean canDeploy(String serviceUnitName,
054: String serviceUnitRootPath) {
055: File xbean = new File(serviceUnitRootPath, getXBeanFile()
056: + ".xml");
057: if (logger.isDebugEnabled()) {
058: logger
059: .debug("Looking for " + xbean + ": "
060: + xbean.exists());
061: }
062: return xbean.exists();
063: }
064:
065: /* (non-Javadoc)
066: * @see org.apache.servicemix.common.Deployer#deploy(java.lang.String, java.lang.String)
067: */
068: public ServiceUnit deploy(String serviceUnitName,
069: String serviceUnitRootPath) throws DeploymentException {
070: Kernel kernel = KernelFactory.newInstance().createKernel(
071: component.getComponentName() + "/" + serviceUnitName);
072: try {
073: // Create service unit
074: XBeanServiceUnit su = new XBeanServiceUnit();
075: su.setKernel(kernel);
076: su.setComponent(component);
077: su.setName(serviceUnitName);
078: su.setRootPath(serviceUnitRootPath);
079: // Load configuration
080: ClassLoader classLoader = component.getClass()
081: .getClassLoader();
082: Thread.currentThread().setContextClassLoader(classLoader);
083:
084: SpringLoader springLoader = createSpringLoader();
085: springLoader.setKernel(kernel);
086: springLoader.setBaseDir(new File(serviceUnitRootPath));
087: springLoader
088: .setXmlPreprocessors(getXmlPreProcessors(serviceUnitRootPath));
089: springLoader
090: .setBeanFactoryPostProcessors(getBeanFactoryPostProcessors(serviceUnitRootPath));
091:
092: ServiceName configurationName = springLoader
093: .load(getXBeanFile());
094: kernel.startService(configurationName);
095: su.setConfiguration(configurationName);
096: // Use SU classloader
097: Thread.currentThread().setContextClassLoader(
098: su.getConfigurationClassLoader());
099: // Retrieve endpoints
100: List services = getServices(kernel);
101: if (services == null || services.size() == 0) {
102: throw failure("deploy", "No endpoints found", null);
103: }
104: for (Iterator iter = services.iterator(); iter.hasNext();) {
105: Endpoint endpoint = (Endpoint) iter.next();
106: endpoint.setServiceUnit(su);
107: validate(endpoint);
108: su.addEndpoint(endpoint);
109: }
110: if (su.getEndpoints().size() == 0) {
111: throw failure("deploy", "No endpoint found", null);
112: }
113: return su;
114: } catch (Throwable e) {
115: // There is a chance the thread context classloader has been changed by the xbean kernel,
116: // so put back a good one
117: Thread.currentThread().setContextClassLoader(
118: getClass().getClassLoader());
119: kernel.destroy();
120: if (e instanceof DeploymentException) {
121: throw ((DeploymentException) e);
122: } else {
123: throw failure("deploy",
124: "Could not deploy xbean service unit", e);
125: }
126: } finally {
127: Thread.currentThread().setContextClassLoader(
128: getClass().getClassLoader());
129: }
130: }
131:
132: /**
133: * A factory method to allow derived classes to create alternative spring loaders
134: */
135: protected SpringLoader createSpringLoader() {
136: return new SpringLoader();
137: }
138:
139: protected List getServices(Kernel kernel)
140: throws DeploymentException {
141: return kernel.getServices(Endpoint.class);
142: }
143:
144: protected List getXmlPreProcessors(String serviceUnitRootPath) {
145: JBIContainer container = null;
146: try {
147: container = ((ComponentContextImpl) component
148: .getComponentContext()).getContainer();
149: } catch (Throwable t) {
150: }
151: FileSystemRepository repository = new FileSystemRepository(
152: new File(serviceUnitRootPath));
153: ClassLoaderXmlPreprocessor classLoaderXmlPreprocessor = new ClassLoaderXmlPreprocessor(
154: repository, container);
155: return Collections.singletonList(classLoaderXmlPreprocessor);
156: }
157:
158: protected List getBeanFactoryPostProcessors(
159: String serviceUnitRootPath) {
160: PropertyPlaceholderConfigurer propertyPlaceholder = new PropertyPlaceholderConfigurer();
161: FileSystemResource propertiesFile = new FileSystemResource(
162: new File(serviceUnitRootPath) + "/" + getXBeanFile()
163: + ".properties");
164: if (propertiesFile.getFile().exists()) {
165: propertyPlaceholder.setLocation(propertiesFile);
166: return Collections.singletonList(propertyPlaceholder);
167: }
168: return Collections.EMPTY_LIST;
169: }
170:
171: }
|