1 17 package org.apache.geronimo.j2ee.deployment; 18 19 import java.net.URI ; 20 import java.net.URISyntaxException ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.Set ; 25 import javax.management.MalformedObjectNameException ; 26 import javax.management.ObjectName ; 27 import javax.naming.Reference ; 28 import javax.xml.namespace.QName ; 29 30 import org.apache.geronimo.common.AmbiguousEJBRefException; 31 import org.apache.geronimo.common.DeploymentException; 32 import org.apache.geronimo.common.UnknownEJBRefException; 33 import org.apache.geronimo.common.UnresolvedEJBRefException; 34 import org.apache.geronimo.deployment.DeploymentContext; 35 import org.apache.geronimo.gbean.GBeanData; 36 import org.apache.geronimo.j2ee.j2eeobjectnames.J2eeContext; 37 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 38 import org.apache.geronimo.kernel.Kernel; 39 40 41 44 public class RefContext { 45 46 private final EJBReferenceBuilder ejbReferenceBuilder; 47 private final ResourceReferenceBuilder resourceReferenceBuilder; 48 private final ServiceReferenceBuilder serviceReferenceBuilder; 49 private final Kernel kernel; 50 51 private final Map ejbRemoteIndex; 52 private final Map ejbLocalIndex; 53 private final Map ejbInterfaceIndex; 54 55 private final Map resourceAdapterIndex; 56 private final Map connectionFactoryIndex; 57 private final Map adminObjectIndex; 58 59 private final Map resourceModuleDataMap; 60 61 62 public RefContext(EJBReferenceBuilder ejbReferenceBuilder, ResourceReferenceBuilder resourceReferenceBuilder, ServiceReferenceBuilder serviceReferenceBuilder, Kernel kernel) { 63 assert ejbReferenceBuilder != null: "ejbReferenceBuilder is null"; 64 assert resourceReferenceBuilder != null: "resourceReferenceBuilder is null"; 65 assert serviceReferenceBuilder != null: "serviceReferenceBuilder is null"; 66 67 ejbRemoteIndex = new HashMap (); 68 ejbLocalIndex = new HashMap (); 69 ejbInterfaceIndex = new HashMap (); 70 resourceAdapterIndex = new HashMap (); 71 connectionFactoryIndex = new HashMap (); 72 adminObjectIndex = new HashMap (); 73 resourceModuleDataMap = new HashMap (); 74 this.ejbReferenceBuilder = ejbReferenceBuilder; 75 this.resourceReferenceBuilder = resourceReferenceBuilder; 76 this.serviceReferenceBuilder = serviceReferenceBuilder; 77 this.kernel = kernel; 78 } 79 80 public static RefContext derivedClientRefContext(RefContext refContext, EJBReferenceBuilder ejbReferenceBuilder, ResourceReferenceBuilder resourceReferenceBuilder, ServiceReferenceBuilder serviceReferenceBuilder) { 81 return new RefContext(refContext, ejbReferenceBuilder, resourceReferenceBuilder, serviceReferenceBuilder); 82 } 83 84 private RefContext(RefContext refContext, EJBReferenceBuilder ejbReferenceBuilder, ResourceReferenceBuilder resourceReferenceBuilder, ServiceReferenceBuilder serviceReferenceBuilder) { 85 assert ejbReferenceBuilder != null: "ejbReferenceBuilder is null"; 86 assert resourceReferenceBuilder != null: "resourceReferenceBuilder is null"; 87 assert refContext != null: "ejbRefContext is null"; 88 89 this.ejbReferenceBuilder = ejbReferenceBuilder; 90 this.resourceReferenceBuilder = resourceReferenceBuilder; 91 this.serviceReferenceBuilder = serviceReferenceBuilder; 92 this.kernel = refContext.kernel; 93 this.ejbRemoteIndex = refContext.ejbRemoteIndex; 94 this.ejbLocalIndex = new HashMap (); this.ejbInterfaceIndex = refContext.ejbInterfaceIndex; 96 resourceAdapterIndex = new HashMap (); 97 this.connectionFactoryIndex = new HashMap (); 98 this.adminObjectIndex = new HashMap (); 99 this.resourceModuleDataMap = new HashMap (); 100 } 101 102 public EJBReferenceBuilder getEjbReferenceBuilder() { 103 return ejbReferenceBuilder; 104 } 105 106 public Map getEJBRemoteIndex() { 107 return ejbRemoteIndex; 108 } 109 110 public Map getEJBLocalIndex() { 111 return ejbLocalIndex; 112 } 113 114 public Map getConnectionFactoryIndex() { 115 return connectionFactoryIndex; 116 } 117 118 public Map getAdminObjectIndex() { 119 return adminObjectIndex; 120 } 121 122 public void addEJBRemoteId(URI modulePath, String name, String containerId, boolean isSession, String home, String remote) throws DeploymentException { 123 Map references = (Map ) ejbRemoteIndex.get(name); 124 if (references == null || references.isEmpty()) { 125 references = new HashMap (); 126 ejbRemoteIndex.put(name, references); 127 } 128 129 EJBRefInfo ejbRefInfo = new EJBRefInfo(false, isSession, home, remote); 130 Map interfacesReferences = (Map ) ejbInterfaceIndex.get(ejbRefInfo); 131 if (interfacesReferences == null || interfacesReferences.isEmpty()) { 132 interfacesReferences = new HashMap (); 133 ejbInterfaceIndex.put(ejbRefInfo, interfacesReferences); 134 } 135 136 addEJBId(modulePath, name, containerId, references, interfacesReferences); 137 } 138 139 public void addEJBLocalId(URI modulePath, String name, String containerId, boolean isSession, String localHome, String local) throws DeploymentException { 140 Map references = (Map ) ejbLocalIndex.get(name); 141 if (references == null || references.isEmpty()) { 142 references = new HashMap (); 143 ejbLocalIndex.put(name, references); 144 } 145 146 EJBRefInfo ejbRefInfo = new EJBRefInfo(true, isSession, localHome, local); 147 Map interfacesReferences = (Map ) ejbInterfaceIndex.get(ejbRefInfo); 148 if (interfacesReferences == null || interfacesReferences.isEmpty()) { 149 interfacesReferences = new HashMap (); 150 ejbInterfaceIndex.put(ejbRefInfo, interfacesReferences); 151 } 152 153 addEJBId(modulePath, name, containerId, references, interfacesReferences); 154 } 155 156 private void addEJBId(URI modulePath, String name, String containerId, Map references, Map interfacesReferences) throws DeploymentException { 157 try { 158 URI ejbURI = new URI (null, null, modulePath.getPath(), name); 159 references.put(ejbURI, containerId); 160 URI moduleURI = new URI (null, null, modulePath.getPath(), null); 161 interfacesReferences.put(moduleURI, containerId); 162 } catch (URISyntaxException e) { 163 throw new DeploymentException(e); 164 } 165 } 166 167 public void addResourceAdapterId(URI modulePath, String name, String containerId) throws DeploymentException { 168 Map references = (Map ) resourceAdapterIndex.get(name); 169 if (references == null || references.isEmpty()) { 170 references = new HashMap (); 171 resourceAdapterIndex.put(name, references); 172 } 173 174 try { 175 URI cfURI = new URI (null, null, modulePath.getPath(), name); 176 references.put(cfURI, containerId); 177 } catch (URISyntaxException e) { 178 throw new DeploymentException(e); 179 } 180 } 181 182 public void addConnectionFactoryId(URI modulePath, String name, String containerId) throws DeploymentException { 183 Map references = (Map ) connectionFactoryIndex.get(name); 184 if (references == null || references.isEmpty()) { 185 references = new HashMap (); 186 connectionFactoryIndex.put(name, references); 187 } 188 189 try { 190 URI cfURI = new URI (null, null, modulePath.getPath(), name); 191 references.put(cfURI, containerId); 192 } catch (URISyntaxException e) { 193 throw new DeploymentException(e); 194 } 195 } 196 197 198 public void addAdminObjectId(URI modulePath, String name, String containerId) throws DeploymentException { 199 Map references = (Map ) adminObjectIndex.get(name); 200 if (references == null || references.isEmpty()) { 201 references = new HashMap (); 202 adminObjectIndex.put(name, references); 203 } 204 205 try { 206 URI cfURI = new URI (null, null, modulePath.getPath(), name); 207 references.put(cfURI, containerId); 208 } catch (URISyntaxException e) { 209 throw new DeploymentException(e); 210 } 211 } 212 213 214 215 217 public Reference getEJBRemoteRef(String objectName, boolean isSession, String home, String remote) throws DeploymentException { 218 return ejbReferenceBuilder.createEJBRemoteReference(objectName, isSession, home, remote); 219 } 220 221 public Reference getCORBARemoteRef(URI corbaURL, String objectName, ObjectName containerName, String home) throws DeploymentException { 222 return ejbReferenceBuilder.createCORBAReference(corbaURL, objectName, containerName, home); 223 } 224 225 public Reference getEJBLocalRef(String objectName, boolean isSession, String localHome, String local) throws DeploymentException { 226 return ejbReferenceBuilder.createEJBLocalReference(objectName, isSession, localHome, local); 227 } 228 229 public Object getHandleDelegateReference() throws DeploymentException { 230 return ejbReferenceBuilder.createHandleDelegateReference(); 231 } 232 233 public Reference getEJBRemoteRef(URI module, String ejbLink, boolean isSession, String home, String remote) throws DeploymentException { 234 String name = ejbLink.substring(ejbLink.lastIndexOf('#') + 1); 235 String containerId = getContainerId(module, ejbLink, (Map ) ejbRemoteIndex.get(name)); 236 return getEJBRemoteRef(containerId, isSession, home, remote); 237 } 238 239 public Reference getEJBLocalRef(URI module, String ejbLink, boolean isSession, String localHome, String local) throws DeploymentException { 240 String name = ejbLink.substring(ejbLink.lastIndexOf('#') + 1); 241 String containerId = getContainerId(module, ejbLink, (Map ) ejbLocalIndex.get(name)); 242 return getEJBLocalRef(containerId, isSession, localHome, local); 243 } 244 245 public Reference getConnectionFactoryRef(String containerId, Class iface) throws DeploymentException { 246 return resourceReferenceBuilder.createResourceRef(containerId, iface); 247 } 248 249 public String getResourceAdapterContainerId(URI module, String resourceLink, J2eeContext j2eeContext) throws DeploymentException, UnknownEJBRefException { 250 String name = resourceLink.substring(resourceLink.lastIndexOf('#') + 1); 251 try { 252 return getContainerId(module, resourceLink, (Map ) resourceAdapterIndex.get(name)); 253 } catch (UnknownEJBRefException e) { 254 ObjectName query = null; 255 try { 256 query = NameFactory.getComponentRestrictedQueryName(null, null, name, NameFactory.JCA_RESOURCE_ADAPTER, j2eeContext); 257 } catch (MalformedObjectNameException e1) { 258 throw new DeploymentException("Could not construct resource adapter object name query", e); 259 } 260 ObjectName containerName = locateUniqueName(query, "resource"); 261 return containerName.getCanonicalName(); 262 } 263 } 264 265 public String getConnectionFactoryContainerId(URI module, String resourceLink, String type, J2eeContext j2eeContext, DeploymentContext context) throws DeploymentException, UnknownEJBRefException { 266 String name = resourceLink.substring(resourceLink.lastIndexOf('#') + 1); 267 try { 268 return getContainerId(module, resourceLink, (Map ) connectionFactoryIndex.get(name)); 269 } catch (UnknownEJBRefException e) { 270 271 ObjectName query = null; 272 try { 273 query = NameFactory.getComponentNameQuery(null, null, null, name, type, j2eeContext); 274 } catch (MalformedObjectNameException e1) { 275 throw new DeploymentException("Could not construct connection factory object name query", e); 276 } 277 Set matches = context.listGBeans(query); 278 if (matches.size() > 1) { 279 throw new DeploymentException("More than one match for query " + matches); 280 } 281 if (matches.size() == 1) { 282 return ((ObjectName ) matches.iterator().next()).getCanonicalName(); 283 } 284 try { 285 query = NameFactory.getComponentRestrictedQueryName(null, null, name, type, j2eeContext); 286 } catch (MalformedObjectNameException e1) { 287 throw new DeploymentException("Could not construct connection factory object name query", e); 288 } 289 ObjectName containerName = locateUniqueName(query, "resource"); 290 return containerName.getCanonicalName(); 291 } 292 } 293 294 public Reference getAdminObjectRef(String containerId, Class iface) throws DeploymentException { 295 return resourceReferenceBuilder.createAdminObjectRef(containerId, iface); 296 } 297 298 public String getAdminObjectContainerId(URI module, String resourceLink, J2eeContext j2eeContext) throws DeploymentException, UnknownEJBRefException { 299 String name = resourceLink.substring(resourceLink.lastIndexOf('#') + 1); 300 try { 301 return getContainerId(module, resourceLink, (Map ) adminObjectIndex.get(name)); 302 } catch (UnknownEJBRefException e) { 303 ObjectName query = null; 304 try { 305 query = NameFactory.getComponentRestrictedQueryName(null, null, name, NameFactory.JCA_ADMIN_OBJECT, j2eeContext); 306 } catch (MalformedObjectNameException e1) { 307 throw new DeploymentException("Could not construct admin object object name query", e); 308 } 309 ObjectName containerName = locateUniqueName(query, "resource"); 310 return containerName.getCanonicalName(); 311 } 312 } 313 314 public Object getServiceReference(Class serviceInterface, URI wsdlURI, URI jaxrpcMappingURI, QName serviceQName, Map portComponentRefMap, List handlerInfos, Map portLocationMap, Map credentialsNameMap, DeploymentContext deploymentContext, Module module, ClassLoader classLoader) throws DeploymentException { 315 return serviceReferenceBuilder.createService(serviceInterface, wsdlURI, jaxrpcMappingURI, serviceQName, portComponentRefMap, handlerInfos, portLocationMap, credentialsNameMap, deploymentContext, module, classLoader); 316 } 317 318 private String getContainerId(URI module, String ejbLink, Map references) throws AmbiguousEJBRefException, UnknownEJBRefException { 319 if (references == null || references.isEmpty()) { 320 throw new UnknownEJBRefException(ejbLink); 321 } 322 323 if (ejbLink.indexOf('#') >= 0) { 325 URI ejbURI = module.resolve(ejbLink).normalize(); 326 String ejbRef = (String ) references.get(ejbURI); 327 if (ejbRef == null) { 328 throw new UnknownEJBRefException(ejbLink); 329 } 330 return ejbRef; 331 } 332 333 337 if (references.size() == 1) { 339 String ejbRef = (String ) references.values().iterator().next(); 340 if (ejbRef == null) { 341 throw new UnknownEJBRefException(ejbLink); 342 } 343 return ejbRef; 344 } 345 346 String ejbRef = (String ) references.get(module.resolve("#" + ejbLink)); 348 if (ejbRef != null) { 349 return ejbRef; 350 } 351 352 throw new AmbiguousEJBRefException(ejbLink); 354 } 355 356 public Reference getImplicitEJBRemoteRef(URI module, String refName, boolean isSession, String home, String remote) throws DeploymentException { 357 EJBRefInfo ejbRefInfo = new EJBRefInfo(false, isSession, home, remote); 358 String containerId = getImplicitContainerId(module, refName, ejbRefInfo); 359 return getEJBRemoteRef(containerId, isSession, home, remote); 360 } 361 362 public Reference getImplicitEJBLocalRef(URI module, String refName, boolean isSession, String localHome, String local) throws DeploymentException { 363 EJBRefInfo ejbRefInfo = new EJBRefInfo(true, isSession, localHome, local); 364 String containerId = getImplicitContainerId(module, refName, ejbRefInfo); 365 return getEJBLocalRef(containerId, isSession, localHome, local); 366 } 367 368 private String getImplicitContainerId(URI module, String refName, EJBRefInfo ejbRefInfo) throws DeploymentException { 369 Map references = (Map ) ejbInterfaceIndex.get(ejbRefInfo); 370 371 if (references == null || references.isEmpty()) { 373 throw new UnresolvedEJBRefException(refName, ejbRefInfo.isLocal(), ejbRefInfo.isSession(), ejbRefInfo.getHomeIntf(), ejbRefInfo.getBeanIntf(), false); 374 } 375 376 if (references.size() == 1) { 378 return (String ) references.values().iterator().next(); 379 } 380 381 String ejbRef = (String ) references.get(module); 383 if (ejbRef != null) { 384 return ejbRef; 385 } 386 387 throw new UnresolvedEJBRefException(refName, ejbRefInfo.isLocal(), ejbRefInfo.isSession(), ejbRefInfo.getHomeIntf(), ejbRefInfo.getBeanIntf(), true); 389 } 390 391 393 public void addResourceAdapterModuleInfo(ObjectName resourceModuleName, GBeanData resourceModuleData) throws DeploymentException { 394 Object old = resourceModuleDataMap.put(resourceModuleName, resourceModuleData); 395 if (old != null) { 396 throw new DeploymentException("Duplicate resource adapter module name: " + resourceModuleName); 397 } 398 } 399 400 public GBeanData getResourceAdapterGBeanData(ObjectName resourceAdapterModuleName) throws DeploymentException { 401 GBeanData resourceModuleData = (GBeanData) resourceModuleDataMap.get(resourceAdapterModuleName); 402 if (resourceModuleData != null) { 403 return (GBeanData) resourceModuleData.getAttribute("resourceAdapterGBeanData"); 404 } 405 return resourceReferenceBuilder.locateResourceAdapterGBeanData(resourceAdapterModuleName); 406 } 407 408 public GBeanData getActivationSpecInfo(ObjectName resourceAdapterModuleName, String messageListenerInterfaceName) throws DeploymentException { 409 GBeanData resourceModuleData = (GBeanData) resourceModuleDataMap.get(resourceAdapterModuleName); 410 if (resourceModuleData != null) { 411 Map activationSpecInfoMap = (Map ) resourceModuleData.getAttribute("activationSpecInfoMap"); 412 return (GBeanData) activationSpecInfoMap.get(messageListenerInterfaceName); 413 } 414 return resourceReferenceBuilder.locateActivationSpecInfo(resourceAdapterModuleName, messageListenerInterfaceName); 415 } 416 417 public GBeanData getAdminObjectInfo(ObjectName resourceAdapterModuleName, String adminObjectInterfaceName) throws DeploymentException { 418 GBeanData resourceModuleData = (GBeanData) resourceModuleDataMap.get(resourceAdapterModuleName); 419 if (resourceModuleData != null) { 420 Map adminObjectInfoMap = (Map ) resourceModuleData.getAttribute("adminObjectInfoMap"); 421 return (GBeanData) adminObjectInfoMap.get(adminObjectInterfaceName); 422 } 423 return resourceReferenceBuilder.locateAdminObjectInfo(resourceAdapterModuleName, adminObjectInterfaceName); 424 } 425 426 public GBeanData getConnectionFactoryInfo(ObjectName resourceAdapterModuleName, String connectionFactoryInterfaceName) throws DeploymentException { 427 GBeanData resourceModuleData = (GBeanData) resourceModuleDataMap.get(resourceAdapterModuleName); 428 if (resourceModuleData != null) { 429 Map managedConnectionFactoryInfoMap = (Map ) resourceModuleData.getAttribute("managedConnectionFactoryInfoMap"); 430 return (GBeanData) managedConnectionFactoryInfoMap.get(connectionFactoryInterfaceName); 431 } 432 return resourceReferenceBuilder.locateConnectionFactoryInfo(resourceAdapterModuleName, connectionFactoryInterfaceName); 433 } 434 435 public GBeanData getResourceAdapterModuleData(ObjectName resourceAdapterModuleName) { 436 return (GBeanData) resourceModuleDataMap.get(resourceAdapterModuleName); 437 } 438 439 public String getMEJBName() throws DeploymentException { 440 ObjectName query = null; 441 try { 442 query = ObjectName.getInstance("*:name=ejb/mgmt/MEJB,*"); 443 } catch (MalformedObjectNameException e) { 444 throw new DeploymentException("We built this name..."); 445 } 446 ObjectName mejbName = locateUniqueName(query, "Management EJB"); 447 return mejbName.getCanonicalName(); 448 } 449 450 private ObjectName locateUniqueName(ObjectName query, String type) throws DeploymentException { 451 Set names = kernel.listGBeans(query); 452 if (names.size() != 1) { 453 throw new DeploymentException("Unknown or ambiguous " + type + " name query: " + query + " match count: " + names.size()); 454 } 455 return (ObjectName ) names.iterator().next(); 456 } 457 458 } 459 | Popular Tags |