1 22 package org.objectweb.petals.jbi.management.deployment.undeploy; 23 24 import java.io.InputStream ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.List ; 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 51 public class AllSUUndeploymentTask implements Task { 52 53 56 protected LifeCycleManagerService managerService; 57 58 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 context) throws Exception { 71 ServiceAssemblyLifeCycle saLifeCycle = (ServiceAssemblyLifeCycle) context 72 .get(DeploymentContextConstants.SA_LIFECYCLE); 73 74 XMLResult xmlResult = (XMLResult) context 75 .get(DeploymentContextConstants.XML_RESULT); 76 77 80 undeploySUS(saLifeCycle, xmlResult); 81 82 } 83 84 95 protected void undeploySUS(ServiceAssemblyLifeCycle saLifeCycle, 96 XMLResult xmlResult) throws Exception { 97 100 String 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 113 114 118 List <ServiceUnit> suList = saLifeCycle.getServiceAssembly() 119 .getServiceUnits(); 120 Collections.reverse(suList); 121 StringBuffer componentOutput = new StringBuffer (); 122 for (ServiceUnit suDesc : suList) { 123 String 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 151 protected String undeploySU(ServiceAssemblyLifeCycle saLifeCycle, 152 ServiceUnit serviceUnit) throws Exception { 153 String msg = ""; 154 String xmlReturn = null; 155 156 159 String suName = DeploymentUtils.getServiceUnitName(serviceUnit); 160 ServiceUnitLifeCycle serviceUnitLifeCycle = saLifeCycle 161 .getServiceUnitsLifeCycles().get(suName); 162 String targetedComponent = serviceUnit.getTargetComponentName(); 163 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 (msg); 172 } 173 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 (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 (msg, e); 191 } 192 193 196 saLifeCycle.getServiceUnitsLifeCycles().remove(suName); 197 198 return xmlReturn; 199 } 200 201 210 protected boolean isValidComponentTask(String componentTask, InputStream xsd) { 211 return XMLResult.isValidComponentTask(componentTask, xsd); 212 } 213 214 public void undo(HashMap context) throws Exception { 215 217 } 218 219 } 220 | Popular Tags |