1 22 package org.objectweb.petals.component.common.basic; 23 24 import java.io.File ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 32 import javax.jbi.JBIException; 33 import javax.jbi.component.ComponentContext; 34 import javax.jbi.component.ServiceUnitManager; 35 import javax.jbi.management.DeploymentException; 36 import javax.jbi.servicedesc.ServiceEndpoint; 37 import javax.xml.namespace.QName ; 38 39 import org.objectweb.petals.component.common.util.ManagementMessageUtil; 40 import org.objectweb.petals.component.common.util.WSDLHelper; 41 import org.objectweb.petals.component.common.util.XMLHelper; 42 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.Node ; 45 import org.w3c.dom.NodeList ; 46 47 56 public abstract class AbstractServiceUnitManager implements ServiceUnitManager { 57 58 61 public static final String EXTENSIONS_XMLNS = "http://petals.objectweb.org/extensions"; 62 63 66 protected static final String ERROR_DUPLICATE_SERVICE_UNIT = "A service unit with the given name is already registered "; 67 68 protected static final String FAILED_ACTIVATE_ENDPOINT = "Activate new endpoint failed "; 69 70 protected static final String FAILED_DEACTIVATE_ENDPOINT = "Deactivate endpoint failed "; 71 72 protected static final String INCOMPLETE_SERVICE_UNIT_PACKAGE = "The deployed service package is incomplete "; 73 74 77 protected ComponentContext context; 78 79 82 protected Logger logger; 83 84 protected Map <ServiceEndpoint, Document > endpointServiceDescriptionMap; 85 86 protected Map <QName , Set <ServiceEndpoint>> servicesByInterfaceMap; 87 88 protected Map <String , String > serviceUnitInstallationRootPath; 89 90 protected Map <String , ServiceEndpoint> serviceUnitMap; 91 92 95 public AbstractServiceUnitManager() { 96 endpointServiceDescriptionMap = new HashMap <ServiceEndpoint, Document >(); 97 serviceUnitMap = new HashMap <String , ServiceEndpoint>(); 98 servicesByInterfaceMap = new HashMap <QName , Set <ServiceEndpoint>>(); 99 serviceUnitInstallationRootPath = new HashMap <String , String >(); 100 } 101 102 110 public AbstractServiceUnitManager(ComponentContext context, Logger logger) 111 throws DeploymentException { 112 this.context = context; 113 this.logger = logger; 114 endpointServiceDescriptionMap = new HashMap <ServiceEndpoint, Document >(); 115 serviceUnitMap = new HashMap <String , ServiceEndpoint>(); 116 servicesByInterfaceMap = new HashMap <QName , Set <ServiceEndpoint>>(); 117 serviceUnitInstallationRootPath = new HashMap <String , String >(); 118 } 119 120 128 public void addServiceForInterface(QName interfaceName, 129 ServiceEndpoint serviceEndpoint) { 130 Set <ServiceEndpoint> endpoints = null; 131 endpoints = servicesByInterfaceMap.get(interfaceName); 132 if (endpoints == null) { 133 endpoints = new HashSet <ServiceEndpoint>(); 134 } 135 endpoints.add(serviceEndpoint); 136 servicesByInterfaceMap.put(interfaceName, endpoints); 137 } 138 139 151 public String deploy(String serviceUnitName, String serviceUnitRootPath) 152 throws DeploymentException { 153 String result = null; 154 if (serviceUnitMap.get(serviceUnitName) == null) { 155 File jbiXmlFile = new File (serviceUnitRootPath + File.separator 156 + "META-INF" + File.separator + "jbi.xml"); 157 Node servicesNode = getNode(jbiXmlFile, "services"); 158 Map <String , String > extensions = getExtensions(servicesNode); 159 logger.log(Level.FINE, "deploy serviceUnitName " 160 + serviceUnitName + "serviceUnitRootPath" 161 + serviceUnitRootPath); 162 result = ManagementMessageUtil.getComponentTaskResult(context 163 .getComponentName(), "deploy", 164 ManagementMessageUtil.TASK_RESULT_SUCCESS); 165 deploy(extensions); 166 } else { 167 throw new DeploymentException(ERROR_DUPLICATE_SERVICE_UNIT); 168 } 169 return result; 170 } 171 172 179 public Document getServiceDescription(ServiceEndpoint se) { 180 Document result = null; 181 for (ServiceEndpoint ep : endpointServiceDescriptionMap.keySet()) { 182 if (se.getServiceName().equals(ep.getServiceName())) { 183 result = endpointServiceDescriptionMap.get(ep); 184 break; 185 } 186 } 187 return result; 188 } 189 190 197 public ServiceEndpoint getServiceUnitEndpoint(String serviceUnitName) { 198 return serviceUnitMap.get(serviceUnitName); 199 } 200 201 209 public void init(String serviceUnitName, String serviceUnitRootPath) 210 throws DeploymentException { 211 logger.log(Level.FINE, "init serviceUnitName " + serviceUnitName 212 + " serviceUnitRootPath " + serviceUnitRootPath); 213 if (serviceUnitInstallationRootPath.put(serviceUnitName, 214 serviceUnitRootPath) != null) { 215 logger.log(Level.SEVERE, ERROR_DUPLICATE_SERVICE_UNIT); 216 throw new DeploymentException(ERROR_DUPLICATE_SERVICE_UNIT); 217 } 218 } 219 220 226 public void removeServiceDescription(ServiceEndpoint ep) { 227 endpointServiceDescriptionMap.remove(ep); 228 } 229 230 236 public void removeServiceUnitEndpoint(String suName) { 237 serviceUnitMap.remove(suName); 238 } 239 240 248 public void setServiceDescription(ServiceEndpoint se, Document serviceDesc) { 249 endpointServiceDescriptionMap.put(se, serviceDesc); 250 } 251 252 260 public void setServiceUnitEndpoint(String serviceUnitName, 261 ServiceEndpoint serviceEP) { 262 serviceUnitMap.put(serviceUnitName, serviceEP); 263 } 264 265 270 public void shutDown(String serviceUnitName) throws DeploymentException { 271 logger.log(Level.FINE, "shutdown serviceUnitName " + serviceUnitName); 272 } 273 274 281 public void start(String serviceUnitName) throws DeploymentException { 282 logger.log(Level.FINE, "start serviceUnitName " + serviceUnitName); 283 String suRootPath = serviceUnitInstallationRootPath 284 .get(serviceUnitName); 285 Document serviceDesc = getServiceUnitWSDL(suRootPath); 286 if (serviceDesc != null) { 287 try { 288 activateEndpointsFromJBIDescription(serviceDesc, 289 serviceUnitName, suRootPath); 290 } catch (Exception ex) { 291 logger.log(Level.SEVERE, FAILED_ACTIVATE_ENDPOINT + ex); 292 throw new DeploymentException(FAILED_ACTIVATE_ENDPOINT, ex); 293 } 294 } else { 295 throw new DeploymentException(INCOMPLETE_SERVICE_UNIT_PACKAGE); 296 } 297 } 298 299 306 public void stop(String serviceUnitName) throws DeploymentException { 307 try { 308 serviceUnitInstallationRootPath.remove(serviceUnitName); 309 deactivateEndpointsFromJBIDescription(serviceUnitName); 310 } catch (Exception e) { 311 throw new DeploymentException(FAILED_DEACTIVATE_ENDPOINT, e); 312 } 313 logger.log(Level.FINE, "stop serviceUnitName " + serviceUnitName); 314 } 315 316 322 public String undeploy(String serviceUnitName, String serviceUnitRootPath) 323 throws DeploymentException { 324 return ManagementMessageUtil.getComponentTaskResult(context 325 .getComponentName(), "undeploy", 326 ManagementMessageUtil.TASK_RESULT_SUCCESS); 327 } 328 329 345 protected ServiceEndpoint activateEndpointsFromJBIDescription( 346 Document serviceDesc, String serviceUnitName, String suRootPath) 347 throws Exception { 348 File jbiXmlFile = new File (suRootPath + File.separator + "META-INF" 349 + File.separator + "jbi.xml"); 350 Node providesNode = getNode(jbiXmlFile, "provides"); 351 ServiceEndpoint ep = null; 352 if (providesNode != null) { 353 QName interfaceName = getInterfaceNameFromJbiXml(providesNode); 354 String endpointName = getEndpointNameFromJbiXml(providesNode); 355 QName serviceName = getServiceNameFromJbiXml(providesNode); 356 ep = context.activateEndpoint(serviceName, endpointName); 357 setServiceDescription(ep, serviceDesc); 358 setServiceUnitEndpoint(serviceUnitName, ep); 359 addServiceForInterface(interfaceName, ep); 360 } 361 return ep; 362 } 363 364 373 protected void deactivateEndpointsFromJBIDescription(String serviceUnitName) 374 throws JBIException { 375 ServiceEndpoint ep = getServiceUnitEndpoint(serviceUnitName); 376 if (ep != null) { 377 context.deactivateEndpoint(ep); 378 removeServiceDescription(ep); 379 removeServiceUnitEndpoint(serviceUnitName); 380 } 381 } 382 383 389 protected abstract void deploy(Map <String , String > extensions); 390 391 398 protected String getEndpointNameFromJbiXml(Node providesNode) { 399 String result = new String (); 400 result = XMLHelper.getAttributeValue(providesNode, "endpoint-name"); 401 return result; 402 } 403 404 412 protected Map <String , String > getExtensions(Node servicesNode) { 413 Map <String , String > extensions = new HashMap <String , String >(); 414 NodeList nl = servicesNode.getChildNodes(); 415 String prefix = XMLHelper.getPrefixForNamespaceURI(servicesNode, 416 EXTENSIONS_XMLNS, false); 417 for (int i = 0; i < nl.getLength(); i++) { 418 Node node = nl.item(i); 419 if (node.getNodeType() == Node.ELEMENT_NODE 420 && node.getNodeName().startsWith(prefix)) { 421 extensions.put(node.getNodeName().replaceFirst(prefix, ""), 422 node.getTextContent()); 423 } 424 } 425 return extensions; 426 } 427 428 435 protected QName getInterfaceNameFromJbiXml(Node providesNode) { 436 QName result = null; 437 result = QName.valueOf(XMLHelper.getAttributeValue(providesNode, 438 "interface-name")); 439 return result; 440 } 441 442 452 protected Node getNode(File jbiXmlFile, String nodeName) { 453 Document jbiDoc = WSDLHelper.createDocumentFromWSDL(jbiXmlFile); 454 Node providesNode = XMLHelper.findChild(jbiDoc, nodeName, true); 455 return providesNode; 456 } 457 458 465 protected QName getServiceNameFromJbiXml(Node providesNode) { 466 QName result = null; 467 result = QName.valueOf(XMLHelper.getAttributeValue(providesNode, 468 "service-name")); 469 return result; 470 } 471 472 478 protected Document getServiceUnitWSDL(String suRootPath) { 479 Document result = null; 480 File [] files = new File (suRootPath).listFiles(); 481 for (File file : files) { 482 if (file.getName().endsWith(".wsdl")) { 483 result = WSDLHelper.createDocumentFromWSDL(file); 484 break; 485 } 486 } 487 return result; 488 } 489 } 490 | Popular Tags |