KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > common > xbean > AbstractXBeanDeployer


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.common.xbean;
18
19 import java.io.File JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
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 JavaDoc getXBeanFile() {
46         return "xbean";
47     }
48
49     /* (non-Javadoc)
50      * @see org.apache.servicemix.common.Deployer#canDeploy(java.lang.String, java.lang.String)
51      */

52     public boolean canDeploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath) {
53         File JavaDoc xbean = new File JavaDoc(serviceUnitRootPath, getXBeanFile() + ".xml");
54         if (logger.isDebugEnabled()) {
55             logger.debug("Looking for " + xbean + ": " + xbean.exists());
56         }
57         return xbean.exists();
58     }
59
60     /* (non-Javadoc)
61      * @see org.apache.servicemix.common.Deployer#deploy(java.lang.String, java.lang.String)
62      */

63     public ServiceUnit deploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath) throws DeploymentException {
64         Kernel kernel = KernelFactory.newInstance().createKernel(component.getComponentName() + "/" + serviceUnitName);
65         try {
66             // Create service unit
67
XBeanServiceUnit su = new XBeanServiceUnit();
68             su.setKernel(kernel);
69             su.setComponent(component);
70             su.setName(serviceUnitName);
71             su.setRootPath(serviceUnitRootPath);
72             // Load configuration
73
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
74
75             SpringLoader springLoader = new SpringLoader();
76             springLoader.setKernel(kernel);
77             springLoader.setBaseDir(new File JavaDoc(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             // Retrieve endpoints
85
List JavaDoc services = getServices(kernel);
86             if (services == null || services.size() == 0) {
87                 throw failure("deploy", "No endpoints found", null);
88             }
89             for (Iterator JavaDoc 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 JavaDoc e) {
103             // There is a chance the thread context classloader has been changed by the xbean kernel,
104
// so put back a good one
105
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 JavaDoc 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 JavaDoc getXmlPreProcessors(String JavaDoc serviceUnitRootPath) {
126         FileSystemRepository repository = new FileSystemRepository(new File JavaDoc(serviceUnitRootPath));
127         ClassLoaderXmlPreprocessor classLoaderXmlPreprocessor = new ClassLoaderXmlPreprocessor(repository);
128         return Collections.singletonList(classLoaderXmlPreprocessor);
129     }
130     
131     protected List JavaDoc getBeanFactoryPostProcessors(String JavaDoc serviceUnitRootPath) {
132         PropertyPlaceholderConfigurer propertyPlaceholder = new PropertyPlaceholderConfigurer();
133         FileSystemResource propertiesFile = new FileSystemResource(
134                 new File JavaDoc(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