1 22 package org.objectweb.petals.component.common.serviceunitmanager.handler; 23 24 import java.io.File ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import java.util.logging.Logger ; 28 29 import javax.jbi.JBIException; 30 import javax.jbi.component.ComponentContext; 31 import javax.jbi.management.DeploymentException; 32 import javax.jbi.servicedesc.ServiceEndpoint; 33 import javax.xml.namespace.QName ; 34 35 import org.objectweb.petals.component.common.serviceunitmanager.manager.PetalsServiceUnitManager; 36 import org.objectweb.petals.component.common.util.WSDLHelper; 37 import org.objectweb.petals.component.common.util.XMLHelper; 38 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Node ; 41 42 52 public abstract class PetalsServiceUnitHandler { 53 54 protected final static String SERVICE_UNIT_PROPERTY_FILE = "serviceUnit.xml"; 55 56 protected static final String FAILED_ACTIVATE_ENDPOINT = "Activate new endpoint failed "; 57 58 protected static final String FAILED_DEACTIVATE_ENDPOINT = "Deactivate endpoint failed "; 59 60 protected static final String INCOMPLETE_SERVICE_UNIT_PACKAGE = "The deployed service package is incomplete "; 61 62 protected static final String INVALID_SERVICE_DESCRIPTION = "The provided service description is not valid "; 63 64 protected static final String ERROR_DUPLICATE_SERVICE_UNIT = "A service unit with the given name is already registered "; 65 66 protected ComponentContext context; 67 68 protected Logger logger; 69 70 protected String SERVICE_UNIT_TYPE; 71 72 protected JBIDescriptor descriptor; 73 74 protected Map <String ,String > serviceUnitInstallationRootPath = new HashMap <String , String >(); 75 76 public PetalsServiceUnitHandler(){ 77 } 78 79 80 public PetalsServiceUnitHandler(ComponentContext context, 81 Logger logger) { 82 this.context = context; 83 this.logger = logger; 84 } 85 86 87 95 abstract public String deploy(String serviceUnitName, 96 String serviceUnitType, String serviceUnitRootPath) throws DeploymentException; 97 98 public ComponentContext getContext() { 99 return context; 100 } 101 102 public Logger getLogger() { 103 return logger; 104 } 105 106 public String getSERVICE_UNIT_TYPE() { 107 return SERVICE_UNIT_TYPE; 108 } 109 110 113 abstract public void init(String serviceUnitName, String serviceUnitRootPath) 114 throws DeploymentException; 115 116 119 public void setContext(ComponentContext context) { 120 this.context = context; 121 } 122 123 126 public void setLogger(Logger logger) { 127 this.logger = logger; 128 } 129 130 public void setSERVICE_UNIT_TYPE(String service_unit_type) { 131 SERVICE_UNIT_TYPE = service_unit_type; 132 } 133 134 137 abstract public void shutDown(String serviceUnitName, 138 PetalsServiceUnitManager epHandler) throws DeploymentException; 139 140 143 abstract public void start(String serviceUnitName, 144 PetalsServiceUnitManager epHandler) throws DeploymentException; 145 146 149 abstract public void stop(String serviceUnitName, 150 PetalsServiceUnitManager epHandler) throws DeploymentException; 151 152 155 abstract public String undeploy(String serviceUnitName, 156 String serviceUnitRootPath) throws DeploymentException; 157 158 167 protected void activateEndpointsFromJBIDescription( 168 PetalsServiceUnitManager epHandler, Document serviceDesc, 169 String serviceUnitName, String suRootPath) throws Exception { 170 File jbiXmlFile = new File (suRootPath + File.separator 171 + "META-INF" + File.separator + "jbi.xml"); 172 177 180 QName interfaceName = getInterfaceNameFromJbiXml(jbiXmlFile); 181 String endpointName = getEndpointNameFromJbiXml(jbiXmlFile); 182 QName serviceName = getServiceNameFromJbiXml(jbiXmlFile); 183 ServiceEndpoint ep = context.activateEndpoint(serviceName, 184 endpointName); 185 epHandler.setServiceDescription(ep, serviceDesc); 186 epHandler.setServiceUnitEndpoint(serviceUnitName, ep); 187 epHandler.addServiceForInterface(interfaceName, ep); 188 epHandler.addSUHandlerForEndpoint(ep, this); 189 } 191 192 198 protected void deactivateEndpointsFromJBIDescription( String serviceUnitName, 199 PetalsServiceUnitManager epHandler) throws JBIException { 200 ServiceEndpoint ep = epHandler.getServiceUnitEndpoint(serviceUnitName); 201 if(ep != null) { 202 context.deactivateEndpoint(ep); 203 epHandler.removeServiceDescription(ep); 204 epHandler.removeServiceUnitEndpoint(serviceUnitName); 205 epHandler.removeServiceForInterface(ep); 206 epHandler.removeSUHandlerForEndpoint(ep); 207 } 208 } 209 210 215 protected String getEndpointNameFromJbiXml(File jbiXml){ 216 String result = new String (); 217 Document jbiDoc = WSDLHelper.createDocumentFromWSDL(jbiXml); 218 Node providesNode = XMLHelper.findChild(jbiDoc, "provides", true); 219 result = XMLHelper.getAttributeValue(providesNode, "endpoint-name"); 220 return result; 221 } 222 223 228 protected QName getInterfaceNameFromJbiXml(File jbiXml){ 229 QName result = null; 230 Document jbiDoc = WSDLHelper.createDocumentFromWSDL(jbiXml); 231 Node providesNode = XMLHelper.findChild(jbiDoc, "provides", true); 232 result = QName.valueOf(XMLHelper.getAttributeValue(providesNode, "interface-name")); 233 return result; 234 } 235 236 241 protected QName getServiceNameFromJbiXml(File jbiXml){ 242 QName result = null; 243 Document jbiDoc = WSDLHelper.createDocumentFromWSDL(jbiXml); 244 Node providesNode = XMLHelper.findChild(jbiDoc, "provides", true); 245 result = QName.valueOf(XMLHelper.getAttributeValue(providesNode, "service-name")); 246 return result; 247 } 248 249 } 250 | Popular Tags |