KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > webservices > builder > SchemaInfoBuilder


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.webservices.builder;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Stack JavaDoc;
30 import java.util.jar.JarFile JavaDoc;
31 import java.util.zip.ZipEntry JavaDoc;
32 import javax.wsdl.Definition;
33 import javax.wsdl.Import;
34 import javax.wsdl.Service;
35 import javax.wsdl.Types;
36 import javax.wsdl.WSDLException;
37 import javax.wsdl.extensions.ExtensibilityElement;
38 import javax.wsdl.extensions.ExtensionRegistry;
39 import javax.wsdl.extensions.UnknownExtensibilityElement;
40 import javax.wsdl.extensions.schema.Schema;
41 import javax.wsdl.factory.WSDLFactory;
42 import javax.wsdl.xml.WSDLLocator;
43 import javax.wsdl.xml.WSDLReader;
44 import javax.xml.namespace.QName JavaDoc;
45
46 import com.ibm.wsdl.extensions.PopulatedExtensionRegistry;
47 import com.ibm.wsdl.extensions.schema.SchemaConstants;
48 import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50 import org.apache.geronimo.webservices.WebServiceContainer;
51 import org.apache.geronimo.common.DeploymentException;
52 import org.apache.geronimo.xbeans.wsdl.DefinitionsDocument;
53 import org.apache.geronimo.xbeans.wsdl.TDefinitions;
54 import org.apache.geronimo.xbeans.wsdl.TPort;
55 import org.apache.geronimo.xbeans.wsdl.TService;
56 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
57 import org.apache.xmlbeans.SchemaField;
58 import org.apache.xmlbeans.SchemaGlobalElement;
59 import org.apache.xmlbeans.SchemaParticle;
60 import org.apache.xmlbeans.SchemaType;
61 import org.apache.xmlbeans.SchemaTypeSystem;
62 import org.apache.xmlbeans.XmlBeans;
63 import org.apache.xmlbeans.XmlCursor;
64 import org.apache.xmlbeans.XmlError;
65 import org.apache.xmlbeans.XmlException;
66 import org.apache.xmlbeans.XmlObject;
67 import org.apache.xmlbeans.XmlOptions;
68 import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
69 import org.w3c.dom.Element JavaDoc;
70 import org.xml.sax.EntityResolver JavaDoc;
71 import org.xml.sax.InputSource JavaDoc;
72 import org.xml.sax.SAXException JavaDoc;
73
74 /**
75  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
76  */

77 public class SchemaInfoBuilder {
78     private static final Log log = LogFactory.getLog(SchemaInfoBuilder.class);
79     private static final SchemaTypeSystem basicTypeSystem;
80 // private static final String[] errorNames = {"Error", "Warning", "Info"};
81
private static final String JavaDoc SOAP_NS = "http://schemas.xmlsoap.org/wsdl/soap/";
82     private static final QName JavaDoc ADDRESS_QNAME = new QName JavaDoc(SOAP_NS, "address");
83     private static final QName JavaDoc LOCATION_QNAME = new QName JavaDoc("", "location");
84
85     static {
86         InputStream JavaDoc is = WSDescriptorParser.class.getClassLoader().getResourceAsStream("META-INF/schema/soap_encoding_1_1.xsd");
87         if (is == null) {
88             throw new RuntimeException JavaDoc("Could not locate soap encoding schema");
89         }
90         ArrayList JavaDoc errors = new ArrayList JavaDoc();
91         XmlOptions xmlOptions = XmlBeansUtil.createXmlOptions(errors);
92         try {
93             SchemaDocument parsed = SchemaDocument.Factory.parse(is, xmlOptions);
94             if (errors.size() != 0) {
95                 throw new XmlException(errors.toArray().toString());
96             }
97
98             basicTypeSystem = XmlBeans.compileXsd(new XmlObject[]{parsed}, XmlBeans.getBuiltinTypeSystem(), xmlOptions);
99             if (errors.size() > 0) {
100                 throw new RuntimeException JavaDoc("Could not compile schema type system: errors: " + errors);
101             }
102         } catch (XmlException e) {
103             throw new RuntimeException JavaDoc("Could not compile schema type system", e);
104         } catch (IOException JavaDoc e) {
105             throw new RuntimeException JavaDoc("Could not compile schema type system", e);
106         } finally {
107             try {
108                 is.close();
109             } catch (IOException JavaDoc ignore) {
110                 // ignore
111
}
112         }
113     }
114
115     private final JarFile JavaDoc moduleFile;
116     private final Definition definition;
117     private final Stack JavaDoc uris = new Stack JavaDoc();
118     private final Map JavaDoc wsdlMap = new HashMap JavaDoc();
119     private final Map JavaDoc schemaTypeKeyToSchemaTypeMap;
120     private final Map JavaDoc complexTypeMap;
121     private final Map JavaDoc elementMap;
122     private final Map JavaDoc simpleTypeMap;
123     private final Map JavaDoc portMap;
124
125
126     public SchemaInfoBuilder(JarFile JavaDoc moduleFile, URI JavaDoc wsdlUri) throws DeploymentException {
127         this(moduleFile, wsdlUri, null, null);
128     }
129
130     public SchemaInfoBuilder(JarFile JavaDoc moduleFile, Definition definition) throws DeploymentException {
131         this(moduleFile, null, definition, null);
132     }
133
134     SchemaInfoBuilder(JarFile JavaDoc moduleFile, URI JavaDoc uri, SchemaTypeSystem schemaTypeSystem) throws DeploymentException {
135         this(moduleFile, uri, null, schemaTypeSystem);
136     }
137
138     SchemaInfoBuilder(JarFile JavaDoc moduleFile, URI JavaDoc uri, Definition definition, SchemaTypeSystem schemaTypeSystem) throws DeploymentException {
139         this.moduleFile = moduleFile;
140         if (uri != null) {
141             uris.push(uri);
142             if (definition == null && schemaTypeSystem == null) {
143                 definition = readWsdl(moduleFile, uri);
144             }
145         } else if (definition != null) {
146             try {
147                 uri = new URI JavaDoc(definition.getDocumentBaseURI());
148                 uris.push(uri);
149             } catch (URISyntaxException JavaDoc e) {
150                 throw new DeploymentException("Could not locate definition", e);
151             }
152         } else {
153             throw new DeploymentException("You must supply uri or definition");
154         }
155         if (schemaTypeSystem == null) {
156             schemaTypeSystem = compileSchemaTypeSystem(definition);
157         }
158         this.definition = definition;
159         schemaTypeKeyToSchemaTypeMap = buildSchemaTypeKeyToSchemaTypeMap(schemaTypeSystem);
160         complexTypeMap = buildComplexTypeMap();
161         simpleTypeMap = buildSimpleTypeMap();
162         elementMap = buildElementMap();
163         portMap = buildPortMap();
164     }
165
166     public Map JavaDoc getSchemaTypeKeyToSchemaTypeMap() {
167         return schemaTypeKeyToSchemaTypeMap;
168     }
169
170     public Definition getDefinition() {
171         return definition;
172     }
173
174     public Map JavaDoc getWsdlMap() {
175         return wsdlMap;
176     }
177
178     /**
179      * Find all the complex types in the previously constructed schema analysis.
180      * Put them in a map from complex type QName to schema fragment.
181      *
182      * @return map of complexType QName to schema fragment
183      */

184     public Map JavaDoc getComplexTypesInWsdl() {
185         return complexTypeMap;
186     }
187
188     private Map JavaDoc buildComplexTypeMap() {
189         Map JavaDoc complexTypeMap = new HashMap JavaDoc();
190         for (Iterator JavaDoc iterator = schemaTypeKeyToSchemaTypeMap.entrySet().iterator(); iterator.hasNext();) {
191             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
192             SchemaTypeKey key = (SchemaTypeKey) entry.getKey();
193             if (!key.isSimpleType() && !key.isAnonymous()) {
194                 QName JavaDoc qName = key.getqName();
195                 SchemaType schemaType = (SchemaType) entry.getValue();
196                 complexTypeMap.put(qName, schemaType);
197             }
198         }
199         return complexTypeMap;
200     }
201
202     public Map JavaDoc getElementToTypeMap() {
203         return elementMap;
204     }
205
206     private Map JavaDoc buildElementMap() {
207         Map JavaDoc elementToTypeMap = new HashMap JavaDoc();
208         for (Iterator JavaDoc iterator = schemaTypeKeyToSchemaTypeMap.entrySet().iterator(); iterator.hasNext();) {
209             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
210             SchemaTypeKey key = (SchemaTypeKey) entry.getKey();
211             if (key.isElement()) {
212                 QName JavaDoc elementQName = key.getqName();
213                 SchemaType schemaType = (SchemaType) entry.getValue();
214                 QName JavaDoc typeQName = schemaType.getName();
215                 elementToTypeMap.put(elementQName, typeQName);
216             }
217         }
218         return elementToTypeMap;
219     }
220
221     /**
222      * Gets a map of all the javax.wsdl.Port instance in the WSDL definition keyed by the port's QName
223      * <p/>
224      * WSDL 1.1 spec: 2.6 "The name attribute provides a unique name among all ports defined within in the enclosing WSDL document."
225      *
226      * @return Map of port QName to javax.wsdl.Port for that QName.
227      */

228
229     public Map JavaDoc getPortMap() {
230         return portMap;
231     }
232
233     private Map JavaDoc buildPortMap() {
234         HashMap JavaDoc ports = new HashMap JavaDoc();
235         if (definition != null) {
236             Collection JavaDoc services = definition.getServices().values();
237             for (Iterator JavaDoc iterator = services.iterator(); iterator.hasNext();) {
238                 Service service = (Service) iterator.next();
239                 ports.putAll(service.getPorts());
240             }
241         }
242         return ports;
243     }
244
245     public Map JavaDoc getSimpleTypeMap() {
246         return simpleTypeMap;
247     }
248
249     private Map JavaDoc buildSimpleTypeMap() {
250         Map JavaDoc simpleTypeMap = new HashMap JavaDoc();
251         for (Iterator JavaDoc iterator = schemaTypeKeyToSchemaTypeMap.entrySet().iterator(); iterator.hasNext();) {
252             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
253             SchemaTypeKey key = (SchemaTypeKey) entry.getKey();
254             if (key.isSimpleType() && !key.isAnonymous()) {
255                 QName JavaDoc qName = key.getqName();
256                 SchemaType schemaType = (SchemaType) entry.getValue();
257                 simpleTypeMap.put(qName, schemaType);
258             }
259         }
260         return simpleTypeMap;
261     }
262
263     public SchemaTypeSystem compileSchemaTypeSystem(Definition definition) throws DeploymentException {
264         List JavaDoc schemaList = new ArrayList JavaDoc();
265         addImportsFromDefinition(definition, schemaList);
266 // System.out.println("Schemas: " + schemaList);
267
Collection JavaDoc errors = new ArrayList JavaDoc();
268         XmlOptions xmlOptions = new XmlOptions();
269         xmlOptions.setErrorListener(errors);
270         xmlOptions.setEntityResolver(new JarEntityResolver());
271         XmlObject[] schemas = (XmlObject[]) schemaList.toArray(new XmlObject[schemaList.size()]);
272         try {
273             SchemaTypeSystem schemaTypeSystem = XmlBeans.compileXsd(schemas, basicTypeSystem, xmlOptions);
274             if (errors.size() > 0) {
275                 boolean wasError = false;
276                 for (Iterator JavaDoc iterator = errors.iterator(); iterator.hasNext();) {
277                     XmlError xmlError = (XmlError) iterator.next();
278                     if(xmlError.getSeverity() == XmlError.SEVERITY_ERROR) {
279                         log.error(xmlError);
280                         wasError = true;
281                     } else if(xmlError.getSeverity() == XmlError.SEVERITY_WARNING) {
282                         log.warn(xmlError);
283                     } else if(xmlError.getSeverity() == XmlError.SEVERITY_INFO) {
284                         log.debug(xmlError);
285                     }
286                 }
287                 if (wasError) {
288                     throw new DeploymentException("Could not compile schema type system, see log for errors");
289                 }
290             }
291             return schemaTypeSystem;
292         } catch (XmlException e) {
293             throw new DeploymentException("Could not compile schema type system: " + schemaList, e);
294         }
295     }
296
297     private void addImportsFromDefinition(Definition definition, List JavaDoc schemaList) throws DeploymentException {
298         Map JavaDoc namespaceMap = definition.getNamespaces();
299         Types types = definition.getTypes();
300         if (types != null) {
301             List JavaDoc schemas = types.getExtensibilityElements();
302             for (Iterator JavaDoc iterator = schemas.iterator(); iterator.hasNext();) {
303                 Object JavaDoc o = iterator.next();
304                 if (o instanceof Schema) {
305                     Schema unknownExtensibilityElement = (Schema) o;
306                     QName JavaDoc elementType = unknownExtensibilityElement.getElementType();
307                     if (new QName JavaDoc("http://www.w3.org/2001/XMLSchema", "schema").equals(elementType)) {
308                         Element element = unknownExtensibilityElement.getElement();
309                         addSchemaElement(element, namespaceMap, schemaList);
310                     }
311                 } else if (o instanceof UnknownExtensibilityElement) {
312                     //This is allegedly obsolete as of axis-wsdl4j-1.2-RC3.jar which includes the Schema extension above.
313
//The change notes imply that imported schemas should end up in Schema elements. They don't, so this is still needed.
314
UnknownExtensibilityElement unknownExtensibilityElement = (UnknownExtensibilityElement) o;
315                     Element element = unknownExtensibilityElement.getElement();
316                     String JavaDoc elementNamespace = element.getNamespaceURI();
317                     String JavaDoc elementLocalName = element.getNodeName();
318                     if ("http://www.w3.org/2001/XMLSchema".equals(elementNamespace) && "schema".equals(elementLocalName)) {
319                         addSchemaElement(element, namespaceMap, schemaList);
320                     }
321                 }
322             }
323         }
324         Map JavaDoc imports = definition.getImports();
325         if (imports != null) {
326             for (Iterator JavaDoc iterator = imports.entrySet().iterator(); iterator.hasNext();) {
327                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
328                 String JavaDoc namespaceURI = (String JavaDoc) entry.getKey();
329                 List JavaDoc importList = (List JavaDoc) entry.getValue();
330                 for (Iterator JavaDoc iterator1 = importList.iterator(); iterator1.hasNext();) {
331                     Import anImport = (Import) iterator1.next();
332                     //according to the 1.1 jwsdl mr shcema imports are supposed to show up here,
333
//but according to the 1.0 spec there is supposed to be no Definition.
334
Definition definition1 = anImport.getDefinition();
335                     if (definition1 != null) {
336                         try {
337                             URI JavaDoc uri = new URI JavaDoc(definition1.getDocumentBaseURI());
338                             uris.push(uri);
339                         } catch (URISyntaxException JavaDoc e) {
340                             throw new DeploymentException("Could not locate definition", e);
341                         }
342                         try {
343                             addImportsFromDefinition(definition1, schemaList);
344                         } finally {
345                             uris.pop();
346                         }
347                     } else {
348                         log.warn("Missing definition in import for namespace " + namespaceURI);
349                     }
350                 }
351             }
352         }
353     }
354
355     private void addSchemaElement(Element element, Map JavaDoc namespaceMap, List JavaDoc schemaList) throws DeploymentException {
356         try {
357             XmlObject xmlObject = parseWithNamespaces(element, namespaceMap);
358             schemaList.add(xmlObject);
359         } catch (XmlException e) {
360             throw new DeploymentException("Could not parse schema element", e);
361         }
362     }
363
364     static XmlObject parseWithNamespaces(Element element, Map JavaDoc namespaceMap) throws XmlException {
365         ArrayList JavaDoc errors = new ArrayList JavaDoc();
366         XmlOptions xmlOptions = XmlBeansUtil.createXmlOptions(errors);
367         SchemaDocument parsed = SchemaDocument.Factory.parse(element, xmlOptions);
368         if (errors.size() != 0) {
369             throw new XmlException(errors.toArray().toString());
370         }
371         XmlCursor cursor = parsed.newCursor();
372         try {
373             cursor.toFirstContentToken();
374             for (Iterator JavaDoc namespaces = namespaceMap.entrySet().iterator(); namespaces.hasNext();) {
375                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) namespaces.next();
376                 cursor.insertNamespace((String JavaDoc) entry.getKey(), (String JavaDoc) entry.getValue());
377             }
378         } finally {
379             cursor.dispose();
380         }
381         return parsed;
382     }
383
384     /**
385      * builds a map of SchemaTypeKey containing jaxrpc-style fake QName and context info to xmlbeans SchemaType object.
386      *
387      * @param schemaTypeSystem
388      * @return Map of SchemaTypeKey to xmlbeans SchemaType object.
389      */

390     private Map JavaDoc buildSchemaTypeKeyToSchemaTypeMap(SchemaTypeSystem schemaTypeSystem) {
391         Map JavaDoc qnameMap = new HashMap JavaDoc();
392         SchemaType[] globalTypes = schemaTypeSystem.globalTypes();
393         for (int i = 0; i < globalTypes.length; i++) {
394             SchemaType globalType = globalTypes[i];
395             QName JavaDoc typeQName = globalType.getName();
396             addSchemaType(typeQName, globalType, false, qnameMap);
397         }
398         SchemaGlobalElement[] globalElements = schemaTypeSystem.globalElements();
399         for (int i = 0; i < globalElements.length; i++) {
400             SchemaGlobalElement globalElement = globalElements[i];
401             addElement(globalElement, null, qnameMap);
402         }
403         return qnameMap;
404     }
405
406     private void addElement(SchemaField element, SchemaTypeKey key, Map JavaDoc qnameMap) {
407         //TODO is this null if element is a ref?
408
QName JavaDoc elementName = element.getName();
409         String JavaDoc elementNamespace = elementName.getNamespaceURI();
410         //"" namespace means local element with elementFormDefault="unqualified"
411
if (elementNamespace == null || elementNamespace.equals("")) {
412             elementNamespace = key.getqName().getNamespaceURI();
413         }
414         String JavaDoc elementQNameLocalName;
415         SchemaTypeKey elementKey;
416         if (key == null) {
417             //top level. rule 2.a,
418
elementQNameLocalName = elementName.getLocalPart();
419             elementKey = new SchemaTypeKey(elementName, true, false, false, elementName);
420         } else {
421             //not top level. rule 2.b, key will be for enclosing Type.
422
QName JavaDoc enclosingTypeQName = key.getqName();
423             String JavaDoc enclosingTypeLocalName = enclosingTypeQName.getLocalPart();
424             elementQNameLocalName = enclosingTypeLocalName + ">" + elementName.getLocalPart();
425             QName JavaDoc subElementName = new QName JavaDoc(elementNamespace, elementQNameLocalName);
426             elementKey = new SchemaTypeKey(subElementName, true, false, true, elementName);
427         }
428         SchemaType schemaType = element.getType();
429         qnameMap.put(elementKey, schemaType);
430 // new Exception("Adding: " + elementKey.getqName().getLocalPart()).printStackTrace();
431
//check if it's an array. maxOccurs is null if unbounded
432
//element should always be a SchemaParticle... this is a workaround for XMLBEANS-137
433
if (element instanceof SchemaParticle) {
434             addArrayForms((SchemaParticle) element, elementKey.getqName(), qnameMap, schemaType);
435         } else {
436             log.warn("element is not a schemaParticle! " + element);
437         }
438         //now, name for type. Rule 1.b, type inside an element
439
String JavaDoc typeQNameLocalPart = ">" + elementQNameLocalName;
440         QName JavaDoc typeQName = new QName JavaDoc(elementNamespace, typeQNameLocalPart);
441         boolean isAnonymous = true;
442         addSchemaType(typeQName, schemaType, isAnonymous, qnameMap);
443     }
444
445     private void addSchemaType(QName JavaDoc typeQName, SchemaType schemaType, boolean anonymous, Map JavaDoc qnameMap) {
446         SchemaTypeKey typeKey = new SchemaTypeKey(typeQName, false, schemaType.isSimpleType(), anonymous, null);
447         qnameMap.put(typeKey, schemaType);
448 // new Exception("Adding: " + typeKey.getqName().getLocalPart()).printStackTrace();
449
//TODO xmlbeans recommends using summary info from getElementProperties and getAttributeProperties instead of traversing the content model by hand.
450
SchemaParticle schemaParticle = schemaType.getContentModel();
451         if (schemaParticle != null) {
452             addSchemaParticle(schemaParticle, typeKey, qnameMap);
453         }
454     }
455
456
457     private void addSchemaParticle(SchemaParticle schemaParticle, SchemaTypeKey key, Map JavaDoc qnameMap) {
458         if (schemaParticle.getParticleType() == SchemaParticle.ELEMENT) {
459             SchemaType elementType = schemaParticle.getType();
460             SchemaField element = elementType.getContainerField();
461             //element will be null if the type is defined elsewhere, such as a built in type.
462
if (element != null) {
463                 addElement(element, key, qnameMap);
464             } else {
465                 QName JavaDoc keyQName = key.getqName();
466                 //TODO I can't distinguish between 3.a and 3.b, so generate names both ways.
467
//3.b
468
String JavaDoc localPart = schemaParticle.getName().getLocalPart();
469                 QName JavaDoc elementName = new QName JavaDoc(keyQName.getNamespaceURI(), localPart);
470                 addArrayForms(schemaParticle, elementName, qnameMap, elementType);
471                 //3.a
472
localPart = keyQName.getLocalPart() + ">" + schemaParticle.getName().getLocalPart();
473                 elementName = new QName JavaDoc(keyQName.getNamespaceURI(), localPart);
474                 addArrayForms(schemaParticle, elementName, qnameMap, elementType);
475             }
476         } else {
477             try {
478                 SchemaParticle[] children = schemaParticle.getParticleChildren();
479                 for (int i = 0; i < children.length; i++) {
480                     SchemaParticle child = children[i];
481                     addSchemaParticle(child, key, qnameMap);
482                 }
483             } catch (NullPointerException JavaDoc e) {
484                 //ignore xmlbeans bug
485
}
486         }
487     }
488
489     private void addArrayForms(SchemaParticle schemaParticle, QName JavaDoc keyName, Map JavaDoc qnameMap, SchemaType elementType) {
490         //it may be a ref or a built in type. If it's an array (maxOccurs >1) form a type for it.
491
if (schemaParticle.getIntMaxOccurs() > 1) {
492             String JavaDoc maxOccurs = schemaParticle.getMaxOccurs() == null ? "unbounded" : "" + schemaParticle.getIntMaxOccurs();
493             int minOccurs = schemaParticle.getIntMinOccurs();
494             QName JavaDoc elementName = schemaParticle.getName();
495             String JavaDoc arrayQNameLocalName = keyName.getLocalPart() + "[" + minOccurs + "," + maxOccurs + "]";
496             String JavaDoc elementNamespace = elementName.getNamespaceURI();
497             if (elementNamespace == null || elementNamespace.equals("")) {
498                 elementNamespace = keyName.getNamespaceURI();
499             }
500             QName JavaDoc arrayName = new QName JavaDoc(elementNamespace, arrayQNameLocalName);
501             SchemaTypeKey arrayKey = new SchemaTypeKey(arrayName, false, false, true, elementName);
502             //TODO not clear we want the schemaType as the value
503
qnameMap.put(arrayKey, elementType);
504 // new Exception("Adding: " + arrayKey.getqName().getLocalPart()).printStackTrace();
505
if (minOccurs == 1) {
506                 arrayQNameLocalName = keyName.getLocalPart() + "[," + maxOccurs + "]";
507                 arrayName = new QName JavaDoc(elementNamespace, arrayQNameLocalName);
508                 arrayKey = new SchemaTypeKey(arrayName, false, false, true, elementName);
509                 //TODO not clear we want the schemaType as the value
510
qnameMap.put(arrayKey, elementType);
511             }
512         }
513     }
514
515
516     public Definition readWsdl(JarFile JavaDoc moduleFile, URI JavaDoc wsdlURI) throws DeploymentException {
517         Definition definition;
518         WSDLFactory wsdlFactory;
519         try {
520             wsdlFactory = WSDLFactory.newInstance();
521         } catch (WSDLException e) {
522             throw new DeploymentException("Could not create WSDLFactory", e);
523         }
524         WSDLReader wsdlReaderNoImport = wsdlFactory.newWSDLReader();
525         wsdlReaderNoImport.setFeature("javax.wsdl.importDocuments", false);
526         ExtensionRegistry extensionRegistry = new PopulatedExtensionRegistry();
527         extensionRegistry.mapExtensionTypes(Types.class, SchemaConstants.Q_ELEM_XSD_1999,
528                 UnknownExtensibilityElement.class);
529         extensionRegistry.registerDeserializer(Types.class, SchemaConstants.Q_ELEM_XSD_1999,
530                 extensionRegistry.getDefaultDeserializer());
531         extensionRegistry.registerSerializer(Types.class, SchemaConstants.Q_ELEM_XSD_1999,
532                 extensionRegistry.getDefaultSerializer());
533
534         extensionRegistry.mapExtensionTypes(Types.class, SchemaConstants.Q_ELEM_XSD_2000,
535                 UnknownExtensibilityElement.class);
536         extensionRegistry.registerDeserializer(Types.class, SchemaConstants.Q_ELEM_XSD_2000,
537                 extensionRegistry.getDefaultDeserializer());
538         extensionRegistry.registerSerializer(Types.class, SchemaConstants.Q_ELEM_XSD_2000,
539                 extensionRegistry.getDefaultSerializer());
540
541         extensionRegistry.mapExtensionTypes(Types.class, SchemaConstants.Q_ELEM_XSD_2001,
542                 UnknownExtensibilityElement.class);
543         extensionRegistry.registerDeserializer(Types.class, SchemaConstants.Q_ELEM_XSD_2001,
544                 extensionRegistry.getDefaultDeserializer());
545         extensionRegistry.registerSerializer(Types.class, SchemaConstants.Q_ELEM_XSD_2001,
546                 extensionRegistry.getDefaultSerializer());
547         wsdlReaderNoImport.setExtensionRegistry(extensionRegistry);
548
549         JarWSDLLocator wsdlLocator = new JarWSDLLocator(wsdlURI);
550         WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
551
552         Thread JavaDoc thread = Thread.currentThread();
553         ClassLoader JavaDoc oldCl = thread.getContextClassLoader();
554         thread.setContextClassLoader(this.getClass().getClassLoader());
555         try {
556             try {
557                 definition = wsdlReader.readWSDL(wsdlLocator);
558             } catch (WSDLException e) {
559                 throw new DeploymentException("Failed to read wsdl document", e);
560             } catch (RuntimeException JavaDoc e) {
561                 throw new DeploymentException(e.getMessage(), e);
562             }
563         } finally {
564             thread.setContextClassLoader(oldCl);
565         }
566
567         return definition;
568     }
569
570     public static ExtensibilityElement getExtensibilityElement(Class JavaDoc clazz, List JavaDoc extensibilityElements) throws DeploymentException {
571         for (Iterator JavaDoc iterator = extensibilityElements.iterator(); iterator.hasNext();) {
572             ExtensibilityElement extensibilityElement = (ExtensibilityElement) iterator.next();
573             if (clazz.isAssignableFrom(extensibilityElement.getClass())) {
574                 return extensibilityElement;
575             }
576         }
577         throw new DeploymentException("No element of class " + clazz.getName() + " found");
578     }
579
580     public String JavaDoc movePortLocation(String JavaDoc portComponentName, String JavaDoc servletLocation) throws DeploymentException {
581         DefinitionsDocument doc = (DefinitionsDocument) wsdlMap.get(uris.get(0));
582         TDefinitions definitions = doc.getDefinitions();
583         TService[] services = definitions.getServiceArray();
584         for (int i = 0; i < services.length; i++) {
585             TService service = services[i];
586             TPort[] ports = service.getPortArray();
587             for (int j = 0; j < ports.length; j++) {
588                 TPort port = ports[j];
589                 if (port.getName().trim().equals(portComponentName)) {
590                     XmlCursor portCursor = port.newCursor();
591                     try {
592                         if (portCursor.toChild(ADDRESS_QNAME)) {
593                             if (servletLocation == null) {
594                                 String JavaDoc original = portCursor.getAttributeText(LOCATION_QNAME);
595                                 URI JavaDoc originalURI = new URI JavaDoc(original);
596                                 servletLocation = originalURI.getPath();
597                             }
598                             portCursor.setAttributeText(LOCATION_QNAME, WebServiceContainer.LOCATION_REPLACEMENT_TOKEN + servletLocation);
599                             return servletLocation;
600                         }
601                     } catch (URISyntaxException JavaDoc e) {
602                         throw new DeploymentException("Could not construct URI for ejb location in wsdl", e);
603                     } finally {
604                         portCursor.dispose();
605                     }
606                 }
607             }
608         }
609         throw new DeploymentException("No port found with name " + portComponentName + " expected at " + servletLocation);
610     }
611
612     private class JarEntityResolver implements EntityResolver JavaDoc {
613
614         private final static String JavaDoc PROJECT_URL_PREFIX = "project://local/";
615
616         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
617             //seems like this must be a bug in xmlbeans...
618
if (systemId.indexOf(PROJECT_URL_PREFIX) > -1) {
619                 systemId = systemId.substring(PROJECT_URL_PREFIX.length());
620             }
621             URI JavaDoc location = ((URI JavaDoc) uris.peek()).resolve(systemId);
622             InputStream JavaDoc wsdlInputStream;
623             try {
624                 ZipEntry JavaDoc entry = moduleFile.getEntry(location.toString());
625                 wsdlInputStream = moduleFile.getInputStream(entry);
626                 XmlObject xmlObject = SchemaDocument.Factory.parse(wsdlInputStream);
627                 wsdlMap.put(location, xmlObject);
628                 wsdlInputStream.close();
629                 wsdlInputStream = moduleFile.getInputStream(entry);
630             } catch (XmlException e) {
631                 throw (IOException JavaDoc) new IOException JavaDoc("Could not parse schema document").initCause(e);
632             }
633             return new InputSource JavaDoc(wsdlInputStream);
634         }
635     }
636
637     class JarWSDLLocator implements WSDLLocator {
638
639         private final List JavaDoc streams = new ArrayList JavaDoc();
640         private final URI JavaDoc wsdlURI;
641         private URI JavaDoc latestImportURI;
642
643         public JarWSDLLocator(URI JavaDoc wsdlURI) {
644             this.wsdlURI = wsdlURI;
645         }
646
647         public InputSource JavaDoc getBaseInputSource() {
648             InputStream JavaDoc wsdlInputStream;
649             ZipEntry JavaDoc entry = moduleFile.getEntry(wsdlURI.toString());
650             if(entry == null){
651                 throw new RuntimeException JavaDoc("The webservices.xml file for the EJB JAR points to a non-existant WSDL file "+wsdlURI.toString());
652             }
653             try {
654                 wsdlInputStream = moduleFile.getInputStream(entry);
655                 DefinitionsDocument definition = DefinitionsDocument.Factory.parse(wsdlInputStream);
656                 wsdlMap.put(wsdlURI, definition);
657                 wsdlInputStream.close();
658                 wsdlInputStream = moduleFile.getInputStream(entry);
659                 streams.add(wsdlInputStream);
660             } catch (Exception JavaDoc e) {
661                 throw new RuntimeException JavaDoc("Could not open stream to wsdl file", e);
662             }
663             return new InputSource JavaDoc(wsdlInputStream);
664         }
665
666         public String JavaDoc getBaseURI() {
667             return wsdlURI.toString();
668         }
669
670         public InputSource JavaDoc getImportInputSource(String JavaDoc parentLocation, String JavaDoc relativeLocation) {
671             URI JavaDoc parentURI = URI.create(parentLocation);
672             latestImportURI = parentURI.resolve(relativeLocation);
673             InputStream JavaDoc importInputStream;
674             try {
675                 ZipEntry JavaDoc entry = moduleFile.getEntry(latestImportURI.toString());
676                 importInputStream = moduleFile.getInputStream(entry);
677                 try {
678                     DefinitionsDocument definition = DefinitionsDocument.Factory.parse(importInputStream);
679                     importInputStream.close();
680                     wsdlMap.put(latestImportURI, definition);
681                     importInputStream.close();
682                 } catch (XmlException e) {
683                     //probably was a schema rather than wsdl. If there are real problems they will show up later.
684
}
685                 importInputStream = moduleFile.getInputStream(entry);
686                 streams.add(importInputStream);
687             } catch (Exception JavaDoc e) {
688                 throw new RuntimeException JavaDoc("Could not open stream to import file", e);
689             }
690             InputSource JavaDoc inputSource = new InputSource JavaDoc(importInputStream);
691             inputSource.setSystemId(getLatestImportURI());
692             return inputSource;
693         }
694
695         public String JavaDoc getLatestImportURI() {
696             return latestImportURI.toString();
697         }
698
699         public void close() {
700             for (Iterator JavaDoc iterator = streams.iterator(); iterator.hasNext();) {
701                 InputStream JavaDoc inputStream = (InputStream JavaDoc) iterator.next();
702                 try {
703                     inputStream.close();
704                 } catch (IOException JavaDoc e) {
705                     //ignore
706
}
707             }
708             streams.clear();
709         }
710     }
711 }
712
Popular Tags