KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jsr181 > Jsr181XBeanDeployer


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.jsr181;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.jbi.management.DeploymentException;
25
26 import org.apache.servicemix.common.BaseComponent;
27 import org.apache.servicemix.common.BaseLifeCycle;
28 import org.apache.servicemix.common.Endpoint;
29 import org.apache.servicemix.common.xbean.AbstractXBeanDeployer;
30 import org.apache.servicemix.common.xbean.XBeanServiceUnit;
31 import org.springframework.beans.BeansException;
32 import org.springframework.beans.factory.BeanFactory;
33 import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
34 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
35 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
36 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
37
38 public class Jsr181XBeanDeployer extends AbstractXBeanDeployer {
39
40     public Jsr181XBeanDeployer(BaseComponent component) {
41         super(component);
42     }
43
44     protected boolean validate(Endpoint endpoint) throws DeploymentException {
45         if (endpoint instanceof Jsr181Endpoint == false) {
46             throw failure("deploy", "Endpoint should be a Jsr181 endpoint", null);
47         }
48         Jsr181Endpoint ep = (Jsr181Endpoint) endpoint;
49         if (ep.getPojo() == null && ep.getPojoClass() == null) {
50             throw failure("deploy", "Endpoint must have a non-null pojo or a pojoClass", null);
51         }
52         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
53         try {
54             ClassLoader JavaDoc cl = ((XBeanServiceUnit)ep.getServiceUnit()).getConfigurationClassLoader();
55             Thread.currentThread().setContextClassLoader(cl);
56             if (logger.isDebugEnabled()) {
57                 logger.debug("Registering endpoint with context classloader " + cl);
58             }
59             ep.registerService();
60         } catch (Exception JavaDoc e) {
61             throw failure("deploy", "Could not register endpoint", e);
62         } finally {
63             Thread.currentThread().setContextClassLoader(old);
64         }
65         return true;
66     }
67
68     protected List JavaDoc getBeanFactoryPostProcessors(String JavaDoc serviceUnitRootPath) {
69         List JavaDoc processors = new ArrayList JavaDoc(super.getBeanFactoryPostProcessors(serviceUnitRootPath));
70         processors.add(new BeanFactoryPostProcessor() {
71             public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException {
72                 Map JavaDoc beans = new HashMap JavaDoc();
73                 beans.put("context", new EndpointComponentContext(((BaseLifeCycle) component.getLifeCycle()).getContext()));
74                 BeanFactory parent = new SimpleBeanFactory(beans);
75                 factory.setParentBeanFactory(parent);
76             }
77         });
78         return processors;
79     }
80     
81     private static class SimpleBeanFactory implements BeanFactory {
82         private final Map JavaDoc beans;
83         public SimpleBeanFactory(Map JavaDoc beans) {
84             this.beans = beans;
85         }
86         public boolean containsBean(String JavaDoc name) {
87             return beans.containsKey(name);
88         }
89         public String JavaDoc[] getAliases(String JavaDoc name) throws NoSuchBeanDefinitionException {
90             Object JavaDoc bean = beans.get(name);
91             if (bean == null) {
92                 throw new NoSuchBeanDefinitionException(name);
93             }
94             return new String JavaDoc[0];
95         }
96         public Object JavaDoc getBean(String JavaDoc name) throws BeansException {
97             return getBean(name, null);
98         }
99         public Object JavaDoc getBean(String JavaDoc name, Class JavaDoc requiredType) throws BeansException {
100             Object JavaDoc bean = beans.get(name);
101             if (bean == null) {
102                 throw new NoSuchBeanDefinitionException(name);
103             }
104             if (requiredType != null && !requiredType.isInstance(bean)) {
105                 throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
106             }
107             return bean;
108         }
109         public Class JavaDoc getType(String JavaDoc name) throws NoSuchBeanDefinitionException {
110             Object JavaDoc bean = beans.get(name);
111             if (bean == null) {
112                 throw new NoSuchBeanDefinitionException(name);
113             }
114             return bean.getClass();
115         }
116         public boolean isSingleton(String JavaDoc name) throws NoSuchBeanDefinitionException {
117             Object JavaDoc bean = beans.get(name);
118             if (bean == null) {
119                 throw new NoSuchBeanDefinitionException(name);
120             }
121             return true;
122         }
123     }
124
125 }
126
Popular Tags