KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > container > SpringJBIContainer


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.jbi.container;
18
19 import javax.jbi.JBIException;
20 import javax.jbi.component.ServiceUnitManager;
21 import javax.resource.spi.work.WorkManager JavaDoc;
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 /**
36  * An enhanced JBI container which adds some Spring helper methods for
37  * easier configuration through spring's XML configuration file.
38  *
39  * @org.apache.xbean.XBean element="container" rootElement="true"
40  * description="The ServiceMix JBI Container"
41  *
42  * @version $Revision: 426415 $
43  */

44 public class SpringJBIContainer extends JBIContainer
45     implements InitializingBean, DisposableBean, BeanFactoryAware, ApplicationContextAware {
46     
47     private String JavaDoc[] componentNames;
48     private ActivationSpec[] activationSpecs;
49     private BeanFactory beanFactory;
50     private ApplicationContext applicationContext;
51     private String JavaDoc[] deployArchives;
52     private Object JavaDoc shutdownLock;
53
54     public void afterPropertiesSet() throws Exception JavaDoc {
55         init();
56         
57         // lets iterate through all the component names and register them
58
if (componentNames != null) {
59             for (int i = 0; i < componentNames.length; i++) {
60                 String JavaDoc 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 JavaDoc 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 JavaDoc e) {
89                 throw new JBIException("Failed to dispose of the Spring BeanFactory due to: " + e, e);
90             }
91         }
92         super.stop();
93     }
94
95     /**
96      * Returns the compoment or POJO registered with the given component ID.
97      *
98      * @param id
99      * @return the Component
100      */

101     public Object JavaDoc getBean(String JavaDoc id) {
102         ComponentMBeanImpl component = getComponent(id);
103         Object JavaDoc bean = component != null ? component.getComponent() : null;
104         if (bean instanceof ComponentAdaptor) {
105             bean = ((ComponentAdaptor) bean).getLifeCycle();
106         }
107         return bean;
108     }
109
110
111     // Properties
112
//-------------------------------------------------------------------------
113
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 JavaDoc[] getComponentNames() {
122         return componentNames;
123     }
124
125     public void setComponentNames(String JavaDoc[] 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 JavaDoc[] getDeployArchives() {
146         return deployArchives;
147     }
148
149     public void setDeployArchives(String JavaDoc[] deployArchives) {
150         this.deployArchives = deployArchives;
151     }
152
153     // Implementation methods
154
//-------------------------------------------------------------------------
155
protected Object JavaDoc lookupBean(String JavaDoc componentName) {
156         Object JavaDoc bean = beanFactory.getBean(componentName);
157         if (bean == null) {
158             throw new IllegalArgumentException JavaDoc("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 JavaDoc createWorkManager() throws JBIException {
173         WorkManagerFactoryBean factory = new WorkManagerFactoryBean();
174         factory.setApplicationContext(applicationContext);
175         try {
176             return factory.getWorkManager();
177         }
178         catch (Exception JavaDoc e) {
179             throw new JBIException("Failed to start WorkManager: " + e, e);
180         }
181     }
182
183     public void destroy() throws Exception JavaDoc {
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 JavaDoc lock) {
196         this.shutdownLock = lock;
197     }
198
199 }
200
Popular Tags