KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > axis > builder > WSDescriptorParser


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.axis.builder;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.math.BigDecimal JavaDoc;
23 import java.math.BigInteger JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URISyntaxException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.util.Calendar JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.jar.JarFile JavaDoc;
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 JavaDoc;
39 import javax.xml.rpc.holders.BigDecimalHolder JavaDoc;
40 import javax.xml.rpc.holders.BigIntegerHolder JavaDoc;
41 import javax.xml.rpc.holders.BooleanHolder JavaDoc;
42 import javax.xml.rpc.holders.BooleanWrapperHolder JavaDoc;
43 import javax.xml.rpc.holders.ByteArrayHolder JavaDoc;
44 import javax.xml.rpc.holders.ByteHolder JavaDoc;
45 import javax.xml.rpc.holders.ByteWrapperHolder JavaDoc;
46 import javax.xml.rpc.holders.CalendarHolder JavaDoc;
47 import javax.xml.rpc.holders.DoubleHolder JavaDoc;
48 import javax.xml.rpc.holders.DoubleWrapperHolder JavaDoc;
49 import javax.xml.rpc.holders.FloatHolder JavaDoc;
50 import javax.xml.rpc.holders.FloatWrapperHolder JavaDoc;
51 import javax.xml.rpc.holders.IntHolder JavaDoc;
52 import javax.xml.rpc.holders.IntegerWrapperHolder JavaDoc;
53 import javax.xml.rpc.holders.LongHolder JavaDoc;
54 import javax.xml.rpc.holders.LongWrapperHolder JavaDoc;
55 import javax.xml.rpc.holders.ObjectHolder JavaDoc;
56 import javax.xml.rpc.holders.QNameHolder JavaDoc;
57 import javax.xml.rpc.holders.ShortHolder JavaDoc;
58 import javax.xml.rpc.holders.ShortWrapperHolder JavaDoc;
59 import javax.xml.rpc.holders.StringHolder JavaDoc;
60 import javax.xml.rpc.handler.HandlerInfo JavaDoc;
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 /**
82  * @version $Rev: $ $Date: $
83  */

84 public class WSDescriptorParser {
85
86
87     public static JavaWsdlMappingType readJaxrpcMapping(JarFile JavaDoc moduleFile, URI JavaDoc jaxrpcMappingURI) throws DeploymentException {
88         String JavaDoc jaxrpcMappingPath = jaxrpcMappingURI.toString();
89         return readJaxrpcMapping(moduleFile, jaxrpcMappingPath);
90     }
91
92     public static JavaWsdlMappingType readJaxrpcMapping(JarFile JavaDoc moduleFile, String JavaDoc jaxrpcMappingPath) throws DeploymentException {
93         JavaWsdlMappingType mapping;
94         InputStream JavaDoc jaxrpcInputStream = null;
95         try {
96             jaxrpcInputStream = moduleFile.getInputStream(moduleFile.getEntry(jaxrpcMappingPath));
97         } catch (IOException JavaDoc 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 JavaDoc 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 JavaDoc getExceptionMap(JavaWsdlMappingType mapping) {
114         Map JavaDoc exceptionMap = new HashMap JavaDoc();
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 JavaDoc exceptionMessageQName = exceptionMapping.getWsdlMessage().getQNameValue();
120                 exceptionMap.put(exceptionMessageQName, exceptionMapping);
121             }
122         }
123         return exceptionMap;
124     }
125
126     public static String JavaDoc getPackageFromNamespace(String JavaDoc 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 JavaDoc rpcHolderClasses = new HashMap JavaDoc();
138
139     static {
140         rpcHolderClasses.put(BigDecimal JavaDoc.class, BigDecimalHolder JavaDoc.class);
141         rpcHolderClasses.put(BigInteger JavaDoc.class, BigIntegerHolder JavaDoc.class);
142         rpcHolderClasses.put(boolean.class, BooleanHolder JavaDoc.class);
143         rpcHolderClasses.put(Boolean JavaDoc.class, BooleanWrapperHolder JavaDoc.class);
144         rpcHolderClasses.put(byte[].class, ByteArrayHolder JavaDoc.class);
145         rpcHolderClasses.put(byte.class, ByteHolder JavaDoc.class);
146         rpcHolderClasses.put(Byte JavaDoc.class, ByteWrapperHolder JavaDoc.class);
147         rpcHolderClasses.put(Calendar JavaDoc.class, CalendarHolder JavaDoc.class);
148         rpcHolderClasses.put(double.class, DoubleHolder JavaDoc.class);
149         rpcHolderClasses.put(Double JavaDoc.class, DoubleWrapperHolder JavaDoc.class);
150         rpcHolderClasses.put(float.class, FloatHolder JavaDoc.class);
151         rpcHolderClasses.put(Float JavaDoc.class, FloatWrapperHolder JavaDoc.class);
152         rpcHolderClasses.put(int.class, IntHolder JavaDoc.class);
153         rpcHolderClasses.put(Integer JavaDoc.class, IntegerWrapperHolder JavaDoc.class);
154         rpcHolderClasses.put(long.class, LongHolder JavaDoc.class);
155         rpcHolderClasses.put(Long JavaDoc.class, LongWrapperHolder JavaDoc.class);
156         rpcHolderClasses.put(Object JavaDoc.class, ObjectHolder JavaDoc.class);
157         rpcHolderClasses.put(QName JavaDoc.class, QNameHolder JavaDoc.class);
158         rpcHolderClasses.put(short.class, ShortHolder JavaDoc.class);
159         rpcHolderClasses.put(Short JavaDoc.class, ShortWrapperHolder JavaDoc.class);
160         rpcHolderClasses.put(String JavaDoc.class, StringHolder JavaDoc.class);
161     }
162
163     public static Class JavaDoc getHolderType(String JavaDoc paramJavaTypeName, boolean isInOnly, QName JavaDoc typeQName, boolean isComplexType, JavaWsdlMappingType mapping, ClassLoader JavaDoc classLoader) throws DeploymentException {
164         Class JavaDoc paramJavaType = null;
165         if (isInOnly) {
166             //IN parameters just use their own type
167
try {
168                 paramJavaType = ClassLoading.loadClass(paramJavaTypeName, classLoader);
169             } catch (ClassNotFoundException JavaDoc e) {
170                 throw new DeploymentException("could not load parameter type", e);
171             }
172             return paramJavaType;
173         } else {
174             //INOUT and OUT parameters use holders. See jaxrpc spec 4.3.5
175
String JavaDoc holderName;
176             if (isComplexType) {
177                 //complex types get mapped:
178
//package is determined from the namespace to package map + ".holders"
179
//class name is the complex type QNMAne local part + "Holder", with the initial character uppercased.
180
String JavaDoc namespace = typeQName.getNamespaceURI();
181                 String JavaDoc packageName = WSDescriptorParser.getPackageFromNamespace(namespace, mapping);
182                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc(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                 //see if it is in the primitive type and simple type mapping
188
try {
189                     paramJavaType = ClassLoading.loadClass(paramJavaTypeName, classLoader);
190                 } catch (ClassNotFoundException JavaDoc e) {
191                     throw new DeploymentException("could not load parameter type", e);
192                 }
193                 Class JavaDoc holder = (Class JavaDoc) rpcHolderClasses.get(paramJavaType);
194                 if (holder != null) {
195                     try {
196                         //TODO use class names in map or make sure we are in the correct classloader to start with.
197
holder = ClassLoading.loadClass(holder.getName(), classLoader);
198                     } catch (ClassNotFoundException JavaDoc e) {
199                         throw new DeploymentException("could not load holder type in correct classloader", e);
200                     }
201                     return holder;
202                 }
203                 //Otherwise, the holder must be in:
204
//package same as type's package + ".holders"
205
//class name same as type name + "Holder"
206
String JavaDoc paramTypeName = paramJavaType.getName();
207                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc(paramTypeName.length() + 14);
208                 int dot = paramTypeName.lastIndexOf(".");
209                 //foo.Bar >>> foo.holders.BarHolder
210
buf.append(paramTypeName.substring(0, dot)).append(".holders").append(paramTypeName.substring(dot)).append("Holder");
211                 holderName = buf.toString();
212             }
213             try {
214                 Class JavaDoc holder = ClassLoading.loadClass(holderName, classLoader);
215                 return holder;
216             } catch (ClassNotFoundException JavaDoc e) {
217                 throw new DeploymentException("Could not load holder class", e);
218             }
219         }
220     }
221
222     public static ServiceEndpointMethodMappingType getMethodMappingForOperation(String JavaDoc 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 JavaDoc portTypeQName) throws DeploymentException {
233         for (int i = 0; i < endpointMappings.length; i++) {
234             ServiceEndpointInterfaceMappingType endpointMapping = endpointMappings[i];
235             QName JavaDoc 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 JavaDoc serviceQName, Definition definition) throws DeploymentException {
244         javax.wsdl.Service service;
245         if (serviceQName != null) {
246             service = definition.getService(serviceQName);
247         } else {
248             Map JavaDoc 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 JavaDoc getMethodForOperation(Class JavaDoc serviceEndpointInterface, Operation operation) throws DeploymentException {
261         Method JavaDoc[] methods = serviceEndpointInterface.getMethods();
262         String JavaDoc opName = operation.getName();
263         Method JavaDoc found = null;
264         for (int i = 0; i < methods.length; i++) {
265             Method JavaDoc 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     /**
280      * Parses a webservice.xml file and returns a map PortInfo instances indexed by the
281      * corresponding ejb-link or servlet-link element .
282      *
283      * @param webservicesType
284      * @param moduleFile
285      * @param isEJB
286      * @return
287      * @throws org.apache.geronimo.common.DeploymentException
288      */

289     public static Map JavaDoc parseWebServiceDescriptor(WebservicesType webservicesType, JarFile JavaDoc moduleFile, boolean isEJB) throws DeploymentException {
290         Map JavaDoc portMap = new HashMap JavaDoc();
291         WebserviceDescriptionType[] webserviceDescriptions = webservicesType.getWebserviceDescriptionArray();
292         for (int i = 0; i < webserviceDescriptions.length; i++) {
293             WebserviceDescriptionType webserviceDescription = webserviceDescriptions[i];
294             URI JavaDoc wsdlURI = null;
295             try {
296                 wsdlURI = new URI JavaDoc(webserviceDescription.getWsdlFile().getStringValue().trim());
297             } catch (URISyntaxException JavaDoc e) {
298                 throw new DeploymentException("could not construct wsdl uri from " + webserviceDescription.getWsdlFile().getStringValue(), e);
299             }
300             URI JavaDoc jaxrpcMappingURI = null;
301             try {
302                 jaxrpcMappingURI = new URI JavaDoc(webserviceDescription.getJaxrpcMappingFile().getStringValue().trim());
303             } catch (URISyntaxException JavaDoc 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 JavaDoc wsdlPortMap = schemaInfoBuilder.getPortMap();
308
309             JavaWsdlMappingType javaWsdlMapping = readJaxrpcMapping(moduleFile, jaxrpcMappingURI);
310             HashMap JavaDoc seiMappings = new HashMap JavaDoc();
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 JavaDoc portComponentName = portComponent.getPortComponentName().getStringValue().trim();
321                 QName JavaDoc portQName = portComponent.getWsdlPort().getQNameValue();
322                 String JavaDoc 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 JavaDoc 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 JavaDoc wsdlLocation = webserviceDescription.getWsdlFile().getStringValue().trim();
343                 URI JavaDoc 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 JavaDoc getAddressLocation(Port port) throws DeploymentException {
356         SOAPAddress soapAddress = (SOAPAddress) SchemaInfoBuilder.getExtensibilityElement(SOAPAddress.class, port.getExtensibilityElements());
357         String JavaDoc locationURIString = soapAddress.getLocationURI();
358         try {
359             URI JavaDoc location = new URI JavaDoc(locationURIString);
360             URI JavaDoc contextPath = new URI JavaDoc(location.getPath());
361             return contextPath;
362         } catch (URISyntaxException JavaDoc e) {
363             throw new DeploymentException("Could not construct web service location URL from " + locationURIString);
364         }
365     }
366
367     public static Map JavaDoc parseWebServiceDescriptor(URL JavaDoc wsDDUrl, JarFile JavaDoc 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 JavaDoc e) {
376             return null;
377         }
378
379     }
380
381     public static List JavaDoc createHandlerInfoList(PortComponentHandlerType[] handlers, ClassLoader JavaDoc classLoader) throws DeploymentException {
382         List JavaDoc list = new ArrayList JavaDoc();
383         for (int i = 0; i < handlers.length; i++) {
384             PortComponentHandlerType handler = handlers[i];
385
386             // Get handler class
387
Class JavaDoc handlerClass = null;
388             String JavaDoc className = handler.getHandlerClass().getStringValue().trim();
389             try {
390                 handlerClass = classLoader.loadClass(className);
391             } catch (ClassNotFoundException JavaDoc e) {
392                 throw new DeploymentException("Unable to load handler class: " + className, e);
393             }
394
395             // config data for the handler
396
Map JavaDoc config = new HashMap JavaDoc();
397             ParamValueType[] paramValues = handler.getInitParamArray();
398             for (int j = 0; j < paramValues.length; j++) {
399                 ParamValueType paramValue = paramValues[j];
400                 String JavaDoc paramName = paramValue.getParamName().getStringValue().trim();
401                 String JavaDoc paramStringValue = paramValue.getParamValue().getStringValue().trim();
402                 config.put(paramName, paramStringValue);
403             }
404
405             // QName array of headers it processes
406
XsdQNameType[] soapHeaderQNames = handler.getSoapHeaderArray();
407             QName JavaDoc[] headers = new QName JavaDoc[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 JavaDoc(handlerClass, config, headers));
414         }
415         return list;
416     }
417 }
418
Popular Tags