KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > management > deployment > undeploy > AllSUUndeploymentTask


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: AllSUUndeploymentTask.java 154 10 oct. 06 ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.management.deployment.undeploy;
23
24 import java.io.InputStream JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28
29 import javax.jbi.component.ServiceUnitManager;
30 import javax.jbi.management.DeploymentException;
31
32 import org.objectweb.petals.jbi.component.lifecycle.ComponentLifeCycle;
33 import org.objectweb.petals.jbi.component.lifecycle.LifeCycleAbstract;
34 import org.objectweb.petals.jbi.component.lifecycle.ServiceAssemblyLifeCycle;
35 import org.objectweb.petals.jbi.component.lifecycle.ServiceUnitLifeCycle;
36 import org.objectweb.petals.jbi.management.deployment.DeploymentContextConstants;
37 import org.objectweb.petals.jbi.management.deployment.DeploymentUtils;
38 import org.objectweb.petals.jbi.management.service.LifeCycleManagerService;
39 import org.objectweb.petals.jbi.management.service.util.XMLResult;
40 import org.objectweb.petals.processor.Task;
41 import org.objectweb.petals.tools.jbicommon.descriptor.ServiceUnit;
42 import org.objectweb.petals.util.LoggingUtil;
43
44 /**
45  * This task executes the undeployment process of a service unit, for each
46  * Service Unit included into the service assembly.
47  *
48  * @author ofabre - EBM Websourcing
49  *
50  */

51 public class AllSUUndeploymentTask implements Task {
52
53     /**
54      * JBI Container :: JMX Agent Admin
55      */

56     protected LifeCycleManagerService managerService;
57
58     /**
59      * logger wrapper
60      */

61     protected LoggingUtil log;
62
63     public AllSUUndeploymentTask(LifeCycleManagerService managerService,
64             LoggingUtil log) {
65         super();
66         this.managerService = managerService;
67         this.log = log;
68     }
69
70     public void execute(HashMap JavaDoc context) throws Exception JavaDoc {
71         ServiceAssemblyLifeCycle saLifeCycle = (ServiceAssemblyLifeCycle) context
72                 .get(DeploymentContextConstants.SA_LIFECYCLE);
73
74         XMLResult xmlResult = (XMLResult) context
75                 .get(DeploymentContextConstants.XML_RESULT);
76
77         /*
78          * Undeploy sus
79          */

80         undeploySUS(saLifeCycle, xmlResult);
81
82     }
83
84     /**
85      * Undeploy all SUs by calling the "undeploySU" method of the corresponding
86      * SUDeploymentManager.
87      *
88      * @param saLifeCycle
89      * the parent {@link ServiceAssemblyLifeCycle}
90      * @param xmlResult
91      * xml task result
92      * @throws Exception
93      * if an error occurs during the undeployment of a service unit
94      */

95     protected void undeploySUS(ServiceAssemblyLifeCycle saLifeCycle,
96             XMLResult xmlResult) throws Exception JavaDoc {
97         /*
98          * Put the service assembly in the shutdown state
99          */

100         String JavaDoc saState = saLifeCycle.getCurrentState();
101         if (LifeCycleAbstract.STARTED.equals(saState)) {
102             saLifeCycle.stop();
103             saLifeCycle.shutDown();
104         } else if (LifeCycleAbstract.STOPPED.equals(saState)) {
105             saLifeCycle.shutDown();
106         }
107         saState = saLifeCycle.getCurrentState();
108         if (saState.equals(LifeCycleAbstract.SHUTDOWN)) {
109             /*
110              * The service assembly is in the SHUTDOWN state We can try to
111              * undeploy it
112              */

113
114             /*
115              * Jbi spec specifies that service units must be undeployed in the
116              * reverse order of the declared list in service assembly descriptor
117              */

118             List JavaDoc<ServiceUnit> suList = saLifeCycle.getServiceAssembly()
119                     .getServiceUnits();
120             Collections.reverse(suList);
121             StringBuffer JavaDoc componentOutput = new StringBuffer JavaDoc();
122             for (ServiceUnit suDesc : suList) {
123                 String JavaDoc xmlReturn = undeploySU(saLifeCycle, suDesc);
124                 if (!isValidComponentTask(xmlReturn, this.getClass()
125                         .getClassLoader().getResourceAsStream(
126                                 "schema/management-message-component.xsd"))) {
127                     xmlReturn = xmlResult.wrapComponentTaskResult(suDesc
128                             .getTargetComponentName(), "undeploy", xmlReturn);
129                 }
130                 componentOutput.append(xmlReturn);
131             }
132             xmlResult.addComponentTaskResult(componentOutput);
133         }
134     }
135
136     /**
137      * Process the undeployment of the related service unit.
138      *
139      * @param saLifeCycle
140      * the service assembly life cycle
141      * @param serviceUnit
142      * the service unit production element
143      * @return a {@link String} xml result of the service unit undeployment (see
144      * section 6.9 of the JSR 208)
145      * @throws Exception
146      * if an error occurs during the undeployment process : can't
147      * retrieve the {@link ComponentLifeCycle}, can't retrieve the
148      * component {@link ServiceUnitManager} (SUM) or an exception is
149      * thrown during the call of the undeploy method of the SUM
150      */

151     protected String JavaDoc undeploySU(ServiceAssemblyLifeCycle saLifeCycle,
152             ServiceUnit serviceUnit) throws Exception JavaDoc {
153         String JavaDoc msg = "";
154         String JavaDoc xmlReturn = null;
155
156         /*
157          * Undeploy it
158          */

159         String JavaDoc suName = DeploymentUtils.getServiceUnitName(serviceUnit);
160         ServiceUnitLifeCycle serviceUnitLifeCycle = saLifeCycle
161                 .getServiceUnitsLifeCycles().get(suName);
162         String JavaDoc targetedComponent = serviceUnit.getTargetComponentName();
163         // Retrieve the component life cycle
164
ComponentLifeCycle clc = managerService
165                 .getComponentByName(targetedComponent);
166         if (clc == null) {
167             msg = "The component ("
168                     + targetedComponent
169                     + ") is not deployed. The service unit cannot be undeployed.";
170             log.error(msg);
171             throw new Exception JavaDoc(msg);
172         }
173         // Retrieve the service unit manager of the component
174
ServiceUnitManager sum = clc.getComponent().getServiceUnitManager();
175         if (sum == null) {
176             msg = "The service unit manager is null for the component ("
177                     + targetedComponent
178                     + "). You may need to initialise the component. The service units cannot be undeployed.";
179             log.error(msg);
180             throw new Exception JavaDoc(msg);
181         }
182
183         try {
184             xmlReturn = sum.undeploy(suName, serviceUnitLifeCycle
185                     .getServiceUnitRootPath());
186         } catch (DeploymentException e) {
187             msg = "Service unit manager of the component (" + targetedComponent
188                     + ") failed to undeploy su (" + suName + ")";
189             log.error(msg, e);
190             throw new Exception JavaDoc(msg, e);
191         }
192
193         /*
194          * Remove the service unit from the service assembly
195          */

196         saLifeCycle.getServiceUnitsLifeCycles().remove(suName);
197
198         return xmlReturn;
199     }
200
201     /**
202      * Check if a component task result is valid against an xml schema
203      *
204      * @param componentTask
205      * xml of the component task
206      * @param xsd
207      * an {@link InputStream} of the xsd file
208      * @return true if valid, false if not valid
209      */

210     protected boolean isValidComponentTask(String JavaDoc componentTask, InputStream JavaDoc xsd) {
211         return XMLResult.isValidComponentTask(componentTask, xsd);
212     }
213
214     public void undo(HashMap JavaDoc context) throws Exception JavaDoc {
215         // Nothing to do
216

217     }
218
219 }
220
Popular Tags