1 17 18 package org.apache.geronimo.connector.deployment; 19 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import javax.naming.Reference ; 25 import javax.xml.namespace.QName ; 26 27 import org.apache.geronimo.common.DeploymentException; 28 import org.apache.geronimo.common.UnresolvedReferenceException; 29 import org.apache.geronimo.gbean.AbstractNameQuery; 30 import org.apache.geronimo.gbean.GBeanInfo; 31 import org.apache.geronimo.gbean.GBeanInfoBuilder; 32 import org.apache.geronimo.j2ee.deployment.Module; 33 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 34 import org.apache.geronimo.kernel.GBeanNotFoundException; 35 import org.apache.geronimo.kernel.config.Configuration; 36 import org.apache.geronimo.kernel.repository.Environment; 37 import org.apache.geronimo.naming.deployment.AbstractNamingBuilder; 38 import org.apache.geronimo.naming.reference.ResourceReference; 39 import org.apache.geronimo.xbeans.geronimo.naming.GerMessageDestinationDocument; 40 import org.apache.geronimo.xbeans.geronimo.naming.GerMessageDestinationType; 41 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType; 42 import org.apache.geronimo.xbeans.geronimo.naming.GerResourceEnvRefDocument; 43 import org.apache.geronimo.xbeans.geronimo.naming.GerResourceEnvRefType; 44 import org.apache.geronimo.xbeans.j2ee.MessageDestinationRefType; 45 import org.apache.geronimo.xbeans.j2ee.MessageDestinationType; 46 import org.apache.geronimo.xbeans.j2ee.ResourceEnvRefType; 47 import org.apache.xmlbeans.QNameSet; 48 import org.apache.xmlbeans.XmlObject; 49 50 53 public class AdminObjectRefBuilder extends AbstractNamingBuilder { 54 private final QNameSet adminOjbectRefQNameSet; 55 private final QNameSet messageDestinationQNameSet; 56 private final QNameSet messageDestinationRefQNameSet; 57 58 private static final QName GER_ADMIN_OBJECT_REF_QNAME = GerResourceEnvRefDocument.type.getDocumentElementName(); 59 private static final QNameSet GER_ADMIN_OBJECT_REF_QNAME_SET = QNameSet.singleton(GER_ADMIN_OBJECT_REF_QNAME); 60 private static final QName GER_MESSAGE_DESTINATION_QNAME = GerMessageDestinationDocument.type.getDocumentElementName(); 61 private static final QNameSet GER_MESSAGE_DESTINATION_QNAME_SET = QNameSet.singleton(GER_MESSAGE_DESTINATION_QNAME); 62 63 public AdminObjectRefBuilder(Environment defaultEnvironment, String [] eeNamespaces) { 64 super(defaultEnvironment); 65 adminOjbectRefQNameSet = buildQNameSet(eeNamespaces, "resource-env-ref"); 66 messageDestinationQNameSet = buildQNameSet(eeNamespaces, "message-destination"); 67 messageDestinationRefQNameSet = buildQNameSet(eeNamespaces, "message-destination-ref"); 68 } 69 70 protected boolean willMergeEnvironment(XmlObject specDD, XmlObject plan) { 71 return specDD.selectChildren(adminOjbectRefQNameSet).length > 0 || specDD.selectChildren(messageDestinationRefQNameSet).length > 0; 72 } 73 74 public void initContext(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module) throws DeploymentException { 75 XmlObject[] specDestinations = convert(specDD.selectChildren(messageDestinationQNameSet), J2EE_CONVERTER, MessageDestinationType.type); 76 XmlObject[] gerDestinations = plan.selectChildren(GER_MESSAGE_DESTINATION_QNAME_SET); 77 Map nameMap = new HashMap (); 78 for (int i = 0; i < gerDestinations.length; i++) { 79 GerMessageDestinationType destination = (GerMessageDestinationType) gerDestinations[i].copy().changeType(GerMessageDestinationType.type); 80 String name = destination.getMessageDestinationName().trim(); 81 nameMap.put(name, destination); 82 boolean found = false; 83 for (int j = 0; j < specDestinations.length; j++) { 84 MessageDestinationType specDestination = (MessageDestinationType) specDestinations[j]; 85 if (specDestination.getMessageDestinationName().getStringValue().trim().equals(name)) { 86 found = true; 87 break; 88 } 89 } 90 if (!found) { 91 throw new DeploymentException("No spec DD message-destination for " + name); 92 } 93 } 94 module.getRootEarContext().registerMessageDestionations(module.getName(), nameMap); 95 } 96 97 98 public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module, Map componentContext) throws DeploymentException { 99 XmlObject[] resourceEnvRefsUntyped = convert(specDD.selectChildren(adminOjbectRefQNameSet), J2EE_CONVERTER, ResourceEnvRefType.type); 100 ClassLoader cl = module.getEarContext().getClassLoader(); 101 XmlObject[] gerResourceEnvRefsUntyped = plan == null? NO_REFS: plan.selectChildren(GER_ADMIN_OBJECT_REF_QNAME_SET); 102 Map refMap = mapResourceEnvRefs(gerResourceEnvRefsUntyped); 103 for (int i = 0; i < resourceEnvRefsUntyped.length; i++) { 104 ResourceEnvRefType resourceEnvRef = (ResourceEnvRefType) resourceEnvRefsUntyped[i]; 105 String name = resourceEnvRef.getResourceEnvRefName().getStringValue().trim(); 106 String type = resourceEnvRef.getResourceEnvRefType().getStringValue().trim(); 107 Class iface; 108 try { 109 iface = cl.loadClass(type); 110 } catch (ClassNotFoundException e) { 111 throw new DeploymentException("could not load class " + type, e); 112 } 113 GerResourceEnvRefType gerResourceEnvRef = (GerResourceEnvRefType) refMap.get(name); 114 try { 115 AbstractNameQuery containerId = getAdminObjectContainerId(name, gerResourceEnvRef); 116 Reference ref = buildAdminObjectReference(localConfiguration, containerId, iface); 117 getJndiContextMap(componentContext).put(ENV + name, ref); 118 } catch (UnresolvedReferenceException e) { 119 throw new DeploymentException("Unable to resolve resource env reference '" + name + "' (" + (e.isMultiple() ? "found multiple matching resources" : "no matching resources found") + ")"); 120 } 121 } 122 123 XmlObject[] messageDestinationRefsUntyped = convert(specDD.selectChildren(messageDestinationRefQNameSet), J2EE_CONVERTER, MessageDestinationRefType.type); 125 126 for (int i = 0; i < messageDestinationRefsUntyped.length; i++) { 127 MessageDestinationRefType messageDestinationRef = (MessageDestinationRefType) messageDestinationRefsUntyped[i]; 128 String name = getStringValue(messageDestinationRef.getMessageDestinationRefName()); 129 String linkName = getStringValue(messageDestinationRef.getMessageDestinationLink()); 130 String type = getStringValue(messageDestinationRef.getMessageDestinationType()); 131 Class iface; 132 try { 133 iface = cl.loadClass(type); 134 } catch (ClassNotFoundException e) { 135 throw new DeploymentException("could not load class " + type, e); 136 } 137 String moduleURI = null; 138 Map messageDestinations = module.getRootEarContext().getMessageDestinations(); 139 GerMessageDestinationType destination = getMessageDestination(linkName, messageDestinations); 140 if (destination != null) { 141 if (destination.isSetAdminObjectLink()) { 142 if (destination.isSetAdminObjectModule()) { 143 moduleURI = destination.getAdminObjectModule().trim(); 144 } 145 linkName = destination.getAdminObjectLink().trim(); 146 } 147 } else { 148 int pos = linkName.indexOf('#'); 150 if (pos > -1) { 151 linkName = linkName.substring(pos + 1); 154 } 155 } 156 157 AbstractNameQuery containerId = buildAbstractNameQuery(null, moduleURI, linkName, NameFactory.JCA_ADMIN_OBJECT, NameFactory.RESOURCE_ADAPTER_MODULE); 160 Reference ref = buildAdminObjectReference(localConfiguration, containerId, iface); 161 getJndiContextMap(componentContext).put(ENV + name, ref); 162 163 } 164 165 } 166 167 public static GerMessageDestinationType getMessageDestination(String messageDestinationLink, Map messageDestinations) throws DeploymentException { 168 GerMessageDestinationType destination = null; 169 int pos = messageDestinationLink.indexOf('#'); 170 if (pos > -1) { 171 String targetModule = messageDestinationLink.substring(0, pos); 172 Map destinations = (Map ) messageDestinations.get(targetModule); 173 if (destinations == null) { 175 StringBuffer sb = new StringBuffer (); 176 for (Iterator mapIterator = messageDestinations.keySet().iterator(); mapIterator.hasNext();) { 177 sb.append(mapIterator.next()).append("\n"); 178 } 179 throw new DeploymentException("Unknown module " + targetModule + " when processing message destination " + messageDestinationLink + 180 "\nKnown modules in deployable unit are:\n" + sb.toString()); 181 } 182 messageDestinationLink = messageDestinationLink.substring(pos + 1); 183 destination = (GerMessageDestinationType) destinations.get(messageDestinationLink); 184 } else { 185 for (Iterator iterator = messageDestinations.values().iterator(); iterator.hasNext();) { 186 Map destinations = (Map ) iterator.next(); 187 GerMessageDestinationType destinationTest = (GerMessageDestinationType) destinations.get(messageDestinationLink); 188 if (destinationTest != null) { 189 if (destination != null) { 190 throw new DeploymentException("Duplicate message destination " + messageDestinationLink + " accessed from a message-destination-link without a module"); 191 } 192 destination = destinationTest; 193 } 194 } 195 } 196 return destination; 197 } 198 199 200 private Reference buildAdminObjectReference(Configuration localConfiguration, AbstractNameQuery containerId, Class iface) throws DeploymentException { 201 try { 202 localConfiguration.findGBean(containerId); 203 } catch (GBeanNotFoundException e) { 204 throw new DeploymentException("Can not resolve admin object ref " + containerId + " in configuration " + localConfiguration.getId()); 205 } 206 return new ResourceReference(localConfiguration.getId(), containerId, iface); 207 } 208 209 private static AbstractNameQuery getAdminObjectContainerId(String name, GerResourceEnvRefType gerResourceEnvRef) { 210 AbstractNameQuery containerId; 211 if (gerResourceEnvRef == null) { 212 containerId = buildAbstractNameQuery(null, null, name, NameFactory.JCA_ADMIN_OBJECT, NameFactory.RESOURCE_ADAPTER_MODULE); 213 } else if (gerResourceEnvRef.isSetMessageDestinationLink()) { 214 containerId = buildAbstractNameQuery(null, null, gerResourceEnvRef.getMessageDestinationLink().trim(), NameFactory.JCA_ADMIN_OBJECT, NameFactory.RESOURCE_ADAPTER_MODULE); 215 } else if (gerResourceEnvRef.isSetAdminObjectLink()) { 216 String moduleURI = null; 217 if (gerResourceEnvRef.isSetAdminObjectModule()) { 218 moduleURI = gerResourceEnvRef.getAdminObjectModule().trim(); 219 } 220 containerId = buildAbstractNameQuery(null, moduleURI, gerResourceEnvRef.getAdminObjectLink().trim(), NameFactory.JCA_ADMIN_OBJECT, NameFactory.RESOURCE_ADAPTER_MODULE); 221 } else { 222 GerPatternType patternType = gerResourceEnvRef.getPattern(); 224 containerId = buildAbstractNameQuery(patternType, NameFactory.JCA_ADMIN_OBJECT, NameFactory.RESOURCE_ADAPTER_MODULE, null); 225 } 226 return containerId; 227 } 228 229 private static Map mapResourceEnvRefs(XmlObject[] refs) { 230 Map refMap = new HashMap (); 231 if (refs != null) { 232 for (int i = 0; i < refs.length; i++) { 233 GerResourceEnvRefType ref = (GerResourceEnvRefType) refs[i].copy().changeType(GerResourceEnvRefType.type); 234 refMap.put(ref.getRefName().trim(), ref); 235 } 236 } 237 return refMap; 238 } 239 240 public QNameSet getSpecQNameSet() { 241 return adminOjbectRefQNameSet; 242 } 243 244 public QNameSet getPlanQNameSet() { 245 return GER_ADMIN_OBJECT_REF_QNAME_SET; 246 } 247 248 public static final GBeanInfo GBEAN_INFO; 249 250 static { 251 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(AdminObjectRefBuilder.class, NameFactory.MODULE_BUILDER); 252 infoBuilder.addAttribute("eeNamespaces", String [].class, true, true); 253 infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true); 254 255 infoBuilder.setConstructor(new String [] {"defaultEnvironment", "eeNamespaces"}); 256 257 GBEAN_INFO = infoBuilder.getBeanInfo(); 258 } 259 260 public static GBeanInfo getGBeanInfo() { 261 return GBEAN_INFO; 262 } 263 264 } 265 | Popular Tags |