1 17 18 package org.apache.geronimo.connector.deployment; 19 20 import java.net.MalformedURLException ; 21 import java.net.URI ; 22 import java.net.URL ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 import java.util.Set ; 26 import java.util.HashSet ; 27 28 import javax.naming.Reference ; 29 import javax.xml.namespace.QName ; 30 31 import org.apache.geronimo.common.DeploymentException; 32 import org.apache.geronimo.common.UnresolvedReferenceException; 33 import org.apache.geronimo.gbean.AbstractNameQuery; 34 import org.apache.geronimo.gbean.GBeanInfo; 35 import org.apache.geronimo.gbean.GBeanInfoBuilder; 36 import org.apache.geronimo.j2ee.deployment.Module; 37 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 38 import org.apache.geronimo.kernel.GBeanNotFoundException; 39 import org.apache.geronimo.kernel.config.Configuration; 40 import org.apache.geronimo.kernel.repository.Environment; 41 import org.apache.geronimo.naming.deployment.AbstractNamingBuilder; 42 import org.apache.geronimo.naming.deployment.ResourceEnvironmentBuilder; 43 import org.apache.geronimo.naming.deployment.ResourceEnvironmentSetter; 44 import org.apache.geronimo.naming.reference.ResourceReference; 45 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType; 46 import org.apache.geronimo.xbeans.geronimo.naming.GerResourceRefDocument; 47 import org.apache.geronimo.xbeans.geronimo.naming.GerResourceRefType; 48 import org.apache.geronimo.xbeans.j2ee.ResourceRefType; 49 import org.apache.xmlbeans.QNameSet; 50 import org.apache.xmlbeans.XmlObject; 51 52 55 public class ResourceRefBuilder extends AbstractNamingBuilder implements ResourceEnvironmentSetter { 56 private static final QName GER_RESOURCE_REF_QNAME = GerResourceRefDocument.type.getDocumentElementName(); 57 private static final QNameSet GER_RESOURCE_REF_QNAME_SET = QNameSet.singleton(GER_RESOURCE_REF_QNAME); 58 59 private final QNameSet resourceRefQNameSet; 60 61 private static final String JAXR_CONNECTION_FACTORY_CLASS = "javax.xml.registry.ConnectionFactory"; 62 private static final String JAVAX_MAIL_SESSION_CLASS = "javax.mail.Session"; 63 64 public ResourceRefBuilder(Environment defaultEnvironment, String [] eeNamespaces) { 65 super(defaultEnvironment); 66 67 resourceRefQNameSet = buildQNameSet(eeNamespaces, "resource-ref"); 68 } 69 70 protected boolean willMergeEnvironment(XmlObject specDD, XmlObject plan) { 71 return specDD.selectChildren(resourceRefQNameSet).length > 0; 72 } 73 74 public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module, Map componentContext) throws DeploymentException { 75 XmlObject[] resourceRefsUntyped = convert(specDD.selectChildren(resourceRefQNameSet), J2EE_CONVERTER, ResourceRefType.type); 76 XmlObject[] gerResourceRefsUntyped = plan == null? NO_REFS: plan.selectChildren(GER_RESOURCE_REF_QNAME_SET); 77 Map refMap = mapResourceRefs(gerResourceRefsUntyped); 78 ClassLoader cl = module.getEarContext().getClassLoader(); 79 80 for (int i = 0; i < resourceRefsUntyped.length; i++) { 81 ResourceRefType resourceRef = (ResourceRefType) resourceRefsUntyped[i]; 82 String name = resourceRef.getResRefName().getStringValue().trim(); 83 String type = resourceRef.getResType().getStringValue().trim(); 84 GerResourceRefType gerResourceRef = (GerResourceRefType) refMap.get(name); 85 Class iface; 86 try { 87 iface = cl.loadClass(type); 88 } catch (ClassNotFoundException e) { 89 throw new DeploymentException("could not load class " + type, e); 90 } 91 if (iface == URL .class) { 92 if (gerResourceRef == null || !gerResourceRef.isSetUrl()) { 93 throw new DeploymentException("No url supplied to resolve: " + name); 94 } 95 try { 96 getJndiContextMap(componentContext).put(ENV + name, new URL (gerResourceRef.getUrl())); 98 } catch (MalformedURLException e) { 99 throw new DeploymentException("Could not convert " + gerResourceRef.getUrl() + " to URL", e); 100 } 101 } else { 102 String j2eeType; 104 105 106 if (JAVAX_MAIL_SESSION_CLASS.equals(type)) { 107 j2eeType = NameFactory.JAVA_MAIL_RESOURCE; 108 } else if (JAXR_CONNECTION_FACTORY_CLASS.equals(type)) { 109 j2eeType = NameFactory.JAXR_CONNECTION_FACTORY; 110 } else { 111 j2eeType = NameFactory.JCA_MANAGED_CONNECTION_FACTORY; 112 } 113 try { 114 AbstractNameQuery containerId = getResourceContainerId(name, j2eeType, null, gerResourceRef); 115 116 try { 117 localConfiguration.findGBean(containerId); 118 } catch (GBeanNotFoundException e) { 119 throw new UnresolvedReferenceException("Resource", false, containerId.toString(), localConfiguration.getId().toString()); 120 } 121 122 Reference ref = new ResourceReference(localConfiguration.getId(), containerId, iface); 123 getJndiContextMap(componentContext).put(ENV + name, ref); 124 } catch (UnresolvedReferenceException e) { 125 126 StringBuffer errorMessage = new StringBuffer ("Unable to resolve resource reference '"); 127 errorMessage.append(name); 128 errorMessage.append("' ("); 129 if (e.isMultiple()) { 130 errorMessage.append("Found multiple matching resources. Try being more specific in a resource-ref mapping in your Geronimo deployment plan."); 131 } else if (gerResourceRef == null) { 132 errorMessage.append("Could not auto-map to resource. Try adding a resource-ref mapping to your Geronimo deployment plan."); 133 } else if (gerResourceRef.isSetResourceLink()) { 134 errorMessage.append("Could not find resource '"); 135 errorMessage.append(gerResourceRef.getResourceLink()); 136 errorMessage.append("'. Perhaps it has not yet been configured, or your application does not have a dependency declared for that resource module?"); 137 } else { 138 errorMessage.append("Could not find the resource specified in your Geronimo deployment plan:"); 139 errorMessage.append(gerResourceRef.getPattern()); 140 } 141 errorMessage.append(")"); 142 143 throw new DeploymentException(errorMessage.toString()); 144 } 145 } 146 } 147 148 } 149 150 public void setResourceEnvironment(ResourceEnvironmentBuilder builder, XmlObject[] resourceRefs, GerResourceRefType[] gerResourceRefs) throws DeploymentException { 151 resourceRefs = convert(resourceRefs, J2EE_CONVERTER, ResourceRefType.type); 152 Map refMap = mapResourceRefs(gerResourceRefs); 153 Set unshareableResources = new HashSet (); 154 Set applicationManagedSecurityResources = new HashSet (); 155 for (int i = 0; i < resourceRefs.length; i++) { 156 ResourceRefType resourceRefType = (ResourceRefType) resourceRefs[i]; 157 158 String type = resourceRefType.getResType().getStringValue().trim(); 159 160 if (!URL .class.getName().equals(type) 161 && !"javax.mail.Session".equals(type) 162 && !JAXR_CONNECTION_FACTORY_CLASS.equals(type)) { 163 164 GerResourceRefType gerResourceRef = (GerResourceRefType) refMap.get(resourceRefType.getResRefName().getStringValue()); 165 AbstractNameQuery containerId = getResourceContainerId(getStringValue(resourceRefType.getResRefName()), NameFactory.JCA_MANAGED_CONNECTION_FACTORY, null, gerResourceRef); 166 167 if ("Unshareable".equals(getStringValue(resourceRefType.getResSharingScope()))) { 168 unshareableResources.add(containerId); 169 } 170 if ("Application".equals(getStringValue(resourceRefType.getResAuth()))) { 171 applicationManagedSecurityResources.add(containerId); 172 } 173 } 174 } 175 builder.setUnshareableResources(unshareableResources); 176 builder.setApplicationManagedSecurityResources(applicationManagedSecurityResources); 177 } 178 179 private Map mapResourceRefs(XmlObject[] refs) { 180 Map refMap = new HashMap (); 181 if (refs != null) { 182 for (int i = 0; i < refs.length; i++) { 183 GerResourceRefType ref = (GerResourceRefType) refs[i].copy().changeType(GerResourceRefType.type); 184 refMap.put(ref.getRefName().trim(), ref); 185 } 186 } 187 return refMap; 188 } 189 190 private AbstractNameQuery getResourceContainerId(String name, String type, URI moduleURI, GerResourceRefType gerResourceRef) { 191 AbstractNameQuery containerId; 192 String module = moduleURI == null ? null : moduleURI.toString(); 193 if (gerResourceRef == null) { 194 containerId = buildAbstractNameQuery(null, module, name, type, NameFactory.RESOURCE_ADAPTER_MODULE); 195 } else if (gerResourceRef.isSetResourceLink()) { 196 containerId = buildAbstractNameQuery(null, module, gerResourceRef.getResourceLink().trim(), type, NameFactory.RESOURCE_ADAPTER_MODULE); 197 } else { 198 GerPatternType patternType = gerResourceRef.getPattern(); 200 containerId = buildAbstractNameQuery(patternType, type, NameFactory.RESOURCE_ADAPTER_MODULE, null); 201 } 202 return containerId; 203 } 204 205 public QNameSet getSpecQNameSet() { 206 return resourceRefQNameSet; 207 } 208 209 public QNameSet getPlanQNameSet() { 210 return GER_RESOURCE_REF_QNAME_SET; 211 } 212 213 public static final GBeanInfo GBEAN_INFO; 214 215 static { 216 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(ResourceRefBuilder.class, NameFactory.MODULE_BUILDER); 217 infoBuilder.addAttribute("eeNamespaces", String [].class, true, true); 218 infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true); 219 220 infoBuilder.setConstructor(new String [] {"defaultEnvironment", "eeNamespaces"}); 221 222 GBEAN_INFO = infoBuilder.getBeanInfo(); 223 } 224 225 public static GBeanInfo getGBeanInfo() { 226 return GBEAN_INFO; 227 } 228 } 229 | Popular Tags |