1 22 package org.objectweb.petals.component.common.basic.binding; 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.component.ComponentContext; 33 import javax.jbi.management.DeploymentException; 34 import javax.jbi.servicedesc.ServiceEndpoint; 35 import javax.xml.namespace.QName ; 36 37 import org.objectweb.petals.component.common.basic.AbstractServiceUnitManager; 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.Node ; 40 41 50 public class SimpleBindingComponentServiceUnitManager extends 51 AbstractServiceUnitManager { 52 53 protected static final String NO_ENDPOINT_FOR_CONSUMES_NODE = "No endpoint was found matching information from the consumes node of the JBI descriptor"; 54 55 protected Map <ServiceEndpoint, String > endpointAddress; 56 57 protected Map <String , Set <String >> extAddressesForSU; 58 59 protected ExternalListenerManager externalListenerManager; 60 61 64 public SimpleBindingComponentServiceUnitManager() { 65 super(); 66 endpointAddress = new HashMap <ServiceEndpoint, String >(); 67 extAddressesForSU = new HashMap <String , Set <String >>(); 68 } 69 70 public void setExternalListenerManager( 71 ExternalListenerManager externalListenerManager) { 72 this.externalListenerManager = externalListenerManager; 73 } 74 75 85 public SimpleBindingComponentServiceUnitManager(ComponentContext context, 86 Logger log) 87 throws DeploymentException { 88 super(context, log); 89 endpointAddress = new HashMap <ServiceEndpoint, String >(); 90 extAddressesForSU = new HashMap <String , Set <String >>(); 91 } 92 93 101 public String getAddressForEndpoint(ServiceEndpoint ep) { 102 return endpointAddress.get(ep); 103 } 104 105 113 public ServiceEndpoint getEndpointForAddress(String address) { 114 for (Map.Entry <ServiceEndpoint, String > entry : endpointAddress.entrySet()) { 115 if (entry.getValue() != null && entry.getValue().equals(address)){ 116 return entry.getKey(); 117 } 118 } 119 return null; 120 } 121 122 128 @Override 129 public void start(String serviceUnitName) throws DeploymentException { 130 logger.log(Level.FINE, "start serviceUnitName " + serviceUnitName); 131 String suRootPath = serviceUnitInstallationRootPath 132 .get(serviceUnitName); 133 Document serviceDesc = getServiceUnitWSDL(suRootPath); 134 try { 135 ServiceEndpoint ep = activateEndpointsFromJBIDescription( 136 serviceDesc, serviceUnitName, suRootPath); 137 registerAddressForEndpoint(ep, suRootPath); 138 String address = registerEndpointForAddress(suRootPath, 139 serviceUnitName); 140 if (address != null) { 141 externalListenerManager.startListening(address); 142 } 143 } catch (Exception ex) { 144 logger.log(Level.SEVERE, FAILED_ACTIVATE_ENDPOINT + ex, ex); 145 throw new DeploymentException(FAILED_ACTIVATE_ENDPOINT, ex); 146 } 147 } 148 149 154 public void stop(String serviceUnitName) throws DeploymentException { 155 try { 156 serviceUnitInstallationRootPath.remove(serviceUnitName); 157 deactivateEndpointsFromJBIDescription(serviceUnitName); 158 ServiceEndpoint ep = serviceUnitMap.get(serviceUnitName); 159 endpointAddress.remove(ep); 160 Set <String > addresses = extAddressesForSU.get(serviceUnitName); 161 if (addresses != null) { 162 for (String address : addresses) { 163 ServiceEndpoint seToRemove = getEndpointForAddress(address); 164 if(seToRemove!= null){ 165 endpointAddress.remove(seToRemove); 166 } 167 externalListenerManager.stopListening(address); 168 } 169 170 } 171 } catch (Exception e) { 172 throw new DeploymentException(FAILED_DEACTIVATE_ENDPOINT, e); 173 } 174 logger.log(Level.FINE, "stop serviceUnitName " + serviceUnitName); 175 } 176 177 182 @Override 183 protected void deploy(Map <String , String > extensions) { 184 } 185 186 195 protected void registerAddressForEndpoint(ServiceEndpoint ep, 196 String suRootPath) { 197 File jbiXmlFile = new File (suRootPath + File.separator + "META-INF" 198 + File.separator + "jbi.xml"); 199 Node providesNode = getNode(jbiXmlFile, "provides"); 200 if (providesNode != null) { 201 Map <String , String > extensions = getExtensions(providesNode); 202 endpointAddress.put(ep, extensions.get("address")); 203 } 204 } 205 206 225 protected String registerEndpointForAddress(String suRootPath, 226 String serviceUnitName) throws Exception { 227 String address = null; 228 229 File jbiXmlFile = new File (suRootPath + File.separator + "META-INF" 230 + File.separator + "jbi.xml"); 231 Node consumesNode = getNode(jbiXmlFile, "consumes"); 232 if (consumesNode != null) { 233 Map <String , String > extensions = getExtensions(consumesNode); 234 address = extensions.get("address"); 235 if (address != null) { 236 QName interfaceName = getInterfaceNameFromJbiXml(consumesNode); 237 String endpointName = getEndpointNameFromJbiXml(consumesNode); 238 QName serviceName = getServiceNameFromJbiXml(consumesNode); 239 ServiceEndpoint endpoint = context.getEndpoint(serviceName, 240 endpointName); 241 if (endpoint == null) { 242 ServiceEndpoint[] endpoints = context 243 .getEndpoints(interfaceName); 244 if (endpoints.length == 0) { 245 throw new Exception (NO_ENDPOINT_FOR_CONSUMES_NODE); 246 } else { 247 endpointAddress.put(endpoints[0],address); 248 } 249 } else { 250 endpointAddress.put(endpoint,address); 251 } 252 registerAddressForSU(address, serviceUnitName); 253 } 254 } 255 return address; 256 } 257 258 266 protected void registerAddressForSU(String address, String serviceUnitName) { 267 Set <String > addresses = extAddressesForSU.get(serviceUnitName); 269 270 if (addresses == null) { 271 addresses = new HashSet <String >(); 272 addresses.add(address); 273 extAddressesForSU.put(serviceUnitName, addresses); 274 } else { 275 addresses.add(address); 277 } 278 } 279 } 280 | Popular Tags |