KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > framework > ServiceUnitLifeCycle


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.framework;
18
19 import java.beans.PropertyChangeEvent JavaDoc;
20 import java.beans.PropertyChangeListener JavaDoc;
21 import java.io.File JavaDoc;
22
23 import javax.jbi.component.ServiceUnitManager;
24 import javax.jbi.management.DeploymentException;
25 import javax.management.JMException JavaDoc;
26 import javax.management.MBeanAttributeInfo JavaDoc;
27 import javax.management.MBeanOperationInfo JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.servicemix.jbi.deployment.Descriptor;
32 import org.apache.servicemix.jbi.deployment.DescriptorFactory;
33 import org.apache.servicemix.jbi.deployment.ServiceUnit;
34 import org.apache.servicemix.jbi.deployment.Services;
35 import org.apache.servicemix.jbi.event.ServiceUnitEvent;
36 import org.apache.servicemix.jbi.event.ServiceUnitListener;
37 import org.apache.servicemix.jbi.management.AttributeInfoHelper;
38 import org.apache.servicemix.jbi.management.MBeanInfoProvider;
39 import org.apache.servicemix.jbi.management.OperationInfoHelper;
40
41 public class ServiceUnitLifeCycle implements ServiceUnitMBean, MBeanInfoProvider {
42
43     private static final Log log = LogFactory.getLog(ServiceUnitLifeCycle.class);
44
45     private ServiceUnit serviceUnit;
46
47     private String JavaDoc currentState = SHUTDOWN;
48     
49     private String JavaDoc serviceAssembly;
50     
51     private Registry registry;
52
53     private PropertyChangeListener JavaDoc listener;
54     
55     private Services services;
56     
57     private File JavaDoc rootDir;
58     
59     public ServiceUnitLifeCycle(ServiceUnit serviceUnit,
60                                 String JavaDoc serviceAssembly,
61                                 Registry registry,
62                                 File JavaDoc rootDir) {
63         this.serviceUnit = serviceUnit;
64         this.serviceAssembly = serviceAssembly;
65         this.registry = registry;
66         this.rootDir = rootDir;
67         Descriptor d = DescriptorFactory.buildDescriptor(getServiceUnitRootPath());
68         if (d != null) {
69             services = d.getServices();
70         }
71     }
72
73     /**
74      * Initialize the service unit.
75      * @throws DeploymentException
76      */

77     public void init() throws DeploymentException {
78         log.info("Initializing service unit: " + getName());
79         checkComponentStarted("init");
80         ServiceUnitManager sum = getServiceUnitManager();
81         File JavaDoc path = getServiceUnitRootPath();
82         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
83         try {
84             Thread.currentThread().setContextClassLoader(getComponentClassLoader());
85             sum.init(getName(), path.getAbsolutePath());
86         } finally {
87             Thread.currentThread().setContextClassLoader(cl);
88         }
89         currentState = STOPPED;
90     }
91     
92     /**
93      * Start the service unit.
94      * @throws DeploymentException
95      */

96     public void start() throws DeploymentException {
97         log.info("Starting service unit: " + getName());
98         checkComponentStarted("start");
99         ServiceUnitManager sum = getServiceUnitManager();
100         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
101         try {
102             Thread.currentThread().setContextClassLoader(getComponentClassLoader());
103             sum.start(getName());
104         } finally {
105             Thread.currentThread().setContextClassLoader(cl);
106         }
107         currentState = STARTED;
108     }
109
110     /**
111      * Stop the service unit. This suspends current messaging activities.
112      * @throws DeploymentException
113      */

114     public void stop() throws DeploymentException {
115         log.info("Stopping service unit: " + getName());
116         checkComponentStarted("stop");
117         ServiceUnitManager sum = getServiceUnitManager();
118         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
119         try {
120             Thread.currentThread().setContextClassLoader(getComponentClassLoader());
121             sum.stop(getName());
122         } finally {
123             Thread.currentThread().setContextClassLoader(cl);
124         }
125         currentState = STOPPED;
126     }
127
128     /**
129      * Shut down the service unit.
130      * This releases resources, preparatory to uninstallation.
131      * @throws DeploymentException
132      */

133     public void shutDown() throws DeploymentException {
134         log.info("Shutting down service unit: " + getName());
135         checkComponentStartedOrStopped("shutDown");
136         ServiceUnitManager sum = getServiceUnitManager();
137         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
138         try {
139             Thread.currentThread().setContextClassLoader(getComponentClassLoader());
140             sum.shutDown(getName());
141         } finally {
142             Thread.currentThread().setContextClassLoader(cl);
143         }
144         currentState = SHUTDOWN;
145     }
146
147     /**
148      * @return the currentState as a String
149      */

150     public String JavaDoc getCurrentState() {
151         return currentState;
152     }
153
154     public boolean isShutDown() {
155         return currentState.equals(SHUTDOWN);
156     }
157
158     public boolean isStopped() {
159         return currentState.equals(STOPPED);
160     }
161
162     public boolean isStarted() {
163         return currentState.equals(STARTED);
164     }
165     
166     /**
167      * @return the name of the ServiceAssembly
168      */

169     public String JavaDoc getName() {
170         return serviceUnit.getIdentification().getName();
171     }
172
173     /**
174      * @return the description of the ServiceAssembly
175      */

176     public String JavaDoc getDescription() {
177         return serviceUnit.getIdentification().getDescription();
178     }
179     
180     public String JavaDoc getComponentName() {
181         return serviceUnit.getTarget().getComponentName();
182     }
183
184     public String JavaDoc getServiceAssembly() {
185         return serviceAssembly;
186     }
187
188     public String JavaDoc getDescriptor() {
189         File JavaDoc suDir = getServiceUnitRootPath();
190         return DescriptorFactory.getDescriptorAsText(suDir);
191     }
192     
193     public Services getServices() {
194         return services;
195     }
196
197     protected void checkComponentStarted(String JavaDoc task) throws DeploymentException {
198         String JavaDoc componentName = getComponentName();
199         String JavaDoc suName = getName();
200         ComponentMBeanImpl lcc = registry.getComponent(componentName);
201         if (lcc == null) {
202             throw ManagementSupport.componentFailure("deploy", componentName, "Target component " + componentName + " for service unit " + suName + " is not installed");
203         }
204         if (!lcc.isStarted()) {
205             throw ManagementSupport.componentFailure("deploy", componentName, "Target component " + componentName + " for service unit " + suName + " is not started");
206         }
207         if (lcc.getServiceUnitManager() == null) {
208             throw ManagementSupport.componentFailure("deploy", componentName, "Target component " + componentName + " for service unit " + suName + " does not accept deployments");
209         }
210     }
211     
212     protected void checkComponentStartedOrStopped(String JavaDoc task) throws DeploymentException {
213         String JavaDoc componentName = getComponentName();
214         String JavaDoc suName = getName();
215         ComponentMBeanImpl lcc = registry.getComponent(componentName);
216         if (lcc == null) {
217             throw ManagementSupport.componentFailure("deploy", componentName, "Target component " + componentName + " for service unit " + suName + " is not installed");
218         }
219         if (!lcc.isStarted() && !lcc.isStopped()) {
220             throw ManagementSupport.componentFailure("deploy", componentName, "Target component " + componentName + " for service unit " + suName + " is not started");
221         }
222         if (lcc.getServiceUnitManager() == null) {
223             throw ManagementSupport.componentFailure("deploy", componentName, "Target component " + componentName + " for service unit " + suName + " does not accept deployments");
224         }
225     }
226     
227     protected File JavaDoc getServiceUnitRootPath() {
228         return rootDir;
229     }
230     
231     protected ServiceUnitManager getServiceUnitManager() {
232         ComponentMBeanImpl lcc = registry.getComponent(getComponentName());
233         return lcc.getServiceUnitManager();
234     }
235
236     protected ClassLoader JavaDoc getComponentClassLoader() {
237         ComponentMBeanImpl lcc = registry.getComponent(getComponentName());
238         // TODO: should retrieve the real component class loader
239
return lcc.getComponent().getClass().getClassLoader();
240     }
241
242     public MBeanAttributeInfo JavaDoc[] getAttributeInfos() throws JMException JavaDoc {
243         AttributeInfoHelper helper = new AttributeInfoHelper();
244         helper.addAttribute(getObjectToManage(), "currentState", "current state of the service unit");
245         helper.addAttribute(getObjectToManage(), "name", "name of the service unit");
246         helper.addAttribute(getObjectToManage(), "componentName", "component name of the service unit");
247         helper.addAttribute(getObjectToManage(), "serviceAssembly", "service assembly name of the service unit");
248         helper.addAttribute(getObjectToManage(), "description", "description of the service unit");
249         return helper.getAttributeInfos();
250     }
251
252     public MBeanOperationInfo JavaDoc[] getOperationInfos() throws JMException JavaDoc {
253         OperationInfoHelper helper = new OperationInfoHelper();
254         helper.addOperation(getObjectToManage(), "getDescriptor", "retrieve the jbi descriptor for this unit");
255         return helper.getOperationInfos();
256     }
257
258     public Object JavaDoc getObjectToManage() {
259         return this;
260     }
261
262     public String JavaDoc getType() {
263         return "ServiceUnit";
264     }
265
266     public String JavaDoc getSubType() {
267         return getComponentName();
268     }
269
270     public void setPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
271         this.listener = listener;
272     }
273
274     protected void firePropertyChanged(String JavaDoc name,Object JavaDoc oldValue, Object JavaDoc newValue){
275         PropertyChangeListener JavaDoc l = listener;
276         if (l != null){
277             PropertyChangeEvent JavaDoc event = new PropertyChangeEvent JavaDoc(this,name,oldValue,newValue);
278             l.propertyChange(event);
279         }
280     }
281
282     public String JavaDoc getKey() {
283         return getComponentName() + "/" + getName();
284     }
285
286     protected void fireEvent(int type) {
287         ServiceUnitEvent event = new ServiceUnitEvent(this, type);
288         ServiceUnitListener[] listeners = (ServiceUnitListener[]) registry.getContainer().getListeners(ServiceUnitListener.class);
289         for (int i = 0; i < listeners.length; i++) {
290             switch (type) {
291             case ServiceUnitEvent.UNIT_STARTED:
292                 listeners[i].unitStarted(event);
293                 break;
294             case ServiceUnitEvent.UNIT_STOPPED:
295                 listeners[i].unitStopped(event);
296                 break;
297             case ServiceUnitEvent.UNIT_SHUTDOWN:
298                 listeners[i].unitShutDown(event);
299                 break;
300             }
301         }
302     }
303
304 }
305
Popular Tags