1 17 package org.apache.geronimo.axis.builder; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.lang.reflect.Method ; 22 import java.math.BigDecimal ; 23 import java.math.BigInteger ; 24 import java.net.URI ; 25 import java.net.URISyntaxException ; 26 import java.net.URL ; 27 import java.net.MalformedURLException ; 28 import java.util.Calendar ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 import java.util.List ; 32 import java.util.ArrayList ; 33 import java.util.jar.JarFile ; 34 import javax.wsdl.Definition; 35 import javax.wsdl.Operation; 36 import javax.wsdl.Port; 37 import javax.wsdl.extensions.soap.SOAPAddress; 38 import javax.xml.namespace.QName ; 39 import javax.xml.rpc.holders.BigDecimalHolder ; 40 import javax.xml.rpc.holders.BigIntegerHolder ; 41 import javax.xml.rpc.holders.BooleanHolder ; 42 import javax.xml.rpc.holders.BooleanWrapperHolder ; 43 import javax.xml.rpc.holders.ByteArrayHolder ; 44 import javax.xml.rpc.holders.ByteHolder ; 45 import javax.xml.rpc.holders.ByteWrapperHolder ; 46 import javax.xml.rpc.holders.CalendarHolder ; 47 import javax.xml.rpc.holders.DoubleHolder ; 48 import javax.xml.rpc.holders.DoubleWrapperHolder ; 49 import javax.xml.rpc.holders.FloatHolder ; 50 import javax.xml.rpc.holders.FloatWrapperHolder ; 51 import javax.xml.rpc.holders.IntHolder ; 52 import javax.xml.rpc.holders.IntegerWrapperHolder ; 53 import javax.xml.rpc.holders.LongHolder ; 54 import javax.xml.rpc.holders.LongWrapperHolder ; 55 import javax.xml.rpc.holders.ObjectHolder ; 56 import javax.xml.rpc.holders.QNameHolder ; 57 import javax.xml.rpc.holders.ShortHolder ; 58 import javax.xml.rpc.holders.ShortWrapperHolder ; 59 import javax.xml.rpc.holders.StringHolder ; 60 import javax.xml.rpc.handler.HandlerInfo ; 61 62 import org.apache.geronimo.common.DeploymentException; 63 import org.apache.geronimo.kernel.ClassLoading; 64 import org.apache.geronimo.schema.SchemaConversionUtils; 65 import org.apache.geronimo.xbeans.j2ee.ExceptionMappingType; 66 import org.apache.geronimo.xbeans.j2ee.JavaWsdlMappingDocument; 67 import org.apache.geronimo.xbeans.j2ee.JavaWsdlMappingType; 68 import org.apache.geronimo.xbeans.j2ee.PackageMappingType; 69 import org.apache.geronimo.xbeans.j2ee.PortComponentHandlerType; 70 import org.apache.geronimo.xbeans.j2ee.PortComponentType; 71 import org.apache.geronimo.xbeans.j2ee.ServiceEndpointInterfaceMappingType; 72 import org.apache.geronimo.xbeans.j2ee.ServiceEndpointMethodMappingType; 73 import org.apache.geronimo.xbeans.j2ee.ServiceImplBeanType; 74 import org.apache.geronimo.xbeans.j2ee.WebserviceDescriptionType; 75 import org.apache.geronimo.xbeans.j2ee.WebservicesDocument; 76 import org.apache.geronimo.xbeans.j2ee.WebservicesType; 77 import org.apache.geronimo.xbeans.j2ee.ParamValueType; 78 import org.apache.geronimo.xbeans.j2ee.XsdQNameType; 79 import org.apache.xmlbeans.XmlException; 80 81 84 public class WSDescriptorParser { 85 86 87 public static JavaWsdlMappingType readJaxrpcMapping(JarFile moduleFile, URI jaxrpcMappingURI) throws DeploymentException { 88 String jaxrpcMappingPath = jaxrpcMappingURI.toString(); 89 return readJaxrpcMapping(moduleFile, jaxrpcMappingPath); 90 } 91 92 public static JavaWsdlMappingType readJaxrpcMapping(JarFile moduleFile, String jaxrpcMappingPath) throws DeploymentException { 93 JavaWsdlMappingType mapping; 94 InputStream jaxrpcInputStream = null; 95 try { 96 jaxrpcInputStream = moduleFile.getInputStream(moduleFile.getEntry(jaxrpcMappingPath)); 97 } catch (IOException e) { 98 throw new DeploymentException("Could not open stream to jaxrpc mapping document", e); 99 } 100 JavaWsdlMappingDocument mappingDocument = null; 101 try { 102 mappingDocument = JavaWsdlMappingDocument.Factory.parse(jaxrpcInputStream); 103 } catch (XmlException e) { 104 throw new DeploymentException("Could not parse jaxrpc mapping document", e); 105 } catch (IOException e) { 106 throw new DeploymentException("Could not read jaxrpc mapping document", e); 107 } 108 mapping = mappingDocument.getJavaWsdlMapping(); 109 return mapping; 110 } 111 112 113 public static Map getExceptionMap(JavaWsdlMappingType mapping) { 114 Map exceptionMap = new HashMap (); 115 if (mapping != null) { 116 ExceptionMappingType[] exceptionMappings = mapping.getExceptionMappingArray(); 117 for (int i = 0; i < exceptionMappings.length; i++) { 118 ExceptionMappingType exceptionMapping = exceptionMappings[i]; 119 QName exceptionMessageQName = exceptionMapping.getWsdlMessage().getQNameValue(); 120 exceptionMap.put(exceptionMessageQName, exceptionMapping); 121 } 122 } 123 return exceptionMap; 124 } 125 126 public static String getPackageFromNamespace(String namespace, JavaWsdlMappingType mapping) throws DeploymentException { 127 PackageMappingType[] packageMappings = mapping.getPackageMappingArray(); 128 for (int i = 0; i < packageMappings.length; i++) { 129 PackageMappingType packageMapping = packageMappings[i]; 130 if (namespace.equals(packageMapping.getNamespaceURI().getStringValue().trim())) { 131 return packageMapping.getPackageType().getStringValue().trim(); 132 } 133 } 134 throw new DeploymentException("Namespace " + namespace + " was not mapped in jaxrpc mapping file"); 135 } 136 137 private static final Map rpcHolderClasses = new HashMap (); 138 139 static { 140 rpcHolderClasses.put(BigDecimal .class, BigDecimalHolder .class); 141 rpcHolderClasses.put(BigInteger .class, BigIntegerHolder .class); 142 rpcHolderClasses.put(boolean.class, BooleanHolder .class); 143 rpcHolderClasses.put(Boolean .class, BooleanWrapperHolder .class); 144 rpcHolderClasses.put(byte[].class, ByteArrayHolder .class); 145 rpcHolderClasses.put(byte.class, ByteHolder .class); 146 rpcHolderClasses.put(Byte .class, ByteWrapperHolder .class); 147 rpcHolderClasses.put(Calendar .class, CalendarHolder .class); 148 rpcHolderClasses.put(double.class, DoubleHolder .class); 149 rpcHolderClasses.put(Double .class, DoubleWrapperHolder .class); 150 rpcHolderClasses.put(float.class, FloatHolder .class); 151 rpcHolderClasses.put(Float .class, FloatWrapperHolder .class); 152 rpcHolderClasses.put(int.class, IntHolder .class); 153 rpcHolderClasses.put(Integer .class, IntegerWrapperHolder .class); 154 rpcHolderClasses.put(long.class, LongHolder .class); 155 rpcHolderClasses.put(Long .class, LongWrapperHolder .class); 156 rpcHolderClasses.put(Object .class, ObjectHolder .class); 157 rpcHolderClasses.put(QName .class, QNameHolder .class); 158 rpcHolderClasses.put(short.class, ShortHolder .class); 159 rpcHolderClasses.put(Short .class, ShortWrapperHolder .class); 160 rpcHolderClasses.put(String .class, StringHolder .class); 161 } 162 163 public static Class getHolderType(String paramJavaTypeName, boolean isInOnly, QName typeQName, boolean isComplexType, JavaWsdlMappingType mapping, ClassLoader classLoader) throws DeploymentException { 164 Class paramJavaType = null; 165 if (isInOnly) { 166 try { 168 paramJavaType = ClassLoading.loadClass(paramJavaTypeName, classLoader); 169 } catch (ClassNotFoundException e) { 170 throw new DeploymentException("could not load parameter type", e); 171 } 172 return paramJavaType; 173 } else { 174 String holderName; 176 if (isComplexType) { 177 String namespace = typeQName.getNamespaceURI(); 181 String packageName = WSDescriptorParser.getPackageFromNamespace(namespace, mapping); 182 StringBuffer buf = new StringBuffer (packageName.length() + typeQName.getLocalPart().length() + 14); 183 buf.append(packageName).append(".holders.").append(typeQName.getLocalPart()).append("Holder"); 184 buf.setCharAt(packageName.length() + 9, Character.toUpperCase(typeQName.getLocalPart().charAt(0))); 185 holderName = buf.toString(); 186 } else { 187 try { 189 paramJavaType = ClassLoading.loadClass(paramJavaTypeName, classLoader); 190 } catch (ClassNotFoundException e) { 191 throw new DeploymentException("could not load parameter type", e); 192 } 193 Class holder = (Class ) rpcHolderClasses.get(paramJavaType); 194 if (holder != null) { 195 try { 196 holder = ClassLoading.loadClass(holder.getName(), classLoader); 198 } catch (ClassNotFoundException e) { 199 throw new DeploymentException("could not load holder type in correct classloader", e); 200 } 201 return holder; 202 } 203 String paramTypeName = paramJavaType.getName(); 207 StringBuffer buf = new StringBuffer (paramTypeName.length() + 14); 208 int dot = paramTypeName.lastIndexOf("."); 209 buf.append(paramTypeName.substring(0, dot)).append(".holders").append(paramTypeName.substring(dot)).append("Holder"); 211 holderName = buf.toString(); 212 } 213 try { 214 Class holder = ClassLoading.loadClass(holderName, classLoader); 215 return holder; 216 } catch (ClassNotFoundException e) { 217 throw new DeploymentException("Could not load holder class", e); 218 } 219 } 220 } 221 222 public static ServiceEndpointMethodMappingType getMethodMappingForOperation(String operationName, ServiceEndpointMethodMappingType[] methodMappings) throws DeploymentException { 223 for (int i = 0; i < methodMappings.length; i++) { 224 ServiceEndpointMethodMappingType methodMapping = methodMappings[i]; 225 if (operationName.equals(methodMapping.getWsdlOperation().getStringValue())) { 226 return methodMapping; 227 } 228 } 229 throw new DeploymentException("No method found for operation named " + operationName); 230 } 231 232 public static ServiceEndpointInterfaceMappingType getServiceEndpointInterfaceMapping(ServiceEndpointInterfaceMappingType[] endpointMappings, QName portTypeQName) throws DeploymentException { 233 for (int i = 0; i < endpointMappings.length; i++) { 234 ServiceEndpointInterfaceMappingType endpointMapping = endpointMappings[i]; 235 QName testPortQName = endpointMapping.getWsdlPortType().getQNameValue(); 236 if (portTypeQName.equals(testPortQName)) { 237 return endpointMapping; 238 } 239 } 240 throw new DeploymentException("Could not find service endpoint interface for port named " + portTypeQName); 241 } 242 243 public static javax.wsdl.Service getService(QName serviceQName, Definition definition) throws DeploymentException { 244 javax.wsdl.Service service; 245 if (serviceQName != null) { 246 service = definition.getService(serviceQName); 247 } else { 248 Map services = definition.getServices(); 249 if (services.size() != 1) { 250 throw new DeploymentException("no serviceQName supplied, and there are " + services.size() + " services"); 251 } 252 service = (javax.wsdl.Service) services.values().iterator().next(); 253 } 254 if (service == null) { 255 throw new DeploymentException("No service wsdl for supplied service qname " + serviceQName); 256 } 257 return service; 258 } 259 260 public static Method getMethodForOperation(Class serviceEndpointInterface, Operation operation) throws DeploymentException { 261 Method [] methods = serviceEndpointInterface.getMethods(); 262 String opName = operation.getName(); 263 Method found = null; 264 for (int i = 0; i < methods.length; i++) { 265 Method method = methods[i]; 266 if (method.getName().equals(opName)) { 267 if (found != null) { 268 throw new DeploymentException("Overloaded methods are not allowed in lightweight mappings"); 269 } 270 found = method; 271 } 272 } 273 if (found == null) { 274 throw new DeploymentException("No method found for operation named " + opName); 275 } 276 return found; 277 } 278 279 289 public static Map parseWebServiceDescriptor(WebservicesType webservicesType, JarFile moduleFile, boolean isEJB) throws DeploymentException { 290 Map portMap = new HashMap (); 291 WebserviceDescriptionType[] webserviceDescriptions = webservicesType.getWebserviceDescriptionArray(); 292 for (int i = 0; i < webserviceDescriptions.length; i++) { 293 WebserviceDescriptionType webserviceDescription = webserviceDescriptions[i]; 294 URI wsdlURI = null; 295 try { 296 wsdlURI = new URI (webserviceDescription.getWsdlFile().getStringValue().trim()); 297 } catch (URISyntaxException e) { 298 throw new DeploymentException("could not construct wsdl uri from " + webserviceDescription.getWsdlFile().getStringValue(), e); 299 } 300 URI jaxrpcMappingURI = null; 301 try { 302 jaxrpcMappingURI = new URI (webserviceDescription.getJaxrpcMappingFile().getStringValue().trim()); 303 } catch (URISyntaxException e) { 304 throw new DeploymentException("Could not construct jaxrpc mapping uri from " + webserviceDescription.getJaxrpcMappingFile(), e); 305 } 306 SchemaInfoBuilder schemaInfoBuilder = new SchemaInfoBuilder(moduleFile, wsdlURI); 307 Map wsdlPortMap = schemaInfoBuilder.getPortMap(); 308 309 JavaWsdlMappingType javaWsdlMapping = readJaxrpcMapping(moduleFile, jaxrpcMappingURI); 310 HashMap seiMappings = new HashMap (); 311 ServiceEndpointInterfaceMappingType[] mappings = javaWsdlMapping.getServiceEndpointInterfaceMappingArray(); 312 for (int j = 0; j < mappings.length; j++) { 313 ServiceEndpointInterfaceMappingType seiMapping = mappings[j]; 314 seiMappings.put(seiMapping.getServiceEndpointInterface().getStringValue(), seiMapping); 315 } 316 317 PortComponentType[] portComponents = webserviceDescription.getPortComponentArray(); 318 for (int j = 0; j < portComponents.length; j++) { 319 PortComponentType portComponent = portComponents[j]; 320 String portComponentName = portComponent.getPortComponentName().getStringValue().trim(); 321 QName portQName = portComponent.getWsdlPort().getQNameValue(); 322 String seiInterfaceName = portComponent.getServiceEndpointInterface().getStringValue().trim(); 323 ServiceImplBeanType serviceImplBeanType = portComponent.getServiceImplBean(); 324 if (isEJB == serviceImplBeanType.isSetServletLink()) { 325 throw new DeploymentException("Wrong kind of web service described in web service descriptor: expected " + (isEJB ? "EJB" : "POJO(Servlet)")); 326 } 327 String linkName; 328 if (serviceImplBeanType.isSetServletLink()) { 329 linkName = serviceImplBeanType.getServletLink().getStringValue().trim(); 330 } else { 331 linkName = serviceImplBeanType.getEjbLink().getStringValue().trim(); 332 } 333 PortComponentHandlerType[] handlers = portComponent.getHandlerArray(); 334 335 Port port = (Port) wsdlPortMap.get(portQName.getLocalPart()); 336 if (port == null) { 337 throw new DeploymentException("No WSDL Port definition for port-component " + portComponentName); 338 } 339 340 ServiceEndpointInterfaceMappingType seiMapping = (ServiceEndpointInterfaceMappingType) seiMappings.get(seiInterfaceName); 341 342 String wsdlLocation = webserviceDescription.getWsdlFile().getStringValue().trim(); 343 URI contextURI = getAddressLocation(port); 344 345 PortInfo portInfo = new PortInfo(portComponentName, portQName, schemaInfoBuilder, javaWsdlMapping, seiInterfaceName, handlers, port, seiMapping, wsdlLocation, contextURI); 346 347 if (portMap.put(linkName, portInfo) != null) { 348 throw new DeploymentException("Ambiguous description of port associated with j2ee component " + linkName); 349 } 350 } 351 } 352 return portMap; 353 } 354 355 private static URI getAddressLocation(Port port) throws DeploymentException { 356 SOAPAddress soapAddress = (SOAPAddress) SchemaInfoBuilder.getExtensibilityElement(SOAPAddress.class, port.getExtensibilityElements()); 357 String locationURIString = soapAddress.getLocationURI(); 358 try { 359 URI location = new URI (locationURIString); 360 URI contextPath = new URI (location.getPath()); 361 return contextPath; 362 } catch (URISyntaxException e) { 363 throw new DeploymentException("Could not construct web service location URL from " + locationURIString); 364 } 365 } 366 367 public static Map parseWebServiceDescriptor(URL wsDDUrl, JarFile moduleFile, boolean isEJB) throws DeploymentException { 368 try { 369 WebservicesDocument webservicesDocument = WebservicesDocument.Factory.parse(wsDDUrl); 370 SchemaConversionUtils.validateDD(webservicesDocument); 371 WebservicesType webservicesType = webservicesDocument.getWebservices(); 372 return parseWebServiceDescriptor(webservicesType, moduleFile, isEJB); 373 } catch (XmlException e) { 374 throw new DeploymentException("Could not read descriptor document", e); 375 } catch (IOException e) { 376 return null; 377 } 378 379 } 380 381 public static List createHandlerInfoList(PortComponentHandlerType[] handlers, ClassLoader classLoader) throws DeploymentException { 382 List list = new ArrayList (); 383 for (int i = 0; i < handlers.length; i++) { 384 PortComponentHandlerType handler = handlers[i]; 385 386 Class handlerClass = null; 388 String className = handler.getHandlerClass().getStringValue().trim(); 389 try { 390 handlerClass = classLoader.loadClass(className); 391 } catch (ClassNotFoundException e) { 392 throw new DeploymentException("Unable to load handler class: " + className, e); 393 } 394 395 Map config = new HashMap (); 397 ParamValueType[] paramValues = handler.getInitParamArray(); 398 for (int j = 0; j < paramValues.length; j++) { 399 ParamValueType paramValue = paramValues[j]; 400 String paramName = paramValue.getParamName().getStringValue().trim(); 401 String paramStringValue = paramValue.getParamValue().getStringValue().trim(); 402 config.put(paramName, paramStringValue); 403 } 404 405 XsdQNameType[] soapHeaderQNames = handler.getSoapHeaderArray(); 407 QName [] headers = new QName [soapHeaderQNames.length]; 408 for (int j = 0; j < soapHeaderQNames.length; j++) { 409 XsdQNameType soapHeaderQName = soapHeaderQNames[j]; 410 headers[j] = soapHeaderQName.getQNameValue(); 411 } 412 413 list.add(new HandlerInfo (handlerClass, config, headers)); 414 } 415 return list; 416 } 417 } 418 | Popular Tags |