KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > management > deployment > DeploymentServiceImpl


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 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: DeploymentServiceImpl.java 154 2006-03-27 15:30:10Z ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.management.deployment;
23
24 import java.net.URI JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import javax.jbi.management.AdminServiceMBean;
34 import javax.jbi.management.LifeCycleMBean;
35
36 import org.objectweb.fractal.fraclet.annotation.FractalComponent;
37 import org.objectweb.fractal.fraclet.annotation.Interface;
38 import org.objectweb.fractal.fraclet.annotation.LifeCycle;
39 import org.objectweb.fractal.fraclet.annotation.LifeCycleType;
40 import org.objectweb.fractal.fraclet.annotation.Monolog;
41 import org.objectweb.fractal.fraclet.annotation.Provides;
42 import org.objectweb.fractal.fraclet.annotation.Requires;
43 import org.objectweb.petals.PetalsException;
44 import org.objectweb.petals.jbi.component.lifecycle.ComponentLifeCycle;
45 import org.objectweb.petals.jbi.component.lifecycle.LifeCycleAbstract;
46 import org.objectweb.petals.jbi.component.lifecycle.ServiceAssemblyLifeCycle;
47 import org.objectweb.petals.jbi.management.service.EndpointService;
48 import org.objectweb.petals.jbi.management.service.LifeCycleManagerService;
49 import org.objectweb.petals.jbi.management.service.PackageHandler;
50 import org.objectweb.petals.jbi.management.service.util.XMLResult;
51 import org.objectweb.petals.jbi.management.service.util.XMLResult.CauseFramework;
52 import org.objectweb.petals.jbi.management.service.util.XMLResult.MessageType;
53 import org.objectweb.petals.jbi.management.service.util.XMLResult.TaskResult;
54 import org.objectweb.petals.jbi.management.systemstate.SystemState;
55 import org.objectweb.petals.processor.TaskProcessor;
56 import org.objectweb.petals.repository.RepositoryService;
57 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptorBuilder;
58 import org.objectweb.petals.tools.jbicommon.descriptor.ServiceUnit;
59 import org.objectweb.petals.util.LoggingUtil;
60 import org.objectweb.petals.util.ParameterCheckHelper;
61 import org.objectweb.util.monolog.api.Logger;
62
63 /**
64  * JBI deployment service implementation. TODO map recovery service with fractal
65  *
66  * @author Olivier Fabre - EBM Websourcing
67  * @author ddesjardins - eBMWebsourcing
68  * @author wjoseph - eBMWebsourcing
69  */

70 @FractalComponent
71 @Provides(interfaces = @Interface(name = "service", signature = org.objectweb.petals.jbi.management.deployment.DeploymentService.class))
72 public class DeploymentServiceImpl implements DeploymentService {
73
74     /**
75      * JBI Container :: Admin Service
76      */

77     @Requires(name = "admin", signature = javax.jbi.management.AdminServiceMBean.class)
78     protected AdminServiceMBean adminSrv;
79
80     /**
81      * List of already deployed service assembly.
82      */

83     protected Map JavaDoc<String JavaDoc, ServiceAssemblyDataHandler> deployedSACache;
84
85     @Requires(name = "endpoint-service", signature = org.objectweb.petals.jbi.management.service.EndpointService.class)
86     protected EndpointService endpointService;
87
88     /**
89      * logger wrapper
90      */

91     protected LoggingUtil log;
92
93     /**
94      * Monolog Logger instance.
95      */

96     @Monolog(name = "logger")
97     protected Logger logger;
98
99     /**
100      * JBI Container :: JMX Agent Admin
101      */

102     @Requires(name = "lifecyclemanager", signature = org.objectweb.petals.jbi.management.service.LifeCycleManagerService.class)
103     protected LifeCycleManagerService managerService;
104
105     /**
106      * Package Handler
107      */

108     protected PackageHandler packageHandler;
109
110     /**
111      * JBI Container :: Recovery Service
112      */

113     @Requires(name = "systemstate", signature = org.objectweb.petals.jbi.management.systemstate.SystemState.class)
114     protected SystemState recoverySrv;
115
116     /**
117      * Platform Component :: Repository Service
118      */

119     @Requires(name = "repository", signature = org.objectweb.petals.repository.RepositoryService.class)
120     protected RepositoryService repositorySrv;
121
122     protected DeploymentTaskFactory deploymentTaskFactory;
123
124     /**
125      * Default constructor.
126      */

127     public DeploymentServiceImpl() {
128     }
129
130     /*
131      * (non-Javadoc)
132      *
133      * @see javax.jbi.management.DeploymentServiceMBean#canDeployToComponent(java.lang.String)
134      */

135     public boolean canDeployToComponent(String JavaDoc componentName) {
136         ParameterCheckHelper.isNullParameterWithLog(componentName,
137                 "componentName", log);
138         ParameterCheckHelper.isEmptyParameterWithLog(componentName,
139                 "componentName", log);
140         boolean result = false;
141         ComponentLifeCycle clc = managerService
142                 .getComponentByName(componentName);
143         result = clc != null
144                 && LifeCycleAbstract.STARTED.equals(clc.getCurrentState())
145                 && clc.getComponent().getServiceUnitManager() != null;
146         return result;
147     }
148
149     /*
150      * (non-Javadoc)
151      *
152      * @see javax.jbi.management.DeploymentServiceMBean#deploy(java.lang.String)
153      */

154     public String JavaDoc deploy(String JavaDoc serviceAssemblyZipUrl) throws Exception JavaDoc {
155         log.start();
156         String JavaDoc result = null;
157
158         /*
159          * Checking parameters
160          */

161         ParameterCheckHelper.isNullParameterWithLog(serviceAssemblyZipUrl,
162                 "serviceAssemblyZipUrl", log);
163         ParameterCheckHelper.isEmptyParameterWithLog(serviceAssemblyZipUrl,
164                 "serviceAssemblyZipUrl", log);
165
166         try {
167
168             /*
169              * Create deployment execution context
170              */

171             HashMap JavaDoc<String JavaDoc, Object JavaDoc> deploymentContext = createDeploymentContext(serviceAssemblyZipUrl);
172
173             /*
174              * Create deployment processor and fill with tasks
175              */

176             TaskProcessor processor = createDeploymentProcessor(deploymentContext);
177
178             /*
179              * Launch the process
180              */

181             if (!processor.process()) {
182                 String JavaDoc msg = "An error occured during the deployment "
183                         + "of the following service assembly: "
184                         + serviceAssemblyZipUrl;
185                 throw new Exception JavaDoc(msg);
186             }
187
188             /*
189              * Create the string result
190              */

191             result = ((XMLResult) deploymentContext
192                     .get(DeploymentContextConstants.XML_RESULT)).getString();
193
194         } catch (Throwable JavaDoc e) {
195             // Catch all throwables to prevent exotic exception type (not easily
196
// deserializable) throwing
197
log.error(e.getMessage(), e);
198             throw new Exception JavaDoc(e.getMessage());
199         }
200
201         //log.info(result);
202
log.end();
203         return result;
204     }
205
206     /**
207      * Create the deployment processor and add all required task for the
208      * complete deployment of a service assembly
209      *
210      * @param deploymentContext
211      * the deployment context
212      * @return the filled deployment processor
213      */

214     protected TaskProcessor createDeploymentProcessor(
215             HashMap JavaDoc<String JavaDoc, Object JavaDoc> deploymentContext) {
216         TaskProcessor processor = new TaskProcessor(deploymentContext, log);
217
218         processor.addTask(deploymentTaskFactory
219                 .getDescriptorAndURIRetrievalTask());
220
221         processor.addTask(deploymentTaskFactory.getPackageCheckingTask());
222
223         processor.addTask(deploymentTaskFactory.getResultCreationTask());
224         processor.addTask(deploymentTaskFactory.getExtractionTask());
225         processor.addTask(deploymentTaskFactory
226                 .getAllConnectionRegistrationTask());
227
228         processor.addTask(deploymentTaskFactory.getAllSUDeploymentTask());
229         processor.addTask(deploymentTaskFactory
230                 .getSaLifeCycleRegistrationTask());
231
232         processor.addTask(deploymentTaskFactory.getStateHolderCreationTask());
233         processor.addTask(deploymentTaskFactory
234                 .getCopyArchiveToInstalledDirTask());
235
236         processor.addTask(deploymentTaskFactory.getDeploymentSuccessTask());
237
238         return processor;
239     }
240
241     /**
242      * Create the deployment context
243      *
244      * @param serviceAssemblyZipUrl
245      * String containing the location URL of the Service Assembly ZIP
246      * file; must be non-null, non-empty, and a legal URL.
247      * @return the Deployment Context HashMap
248      */

249     protected HashMap JavaDoc<String JavaDoc, Object JavaDoc> createDeploymentContext(
250             String JavaDoc serviceAssemblyZipUrl) {
251         HashMap JavaDoc<String JavaDoc, Object JavaDoc> deploymentContext = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
252         deploymentContext.put(DeploymentContextConstants.ARCHIVE_URL,
253                 serviceAssemblyZipUrl);
254         return deploymentContext;
255     }
256
257     /*
258      * (non-Javadoc)
259      *
260      * @see javax.jbi.management.DeploymentServiceMBean#getComponentsForDeployedServiceAssembly(java.lang.String)
261      */

262     public String JavaDoc[] getComponentsForDeployedServiceAssembly(
263             String JavaDoc serviceAssemblyName) throws Exception JavaDoc {
264         ParameterCheckHelper.isNullParameterWithLog(serviceAssemblyName,
265                 "serviceAssemblyName", log);
266         ParameterCheckHelper.isEmptyParameterWithLog(serviceAssemblyName,
267                 "serviceAssemblyName", log);
268         List JavaDoc<String JavaDoc> result = new ArrayList JavaDoc<String JavaDoc>();
269         ServiceAssemblyLifeCycle saLifeCycle = managerService
270                 .getServiceAssemblyByName(serviceAssemblyName);
271         if (saLifeCycle != null) {
272             List JavaDoc<ServiceUnit> sus = saLifeCycle.getServiceAssembly()
273                     .getServiceUnits();
274             for (ServiceUnit unit : sus) {
275                 result.add(unit.getTargetComponentName());
276             }
277         } else {
278             String JavaDoc msg = "Cannot retrieve service assembly with the specified name : "
279                     + serviceAssemblyName;
280             log.error(msg);
281             throw new Exception JavaDoc(msg);
282         }
283         return result.toArray(new String JavaDoc[0]);
284     }
285
286     public Map JavaDoc<String JavaDoc, ServiceAssemblyDataHandler> getDeployedSACache() {
287         return deployedSACache;
288     }
289
290     /*
291      * (non-Javadoc)
292      *
293      * @see javax.jbi.management.DeploymentServiceMBean#getDeployedServiceAssemblies()
294      */

295     public String JavaDoc[] getDeployedServiceAssemblies() throws Exception JavaDoc {
296         Set JavaDoc<String JavaDoc> sas = managerService.getServiceAssemblyLifeCycles()
297                 .keySet();
298         return sas.toArray(new String JavaDoc[0]);
299     }
300
301     /*
302      * (non-Javadoc)
303      *
304      * @see javax.jbi.management.DeploymentServiceMBean#getDeployedServiceAssembliesForComponent(java.lang.String)
305      */

306     public String JavaDoc[] getDeployedServiceAssembliesForComponent(
307             String JavaDoc componentName) throws Exception JavaDoc {
308         ParameterCheckHelper.isNullParameterWithLog(componentName,
309                 "componentName", log);
310         ParameterCheckHelper.isEmptyParameterWithLog(componentName,
311                 "componentName", log);
312         Set JavaDoc<String JavaDoc> result = new HashSet JavaDoc<String JavaDoc>();
313         Map JavaDoc<String JavaDoc, ServiceAssemblyLifeCycle> saLifeCycles = managerService
314                 .getServiceAssemblyLifeCycles();
315         for (ServiceAssemblyLifeCycle salc : saLifeCycles.values()) {
316             List JavaDoc<ServiceUnit> sus = salc.getServiceAssembly().getServiceUnits();
317             for (ServiceUnit unit : sus) {
318                 if (componentName.equals(unit.getTargetComponentName())) {
319                     result.add(salc.getServiceAssembly().getIdentification()
320                             .getName());
321                 }
322             }
323         }
324         return result.toArray(new String JavaDoc[0]);
325     }
326
327     /*
328      * (non-Javadoc)
329      *
330      * @see javax.jbi.management.DeploymentServiceMBean#getDeployedServiceUnitList(java.lang.String)
331      */

332     public String JavaDoc[] getDeployedServiceUnitList(String JavaDoc componentName)
333             throws Exception JavaDoc {
334         ParameterCheckHelper.isNullParameterWithLog(componentName,
335                 "componentName", log);
336         ParameterCheckHelper.isEmptyParameterWithLog(componentName,
337                 "componentName", log);
338         Set JavaDoc<String JavaDoc> result = new HashSet JavaDoc<String JavaDoc>();
339         Map JavaDoc<String JavaDoc, ServiceAssemblyLifeCycle> saLifeCycles = managerService
340                 .getServiceAssemblyLifeCycles();
341         for (ServiceAssemblyLifeCycle salc : saLifeCycles.values()) {
342             List JavaDoc<ServiceUnit> sus = salc.getServiceAssembly().getServiceUnits();
343             for (ServiceUnit unit : sus) {
344                 if (componentName.equals(unit.getTargetComponentName())) {
345                     result.add(unit.getIdentification().getName());
346                 }
347             }
348         }
349         return result.toArray(new String JavaDoc[0]);
350     }
351
352     public String JavaDoc getServiceAssemblyDescriptor(String JavaDoc serviceAssemblyName)
353             throws Exception JavaDoc {
354         ParameterCheckHelper.isNullParameterWithLog(serviceAssemblyName,
355                 "serviceAssemblyName", log);
356         ParameterCheckHelper.isEmptyParameterWithLog(serviceAssemblyName,
357                 "serviceAssemblyName", log);
358         String JavaDoc result = "Service Assembly doesn't exist :"
359                 + serviceAssemblyName;
360         ServiceAssemblyLifeCycle saLifeCycle = managerService
361                 .getServiceAssemblyByName(serviceAssemblyName);
362         if (saLifeCycle != null) {
363             URI JavaDoc installationRoot = saLifeCycle.getInstallationRoot();
364             // Load JBI descriptor as string
365
URI JavaDoc jbiXmlUri = installationRoot
366                     .resolve(JBIDescriptorBuilder.JBI_DESCRIPTOR_RESOURCE
367                             .replace('\\', '/'));
368             result = getJbiDescriptorAsString(jbiXmlUri);
369         }
370         return result;
371     }
372
373     /*
374      * (non-Javadoc)
375      *
376      * @see javax.jbi.management.DeploymentServiceMBean#getState(java.lang.String)
377      */

378     public String JavaDoc getState(String JavaDoc serviceAssemblyName) throws Exception JavaDoc {
379         ParameterCheckHelper.isNullParameterWithLog(serviceAssemblyName,
380                 "serviceAssemblyName", log);
381         ParameterCheckHelper.isEmptyParameterWithLog(serviceAssemblyName,
382                 "serviceAssemblyName", log);
383         String JavaDoc result = null;
384         String JavaDoc msg;
385         ServiceAssemblyLifeCycle saLifeCycle = managerService
386                 .getServiceAssemblyByName(serviceAssemblyName);
387         if (saLifeCycle != null) {
388             result = saLifeCycle.getCurrentState();
389         } else {
390             msg = "Cannot retrieve service "
391                     + "assembly with the specified name : "
392                     + serviceAssemblyName;
393             log.error(msg);
394             throw new Exception JavaDoc(msg);
395         }
396         if (!(LifeCycleMBean.SHUTDOWN.equals(result)
397                 || LifeCycleMBean.STARTED.equals(result) || LifeCycleMBean.STOPPED
398                 .equals(result))) {
399             msg = "Service Assembly state isn't correctly defined";
400             log.error(msg);
401             throw new Exception JavaDoc(msg);
402         }
403         return result;
404     }
405
406     /*
407      * (non-Javadoc)
408      *
409      * @see javax.jbi.management.DeploymentServiceMBean#isDeployedServiceUnit(java.lang.String,
410      * java.lang.String)
411      */

412     public boolean isDeployedServiceUnit(String JavaDoc componentName,
413             String JavaDoc serviceUnitName) throws Exception JavaDoc {
414         ParameterCheckHelper.isNullParameterWithLog(serviceUnitName,
415                 "serviceUnitName", log);
416         ParameterCheckHelper.isEmptyParameterWithLog(serviceUnitName,
417                 "serviceUnitName", log);
418         ParameterCheckHelper.isNullParameterWithLog(componentName,
419                 "componentName", log);
420         ParameterCheckHelper.isEmptyParameterWithLog(componentName,
421                 "componentName", log);
422         boolean result = false;
423         Map JavaDoc<String JavaDoc, ServiceAssemblyLifeCycle> saLifeCycles = managerService
424                 .getServiceAssemblyLifeCycles();
425         for (Iterator JavaDoc iter = saLifeCycles.values().iterator(); iter.hasNext()
426                 && !result;) {
427             ServiceAssemblyLifeCycle salc = (ServiceAssemblyLifeCycle) iter
428                     .next();
429             List JavaDoc<ServiceUnit> sus = salc.getServiceAssembly().getServiceUnits();
430             for (Iterator JavaDoc iter2 = sus.iterator(); iter2.hasNext() && !result;) {
431                 ServiceUnit unit = (ServiceUnit) iter2.next();
432                 if (componentName.equals(unit.getTargetComponentName())
433                         && serviceUnitName.equals(unit.getIdentification()
434                                 .getName())) {
435                     result = true;
436                 }
437             }
438         }
439         return result;
440     }
441
442     /*
443      * (non-Javadoc)
444      *
445      * @see javax.jbi.management.DeploymentServiceMBean#shutDown(java.lang.String)
446      */

447     public String JavaDoc shutDown(String JavaDoc serviceAssemblyName) throws Exception JavaDoc {
448         ParameterCheckHelper.isNullParameterWithLog(serviceAssemblyName,
449                 "serviceAssemblyName", log);
450         ParameterCheckHelper.isEmptyParameterWithLog(serviceAssemblyName,
451                 "serviceAssemblyName", log);
452         log.start();
453
454         String JavaDoc msg = null;
455         XMLResult xmlResult = new XMLResult("shutdown");
456
457         ServiceAssemblyLifeCycle saLifeCycle = managerService
458                 .getServiceAssemblyByName(serviceAssemblyName);
459         if (saLifeCycle != null) {
460             try {
461                 saLifeCycle.shutDown();
462             } catch (Throwable JavaDoc e) {
463                 msg = e.getMessage();
464                 log.error(msg, e);
465                 throw new Exception JavaDoc(msg);
466             }
467         } else {
468             msg = "The service assembly \"" + serviceAssemblyName
469                     + "\" is not deployed";
470             throw new Exception JavaDoc(msg);
471         }
472         xmlResult.addFrameworkTaskResult(TaskResult.SUCCESS, MessageType.INFO,
473                 "Successfully shutdown service assembly : {1}",
474                 new String JavaDoc[] { serviceAssemblyName }, CauseFramework.YES);
475         String JavaDoc output = xmlResult.getString();
476         try {
477             XMLResult.validateManagementMessage(output, this.getClass()
478                     .getClassLoader().getResourceAsStream(
479                             "schema/management-message.xsd"));
480         } catch (Exception JavaDoc e1) {
481             log.error("Problem while validating management message", e1);
482         }
483         log.info(serviceAssemblyName+" successfully shutdown");
484         log.end();
485         return output;
486     }
487
488     /*
489      * (non-Javadoc)
490      *
491      * @see javax.jbi.management.DeploymentServiceMBean#start(java.lang.String)
492      */

493     public String JavaDoc start(String JavaDoc serviceAssemblyName) throws Exception JavaDoc {
494         ParameterCheckHelper.isNullParameterWithLog(serviceAssemblyName,
495                 "serviceAssemblyName", log);
496         ParameterCheckHelper.isEmptyParameterWithLog(serviceAssemblyName,
497                 "serviceAssemblyName", log);
498         log.start();
499
500         String JavaDoc msg = null;
501         XMLResult xmlResult = new XMLResult("start");
502
503         ServiceAssemblyLifeCycle saLifeCycle = managerService
504                 .getServiceAssemblyByName(serviceAssemblyName);
505         if (saLifeCycle != null) {
506             try {
507                 saLifeCycle.start();
508             } catch (Exception JavaDoc e) {
509                 e.printStackTrace();
510                 msg = e.getMessage();
511                 throw new Exception JavaDoc(msg);
512             }
513         } else {
514             msg = "The service assembly \"" + serviceAssemblyName
515                     + "\" is not deployed";
516             throw new Exception JavaDoc(msg);
517         }
518         xmlResult.addFrameworkTaskResult(TaskResult.SUCCESS, MessageType.INFO,
519                 "Successfully started service assembly : {1}",
520                 new String JavaDoc[] { serviceAssemblyName }, CauseFramework.YES);
521         String JavaDoc output = xmlResult.getString();
522         try {
523             XMLResult.validateManagementMessage(output, this.getClass()
524                     .getClassLoader().getResourceAsStream(
525                             "schema/management-message.xsd"));
526         } catch (Exception JavaDoc e1) {
527             log.error("Problem while validating management message", e1);
528         }
529
530         log.info(serviceAssemblyName+" successfully started");
531         log.end();
532         return output;
533     }
534
535     /*
536      * (non-Javadoc)
537      *
538      * @see javax.jbi.management.DeploymentServiceMBean#stop(java.lang.String)
539      */

540     public String JavaDoc stop(String JavaDoc serviceAssemblyName) throws Exception JavaDoc {
541         ParameterCheckHelper.isNullParameterWithLog(serviceAssemblyName,
542                 "serviceAssemblyName", log);
543         ParameterCheckHelper.isEmptyParameterWithLog(serviceAssemblyName,
544                 "serviceAssemblyName", log);
545         log.start();
546
547         String JavaDoc msg = null;
548         XMLResult xmlResult = new XMLResult("stop");
549
550         ServiceAssemblyLifeCycle saLifeCycle = managerService
551                 .getServiceAssemblyByName(serviceAssemblyName);
552         if (saLifeCycle != null) {
553             try {
554                 saLifeCycle.stop();
555             } catch (Throwable JavaDoc e) {
556                 msg = e.getMessage();
557                 log.error(msg, e);
558                 throw new Exception JavaDoc(msg);
559             }
560         } else {
561             msg = "The service assembly \"" + serviceAssemblyName
562                     + "\" is not deployed";
563             throw new Exception JavaDoc(msg);
564         }
565         xmlResult.addFrameworkTaskResult(TaskResult.SUCCESS, MessageType.INFO,
566                 "Successfully stopped service assembly : {1}",
567                 new String JavaDoc[] { serviceAssemblyName }, CauseFramework.YES);
568         String JavaDoc output = xmlResult.getString();
569         try {
570             XMLResult.validateManagementMessage(output, this.getClass()
571                     .getClassLoader().getResourceAsStream(
572                             "schema/management-message.xsd"));
573         } catch (Exception JavaDoc e1) {
574             log.error("Problem while validating management message", e1);
575         }
576
577         log.info(serviceAssemblyName+" successfully stoped");
578         log.end();
579         return output;
580     }
581
582     /*
583      * (non-Javadoc)
584      *
585      * @see javax.jbi.management.DeploymentServiceMBean#undeploy(java.lang.String)
586      */

587     public String JavaDoc undeploy(String JavaDoc serviceAssemblyName) throws Exception JavaDoc {
588         log.start();
589
590         String JavaDoc output = "";
591
592         ParameterCheckHelper.isNullParameterWithLog(serviceAssemblyName,
593                 "serviceAssemblyZipUrl", log);
594         ParameterCheckHelper.isEmptyParameterWithLog(serviceAssemblyName,
595                 "serviceAssemblyZipUrl", log);
596         try {
597
598             /*
599              * Create undeployment execution context
600              */

601             HashMap JavaDoc<String JavaDoc, Object JavaDoc> undeploymentContext = createUndeploymentContext(serviceAssemblyName);
602
603             /*
604              * Create undeployment processor and fill with tasks
605              */

606             TaskProcessor processor = createUndeploymentProcessor(undeploymentContext);
607
608             /*
609              * Launch the process
610              */

611             if (!processor.process()) {
612                 String JavaDoc msg = "An error occured during the undeployment "
613                         + "of the following service assembly: "
614                         + serviceAssemblyName;
615                 throw new Exception JavaDoc(msg);
616             }
617
618             /*
619              * Create the string result
620              */

621             output = ((XMLResult) undeploymentContext
622                     .get(DeploymentContextConstants.XML_RESULT)).getString();
623
624         } catch (Throwable JavaDoc e) {
625             // Catch all throwables to prevent exotic exception type (not easily
626
// deserializable) throwing
627
log.error(e.getMessage(), e);
628             throw new Exception JavaDoc(e.getMessage());
629         }
630
631         log.info(serviceAssemblyName+" successfully undeployed");
632         log.end();
633         return output;
634     }
635
636     /**
637      * Create the undeployment processor and add all required task for the
638      * complete undeployment of a service assembly
639      *
640      * @param undeploymentContext
641      * the deployment context
642      * @return filled undeployment processor
643      */

644     protected TaskProcessor createUndeploymentProcessor(
645             HashMap JavaDoc<String JavaDoc, Object JavaDoc> undeploymentContext) {
646         TaskProcessor processor = new TaskProcessor(undeploymentContext, log);
647
648         processor.addTask(deploymentTaskFactory
649                 .getXmlUndeploymentResultCreationTask());
650         processor.addTask(deploymentTaskFactory.getSaLifeCycleRetrievalTask());
651         processor.addTask(deploymentTaskFactory.getAllSUUndeploymentTask());
652         processor.addTask(deploymentTaskFactory.getAllConnectionsRemovalTask());
653         processor.addTask(deploymentTaskFactory
654                 .getSaLifeCycleUnregistrationTask());
655         processor.addTask(deploymentTaskFactory.getSaStateHolderRemovalTask());
656         processor.addTask(deploymentTaskFactory.getSaCacheRemovalTask());
657         processor.addTask(deploymentTaskFactory.getSaFolderRemovalTask());
658         processor.addTask(deploymentTaskFactory.getDeleteArchiveFromInstalledDirTask());
659
660         return processor;
661     }
662
663     /**
664      * Create the undeployment context
665      *
666      * @param serviceAssemblyName
667      * the service assembly name
668      * @return the undeployment context
669      * @throws PetalsException
670      * if error occured during JBI descriptor loading
671      */

672     protected HashMap JavaDoc<String JavaDoc, Object JavaDoc> createUndeploymentContext(
673             String JavaDoc serviceAssemblyName) throws PetalsException {
674         HashMap JavaDoc<String JavaDoc, Object JavaDoc> undeploymentContext = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
675
676         ServiceAssemblyDataHandler handler = deployedSACache
677                 .get(serviceAssemblyName);
678
679         undeploymentContext.put(DeploymentContextConstants.SA_DATA_HANDLER,
680                 handler);
681         return undeploymentContext;
682     }
683
684     /**
685      * Return a string representation of the JBI descriptor. This descriptor in
686      * found thanks to the jbiXmlUri
687      *
688      * @param jbiXmlUri
689      * the URI of the deployment descriptor
690      * @return a string representing the JBI deployment descriptor
691      */

692     protected String JavaDoc getJbiDescriptorAsString(URI JavaDoc jbiXmlUri) {
693         JBIDescriptorBuilder builder = new JBIDescriptorBuilder(jbiXmlUri,
694                 (java.util.logging.Logger JavaDoc) logger);
695         return builder.getJbiDescriptorAsString();
696     }
697
698     @LifeCycle(on = LifeCycleType.START)
699     protected void start() {
700         log = new LoggingUtil(logger);
701         log.start();
702
703         /*
704          * initialize packages loading cache
705          */

706         deployedSACache = new HashMap JavaDoc<String JavaDoc, ServiceAssemblyDataHandler>();
707
708         /*
709          * Initialize package handler
710          */

711         packageHandler = new PackageHandler(logger, repositorySrv);
712
713         /*
714          * Initialize DeploymentTaskFactory
715          */

716         deploymentTaskFactory = new DeploymentTaskFactory(packageHandler,
717                 endpointService, this, recoverySrv, repositorySrv, log,
718                 managerService);
719
720         log.end();
721     }
722
723     @LifeCycle(on = LifeCycleType.STOP)
724     public void stop() {
725         log.start();
726
727         logger = null;
728
729         deployedSACache = null;
730
731         log.end();
732     }
733
734     /**
735      * Shutdown the DeploymentService. All service assemblies are undeployed.
736      */

737     public void shutdown() throws Exception JavaDoc {
738         // Undeploy all service assemblies
739
Map JavaDoc<String JavaDoc, ServiceAssemblyDataHandler> removeList = new HashMap JavaDoc<String JavaDoc, ServiceAssemblyDataHandler>(
740                 deployedSACache);
741         for (String JavaDoc saName : removeList.keySet()) {
742             undeploy(saName);
743         }
744     }
745
746     public DeploymentTaskFactory getDeploymentTaskFactory() {
747         return deploymentTaskFactory;
748     }
749
750 }
751
Popular Tags