KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > framework > DeploymentService


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.jbi.framework;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.jbi.JBIException;
27 import javax.jbi.component.ServiceUnitManager;
28 import javax.jbi.management.DeploymentException;
29 import javax.jbi.management.DeploymentServiceMBean;
30 import javax.management.JMException JavaDoc;
31 import javax.management.MBeanAttributeInfo JavaDoc;
32 import javax.management.MBeanOperationInfo JavaDoc;
33 import javax.xml.parsers.DocumentBuilder JavaDoc;
34 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
35 import javax.xml.parsers.ParserConfigurationException JavaDoc;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.apache.servicemix.jbi.container.EnvironmentContext;
40 import org.apache.servicemix.jbi.container.JBIContainer;
41 import org.apache.servicemix.jbi.container.ServiceAssemblyEnvironment;
42 import org.apache.servicemix.jbi.deployment.Descriptor;
43 import org.apache.servicemix.jbi.deployment.DescriptorFactory;
44 import org.apache.servicemix.jbi.deployment.ServiceAssembly;
45 import org.apache.servicemix.jbi.deployment.ServiceUnit;
46 import org.apache.servicemix.jbi.management.AttributeInfoHelper;
47 import org.apache.servicemix.jbi.management.BaseSystemService;
48 import org.apache.servicemix.jbi.management.OperationInfoHelper;
49 import org.apache.servicemix.jbi.management.ParameterHelper;
50 import org.apache.servicemix.jbi.util.DOMUtil;
51 import org.apache.servicemix.jbi.util.FileUtil;
52 import org.w3c.dom.Document JavaDoc;
53 import org.w3c.dom.Element JavaDoc;
54 import org.w3c.dom.NodeList JavaDoc;
55 import org.xml.sax.InputSource JavaDoc;
56 import org.xml.sax.SAXException JavaDoc;
57
58 /**
59  * The deployment service MBean allows administrative tools to manage service assembly deployments.
60  *
61  * @version $Revision: 427935 $
62  */

63 public class DeploymentService extends BaseSystemService implements DeploymentServiceMBean {
64     
65     private static final Log log = LogFactory.getLog(DeploymentService.class);
66     private EnvironmentContext environmentContext;
67     private Registry registry;
68     
69     //
70
// ServiceMix service implementation
71
//
72

73     /**
74      * Initialize the Service
75      *
76      * @param container
77      * @throws JBIException
78      * @throws DeploymentException
79      */

80     public void init(JBIContainer container) throws JBIException {
81         this.environmentContext = container.getEnvironmentContext();
82         this.registry = container.getRegistry();
83         super.init(container);
84         buildState();
85     }
86     
87     protected Class JavaDoc getServiceMBean() {
88         return DeploymentServiceMBean.class;
89     }
90
91     public void start() throws javax.jbi.JBIException {
92         super.start();
93         String JavaDoc[] sas = registry.getDeployedServiceAssemblies();
94         for (int i = 0; i < sas.length; i++) {
95             try {
96                 ServiceAssemblyLifeCycle sa = registry.getServiceAssembly(sas[i]);
97                 sa.restore();
98             } catch (Exception JavaDoc e) {
99                 log.error("Unable to restore state for service assembly " + sas[i], e);
100             }
101         }
102     }
103     
104     /**
105      * Get an array of MBeanAttributeInfo
106      *
107      * @return array of AttributeInfos
108      * @throws JMException
109      */

110     public MBeanAttributeInfo JavaDoc[] getAttributeInfos() throws JMException JavaDoc {
111         AttributeInfoHelper helper = new AttributeInfoHelper();
112         helper.addAttribute(getObjectToManage(), "deployedServiceAssemblies", "list of deployed SAs");
113         return AttributeInfoHelper.join(super.getAttributeInfos(), helper.getAttributeInfos());
114     }
115
116     /**
117      * Get an array of MBeanOperationInfo
118      *
119      * @return array of OperationInfos
120      * @throws JMException
121      */

122     public MBeanOperationInfo JavaDoc[] getOperationInfos() throws JMException JavaDoc {
123         OperationInfoHelper helper = new OperationInfoHelper();
124         ParameterHelper ph = helper.addOperation(getObjectToManage(), "deploy", 1, "deploy An SA");
125         ph.setDescription(0, "saZipURL", "location of SA zip file");
126         ph = helper.addOperation(getObjectToManage(), "undeploy", 1, "undeploy An SA");
127         ph.setDescription(0, "saName", "SA name");
128         ph = helper.addOperation(getObjectToManage(), "getDeployedServiceUnitList", 1,
129                 "list of SU's currently deployed");
130         ph.setDescription(0, "componentName", "Component name");
131         ph = helper.addOperation(getObjectToManage(), "getServiceAssemblyDescriptor", 1, "Get descriptor for a SA");
132         ph.setDescription(0, "saName", "SA name");
133         ph = helper.addOperation(getObjectToManage(), "getDeployedServiceAssembliesForComponent", 1,
134                 "list of SA's for a Component");
135         ph.setDescription(0, "componentName", "Component name");
136         ph = helper.addOperation(getObjectToManage(), "getComponentsForDeployedServiceAssembly", 1,
137                 "list of Components for a SA");
138         ph.setDescription(0, "saName", "SA name");
139         ph = helper.addOperation(getObjectToManage(), "isDeployedServiceUnit", 2, "is SU deployed at a Component ?");
140         ph.setDescription(0, "componentName", "Component name");
141         ph.setDescription(1, "suName", "SU name");
142         ph = helper
143                 .addOperation(getObjectToManage(), "canDeployToComponent", 1, "Can a SU be deployed to a Component?");
144         ph.setDescription(0, "componentName", "Component name");
145         ph = helper.addOperation(getObjectToManage(), "start", 1, "start an SA");
146         ph.setDescription(0, "saName", "SA name");
147         ph = helper.addOperation(getObjectToManage(), "stop", 1, "stop an SA");
148         ph.setDescription(0, "saName", "SA name");
149         ph = helper.addOperation(getObjectToManage(), "shutDown", 1, "shutDown an SA");
150         ph.setDescription(0, "saName", "SA name");
151         ph = helper.addOperation(getObjectToManage(), "getState", 1, "Running state of an SA");
152         ph.setDescription(0, "saName", "SA name");
153         return OperationInfoHelper.join(super.getOperationInfos(), helper.getOperationInfos());
154     }
155     /**
156      * Get the description
157      *
158      * @return description
159      */

160     public String JavaDoc getDescription() {
161         return "Allows admin tools to manage service deployments";
162     }
163
164     //
165
// DeploymentServiceMBean implementation
166
//
167

168     /**
169      * Deploys the given SA to the JBI environment.
170      *
171      * @param saZipURL String containing the location of the Service Assembly zip file.
172      * @return Result/Status of the SA deployment in xml format.
173      * @throws Exception in xml format if complete deployment fails.
174      */

175     public String JavaDoc deploy(String JavaDoc saZipURL) throws Exception JavaDoc {
176         try {
177             if (saZipURL == null) {
178                 throw ManagementSupport.failure("deploy", "saZipURL must not be null");
179             }
180             File JavaDoc tmpDir = null;
181             try {
182                 tmpDir = AutoDeploymentService.unpackLocation(environmentContext.getTmpDir(), saZipURL);
183             } catch (Exception JavaDoc e) {
184                 throw ManagementSupport.failure("deploy", "Unable to unpack archive: " + saZipURL, e);
185             }
186             // unpackLocation returns null if no jbi descriptor is found
187
if (tmpDir == null) {
188                 throw ManagementSupport.failure("deploy", "Unable to find jbi descriptor: " + saZipURL);
189             }
190             Descriptor root = null;
191             try {
192                 root = DescriptorFactory.buildDescriptor(tmpDir);
193             } catch (Exception JavaDoc e) {
194                 throw ManagementSupport.failure("deploy", "Unable to build jbi descriptor: " + saZipURL, e);
195             }
196             if (root == null) {
197                 throw ManagementSupport.failure("deploy", "Unable to find jbi descriptor: " + saZipURL);
198             }
199             ServiceAssembly sa = root.getServiceAssembly();
200             if (sa == null) {
201                 throw ManagementSupport.failure("deploy", "JBI descriptor is not an assembly descriptor: " + saZipURL);
202             }
203             return deployServiceAssembly(tmpDir, sa);
204         } catch (Exception JavaDoc e) {
205             log.error("Error deploying service assembly", e);
206             throw e;
207         }
208     }
209
210     /**
211      * Undeploys the given SA from the JBI environment.
212      *
213      * @param saName name of the SA that has to be undeployed.
214      * @return Result/Status of the SA undeployment.
215      * @throws Exception if compelete undeployment fails.
216      */

217     public String JavaDoc undeploy(String JavaDoc saName) throws Exception JavaDoc {
218         if (saName == null) {
219             throw ManagementSupport.failure("undeploy", "SA name must not be null");
220         }
221         ServiceAssemblyLifeCycle sa = registry.getServiceAssembly(saName);
222         if (sa == null) {
223             throw ManagementSupport.failure("undeploy", "SA has not been deployed: " + saName);
224         }
225         String JavaDoc state = sa.getCurrentState();
226         if (!DeploymentServiceMBean.SHUTDOWN.equals(state)) {
227             throw ManagementSupport.failure("undeploy", "SA must be shut down: " + saName);
228         }
229         try {
230             // Make sure the all service units in the assembly are shutdown.
231
// SUs can have different states (if a previous shutdown failed).
232
try {
233                 sa.shutDown();
234             } catch (Exception JavaDoc e) {
235             }
236
237             String JavaDoc result = null;
238             String JavaDoc assemblyName = sa.getName();
239             registry.unregisterServiceAssembly(assemblyName);
240             ServiceUnitLifeCycle[] sus = sa.getDeployedSUs();
241             if (sus != null) {
242                 for (int i = 0;i < sus.length; i++) {
243                     undeployServiceUnit(sus[i]);
244                 }
245             }
246             FileUtil.deleteFile(sa.getEnvironment().getRootDir());
247
248             return result;
249         } catch (Exception JavaDoc e) {
250             log.info("Unable to undeploy assembly", e);
251             throw e;
252         }
253     }
254
255     /**
256      * Returns a list of Service Units that are currently deployed to the given component.
257      *
258      * @param componentName name of the component.
259      * @return List of deployed ASA Ids.
260      */

261     public String JavaDoc[] getDeployedServiceUnitList(String JavaDoc componentName) throws Exception JavaDoc {
262         try {
263             ServiceUnitLifeCycle[] sus = registry.getDeployedServiceUnits(componentName);
264             String JavaDoc[] names = new String JavaDoc[sus.length];
265             for (int i = 0; i < names.length; i++) {
266                 names[i] = sus[i].getName();
267             }
268             return names;
269         } catch (Exception JavaDoc e) {
270             log.info("Unable to get deployed service unit list", e);
271             throw e;
272         }
273     }
274
275     /**
276      * Returns a list of Service Assemblies deployed to the JBI enviroment.
277      *
278      * @return list of Service Assembly Name's.
279      */

280     public String JavaDoc[] getDeployedServiceAssemblies() throws Exception JavaDoc {
281         try {
282             return registry.getDeployedServiceAssemblies();
283         } catch (Exception JavaDoc e) {
284             log.info("Unable to get deployed service assemblies", e);
285             throw e;
286         }
287     }
288
289     /**
290      * Returns the descriptor of the Service Assembly that was deployed to the JBI enviroment.
291      *
292      * @param saName name of the service assembly.
293      * @return descriptor of the Service Assembly.
294      */

295     public String JavaDoc getServiceAssemblyDescriptor(String JavaDoc saName) throws Exception JavaDoc {
296         ServiceAssemblyLifeCycle sa = registry.getServiceAssembly(saName);
297         if (sa != null) {
298             return sa.getDescriptor();
299         } else {
300             return null;
301         }
302     }
303
304     /**
305      * Returns a list of Service Assemblies that contain SUs for the given component.
306      *
307      * @param componentName name of the component.
308      * @return list of Service Assembly names.
309      * @throws Exception if unable to retrieve service assembly list.
310      */

311     public String JavaDoc[] getDeployedServiceAssembliesForComponent(String JavaDoc componentName) throws Exception JavaDoc {
312         try {
313             return registry.getDeployedServiceAssembliesForComponent(componentName);
314         } catch (Exception JavaDoc e) {
315             log.info("Error in getDeployedServiceAssembliesForComponent", e);
316             throw e;
317         }
318     }
319
320     /**
321      * Returns a list of components(to which SUs are targeted for) in a Service Assembly.
322      *
323      * @param saName name of the service assembly.
324      * @return list of component names.
325      * @throws Exception if unable to retrieve component list.
326      */

327     public String JavaDoc[] getComponentsForDeployedServiceAssembly(String JavaDoc saName) throws Exception JavaDoc {
328         try {
329             return registry.getComponentsForDeployedServiceAssembly(saName);
330         } catch (Exception JavaDoc e) {
331             log.info("Error in getComponentsForDeployedServiceAssembly", e);
332             throw e;
333         }
334     }
335
336     /**
337      * Returns a boolean value indicating whether the SU is currently deployed.
338      *
339      * @param componentName - name of component.
340      * @param suName - name of the Service Unit.
341      * @return boolean value indicating whether the SU is currently deployed.
342      * @throws Exception if unable to return status of service unit.
343      */

344     public boolean isDeployedServiceUnit(String JavaDoc componentName, String JavaDoc suName) throws Exception JavaDoc {
345         try {
346             return registry.isSADeployedServiceUnit(componentName, suName);
347         } catch (Exception JavaDoc e) {
348             log.info("Error in isSADeployedServiceUnit", e);
349             throw e;
350         }
351     }
352
353     /**
354      * Returns a boolean value indicating whether the SU can be deployed to a component.
355      *
356      * @param componentName - name of the component.
357      * @return boolean value indicating whether the SU can be deployed.
358      */

359     public boolean canDeployToComponent(String JavaDoc componentName) {
360         ComponentMBeanImpl lcc = container.getComponent(componentName);
361         return lcc != null && lcc.isStarted() && lcc.getServiceUnitManager() != null;
362     }
363
364     /**
365      * Starts the service assembly and puts it in STARTED state.
366      *
367      * @param serviceAssemblyName - name of the service assembly.
368      * @return Result/Status of this operation.
369      * @throws Exception if operation fails.
370      */

371     public String JavaDoc start(String JavaDoc serviceAssemblyName) throws Exception JavaDoc {
372         try {
373             ServiceAssemblyLifeCycle sa = registry.getServiceAssembly(serviceAssemblyName);
374             return sa.start(true);
375         } catch (Exception JavaDoc e) {
376             log.info("Error in start", e);
377             throw e;
378         }
379     }
380     
381     
382
383     /**
384      * Stops the service assembly and puts it in STOPPED state.
385      *
386      * @param serviceAssemblyName - name of the service assembly.
387      * @return Result/Status of this operation.
388      * @throws Exception if operation fails.
389      */

390     public String JavaDoc stop(String JavaDoc serviceAssemblyName) throws Exception JavaDoc {
391         try {
392             ServiceAssemblyLifeCycle sa = registry.getServiceAssembly(serviceAssemblyName);
393             return sa.stop(true, false);
394         } catch (Exception JavaDoc e) {
395             log.info("Error in stop", e);
396             throw e;
397         }
398     }
399
400     /**
401      * Shutdown the service assembly and puts it in SHUTDOWN state.
402      *
403      * @param serviceAssemblyName - name of the service assembly.
404      * @return Result/Status of this operation.
405      * @throws Exception if operation fails.
406      */

407     public String JavaDoc shutDown(String JavaDoc serviceAssemblyName) throws Exception JavaDoc {
408         try {
409             ServiceAssemblyLifeCycle sa = registry.getServiceAssembly(serviceAssemblyName);
410             return sa.shutDown(true);
411         } catch (Exception JavaDoc e) {
412             log.info("Error in shutDown", e);
413             throw e;
414         }
415     }
416
417     /**
418      * Returns the state of service assembly.
419      *
420      * @param serviceAssemblyName - name of the service assembly.
421      * @return State of the service assembly.
422      * @throws Exception if operation fails.
423      */

424     public String JavaDoc getState(String JavaDoc serviceAssemblyName) throws Exception JavaDoc {
425         try {
426             ServiceAssemblyLifeCycle sa = registry.getServiceAssembly(serviceAssemblyName);
427             return sa.getCurrentState();
428         } catch (Exception JavaDoc e) {
429             log.info("Error in getState", e);
430             throw e;
431         }
432     }
433
434     /**
435      * See if an Sa is already deployed
436      *
437      * @param serviceAssemblyName - name of the service assembly.
438      * @return true if already deployed
439      */

440     protected boolean isSaDeployed(String JavaDoc serviceAssemblyName) {
441         return registry.getServiceAssembly(serviceAssemblyName) != null;
442     }
443
444     /**
445      * Deploy an SA
446      *
447      * @param tmpDir
448      * @param sa
449      * @return result/status of the deployment in xml format
450      * @throws Exception in xml format
451      */

452     protected String JavaDoc deployServiceAssembly(File JavaDoc tmpDir, ServiceAssembly sa) throws Exception JavaDoc {
453         String JavaDoc assemblyName = sa.getIdentification().getName();
454         ServiceAssemblyEnvironment env = environmentContext.getNewServiceAssemblyEnvironment(assemblyName);
455         File JavaDoc saDirectory = env.getInstallDir();
456
457         // move the assembly to a well-named holding area
458
if (log.isDebugEnabled()) {
459             log.debug("Moving " + tmpDir.getAbsolutePath() + " to " + saDirectory.getAbsolutePath());
460         }
461         saDirectory.getParentFile().mkdirs();
462         if (!tmpDir.renameTo(saDirectory)) {
463             throw ManagementSupport.failure("deploy", "Failed to rename " + tmpDir + " to " + saDirectory);
464         }
465         // Check all SUs requirements
466
ServiceUnit[] sus = sa.getServiceUnits();
467         if (sus != null) {
468             for (int i = 0; i < sus.length; i++) {
469                 String JavaDoc suName = sus[i].getIdentification().getName();
470                 String JavaDoc artifact = sus[i].getTarget().getArtifactsZip();
471                 String JavaDoc componentName = sus[i].getTarget().getComponentName();
472                 File JavaDoc artifactFile = new File JavaDoc(saDirectory, artifact);
473                 if (!artifactFile.exists()) {
474                     throw ManagementSupport.failure("deploy", "Artifact " + artifact + " not found for service unit " + suName);
475                 }
476                 ComponentMBeanImpl lcc = container.getComponent(componentName);
477                 if (lcc == null) {
478                     throw ManagementSupport.failure("deploy", "Target component " + componentName + " for service unit " + suName + " is not installed");
479                 }
480                 if (!lcc.isStarted()) {
481                     throw ManagementSupport.failure("deploy", "Target component " + componentName + " for service unit " + suName + " is not started");
482                 }
483                 if (lcc.getServiceUnitManager() == null) {
484                     throw ManagementSupport.failure("deploy", "Target component " + componentName + " for service unit " + suName + " does not accept deployments");
485                 }
486                 // TODO: check duplicates here ?
487
if (isDeployedServiceUnit(componentName, suName)) {
488                     throw ManagementSupport.failure("deploy", "Service unit " + suName + " is already deployed on component " + componentName);
489                 }
490             }
491         }
492         // Everything seems ok, so deploy all SUs
493
int nbSuccess = 0;
494         int nbFailures = 0;
495         List JavaDoc componentResults = new ArrayList JavaDoc();
496         List JavaDoc suKeys = new ArrayList JavaDoc();
497         if (sus != null) {
498             for (int i = 0; i < sus.length; i++) {
499                 File JavaDoc targetDir = null;
500                 String JavaDoc suName = sus[i].getIdentification().getName();
501                 String JavaDoc artifact = sus[i].getTarget().getArtifactsZip();
502                 String JavaDoc componentName = sus[i].getTarget().getComponentName();
503                 // TODO: skip duplicates
504
// Unpack SU
505
try {
506                     File JavaDoc artifactFile = new File JavaDoc(saDirectory, artifact);
507                     targetDir = env.getServiceUnitDirectory(componentName, suName);
508                     if (log.isDebugEnabled()) {
509                         log.debug("Unpack service unit archive " + artifactFile + " to " + targetDir);
510                     }
511                     FileUtil.unpackArchive(artifactFile, targetDir);
512                 } catch (IOException JavaDoc e) {
513                     nbFailures++;
514                     componentResults.add(ManagementSupport.createComponentFailure(
515                             "deploy", componentName,
516                             "Error unpacking service unit", e));
517                     continue;
518                 }
519                 // Deploy it
520
boolean success = false;
521                 try {
522                     ComponentMBeanImpl lcc = container.getComponent(componentName);
523                     ServiceUnitManager sum = lcc.getServiceUnitManager();
524                     ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
525                     try {
526                         Thread.currentThread().setContextClassLoader(lcc.getComponent().getClass().getClassLoader());
527                         String JavaDoc resultMsg = sum.deploy(suName, targetDir.getAbsolutePath());
528                         success = getComponentTaskResult(resultMsg, componentName, componentResults, true);
529                     } finally {
530                         Thread.currentThread().setContextClassLoader(cl);
531                     }
532                     // TODO: need to register the SU somewhere to keep track of its state
533
} catch (Exception JavaDoc e) {
534                     getComponentTaskError(e, componentName, componentResults);
535                 }
536                 if (success) {
537                     nbSuccess++;
538                     suKeys.add(registry.registerServiceUnit(sus[i], assemblyName, targetDir));
539                 } else {
540                     nbFailures++;
541                 }
542             }
543         }
544         // Note: the jbi spec says that if at least one deployment succeeds,
545
// this should be a SUCCESS. However, ServiceMix handles SA in an
546
// atomic way: for a given operation on an SA, all operations on SU
547
// should succeed. This is clearly a minor violation of the spec.
548
//
549
// Failure
550
if (nbFailures > 0) {
551             // Undeploy SUs
552
for (Iterator JavaDoc iter = suKeys.iterator(); iter.hasNext();) {
553                 try {
554                     String JavaDoc suName = (String JavaDoc) iter.next();
555                     ServiceUnitLifeCycle su = registry.getServiceUnit(suName);
556                     undeployServiceUnit(su);
557                 } catch (Exception JavaDoc e) {
558                     log.warn("Error undeploying SU", e);
559                 }
560             }
561             // Delete SA deployment directory
562
FileUtil.deleteFile(saDirectory);
563             throw ManagementSupport.failure("deploy", componentResults);
564         }
565         // Success
566
else {
567             // Register SA
568
String JavaDoc[] deployedSUs = (String JavaDoc[]) suKeys.toArray(new String JavaDoc[suKeys.size()]);
569             ServiceAssemblyLifeCycle salc = registry.registerServiceAssembly(sa, deployedSUs, env);
570             salc.writeRunningState();
571             // Build result string
572
if (nbFailures > 0) {
573                 return ManagementSupport.createWarningMessage("deploy", "Failed to deploy some service units", componentResults);
574             } else {
575                 return ManagementSupport.createSuccessMessage("deploy", componentResults);
576             }
577         }
578     }
579     
580     protected void getComponentTaskError(Exception JavaDoc exception, String JavaDoc component, List JavaDoc results) {
581         Element JavaDoc result = null;
582         try {
583             Document JavaDoc doc = parse(exception.getMessage());
584             result = getElement(doc, "component-task-result");
585         } catch (Exception JavaDoc e) {
586             result = ManagementSupport.createComponentFailure(
587                     "deploy", component,
588                     "Unable to parse result string", exception);
589         }
590         if (result != null) {
591             results.add(result);
592         }
593     }
594
595     protected boolean getComponentTaskResult(String JavaDoc resultMsg, String JavaDoc component, List JavaDoc results, boolean success) {
596         Element JavaDoc result = null;
597         try {
598             Document JavaDoc doc = parse(resultMsg);
599             result = getElement(doc, "component-task-result");
600             Element JavaDoc e = getChildElement(result, "component-task-result-details");
601             e = getChildElement(e, "task-result-details");
602             e = getChildElement(e, "task-result");
603             String JavaDoc r = DOMUtil.getElementText(e);
604             if (!"SUCCESS".equals(r)) {
605                 success = false;
606             }
607         } catch (Exception JavaDoc e) {
608             // The component did not throw an exception upon deployment,
609
// but the result string is not compliant, so issue a warning
610
// and consider this is a successfull deployment
611
try {
612                 if (success) {
613                     result = ManagementSupport.createComponentWarning(
614                             "deploy", component,
615                             "Unable to parse result string", e);
616                 } else {
617                     result = ManagementSupport.createComponentFailure(
618                             "deploy", component,
619                             "Unable to parse result string", e);
620                 }
621             } catch (Exception JavaDoc e2) {
622                 log.error(e2);
623                 result = null;
624             }
625         }
626         if (result != null) {
627             results.add(result);
628         }
629         return success;
630     }
631     
632     protected Document JavaDoc parse(String JavaDoc result) throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
633         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
634         factory.setNamespaceAware(true);
635         factory.setIgnoringElementContentWhitespace(true);
636         factory.setIgnoringComments(true);
637         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
638         return builder.parse(new InputSource JavaDoc(new StringReader JavaDoc(result)));
639     }
640     
641     protected Element JavaDoc getElement(Document JavaDoc doc, String JavaDoc name) {
642         NodeList JavaDoc l = doc.getElementsByTagNameNS("http://java.sun.com/xml/ns/jbi/management-message", name);
643         Element JavaDoc e = (Element JavaDoc) l.item(0);
644         return e;
645     }
646     
647     protected Element JavaDoc getChildElement(Element JavaDoc element, String JavaDoc name) {
648         NodeList JavaDoc l = element.getElementsByTagNameNS("http://java.sun.com/xml/ns/jbi/management-message", name);
649         Element JavaDoc e = (Element JavaDoc) l.item(0);
650         return e;
651     }
652
653     protected void undeployServiceUnit(ServiceUnitLifeCycle su) throws DeploymentException {
654         String JavaDoc name = su.getName();
655         String JavaDoc componentName = su.getComponentName();
656         File JavaDoc targetDir = su.getServiceUnitRootPath();
657         registry.unregisterServiceUnit(su.getKey());
658         // unpack the artifact
659
// now get the component and give it a SA
660
ComponentMBeanImpl component = container.getComponent(componentName);
661         if (component != null) {
662             ServiceUnitManager sum = component.getServiceUnitManager();
663             if (sum != null) {
664                 ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
665                 try {
666                     Thread.currentThread().setContextClassLoader(component.getComponent().getClass().getClassLoader());
667                     sum.undeploy(name, targetDir.getAbsolutePath());
668                 } finally {
669                     Thread.currentThread().setContextClassLoader(cl);
670                 }
671                 FileUtil.deleteFile(targetDir);
672             }
673         }
674         else {
675             FileUtil.deleteFile(targetDir);
676         }
677         log.info("UnDeployed ServiceUnit " + name + " from Component: " + componentName);
678     }
679
680     /**
681      * Find runnning state and things deployed before shutdown
682      */

683     protected void buildState() {
684         log.info("Restoring service assemblies");
685         // walk through deployed SA's
686
File JavaDoc top = environmentContext.getServiceAssembliesDir();
687         if (top != null && top.exists() && top.isDirectory()) {
688             File JavaDoc[] files = top.listFiles();
689             if (files != null) {
690                 // Initialize all assemblies
691
for (int i = 0; i < files.length; i++) {
692                     if (files[i].isDirectory()) {
693                         String JavaDoc assemblyName = files[i].getName();
694                         try {
695                             ServiceAssemblyEnvironment env = environmentContext.getServiceAssemblyEnvironment(assemblyName);
696                             Descriptor root = DescriptorFactory.buildDescriptor(env.getInstallDir());
697                             if (root != null) {
698                                 ServiceAssembly sa = root.getServiceAssembly();
699                                 if (sa != null && sa.getIdentification() != null) {
700                                     registry.registerServiceAssembly(sa, env);
701                                 }
702                             }
703                         } catch(Exception JavaDoc e) {
704                             log.error("Failed to initialized service assembly: " + assemblyName,e);
705                         }
706                     }
707                 }
708             }
709         }
710     }
711
712 }
713
Popular Tags