KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > common > BaseServiceUnitManager


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;
18
19 import org.apache.commons.logging.Log;
20
21 import javax.jbi.component.ServiceUnitManager;
22 import javax.jbi.management.DeploymentException;
23 import javax.jbi.management.LifeCycleMBean;
24
25 /**
26  * A simple service unit manager.
27  * This service unit manager uses {@link Deployer} objects
28  * to handle different type of service units.
29  *
30  * @author Guillaume Nodet
31  * @version $Revision: 426415 $
32  * @since 3.0
33  */

34 public class BaseServiceUnitManager implements ServiceUnitManager {
35
36     protected final transient Log logger;
37     
38     protected BaseComponent component;
39     
40     protected Deployer[] deployers;
41     
42     protected boolean persistent;
43     
44     public BaseServiceUnitManager(BaseComponent component, Deployer[] deployers) {
45         this(component, deployers, false);
46     }
47
48     public BaseServiceUnitManager(BaseComponent component, Deployer[] deployers, boolean persistent) {
49         this.component = component;
50         this.logger = component.logger;
51         this.deployers = deployers;
52         this.persistent = persistent;
53     }
54     
55     /* (non-Javadoc)
56      * @see javax.jbi.component.ServiceUnitManager#deploy(java.lang.String, java.lang.String)
57      */

58     public synchronized String JavaDoc deploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath) throws DeploymentException {
59         try {
60             if (logger.isDebugEnabled()) {
61                 logger.debug("Deploying service unit");
62             }
63             if (serviceUnitName == null || serviceUnitName.length() == 0) {
64                 throw new IllegalArgumentException JavaDoc("serviceUnitName should be non null and non empty");
65             }
66             if (getServiceUnit(serviceUnitName) != null) {
67                 throw failure("deploy", "Service Unit '" + serviceUnitName + "' is already deployed", null);
68             }
69             ServiceUnit su = doDeploy(serviceUnitName, serviceUnitRootPath);
70             if (su == null) {
71                 throw failure("deploy", "Unable to find suitable deployer for Service Unit '" + serviceUnitName + "'", null);
72             }
73             component.getRegistry().registerServiceUnit(su);
74             if (logger.isDebugEnabled()) {
75                 logger.debug("Service unit deployed");
76             }
77             return createSuccessMessage("deploy");
78         } catch (DeploymentException e) {
79             throw e;
80         } catch (Exception JavaDoc e) {
81             throw failure("deploy", "Unable to deploy service unit", e);
82         }
83     }
84     
85     protected ServiceUnit doDeploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath) throws Exception JavaDoc {
86         for (int i = 0; i < deployers.length; i++) {
87             if (deployers[i].canDeploy(serviceUnitName, serviceUnitRootPath)) {
88                 return deployers[i].deploy(serviceUnitName, serviceUnitRootPath);
89             }
90         }
91         return null;
92     }
93
94     /* (non-Javadoc)
95      * @see javax.jbi.component.ServiceUnitManager#init(java.lang.String, java.lang.String)
96      */

97     public synchronized void init(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath) throws DeploymentException {
98         try {
99             if (logger.isDebugEnabled()) {
100                 logger.debug("Initializing service unit");
101             }
102             if (serviceUnitName == null || serviceUnitName.length() == 0) {
103                 throw new IllegalArgumentException JavaDoc("serviceUnitName should be non null and non empty");
104             }
105             if (getServiceUnit(serviceUnitName) == null) {
106                 if (!persistent) {
107                     ServiceUnit su = doDeploy(serviceUnitName, serviceUnitRootPath);
108                     if (su == null) {
109                         throw failure("deploy", "Unable to find suitable deployer for Service Unit '" + serviceUnitName + "'", null);
110                     }
111                     component.getRegistry().registerServiceUnit(su);
112                 } else {
113                     throw failure("init", "Service Unit '" + serviceUnitName + "' is not deployed", null);
114                 }
115             }
116             doInit(serviceUnitName, serviceUnitRootPath);
117             if (logger.isDebugEnabled()) {
118                 logger.debug("Service unit initialized");
119             }
120         } catch (DeploymentException e) {
121             throw e;
122         } catch (Exception JavaDoc e) {
123             throw failure("init", "Unable to init service unit", e);
124         }
125     }
126
127     protected void doInit(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath) throws Exception JavaDoc {
128     }
129
130     /* (non-Javadoc)
131      * @see javax.jbi.component.ServiceUnitManager#start(java.lang.String)
132      */

133     public synchronized void start(String JavaDoc serviceUnitName) throws DeploymentException {
134         try {
135             if (logger.isDebugEnabled()) {
136                 logger.debug("Starting service unit");
137             }
138             if (serviceUnitName == null || serviceUnitName.length() == 0) {
139                 throw new IllegalArgumentException JavaDoc("serviceUnitName should be non null and non empty");
140             }
141             ServiceUnit su = (ServiceUnit) getServiceUnit(serviceUnitName);
142             if (su == null) {
143                 throw failure("start", "Service Unit '" + serviceUnitName + "' is not deployed", null);
144             }
145             if (!LifeCycleMBean.STOPPED.equals(su.getCurrentState()) &&
146                 !LifeCycleMBean.SHUTDOWN.equals(su.getCurrentState())) {
147                 throw failure("start", "ServiceUnit should be in a SHUTDOWN or STOPPED state", null);
148             }
149             su.start();
150             if (logger.isDebugEnabled()) {
151                 logger.debug("Service unit started");
152             }
153         } catch (DeploymentException e) {
154             throw e;
155         } catch (Exception JavaDoc e) {
156             throw failure("start", "Unable to start service unit", e);
157         }
158     }
159
160     /* (non-Javadoc)
161      * @see javax.jbi.component.ServiceUnitManager#stop(java.lang.String)
162      */

163     public synchronized void stop(String JavaDoc serviceUnitName) throws DeploymentException {
164         try {
165             if (logger.isDebugEnabled()) {
166                 logger.debug("Stopping service unit");
167             }
168             if (serviceUnitName == null || serviceUnitName.length() == 0) {
169                 throw new IllegalArgumentException JavaDoc("serviceUnitName should be non null and non empty");
170             }
171             ServiceUnit su = (ServiceUnit) getServiceUnit(serviceUnitName);
172             if (su == null) {
173                 throw failure("stop", "Service Unit '" + serviceUnitName + "' is not deployed", null);
174             }
175             if (!LifeCycleMBean.STARTED.equals(su.getCurrentState())) {
176                 throw failure("stop", "ServiceUnit should be in a SHUTDOWN state", null);
177             }
178             su.stop();
179             if (logger.isDebugEnabled()) {
180                 logger.debug("Service unit stopped");
181             }
182         } catch (DeploymentException e) {
183             throw e;
184         } catch (Exception JavaDoc e) {
185             throw failure("stop", "Unable to stop service unit", e);
186         }
187     }
188
189     /* (non-Javadoc)
190      * @see javax.jbi.component.ServiceUnitManager#shutDown(java.lang.String)
191      */

192     public synchronized void shutDown(String JavaDoc serviceUnitName) throws DeploymentException {
193         try {
194             if (logger.isDebugEnabled()) {
195                 logger.debug("Shutting down service unit");
196             }
197             if (serviceUnitName == null || serviceUnitName.length() == 0) {
198                 throw new IllegalArgumentException JavaDoc("serviceUnitName should be non null and non empty");
199             }
200             ServiceUnit su = (ServiceUnit) getServiceUnit(serviceUnitName);
201             if (su == null) {
202                 throw failure("shutDown", "Service Unit '" + serviceUnitName + "' is not deployed", null);
203             }
204             su.shutDown();
205             if (logger.isDebugEnabled()) {
206                 logger.debug("Service unit shut down");
207             }
208         } catch (DeploymentException e) {
209             throw e;
210         } catch (Exception JavaDoc e) {
211             throw failure("shutDown", "Unable to shutdown service unit", e);
212         }
213     }
214
215     /* (non-Javadoc)
216      * @see javax.jbi.component.ServiceUnitManager#undeploy(java.lang.String, java.lang.String)
217      */

218     public synchronized String JavaDoc undeploy(String JavaDoc serviceUnitName, String JavaDoc serviceUnitRootPath) throws DeploymentException {
219         try {
220             if (logger.isDebugEnabled()) {
221                 logger.debug("Undeploying service unit");
222             }
223             if (logger.isDebugEnabled()) {
224                 logger.debug("Shutting down service unit");
225             }
226             if (serviceUnitName == null || serviceUnitName.length() == 0) {
227                 throw new IllegalArgumentException JavaDoc("serviceUnitName should be non null and non empty");
228             }
229             ServiceUnit su = (ServiceUnit) getServiceUnit(serviceUnitName);
230             if (su == null) {
231                 throw failure("undeploy", "Service Unit '" + serviceUnitName + "' is not deployed", null);
232             }
233             if (!LifeCycleMBean.SHUTDOWN.equals(su.getCurrentState())) {
234                 throw failure("undeploy", "ServiceUnit should be in a SHUTDOWN state", null);
235             }
236             doUndeploy(su);
237             component.getRegistry().unregisterServiceUnit(su);
238             if (logger.isDebugEnabled()) {
239                 logger.debug("Service unit undeployed");
240             }
241             return createSuccessMessage("undeploy");
242         } catch (DeploymentException e) {
243             throw e;
244         } catch (Exception JavaDoc e) {
245             throw failure("undeploy", "Unable to undeploy service unit", e);
246         }
247     }
248
249     protected void doUndeploy(ServiceUnit su) throws Exception JavaDoc {
250         for (int i = 0; i < deployers.length; i++) {
251             if (deployers[i].canDeploy(su.getName(), su.getRootPath())) {
252                 deployers[i].undeploy(su);
253                 return;
254             }
255         }
256         throw failure("undeploy", "Unable to find suitable deployer for Service Unit '" + su.getName() + "'", null);
257     }
258     
259     protected DeploymentException failure(String JavaDoc task, String JavaDoc info, Exception JavaDoc e) throws DeploymentException {
260         ManagementSupport.Message msg = new ManagementSupport.Message();
261         msg.setComponent(component.getComponentName());
262         msg.setTask(task);
263         msg.setResult("FAILED");
264         msg.setType("ERROR");
265         msg.setException(e);
266         msg.setMessage(info);
267         return new DeploymentException(ManagementSupport.createComponentMessage(msg));
268     }
269
270     protected String JavaDoc createSuccessMessage(String JavaDoc task) {
271         ManagementSupport.Message msg = new ManagementSupport.Message();
272         msg.setComponent(component.getComponentName());
273         msg.setTask(task);
274         msg.setResult("SUCCESS");
275         return ManagementSupport.createComponentMessage(msg);
276     }
277     
278     protected ServiceUnit getServiceUnit(String JavaDoc name) {
279         return component.getRegistry().getServiceUnit(name);
280     }
281     
282 }
283
Popular Tags