KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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.ByteArrayOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.net.URI JavaDoc;
23 import java.net.URISyntaxException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.wsdl.Binding;
33 import javax.wsdl.BindingInput;
34 import javax.wsdl.BindingOperation;
35 import javax.wsdl.Port;
36 import javax.wsdl.extensions.soap.SOAPAddress;
37 import javax.wsdl.extensions.soap.SOAPBinding;
38 import javax.wsdl.extensions.soap.SOAPBody;
39 import javax.xml.namespace.QName JavaDoc;
40
41 import org.apache.axis.constants.Style;
42 import org.apache.axis.constants.Use;
43 import org.apache.axis.description.JavaServiceDesc;
44 import org.apache.axis.description.OperationDesc;
45 import org.apache.axis.encoding.TypeMapping;
46 import org.apache.axis.encoding.TypeMappingRegistryImpl;
47 import org.apache.geronimo.axis.client.TypeInfo;
48 import org.apache.geronimo.axis.server.ReadOnlyServiceDesc;
49 import org.apache.geronimo.axis.server.ServiceInfo;
50 import org.apache.geronimo.common.DeploymentException;
51 import org.apache.geronimo.xbeans.j2ee.JavaXmlTypeMappingType;
52 import org.apache.geronimo.xbeans.j2ee.ServiceEndpointMethodMappingType;
53 import org.apache.geronimo.xbeans.wsdl.DefinitionsDocument;
54 import org.apache.geronimo.xbeans.wsdl.TDefinitions;
55 import org.apache.geronimo.xbeans.wsdl.TImport;
56 import org.apache.geronimo.xbeans.wsdl.TTypes;
57 import org.apache.geronimo.webservices.builder.PortInfo;
58 import org.apache.geronimo.webservices.builder.SchemaInfoBuilder;
59 import org.apache.geronimo.webservices.builder.WSDescriptorParser;
60 import org.apache.xmlbeans.XmlCursor;
61 import org.apache.xmlbeans.XmlObject;
62 import org.apache.xmlbeans.impl.xb.xsdschema.ImportDocument;
63 import org.apache.xmlbeans.impl.xb.xsdschema.IncludeDocument;
64 import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
65
66 /**
67  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
68  */

69 public class AxisServiceBuilder {
70
71     public static final String JavaDoc XSD_NS = "http://www.w3.org/2001/XMLSchema";
72     public static final QName JavaDoc SCHEMA_QNAME = new QName JavaDoc(XSD_NS, "schema");
73
74
75     public static ServiceInfo createServiceInfo(PortInfo portInfo, ClassLoader JavaDoc classLoader) throws DeploymentException {
76         JavaServiceDesc serviceDesc = createServiceDesc(portInfo, classLoader);
77         List JavaDoc handlerInfos = WSDescriptorParser.createHandlerInfoList(portInfo.getHandlers(), classLoader);
78         SchemaInfoBuilder schemaInfoBuilder = portInfo.getSchemaInfoBuilder();
79         Map JavaDoc rawWsdlMap = schemaInfoBuilder.getWsdlMap();
80         Map JavaDoc wsdlMap = rewriteWsdlMap(portInfo, rawWsdlMap);
81         return new ServiceInfo(serviceDesc, handlerInfos, wsdlMap);
82     }
83
84     public static JavaServiceDesc createServiceDesc(PortInfo portInfo, ClassLoader JavaDoc classLoader) throws DeploymentException {
85
86         Port port = portInfo.getPort();
87
88         Class JavaDoc serviceEndpointInterface = null;
89         try {
90             serviceEndpointInterface = classLoader.loadClass(portInfo.getServiceEndpointInterfaceName());
91         } catch (ClassNotFoundException JavaDoc e) {
92             throw (DeploymentException) new DeploymentException("Unable to load the service-endpoint interface for port-component " + portInfo.getPortComponentName()).initCause(e);
93         }
94
95         Map JavaDoc exceptionMap = WSDescriptorParser.getExceptionMap(portInfo.getJavaWsdlMapping());
96         SchemaInfoBuilder schemaInfoBuilder = portInfo.getSchemaInfoBuilder();
97         Map JavaDoc schemaTypeKeyToSchemaTypeMap = schemaInfoBuilder.getSchemaTypeKeyToSchemaTypeMap();
98
99         JavaServiceDesc serviceDesc = new JavaServiceDesc();
100         String JavaDoc serviceName = portInfo.getPortQName().toString();
101         String JavaDoc location = getAddressLocation(port);
102         serviceDesc.setName(serviceName);
103         serviceDesc.setEndpointURL(location);
104         serviceDesc.setWSDLFile(portInfo.getWsdlLocation());
105         Binding binding = port.getBinding();
106
107         serviceDesc.setStyle(getStyle(binding));
108
109
110         BindingInput bindingInput = ((BindingOperation) binding.getBindingOperations().get(0)).getBindingInput();
111         SOAPBody soapBody = (SOAPBody) SchemaInfoBuilder.getExtensibilityElement(SOAPBody.class, bindingInput.getExtensibilityElements());
112
113         if (soapBody.getUse() != null) {
114             Use use = Use.getUse(soapBody.getUse());
115             serviceDesc.setUse(use);
116         } else {
117             serviceDesc.setUse(Use.ENCODED);
118         }
119         boolean hasEncoded = serviceDesc.getUse() == Use.ENCODED;
120
121         boolean isLightweight = portInfo.getServiceEndpointInterfaceMapping() == null;
122
123 // if (isLightweight) {
124
// validateLightweightMapping(portInfo.getDefinition());
125
// }
126

127         Collection JavaDoc operations = new ArrayList JavaDoc();
128         Set JavaDoc wrapperElementQNames = buildOperations(binding, serviceEndpointInterface, isLightweight, portInfo, exceptionMap, classLoader, operations);
129         for (Iterator JavaDoc iter = operations.iterator(); iter.hasNext();) {
130             OperationDesc operation = (OperationDesc) iter.next();
131             serviceDesc.addOperationDesc(operation);
132         }
133
134         TypeMappingRegistryImpl tmr = new TypeMappingRegistryImpl();
135         tmr.doRegisterFromVersion("1.3");
136
137         TypeMapping typeMapping = tmr.getOrMakeTypeMapping(serviceDesc.getUse().getEncoding());
138
139         serviceDesc.setTypeMappingRegistry(tmr);
140         serviceDesc.setTypeMapping(typeMapping);
141
142         List JavaDoc typeInfo;
143         if (isLightweight) {
144             LightweightTypeInfoBuilder builder = new LightweightTypeInfoBuilder(classLoader, schemaTypeKeyToSchemaTypeMap, wrapperElementQNames);
145             typeInfo = builder.buildTypeInfo(portInfo.getJavaWsdlMapping());
146         } else {
147             HeavyweightTypeInfoBuilder builder = new HeavyweightTypeInfoBuilder(classLoader, schemaTypeKeyToSchemaTypeMap, wrapperElementQNames, operations, hasEncoded);
148             typeInfo = builder.buildTypeInfo(portInfo.getJavaWsdlMapping());
149         }
150
151         // We register type mappings and invoke serviceDesc.getOperations to trigger an introspection of the
152
// operations. By doing these operations during deployment, no introspection is required during runtime.
153
TypeInfo.register(typeInfo, typeMapping);
154         serviceDesc.getOperations();
155
156         return new ReadOnlyServiceDesc(serviceDesc, typeInfo);
157     }
158
159     private static Set JavaDoc buildOperations(Binding binding, Class JavaDoc serviceEndpointInterface, boolean lightweight, PortInfo portInfo, Map JavaDoc exceptionMap, ClassLoader JavaDoc classLoader, Collection JavaDoc operations) throws DeploymentException {
160         Set JavaDoc wrappedElementQNames = new HashSet JavaDoc();
161
162         SOAPBinding soapBinding = (SOAPBinding) SchemaInfoBuilder.getExtensibilityElement(SOAPBinding.class, binding.getExtensibilityElements());
163         String JavaDoc portStyleString = soapBinding.getStyle();
164         Style portStyle = Style.getStyle(portStyleString);
165
166         List JavaDoc bindingOperations = binding.getBindingOperations();
167         for (int i = 0; i < bindingOperations.size(); i++) {
168             BindingOperation bindingOperation = (BindingOperation) bindingOperations.get(i);
169
170             OperationDescBuilder operationDescBuilder;
171             if (lightweight) {
172                 Method JavaDoc method = WSDescriptorParser.getMethodForOperation(serviceEndpointInterface, bindingOperation.getOperation());
173                 operationDescBuilder = new LightweightOperationDescBuilder(bindingOperation, method);
174             } else {
175                 String JavaDoc operationName = bindingOperation.getOperation().getName();
176                 ServiceEndpointMethodMappingType[] methodMappings = portInfo.getServiceEndpointInterfaceMapping().getServiceEndpointMethodMappingArray();
177                 ServiceEndpointMethodMappingType methodMapping = WSDescriptorParser.getMethodMappingForOperation(operationName, methodMappings);
178                 JavaXmlTypeMappingType[] javaXmlTypeMappingTypes = portInfo.getJavaWsdlMapping().getJavaXmlTypeMappingArray();
179                 operationDescBuilder = new HeavyweightOperationDescBuilder(bindingOperation, portInfo.getJavaWsdlMapping(), methodMapping, portStyle, exceptionMap, portInfo.getSchemaInfoBuilder(), javaXmlTypeMappingTypes, classLoader, serviceEndpointInterface);
180                 Set JavaDoc wrappedElementQNamesForOper = ((HeavyweightOperationDescBuilder) operationDescBuilder).getWrapperElementQNames();
181                 wrappedElementQNames.addAll(wrappedElementQNamesForOper);
182             }
183
184             operations.add(operationDescBuilder.buildOperationDesc());
185         }
186
187         return wrappedElementQNames;
188     }
189
190
191     private static Style getStyle(Binding binding) throws DeploymentException {
192         SOAPBinding soapBinding = (SOAPBinding) SchemaInfoBuilder.getExtensibilityElement(SOAPBinding.class, binding.getExtensibilityElements());
193         String JavaDoc portStyleString = soapBinding.getStyle();
194         Style portStyle = Style.getStyle(portStyleString);
195         return portStyle;
196     }
197
198     private static String JavaDoc getAddressLocation(Port port) throws DeploymentException {
199         SOAPAddress soapAddress = (SOAPAddress) SchemaInfoBuilder.getExtensibilityElement(SOAPAddress.class, port.getExtensibilityElements());
200         String JavaDoc locationURIString = soapAddress.getLocationURI();
201         return locationURIString;
202     }
203
204     private static Map JavaDoc rewriteWsdlMap(PortInfo portInfo, Map JavaDoc rawWsdlMap) throws DeploymentException {
205         URI JavaDoc contextURI = portInfo.getContextURI();
206         Map JavaDoc wsdlMap = new HashMap JavaDoc();
207         for (Iterator JavaDoc iterator = rawWsdlMap.entrySet().iterator(); iterator.hasNext();) {
208             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
209             URI JavaDoc key = (URI JavaDoc) entry.getKey();
210             Object JavaDoc value = entry.getValue();
211             if (value instanceof SchemaDocument) {
212                 SchemaDocument schemaDocument = (SchemaDocument) ((SchemaDocument) value).copy();
213                 SchemaDocument.Schema schema = schemaDocument.getSchema();
214                 rewriteSchema(schema, contextURI, key);
215                 String JavaDoc schemaString = xmlObjectToString(schemaDocument);
216                 wsdlMap.put(key.toString(), schemaString);
217             } else if (value instanceof DefinitionsDocument) {
218                 DefinitionsDocument doc = (DefinitionsDocument) ((DefinitionsDocument) value).copy();
219                 TDefinitions definitions = doc.getDefinitions();
220                 TImport[] imports = definitions.getImportArray();
221                 for (int i = 0; i < imports.length; i++) {
222                     TImport anImport = imports[i];
223                     String JavaDoc importLocation = anImport.getLocation().trim();
224                     if (!importLocation.startsWith("http://")) {
225                         URI JavaDoc updated = buildQueryURI(contextURI, key, importLocation);
226                         anImport.setLocation(updated.toString());
227                     }
228                 }
229                 TTypes[] types = definitions.getTypesArray();
230                 for (int i = 0; i < types.length; i++) {
231                     TTypes type = types[i];
232                     XmlCursor typeCursor = type.newCursor();
233                     try {
234                         if (typeCursor.toChild(SCHEMA_QNAME)) {
235                             do {
236                                 SchemaDocument.Schema schema = (SchemaDocument.Schema) typeCursor.getObject();
237                                 rewriteSchema(schema, contextURI, key);
238                             } while (typeCursor.toNextSibling(SCHEMA_QNAME));
239                         }
240                     } finally {
241                         typeCursor.dispose();
242                     }
243                 }
244                 String JavaDoc docString = xmlObjectToString(doc);
245                 wsdlMap.put(key.toString(), docString);
246             } else {
247                 throw new DeploymentException("Unexpected element in wsdlMap at location: " + key + ", value: " + value);
248             }
249         }
250         return wsdlMap;
251     }
252
253     static String JavaDoc xmlObjectToString(XmlObject xmlObject) throws DeploymentException {
254         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
255         try {
256             xmlObject.save(baos);
257             baos.flush();
258             String JavaDoc result = new String JavaDoc(baos.toByteArray());
259             return result;
260         } catch (IOException JavaDoc e) {
261             throw new DeploymentException("Could not write xml object to string", e);
262         }
263     }
264
265     private static void rewriteSchema(SchemaDocument.Schema schema, URI JavaDoc contextURI, URI JavaDoc key) throws DeploymentException {
266         ImportDocument.Import[] imports = schema.getImportArray();
267         for (int i = 0; i < imports.length; i++) {
268             ImportDocument.Import anImport = imports[i];
269             if (anImport.isSetSchemaLocation()) {
270                 String JavaDoc schemaLocation = anImport.getSchemaLocation();
271                 URI JavaDoc absoluteSchemLocation = buildQueryURI(contextURI, key, schemaLocation);
272                 anImport.setSchemaLocation(absoluteSchemLocation.toString());
273             }
274         }
275         IncludeDocument.Include[] includes = schema.getIncludeArray();
276         for (int i = 0; i < includes.length; i++) {
277             IncludeDocument.Include include = includes[i];
278             String JavaDoc schemaLocation = include.getSchemaLocation();
279             URI JavaDoc absoluteSchemLocation = buildQueryURI(contextURI, key, schemaLocation);
280             include.setSchemaLocation(absoluteSchemLocation.toString());
281         }
282     }
283
284     private static URI JavaDoc buildQueryURI(URI JavaDoc contextURI, URI JavaDoc key, String JavaDoc importLocation) throws DeploymentException {
285         try {
286             URI JavaDoc importLocationURI = new URI JavaDoc(importLocation);
287             if (importLocationURI.isAbsolute() || importLocationURI.getPath().startsWith("/")) {
288                 return importLocationURI;
289             }
290             URI JavaDoc queryURI = new URI JavaDoc(null,
291                     null,
292                     contextURI.getPath(),
293                     "wsdl=" + key.resolve(importLocationURI),
294                     null);
295             return queryURI;
296         } catch (URISyntaxException JavaDoc e) {
297             throw new DeploymentException("Could not construct wsdl location URI", e);
298         }
299     }
300
301
302 }
303
Popular Tags