1 22 23 package org.objectweb.petals.component.common.su; 24 25 import java.io.File ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.logging.Level ; 34 import java.util.logging.Logger ; 35 36 import javax.jbi.JBIException; 37 import javax.jbi.component.ComponentContext; 38 import javax.jbi.component.ServiceUnitManager; 39 import javax.jbi.management.DeploymentException; 40 import javax.jbi.servicedesc.ServiceEndpoint; 41 42 import org.objectweb.petals.component.common.AbstractComponent; 43 import org.objectweb.petals.component.common.PEtALSComponentSDKException; 44 import org.objectweb.petals.component.common.util.ManagementMessageUtil; 45 import org.objectweb.petals.component.common.util.PetalsExtensionsUtil; 46 import org.objectweb.petals.component.common.util.WSDLHelper; 47 import org.objectweb.petals.tools.jbicommon.descriptor.Consumes; 48 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor; 49 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptorBuilder; 50 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptorException; 51 import org.objectweb.petals.tools.jbicommon.descriptor.Provides; 52 import org.w3c.dom.Document ; 53 54 62 public class SimpleServiceUnitManager implements ServiceUnitManager { 63 64 private static final String JBI_XML = "jbi.xml"; 65 66 private static final String META_INF = "META-INF"; 67 68 private Map <ServiceEndpoint, ServiceUnitDataHandler> suHandlerCache; 69 70 73 private final ComponentContext context; 74 75 78 private Logger logger; 80 private final Map <String , ServiceUnitDataHandler> serviceUnitDatas; 81 82 private final AbstractComponent component; 83 84 public SimpleServiceUnitManager(AbstractComponent engine, 85 ComponentContext context, Logger logger) { 86 super(); 87 this.context = context; 88 this.logger = logger; 89 this.component = engine; 90 serviceUnitDatas = new HashMap <String , ServiceUnitDataHandler>(); 91 suHandlerCache = new HashMap <ServiceEndpoint, ServiceUnitDataHandler>(); 92 } 93 94 public String deploy(final String serviceUnitName, final String suRootPath) 95 throws DeploymentException { 96 logger.log(Level.FINE, "deploy serviceUnitName " + serviceUnitName 97 + "serviceUnitRootPath" + suRootPath); 98 if (serviceUnitDatas.get(serviceUnitName) == null) { 99 ServiceUnitDataHandler handler = new ServiceUnitDataHandler(); 100 handler.setInstallRoot(suRootPath); 101 handler.setName(serviceUnitName); 102 File jbiXmlFile = new File (suRootPath + File.separator + META_INF 103 + File.separator + JBI_XML); 104 JBIDescriptor descriptor = null; 105 try { 106 descriptor = new JBIDescriptorBuilder(jbiXmlFile.toURI(), 107 logger).build(); 108 } catch (JBIDescriptorException e) { 109 String msg = "Bad JBI Descriptor"; 110 logger.log(Level.SEVERE, msg); 111 throw new DeploymentException(msg, e); 112 } 113 if (descriptor != null) { 114 handler.setDescriptor(descriptor); 115 116 try { 117 processConsumesNodes(handler); 118 } catch (JBIException ex) { 119 String msg = "Failed to process consumes nodes"; 120 logger.log(Level.SEVERE, msg, ex); 121 throw new DeploymentException(msg, ex); 122 } 123 124 serviceUnitDatas.put(serviceUnitName, handler); 125 126 if (component.getServiceUnitListener() != null) { 127 try { 128 component.getServiceUnitListener().onSUDeployed( 129 serviceUnitName, suRootPath, descriptor); 130 } catch (PEtALSComponentSDKException e) { 131 serviceUnitDatas.remove(serviceUnitName); 133 134 throw new DeploymentException( 135 "The ServiceUnit listener failed to process.", e); 136 } 137 } 138 } 139 } else { 140 throw new DeploymentException( 141 "A service unit with the given name is already registered"); 142 } 143 String result = ManagementMessageUtil.getComponentTaskResult(context 144 .getComponentName(), "deploy", 145 ManagementMessageUtil.TASK_RESULT_SUCCESS); 146 return result; 147 } 148 149 156 public Consumes getConsumesFromAddress(final String address) { 157 Consumes result = null; 158 for (Iterator iter = serviceUnitDatas.values().iterator(); iter 159 .hasNext() 160 && result == null;) { 161 ServiceUnitDataHandler handler = (ServiceUnitDataHandler) iter 162 .next(); 163 Map <String , Consumes> addressToConsumes = handler 164 .getAddressToConsumes(); 165 result = addressToConsumes.get(address); 166 } 167 return result; 168 } 169 170 177 public Provides getProvidesFromEndpoint( 178 final ServiceEndpoint serviceEndpoint) { 179 Provides result = null; 180 for (Iterator iter = serviceUnitDatas.values().iterator(); iter 181 .hasNext() 182 && result == null;) { 183 ServiceUnitDataHandler handler = (ServiceUnitDataHandler) iter 184 .next(); 185 Map <ServiceEndpoint, Provides> epJBIDescs = handler.getEpJBIDesc(); 186 result = epJBIDescs.get(serviceEndpoint); 187 } 188 return result; 189 } 190 191 199 public Document getServiceDescription(final ServiceEndpoint serviceEndpoint) { 200 Document result = null; 201 for (Iterator iter = serviceUnitDatas.values().iterator(); iter 202 .hasNext() 203 && result == null;) { 204 ServiceUnitDataHandler handler = (ServiceUnitDataHandler) iter 205 .next(); 206 Map <ServiceEndpoint, Document > epServDescs = handler 207 .getEpServiceDesc(); 208 result = epServDescs.get(serviceEndpoint); 209 } 210 return result; 211 } 212 213 public void init(final String serviceUnitName, final String suRootPath) 214 throws DeploymentException { 215 logger.log(Level.FINE, "init serviceUnitName " + serviceUnitName 216 + " serviceUnitRootPath " + suRootPath); 217 } 218 219 public void shutDown(final String serviceUnitName) 220 throws DeploymentException { 221 logger.log(Level.FINE, "shutdown serviceUnitName " + serviceUnitName); 222 } 223 224 public void start(final String serviceUnitName) throws DeploymentException { 225 logger.log(Level.FINE, "start serviceUnitName " + serviceUnitName); 226 ServiceUnitDataHandler handler = serviceUnitDatas.get(serviceUnitName); 227 try { 228 processProvidesNodes(handler); 229 } catch (JBIException ex) { 230 String msg = "Failed to start service unit : " + serviceUnitName; 231 logger.log(Level.SEVERE, msg, ex); 232 throw new DeploymentException(msg, ex); 233 } 234 if (component.getServiceUnitListener() != null) { 235 try { 236 component.getServiceUnitListener().onSUStarted(serviceUnitName); 237 } catch (PEtALSComponentSDKException e) { 238 throw new DeploymentException( 239 "The ServiceUnit listener failed to process.", e); 240 } 241 } 242 } 243 244 public void stop(final String serviceUnitName) throws DeploymentException { 245 if (component.getServiceUnitListener() != null) { 246 try { 247 component.getServiceUnitListener().onSUStopped(serviceUnitName); 248 } catch (PEtALSComponentSDKException e) { 249 throw new DeploymentException( 250 "The ServiceUnit listener failed to process.", e); 251 } 252 } 253 try { 254 ServiceUnitDataHandler handler = serviceUnitDatas 255 .get(serviceUnitName); 256 List <ServiceEndpoint> endpoints = getServiceUnitEndpoints(serviceUnitName); 257 for (ServiceEndpoint endpoint : endpoints) { 258 context.deactivateEndpoint(endpoint); 259 handler.removeEndpoint(endpoint); 260 } 261 262 } catch (JBIException e) { 263 throw new DeploymentException("Deactivate endpoint failed", e); 264 } 265 logger.log(Level.FINE, "stop serviceUnitName " + serviceUnitName); 266 } 267 268 public String undeploy(final String serviceUnitName, 269 final String serviceUnitRootPath) throws DeploymentException { 270 ServiceUnitDataHandler handler = serviceUnitDatas.get(serviceUnitName); 271 272 if (component.getServiceUnitListener() != null) { 273 try { 274 component.getServiceUnitListener().onSUUndeployed( 275 serviceUnitName, serviceUnitRootPath, 276 handler.getDescriptor()); 277 } catch (PEtALSComponentSDKException e) { 278 throw new DeploymentException( 279 "The ServiceUnit listener failed to process.", e); 280 } 281 } 282 283 for (ServiceEndpoint se : suHandlerCache.keySet()) { 285 if (suHandlerCache.get(se).equals(handler)) { 286 suHandlerCache.remove(se); 287 } 288 } 289 290 serviceUnitDatas.remove(serviceUnitName); 291 return ManagementMessageUtil.getComponentTaskResult(context 292 .getComponentName(), "undeploy", 293 ManagementMessageUtil.TASK_RESULT_SUCCESS); 294 } 295 296 308 @SuppressWarnings ("unchecked") 309 protected Document getEnpointWSDLDesc(String installRoot, Provides provides) { 310 311 Document wsdlDesc = null; 312 313 String wsdlFileLocation = PetalsExtensionsUtil 314 .extractValueFromKeyValueExtension(provides.getExtensions(), 315 PetalsExtensionsUtil.WSDL_LOCATION); 316 317 if (wsdlFileLocation != null) { 318 319 if (wsdlFileLocation.toLowerCase().startsWith("http:") 320 || wsdlFileLocation.toLowerCase().startsWith("https:") 321 || wsdlFileLocation.toLowerCase().startsWith("file:")) { 322 323 try { 324 URL url = new URL (wsdlFileLocation); 325 wsdlDesc = WSDLHelper.createDocumentFromWSDL(url); 326 } catch (MalformedURLException e) { 327 } 328 329 } else { 330 if (!wsdlFileLocation.startsWith(File.separator)) { 332 wsdlFileLocation = File.separator + wsdlFileLocation; 333 } 334 335 File wsdlFile = new File (installRoot + wsdlFileLocation); 336 wsdlDesc = WSDLHelper.createDocumentFromWSDL(wsdlFile); 337 } 338 } 339 340 if (wsdlDesc == null) { 342 wsdlDesc = WSDLHelper.createLightWSDL20( 343 provides.getInterfaceName(), provides.getServiceName(), 344 provides.getEndpointName()); 345 } 346 347 return wsdlDesc; 348 } 349 350 protected void processConsumesNode(final ServiceUnitDataHandler handler, 351 final Consumes consumes) throws JBIException { 352 String address = PetalsExtensionsUtil 353 .extractValueFromKeyValueExtension(consumes.getExtensions(), 354 PetalsExtensionsUtil.ADDRESS); 355 356 if (address != null) { 357 handler.addAddressToConsumes(address, consumes); 358 } 359 } 360 361 protected void processConsumesNodes(final ServiceUnitDataHandler handler) 362 throws JBIException { 363 JBIDescriptor descriptor = handler.getDescriptor(); 364 List <Consumes> consumesList = descriptor.getServices().getConsumes(); 365 for (Consumes consumes : consumesList) { 366 processConsumesNode(handler, consumes); 367 } 368 } 369 370 protected ServiceEndpoint processProvidesNode( 371 final ServiceUnitDataHandler handler, final Provides provides) 372 throws JBIException { 373 ServiceEndpoint ep = context.activateEndpoint( 374 provides.getServiceName(), provides.getEndpointName()); 375 handler.addEndpointDescription(ep, getEnpointWSDLDesc(handler 376 .getInstallRoot(), provides)); 377 handler.addJBIDescription(ep, provides); 378 return ep; 379 } 380 381 protected void processProvidesNodes(final ServiceUnitDataHandler handler) 382 throws JBIException { 383 JBIDescriptor descriptor = handler.getDescriptor(); 384 List <Provides> providesList = descriptor.getServices().getProvides(); 385 for (Provides provides : providesList) { 386 processProvidesNode(handler, provides); 387 } 388 } 389 390 private List <ServiceEndpoint> getServiceUnitEndpoints( 391 final String serviceUnitName) { 392 return new ArrayList <ServiceEndpoint>(serviceUnitDatas.get( 393 serviceUnitName).getEpServiceDesc().keySet()); 394 } 395 396 401 public Map <String , ServiceUnitDataHandler> getServiceUnitDatas() { 402 return serviceUnitDatas; 403 } 404 405 410 public Logger getLogger() { 411 return logger; 412 } 413 414 422 public String getSUInstallRootForActivatedEp(ServiceEndpoint ep) { 423 ServiceUnitDataHandler handler = getSuDataHandlerForEp(ep); 424 return handler.getInstallRoot(); 425 } 426 427 435 public ServiceUnitDataHandler getSuDataHandlerForEp(ServiceEndpoint ep) { 436 437 ServiceUnitDataHandler result = suHandlerCache.get(ep); 438 439 if (result == null) { 440 for (Iterator iter = getServiceUnitDatas().values().iterator(); iter 441 .hasNext() 442 && result == null;) { 443 ServiceUnitDataHandler element = (ServiceUnitDataHandler) iter 444 .next(); 445 for (Iterator iterator2 = element.getEpJBIDesc().keySet() 446 .iterator(); iterator2.hasNext() && result == null;) { 447 ServiceEndpoint element2 = (ServiceEndpoint) iterator2 448 .next(); 449 if (element2.equals(ep)) { 450 result = element; 451 } 452 } 453 } 454 } 455 return result; 456 } 457 458 } 459 | Popular Tags |