KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > management > deployment > deploy > SUToComponentDeploymentTask


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: SUToComponentDeploymentTask.java 154 6 oct. 06 ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.management.deployment.deploy;
23
24 import java.net.URI JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 import javax.jbi.component.ServiceUnitManager;
28 import javax.jbi.management.DeploymentException;
29 import javax.jbi.management.LifeCycleMBean;
30
31 import org.objectweb.petals.jbi.component.lifecycle.ComponentLifeCycle;
32 import org.objectweb.petals.jbi.management.deployment.DeploymentContextConstants;
33 import org.objectweb.petals.jbi.management.deployment.DeploymentUtils;
34 import org.objectweb.petals.jbi.management.service.LifeCycleManagerService;
35 import org.objectweb.petals.jbi.management.service.ManagementException;
36 import org.objectweb.petals.processor.Task;
37 import org.objectweb.petals.tools.jbicommon.descriptor.ServiceUnit;
38 import org.objectweb.petals.util.LoggingUtil;
39
40 /**
41  * This task Deploies service unit to the targeted component.
42  *
43  * @author ofabre - EBM Websourcing
44  *
45  */

46 public class SUToComponentDeploymentTask implements Task {
47
48     /**
49      * logger wrapper
50      */

51     protected LoggingUtil log;
52
53     /**
54      * JBI Container :: JMX Agent Admin
55      */

56     protected LifeCycleManagerService managerService;
57
58     public SUToComponentDeploymentTask(LoggingUtil log,
59         LifeCycleManagerService managerService) {
60         super();
61         this.log = log;
62         this.managerService = managerService;
63     }
64
65     @SuppressWarnings JavaDoc("unchecked")
66     public void execute(HashMap JavaDoc context) throws Exception JavaDoc {
67
68         URI JavaDoc installationRoot = (URI JavaDoc) context
69             .get(DeploymentContextConstants.SU_INSTALL_ROOT);
70
71         ServiceUnit serviceUnit = (ServiceUnit) context
72             .get(DeploymentContextConstants.SU_DESCRIPTOR);
73
74         String JavaDoc xmlResult = deploySUtoComponent(installationRoot, serviceUnit);
75
76         /*
77          * Fill context
78          */

79         context.put(DeploymentContextConstants.COMPONENT_XML_RESULT, xmlResult);
80
81     }
82
83     /**
84      * Deploy the given service unit to the targeted component. The component
85      * must be in the STARTED state to allow deployment of service units.
86      *
87      * @param installationRoot
88      * the su installation root {@link URI}
89      * @param serviceUnit
90      * the service unit production element
91      * @return a {@link String} xml result of the service unit deployment on the
92      * targeted component (see section 6.9 of the JSR 208)
93      * @throws ManagementException
94      * if component and related object are missing, if the call of
95      * the service unit manager method "deploy" fails or if the
96      * component isn't in the STARTED state
97      */

98     protected String JavaDoc deploySUtoComponent(URI JavaDoc installationRoot,
99         ServiceUnit serviceUnit) throws ManagementException {
100
101         /*
102          * Retrieve the target component life cycle
103          */

104         String JavaDoc targetedComponent = DeploymentUtils
105             .getServiceUnitTargetedComponent(serviceUnit);
106         ComponentLifeCycle clc = managerService
107             .getComponentByName(targetedComponent);
108         if (clc == null) {
109             String JavaDoc msg = "The service unit can not be deployed, as the component '"
110                 + targetedComponent + "' is not installed.";
111             log.error(msg);
112             throw new ManagementException(msg);
113         }
114
115         /*
116          * The component must be in the STARTED state to allow deployment of
117          * service units
118          */

119         if (!LifeCycleMBean.STARTED.equals(clc.getCurrentState())) {
120             String JavaDoc msg = "Component "
121                 + targetedComponent
122                 + " isn't started : you can't deploy service units on a not started component !";
123             log.error(msg);
124             throw new ManagementException(msg);
125         }
126
127         /*
128          * Retrieve the service unit manager of the component that allow to
129          * process deployment
130          */

131         ServiceUnitManager sum = clc.getComponent().getServiceUnitManager();
132         if (sum == null) {
133             String JavaDoc msg = "Component "
134                 + targetedComponent
135                 + " doesn't support deployment : service unit manager is null !";
136             log.error(msg);
137             throw new ManagementException(msg);
138         }
139
140         String JavaDoc xmlReturn = null;
141         try {
142             xmlReturn = sum.deploy(DeploymentUtils
143                 .getServiceUnitName(serviceUnit), installationRoot.getPath());
144         } catch (DeploymentException e) {
145             String JavaDoc msg = "Deployement of the service unit "
146                 + DeploymentUtils.getServiceUnitName(serviceUnit)
147                 + " has failed (caused by a component \"" + targetedComponent
148                 + "\" su manager exception).";
149             log.error(msg, e);
150             throw new ManagementException(msg, e);
151         }
152         return xmlReturn;
153     }
154
155     public void undo(HashMap JavaDoc context) {
156         URI JavaDoc installationRoot = (URI JavaDoc) context
157             .get(DeploymentContextConstants.SU_INSTALL_ROOT);
158
159         ServiceUnit serviceUnit = (ServiceUnit) context
160             .get(DeploymentContextConstants.SU_DESCRIPTOR);
161
162         try {
163             undeploySUFromComponent(installationRoot, serviceUnit);
164         } catch (ManagementException e) {
165             String JavaDoc msg = "Failed to revert a SUToComponentDeploymentTask";
166             log.error(msg, e);
167         }
168     }
169
170     /**
171      * Undeploy the given service unit from the targeted component. The
172      * component must be in the STARTED state to allow deployment of service
173      * units.
174      *
175      * @param installationRoot
176      * the su installation root {@link URI}
177      * @param serviceUnit
178      * the service unit production element
179      * @return a {@link String} xml result of the service unit deployment on the
180      * targeted component (see section 6.9 of the JSR 208)
181      * @throws ManagementException
182      * if component and related object are missing, if the call of
183      * the service unit manager method "undeploy" fails or if the
184      * component isn't in the STARTED state
185      */

186     public String JavaDoc undeploySUFromComponent(URI JavaDoc installationRoot,
187         ServiceUnit serviceUnit) throws ManagementException {
188         /*
189          * Retrieve the target component life cycle
190          */

191         String JavaDoc targetedComponent = DeploymentUtils
192             .getServiceUnitTargetedComponent(serviceUnit);
193         ComponentLifeCycle clc = managerService
194             .getComponentByName(targetedComponent);
195         if (clc == null) {
196             String JavaDoc msg = "The component lifecycle element for service unit deployment must be non null.";
197             log.error(msg);
198             throw new ManagementException(msg);
199         }
200
201         /*
202          * The component must be in the STARTED state to allow deployment of
203          * service units
204          */

205         if (!LifeCycleMBean.STARTED.equals(clc.getCurrentState())) {
206             String JavaDoc msg = "Component "
207                 + targetedComponent
208                 + " isn't started : you can not deploy a service unit on a component that has not been started.";
209             log.error(msg);
210             throw new ManagementException(msg);
211         }
212
213         /*
214          * Retrieve the service unit manager of the component that allow to
215          * process deployment
216          */

217         ServiceUnitManager sum = clc.getComponent().getServiceUnitManager();
218         if (sum == null) {
219             String JavaDoc msg = "Component "
220                 + targetedComponent
221                 + " doesn't support deployment : service unit manager is null !";
222             log.error(msg);
223             throw new ManagementException(msg);
224         }
225
226         String JavaDoc xmlReturn = null;
227         try {
228             xmlReturn = sum.undeploy(DeploymentUtils
229                 .getServiceUnitName(serviceUnit), installationRoot.getPath());
230         } catch (DeploymentException e) {
231             String JavaDoc msg = "Undeployement of the service unit "
232                 + DeploymentUtils.getServiceUnitName(serviceUnit)
233                 + " has failed (caused by a targeted component \""
234                 + DeploymentUtils.getServiceUnitName(serviceUnit)
235                 + "\" su manager exception).";
236             log.error(msg, e);
237             throw new ManagementException(msg, e);
238         }
239         return xmlReturn;
240     }
241 }
242
Popular Tags