KickJava   Java API By Example, From Geeks To Geeks.

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


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

22 package org.objectweb.petals.jbi.management.deployment.deploy;
23
24 import java.io.InputStream JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.management.ObjectName JavaDoc;
31
32 import org.objectweb.petals.jbi.component.lifecycle.ServiceAssemblyLifeCycle;
33 import org.objectweb.petals.jbi.management.deployment.DeploymentContextConstants;
34 import org.objectweb.petals.jbi.management.deployment.DeploymentServiceImpl;
35 import org.objectweb.petals.jbi.management.deployment.DeploymentTaskFactory;
36 import org.objectweb.petals.jbi.management.deployment.DeploymentUtils;
37 import org.objectweb.petals.jbi.management.service.LifeCycleManagerService;
38 import org.objectweb.petals.jbi.management.service.ManagementException;
39 import org.objectweb.petals.jbi.management.service.util.XMLResult;
40 import org.objectweb.petals.jbi.management.systemstate.SystemState;
41 import org.objectweb.petals.processor.Task;
42 import org.objectweb.petals.processor.TaskProcessor;
43 import org.objectweb.petals.repository.RepositoryService;
44 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor;
45 import org.objectweb.petals.tools.jbicommon.descriptor.ServiceUnit;
46 import org.objectweb.petals.util.LoggingUtil;
47
48 /**
49  * This task creates service assembly lifecycle element and for each Service
50  * Unit included into the service assembly, executes the deployment process of a
51  * service unit
52  *
53  * @author ofabre - EBM Websourcing
54  *
55  */

56 public class AllSUDeploymentTask implements Task {
57
58     /**
59      * JBI Container :: Recovery Service
60      */

61     protected SystemState recoverySrv;
62
63     /**
64      * Platform Component :: Repository Service
65      */

66     protected RepositoryService repositorySrv;
67
68     /**
69      * JBI Container :: JMX Agent Admin
70      */

71     protected LifeCycleManagerService managerService;
72
73     protected DeploymentServiceImpl deploymentService;
74
75     /**
76      * logger wrapper
77      */

78     protected LoggingUtil log;
79
80     public AllSUDeploymentTask(SystemState recoverySrv,
81             RepositoryService repositorySrv,
82             LifeCycleManagerService managerService,
83             DeploymentServiceImpl deploymentService, LoggingUtil log) {
84         super();
85         this.recoverySrv = recoverySrv;
86         this.repositorySrv = repositorySrv;
87         this.managerService = managerService;
88         this.deploymentService = deploymentService;
89         this.log = log;
90     }
91
92     @SuppressWarnings JavaDoc("unchecked")
93     public void execute(HashMap JavaDoc context) throws Exception JavaDoc {
94         URI JavaDoc installationRoot = (URI JavaDoc) context
95                 .get(DeploymentContextConstants.SA_INSTALL_ROOT);
96
97         JBIDescriptor descriptor = (JBIDescriptor) context
98                 .get(DeploymentContextConstants.SA_DESCRIPTOR);
99
100         XMLResult xmlResult = (XMLResult) context
101                 .get(DeploymentContextConstants.XML_RESULT);
102
103         Map JavaDoc<String JavaDoc, URI JavaDoc> suInstallRoots = (Map JavaDoc<String JavaDoc, URI JavaDoc>) context
104                 .get(DeploymentContextConstants.SU_INSTALL_ROOTS);
105
106         List JavaDoc<TaskProcessor> suTaskProcessors = (List JavaDoc<TaskProcessor>) context
107                 .get(DeploymentContextConstants.SU_TASK_PROCESSORS);
108
109         /*
110          * Create service assembly lifecycle element
111          */

112         ServiceAssemblyLifeCycle saLifeCycle = createSALifeCycle(
113                 installationRoot, descriptor);
114
115         /*
116          * For each Service Unit included into the service assembly : execute
117          * the deployment process of a service unit
118          */

119         deploySUS(installationRoot, saLifeCycle, descriptor, suInstallRoots,
120                 xmlResult, suTaskProcessors);
121
122         /*
123          * Fill Context
124          */

125         context.put(DeploymentContextConstants.SA_LIFECYCLE, saLifeCycle);
126     }
127
128     /**
129      * * Deploy all Service Units to the JBI container
130      *
131      * @param installationRoot
132      * the root {@link URI} of the service assembly
133      * @param saLifeCycle
134      * the {@link ServiceAssemblyLifeCycle} of the service assembly
135      * @param descriptor
136      * the service assembly jbi descriptor
137      * @param suInstallRoots
138      * a map of all SU install roots
139      * @param xmlResult
140      * xml task result
141      * @throws ManagementException
142      * if an sus deployment fails caused by a severe error of the
143      * container
144      *
145      */

146     protected void deploySUS(URI JavaDoc installationRoot,
147             ServiceAssemblyLifeCycle saLifeCycle, JBIDescriptor descriptor,
148             Map JavaDoc<String JavaDoc, URI JavaDoc> suInstallRoots, XMLResult xmlResult,
149             List JavaDoc<TaskProcessor> suTaskProcessors) throws Exception JavaDoc {
150         List JavaDoc<ServiceUnit> serviceUnits = descriptor.getServiceAssembly()
151                 .getServiceUnits();
152         if (serviceUnits != null) {
153             StringBuffer JavaDoc componentOutput = new StringBuffer JavaDoc();
154             for (ServiceUnit serviceUnit : serviceUnits) {
155
156                 /*
157                  * Create Deployment context
158                  */

159                 HashMap JavaDoc<String JavaDoc, Object JavaDoc> suDeploymentContext = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
160                 suDeploymentContext.put(
161                         DeploymentContextConstants.SA_LIFECYCLE, saLifeCycle);
162                 suDeploymentContext.put(
163                         DeploymentContextConstants.SU_DESCRIPTOR, serviceUnit);
164                 suDeploymentContext.put(
165                         DeploymentContextConstants.SU_INSTALL_ROOT,
166                         suInstallRoots.get(DeploymentUtils
167                                 .getServiceUnitName(serviceUnit)));
168
169                 /*
170                  * Create Deployment tasks
171                  */

172                 DeploymentTaskFactory deploymentTaskFactory = deploymentService
173                         .getDeploymentTaskFactory();
174                 Task suToComponentDeploymentTask = deploymentTaskFactory
175                         .getSuToComponentDeploymentTask();
176                 Task createAndRegisterSULifeCycleTask = deploymentTaskFactory
177                         .getCreateAndRegisterSULifeCycleTask();
178
179                 /*
180                  * Create processor and add tasks
181                  */

182                 TaskProcessor processor = new TaskProcessor(
183                         suDeploymentContext, log);
184                 processor.addTask(suToComponentDeploymentTask);
185                 processor.addTask(createAndRegisterSULifeCycleTask);
186                 if (suTaskProcessors != null) {
187                     suTaskProcessors.add(processor);
188                 }
189
190                 /*
191                  * Launch processor
192                  */

193                 if (!processor.process()) {
194                     String JavaDoc msg = "Error occured during the deployment of a ServiceUnit";
195                     throw new Exception JavaDoc(msg);
196                 }
197                 String JavaDoc xmlReturn = (String JavaDoc) suDeploymentContext
198                         .get(DeploymentContextConstants.COMPONENT_XML_RESULT);
199                 if (!isValidComponentTask(xmlReturn, this.getClass()
200                         .getClassLoader().getResourceAsStream(
201                                 "schema/management-message-component.xsd"))) {
202                     xmlReturn = xmlResult.wrapComponentTaskResult(serviceUnit
203                             .getTargetComponentName(), "deploy", xmlReturn);
204                 }
205                 componentOutput.append(xmlReturn);
206             }
207             xmlResult.addComponentTaskResult(componentOutput);
208         }
209     }
210
211     /**
212      * Creates a new {@link ServiceAssemblyLifeCycle}
213      *
214      * @param installationRoot
215      * the root {@link URI} of the service assembly into the
216      * repository
217      * @param descriptor
218      * the service assembly jbi descriptor
219      * @return a new {@link ServiceAssemblyLifeCycle}
220      * @throws ManagementException
221      * if deployment service MBean name retrieval fails
222      */

223     protected ServiceAssemblyLifeCycle createSALifeCycle(URI JavaDoc installationRoot,
224             JBIDescriptor descriptor) throws ManagementException {
225         // Retrieve the DeploymentServiceImpl ObjectName
226
ObjectName JavaDoc objectName = managerService.getDeploymentServiceMBeanName();
227
228         return new ServiceAssemblyLifeCycle(objectName, log, descriptor
229                 .getServiceAssembly(), repositorySrv, recoverySrv,
230                 installationRoot);
231
232     }
233
234     /**
235      * Check if a component task result is valid against an xml schema
236      *
237      * @param componentTask
238      * xml of the component task
239      * @param xsd
240      * an {@link InputStream} of the xsd file
241      * @return true if valid, false if not valid
242      */

243     protected boolean isValidComponentTask(String JavaDoc componentTask, InputStream JavaDoc xsd) {
244         return XMLResult.isValidComponentTask(componentTask, xsd);
245     }
246
247     @SuppressWarnings JavaDoc("unchecked")
248     public void undo(HashMap JavaDoc context) {
249         List JavaDoc<TaskProcessor> suTaskProcessors = (List JavaDoc<TaskProcessor>) context
250                 .get(DeploymentContextConstants.SU_TASK_PROCESSORS);
251
252         if (suTaskProcessors != null) {
253             for (TaskProcessor processor : suTaskProcessors) {
254                 processor.unprocess();
255             }
256         }
257     }
258
259 }
260
Popular Tags