1 22 package org.objectweb.petals.component.common.serviceunitmanager.manager; 23 24 import java.io.File ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.logging.Level ; 31 import java.util.logging.Logger ; 32 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.serviceunitmanager.handler.ExternalServiceManager; 40 import org.objectweb.petals.component.common.serviceunitmanager.handler.PetalsServiceUnitHandler; 41 import org.objectweb.petals.component.common.util.ManagementMessageUtil; 42 import org.objectweb.petals.component.common.util.WSDLHelper; 43 import org.objectweb.petals.component.common.util.XMLHelper; 44 45 import org.w3c.dom.Document ; 46 import org.w3c.dom.Node ; 47 48 56 public class PetalsServiceUnitManager implements ServiceUnitManager { 57 58 private static final String NO_HANDLER_FOR_THIS_TYPE = "No registered handlers for this type of service unit "; 59 60 private static final String DUPLICATE_SERVICE_UNIT = "Another service unit with the same name is already deployed "; 61 62 private static final String HANDLER_FOR_THIS_SERVICE_NOT_FOUND = "Handler for this service name unit not found "; 63 64 public static final String EXTENSIONS_XMLNS = "http://petals.objectweb.org/extensions"; 65 66 private Map <ServiceEndpoint, Document > serviceDescriptionMap; 67 68 private Map <String , ServiceEndpoint> serviceUnitMap; 69 70 private Map <String , PetalsServiceUnitHandler> handlersMap; 71 72 private Map <QName , Set <ServiceEndpoint>> servicesByInterfaceMap; 73 74 private Set <PetalsServiceUnitHandler> handlersSet; 75 76 private Map <ServiceEndpoint, PetalsServiceUnitHandler> endpointSUHandler; 77 78 protected ComponentContext context; 79 80 protected Logger logger; 81 82 90 public PetalsServiceUnitManager(ComponentContext context, Logger logger) 91 throws DeploymentException { 92 this.context = context; 93 this.logger = logger; 94 serviceDescriptionMap = new HashMap <ServiceEndpoint, Document >(); 95 serviceUnitMap = new HashMap <String , ServiceEndpoint>(); 96 handlersMap = new HashMap <String , PetalsServiceUnitHandler>(); 97 handlersSet = new HashSet <PetalsServiceUnitHandler>(); 98 handlersSet.add(new ExternalServiceManager(context, logger)); 99 servicesByInterfaceMap = new HashMap <QName , Set <ServiceEndpoint>>(); 100 endpointSUHandler = new HashMap <ServiceEndpoint, PetalsServiceUnitHandler>(); 101 } 102 103 106 public PetalsServiceUnitManager() { 107 serviceDescriptionMap = new HashMap <ServiceEndpoint, Document >(); 108 serviceUnitMap = new HashMap <String , ServiceEndpoint>(); 109 handlersMap = new HashMap <String , PetalsServiceUnitHandler>(); 110 handlersSet = new HashSet <PetalsServiceUnitHandler>(); 111 handlersSet.add(new ExternalServiceManager(context, logger)); 112 servicesByInterfaceMap = new HashMap <QName , Set <ServiceEndpoint>>(); 113 } 114 115 116 122 public void addNewHandler(PetalsServiceUnitHandler suh) { 123 if (!isHandlerPresent(suh.getClass().getName())) { 124 handlersSet.add(suh); 125 } 126 } 127 128 133 public void addServiceForInterface(QName interfaceName, 134 ServiceEndpoint serviceEndpoint) { 135 Set <ServiceEndpoint> endpoints = null; 136 endpoints = servicesByInterfaceMap.get(interfaceName); 137 if (endpoints == null) { 138 endpoints = new HashSet <ServiceEndpoint>(); 139 } 140 endpoints.add(serviceEndpoint); 141 servicesByInterfaceMap.put(interfaceName, endpoints); 142 } 143 144 145 150 public void addSUHandlerForEndpoint(ServiceEndpoint ep, PetalsServiceUnitHandler handler) { 151 endpointSUHandler.put(ep, handler); 152 } 153 154 public String deploy(String serviceUnitName, String serviceUnitRootPath) 155 throws DeploymentException { 156 String result = null; 157 if (serviceUnitMap.get(serviceUnitName) == null) { 158 String serviceUnitType = getServiceUnitType(serviceUnitRootPath); 159 PetalsServiceUnitHandler serviceUnitManager = null; 160 Iterator <PetalsServiceUnitHandler> it = handlersSet.iterator(); 161 while (result == null && it.hasNext()) { 162 serviceUnitManager = it.next(); 163 result = serviceUnitManager.deploy(serviceUnitName, 164 serviceUnitType, serviceUnitRootPath); 165 } 166 if (result != null) { 167 handlersMap.put(serviceUnitName, serviceUnitManager); 168 } else { 169 logger.log(Level.SEVERE, NO_HANDLER_FOR_THIS_TYPE); 170 throw new DeploymentException(NO_HANDLER_FOR_THIS_TYPE); 171 } 172 } else { 173 throw new DeploymentException(DUPLICATE_SERVICE_UNIT); 174 } 175 return result; 176 } 177 178 public ServiceEndpoint findEndpointForService(QName serviceName) { 179 ServiceEndpoint result = null; 180 for(ServiceEndpoint endpoint : serviceUnitMap.values()) { 181 if (endpoint.getServiceName().equals(serviceName)){ 182 result = endpoint; 183 break; 184 } 185 } 186 return result; 187 } 188 189 196 public Document getServiceDescription(ServiceEndpoint se) { 197 Document result = null; 198 for (ServiceEndpoint ep : serviceDescriptionMap.keySet()) { 199 if (se.getServiceName().equals(ep.getServiceName())) { 200 result = serviceDescriptionMap.get(ep); 201 break; 202 } 203 } 204 return result; 205 } 206 207 public Set <ServiceEndpoint> getServicesForInterface(String interfaceName) { 208 return servicesByInterfaceMap.get(interfaceName); 209 } 210 211 218 public ServiceEndpoint getServiceUnitEndpoint(String serviceUnitName) { 219 return serviceUnitMap.get(serviceUnitName); 220 } 221 222 223 228 public PetalsServiceUnitHandler getServiceUnitHandlerForEndpoint(ServiceEndpoint ep) { 229 return endpointSUHandler.get(ep); 230 } 231 232 public void init(String serviceUnitName, String serviceUnitRootPath) 233 throws DeploymentException { 234 235 PetalsServiceUnitHandler serviceUnitManager = handlersMap 236 .get(serviceUnitName); 237 if (serviceUnitManager != null) { 238 serviceUnitManager.init(serviceUnitName, serviceUnitRootPath); 239 } else { 240 logger.log(Level.SEVERE, HANDLER_FOR_THIS_SERVICE_NOT_FOUND); 241 throw new DeploymentException(HANDLER_FOR_THIS_SERVICE_NOT_FOUND); 242 } 243 } 244 245 249 public void removeServiceDescription (ServiceEndpoint ep) { 250 serviceDescriptionMap.remove(ep); 251 } 252 253 public void removeServiceForInterface(ServiceEndpoint serviceEndpoint) { 254 for (Set <ServiceEndpoint> endpoints : servicesByInterfaceMap.values()){ 255 endpoints.remove(serviceEndpoint); 256 } 257 } 258 259 263 public void removeServiceUnitEndpoint (String suName) { 264 serviceUnitMap.remove(suName); 265 } 266 267 271 public void removeSUHandlerForEndpoint(ServiceEndpoint ep) { 272 endpointSUHandler.remove(ep); 273 } 274 275 283 public void setServiceDescription(ServiceEndpoint se, Document serviceDesc) { 284 serviceDescriptionMap.put(se, serviceDesc); 285 } 286 287 295 public void setServiceUnitEndpoint(String serviceUnitName, 296 ServiceEndpoint serviceEP) { 297 serviceUnitMap.put(serviceUnitName, serviceEP); 298 } 299 300 public void shutDown(String serviceUnitName) throws DeploymentException { 301 PetalsServiceUnitHandler serviceUnitManager = handlersMap 302 .get(serviceUnitName); 303 if (serviceUnitManager != null) { 304 serviceUnitManager.shutDown(serviceUnitName, this); 305 } else { 306 logger.log(Level.SEVERE, HANDLER_FOR_THIS_SERVICE_NOT_FOUND); 307 throw new DeploymentException(HANDLER_FOR_THIS_SERVICE_NOT_FOUND); 308 } 309 } 310 311 public void start(String serviceUnitName) throws DeploymentException { 312 PetalsServiceUnitHandler serviceUnitManager = handlersMap 313 .get(serviceUnitName); 314 if (serviceUnitManager != null) { 315 serviceUnitManager.start(serviceUnitName, this); 316 } else { 317 logger.log(Level.SEVERE, HANDLER_FOR_THIS_SERVICE_NOT_FOUND); 318 throw new DeploymentException(HANDLER_FOR_THIS_SERVICE_NOT_FOUND); 319 } 320 } 321 322 public void stop(String serviceUnitName) throws DeploymentException { 323 PetalsServiceUnitHandler serviceUnitManager = handlersMap 324 .get(serviceUnitName); 325 if (serviceUnitManager != null) { 326 serviceUnitManager.stop(serviceUnitName, this); 327 } else { 328 logger.log(Level.SEVERE, HANDLER_FOR_THIS_SERVICE_NOT_FOUND); 329 throw new DeploymentException(HANDLER_FOR_THIS_SERVICE_NOT_FOUND); 330 } 331 } 332 333 public String undeploy(String serviceUnitName, String serviceUnitRootPath) 334 throws DeploymentException { 335 PetalsServiceUnitHandler serviceUnitManager = handlersMap 336 .get(serviceUnitName); 337 if (serviceUnitManager != null) { 338 String result = serviceUnitManager.undeploy(serviceUnitName, 339 serviceUnitRootPath); 340 if (ManagementMessageUtil.getComponentTaskResult( 341 context.getComponentName(), "undeploy", 342 ManagementMessageUtil.TASK_RESULT_SUCCESS).equals(result)) { 343 handlersMap.remove(serviceUnitName); 344 } 345 return result; 346 } else { 347 logger.log(Level.SEVERE, HANDLER_FOR_THIS_SERVICE_NOT_FOUND); 348 throw new DeploymentException(HANDLER_FOR_THIS_SERVICE_NOT_FOUND); 349 } 350 } 351 352 360 protected boolean isHandlerPresent(String className) { 361 for (PetalsServiceUnitHandler suh : handlersSet) { 362 if (suh.getClass().getName().equals(className)) { 363 return true; 364 } 365 } 366 return false; 367 } 368 369 370 377 protected String getServiceUnitType(String suRootPath) { 378 String result = null; 379 File jbiXmlFile = new File (suRootPath + File.separator + "META-INF" 380 + File.separator + "jbi.xml"); 381 if (jbiXmlFile.exists()) { 382 Document configDoc = WSDLHelper.createDocumentFromWSDL(jbiXmlFile); 383 Node node = XMLHelper.findChild(configDoc, "services", true); 384 if (node != null) { 385 if (node != null) { 386 Node typeNode = XMLHelper.findChild(node, EXTENSIONS_XMLNS, 387 "type", true); 388 if (typeNode != null 389 && typeNode.getNodeType() == Node.ELEMENT_NODE) { 390 result = typeNode.getTextContent(); 391 } 392 } 393 } 394 } 395 return result; 396 } 397 398 } 399 | Popular Tags |