summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/branches/sca-java-1.6.2/tools/java2wsdl/src/main/java/org/apache/tuscany/tools/java2wsdl/generate/TuscanyJava2OMBuilder.java
blob: fd10c77f8b0f7b7525a220409701a75c2e8d6cdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.tuscany.tools.java2wsdl.generate;

import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMNode;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.ws.commons.schema.XmlSchema;
import org.codehaus.jam.JMethod;

public class TuscanyJava2OMBuilder implements TuscanyJava2WSDLConstants {

    private TuscanyTypeTable typeTable = null;

    private static int prefixCount = 1;

    private static final String NAMESPACE_PREFIX = "ns";

    private JMethod method[];

    private Collection schemaCollection;

    private GenerationParameters generationParams;

    private OMNamespace ns1;

    private OMNamespace soap;

    private OMNamespace soap12;

    private OMNamespace tns;

    private OMNamespace wsdl;

    private OMNamespace mime;

    private OMNamespace http;

    public TuscanyJava2OMBuilder(JMethod[] method,
                                 Collection schemaCollection,
                                 TuscanyTypeTable typeTab,
                                 GenerationParameters genParams) {
        this.method = method;
        this.schemaCollection = schemaCollection;
        this.typeTable = typeTab;
        this.generationParams = genParams;
    }

    public OMElement generateOM() throws Exception {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        wsdl = fac.createOMNamespace(WSDL_NAMESPACE, DEFAULT_WSDL_NAMESPACE_PREFIX);
        OMElement ele = fac.createOMElement("definitions", wsdl);

        ele.addAttribute("targetNamespace", generationParams.getTargetNamespace(), null);
        generateNamespaces(fac, ele);
        generateTypes(fac, ele);
        generateMessages(fac, ele);
        generatePortType(fac, ele);
        generateBinding(fac, ele);
        generateService(fac, ele);
        return ele;
    }

    private void generateNamespaces(OMFactory fac, OMElement defintions) throws Exception {
        soap = defintions.declareNamespace(URI_WSDL11_SOAP, SOAP11_PREFIX);
        tns =
            defintions.declareNamespace(generationParams.getTargetNamespace(), generationParams
                .getTargetNamespacePrefix());
        soap12 = defintions.declareNamespace(URI_WSDL12_SOAP, SOAP12_PREFIX);
        http = defintions.declareNamespace(HTTP_NAMESPACE, HTTP_PREFIX);
        mime = defintions.declareNamespace(MIME_NAMESPACE, MIME_PREFIX);
    }

    private void generateTypes(OMFactory fac, OMElement defintions) throws Exception {
        OMElement wsdlTypes = fac.createOMElement("types", wsdl);
        StringWriter writer = new StringWriter();

        // wrap the Schema elements with this start and end tags to create a
        // document root
        // under which the schemas can fall into
        writer.write("<xmlSchemas>");
        writeSchemas(writer);
        writer.write("</xmlSchemas>");

        XMLStreamReader xmlReader =
            XMLInputFactory.newInstance().createXMLStreamReader(new ByteArrayInputStream(writer.toString().getBytes()));

        StAXOMBuilder staxOMBuilders = new StAXOMBuilder(fac, xmlReader);
        OMElement documentElement = staxOMBuilders.getDocumentElement();

        Iterator iterator = documentElement.getChildElements();
        while (iterator.hasNext()) {
            wsdlTypes.addChild((OMNode)iterator.next());
        }
        defintions.addChild(wsdlTypes);
    }

    private void writeSchemas(StringWriter writer) {
        Iterator iterator = schemaCollection.iterator();
        XmlSchema xmlSchema = null;

        while (iterator.hasNext()) {
            xmlSchema = (XmlSchema)iterator.next();
            // typeIterator = xmlSchema.getSchemaTypes().getValues();
            /*
             * while (typeIterator.hasNext()) {
             * xmlSchema.getItems().add((XmlSchemaObject) typeIterator.next()); }
             */
            xmlSchema.write(writer);
        }
    }

    private void generateMessages(OMFactory fac, OMElement definitions) throws Exception {
        Hashtable namespaceMap = new Hashtable();
        String namespacePrefix = null;
        String namespaceURI = null;
        QName messagePartType = null;
        for (int i = 0; i < method.length; i++) {
            JMethod jmethod = method[i];

            if (jmethod.isPublic()) {
                // Request Message
                OMElement requestMessge = fac.createOMElement(MESSAGE_LOCAL_NAME, wsdl);
                requestMessge.addAttribute(ATTRIBUTE_NAME, jmethod.getSimpleName() + MESSAGE_SUFFIX, null);
                definitions.addChild(requestMessge);

                // only if a type for the message part has already been defined
                if ((messagePartType =
                    typeTable.getComplexSchemaTypeName(generationParams.getSchemaTargetNamespace(), jmethod
                        .getSimpleName())) != null) {
                    namespaceURI = messagePartType.getNamespaceURI();
                    // avoid duplicate namespaces
                    if ((namespacePrefix = (String)namespaceMap.get(namespaceURI)) == null) {
                        namespacePrefix = generatePrefix();
                        namespaceMap.put(namespaceURI, namespacePrefix);
                    }

                    OMElement requestPart = fac.createOMElement(PART_ATTRIBUTE_NAME, wsdl);
                    requestMessge.addChild(requestPart);
                    requestPart.addAttribute(ATTRIBUTE_NAME, "part1", null);

                    requestPart.addAttribute(ELEMENT_ATTRIBUTE_NAME, namespacePrefix + COLON_SEPARATOR
                        + jmethod.getSimpleName(), null);
                }

                // only if a type for the message part has already been defined
                if ((messagePartType =
                    typeTable.getComplexSchemaTypeName(generationParams.getSchemaTargetNamespace(), jmethod
                        .getSimpleName() + RESPONSE)) != null) {
                    namespaceURI = messagePartType.getNamespaceURI();
                    if ((namespacePrefix = (String)namespaceMap.get(namespaceURI)) == null) {
                        namespacePrefix = generatePrefix();
                        namespaceMap.put(namespaceURI, namespacePrefix);
                    }
                    // Response Message
                    OMElement responseMessge = fac.createOMElement(MESSAGE_LOCAL_NAME, wsdl);
                    responseMessge.addAttribute(ATTRIBUTE_NAME, jmethod.getSimpleName() + RESPONSE_MESSAGE, null);
                    definitions.addChild(responseMessge);
                    OMElement responsePart = fac.createOMElement(PART_ATTRIBUTE_NAME, wsdl);
                    responseMessge.addChild(responsePart);
                    responsePart.addAttribute(ATTRIBUTE_NAME, "part1", null);

                    responsePart.addAttribute(ELEMENT_ATTRIBUTE_NAME, namespacePrefix + COLON_SEPARATOR
                        + jmethod.getSimpleName()
                        + RESPONSE, null);
                }
            }
        }

        // now add these unique namespaces to the the definitions element
        Enumeration enumeration = namespaceMap.keys();
        while (enumeration.hasMoreElements()) {
            namespaceURI = (String)enumeration.nextElement();
            definitions.declareNamespace(namespaceURI, (String)namespaceMap.get(namespaceURI));
        }
    }

    /**
     * Generate the porttypes
     */
    private void generatePortType(OMFactory fac, OMElement defintions) {
        JMethod jmethod = null;
        OMElement operation = null;
        OMElement message = null;
        OMElement portType = fac.createOMElement(PORT_TYPE_LOCAL_NAME, wsdl);
        defintions.addChild(portType);
        // changed default PortType name to match Java interface name
        // instead of appending "PortType".
        portType.addAttribute(ATTRIBUTE_NAME, generationParams.getServiceName(), null);
        // adding message refs
        for (int i = 0; i < method.length; i++) {
            jmethod = method[i];

            if (jmethod.isPublic()) {
                operation = fac.createOMElement(OPERATION_LOCAL_NAME, wsdl);
                portType.addChild(operation);
                operation.addAttribute(ATTRIBUTE_NAME, jmethod.getSimpleName(), null);

                message = fac.createOMElement(IN_PUT_LOCAL_NAME, wsdl);
                message.addAttribute(MESSAGE_LOCAL_NAME, tns.getPrefix() + COLON_SEPARATOR
                    + jmethod.getSimpleName()
                    + MESSAGE_SUFFIX, null);
                operation.addChild(message);

                if (!jmethod.getReturnType().isVoidType() ||
                    jmethod.getAnnotation("org.osoa.sca.annotations.OneWay") == null) {
                    message = fac.createOMElement(OUT_PUT_LOCAL_NAME, wsdl);
                    message.addAttribute(MESSAGE_LOCAL_NAME, tns.getPrefix() + COLON_SEPARATOR
                        + jmethod.getSimpleName()
                        + RESPONSE_MESSAGE, null);
                    operation.addChild(message);
                }
            }
        }

    }

    /**
     * Generate the service
     */
    public void generateService(OMFactory fac, OMElement defintions) {
        OMElement service = fac.createOMElement(SERVICE_LOCAL_NAME, wsdl);
        defintions.addChild(service);
        // Add "WebService" to the end of WSDL service name
        service.addAttribute(ATTRIBUTE_NAME, generationParams.getServiceName() + WSDL_SERVICE_SUFFIX, null);
        OMElement port = fac.createOMElement(PORT, wsdl);
        service.addChild(port);
        port.addAttribute(ATTRIBUTE_NAME, generationParams.getServiceName() + SOAP11PORT, null);
        port.addAttribute(BINDING_LOCAL_NAME, tns.getPrefix() + COLON_SEPARATOR
            + generationParams.getServiceName()
            + BINDING_NAME_SUFFIX, null);
        addExtensionElement(fac,
                            port,
                            soap,
                            SOAP_ADDRESS,
                            LOCATION,
                            generationParams.getLocationUri() + generationParams.getServiceName());

        port = fac.createOMElement(PORT, wsdl);
        service.addChild(port);
        port.addAttribute(ATTRIBUTE_NAME, generationParams.getServiceName() + SOAP12PORT, null);
        port.addAttribute(BINDING_LOCAL_NAME, tns.getPrefix() + COLON_SEPARATOR
            + generationParams.getServiceName()
            + SOAP12BINDING_NAME_SUFFIX, null);
        addExtensionElement(fac,
                            port,
                            soap12,
                            SOAP_ADDRESS,
                            LOCATION,
                            generationParams.getLocationUri() + generationParams.getServiceName());
    }

    /**
     * Generate the bindings
     */
    private void generateBinding(OMFactory fac, OMElement defintions) throws Exception {
        generateSoap11Binding(fac, defintions);
        generateSoap12Binding(fac, defintions);
    }

    private void generateSoap11Binding(OMFactory fac, OMElement defintions) throws Exception {
        OMElement binding = fac.createOMElement(BINDING_LOCAL_NAME, wsdl);
        defintions.addChild(binding);
        binding.addAttribute(ATTRIBUTE_NAME, generationParams.getServiceName() + BINDING_NAME_SUFFIX, null);
        // changed default PortType name to match Java interface name
        // instead of appending "PortType".
        binding.addAttribute("type", tns.getPrefix() + COLON_SEPARATOR + generationParams.getServiceName(), null);

        addExtensionElement(fac, binding, soap, BINDING_LOCAL_NAME, TRANSPORT, TRANSPORT_URI, STYLE, generationParams
            .getStyle());

        for (int i = 0; i < method.length; i++) {
            JMethod jmethod = method[i];
            if (jmethod.isPublic()) {
                OMElement operation = fac.createOMElement(OPERATION_LOCAL_NAME, wsdl);
                binding.addChild(operation);

                addExtensionElement(fac,
                                    operation,
                                    soap,
                                    OPERATION_LOCAL_NAME,
                                    SOAP_ACTION,
                                    URN_PREFIX + COLON_SEPARATOR + jmethod.getSimpleName(),
                                    STYLE,
                                    generationParams.getStyle());
                operation.addAttribute(ATTRIBUTE_NAME, jmethod.getSimpleName(), null);

                OMElement input = fac.createOMElement(IN_PUT_LOCAL_NAME, wsdl);
                addExtensionElement(fac,
                                    input,
                                    soap,
                                    SOAP_BODY,
                                    SOAP_USE,
                                    generationParams.getUse(),
                                    "namespace",
                                    generationParams.getTargetNamespace());
                operation.addChild(input);

                if (!jmethod.getReturnType().isVoidType()) {
                    OMElement output = fac.createOMElement(OUT_PUT_LOCAL_NAME, wsdl);
                    addExtensionElement(fac,
                                        output,
                                        soap,
                                        SOAP_BODY,
                                        SOAP_USE,
                                        generationParams.getUse(),
                                        "namespace",
                                        generationParams.getTargetNamespace());
                    operation.addChild(output);
                }
            }
        }
    }

    private void generateSoap12Binding(OMFactory fac, OMElement defintions) throws Exception {
        OMElement binding = fac.createOMElement(BINDING_LOCAL_NAME, wsdl);
        defintions.addChild(binding);
        binding.addAttribute(ATTRIBUTE_NAME, generationParams.getServiceName() + SOAP12BINDING_NAME_SUFFIX, null);
        // changed default PortType name to match Java interface name
        // instead of appending "PortType".
        binding.addAttribute("type", tns.getPrefix() + COLON_SEPARATOR + generationParams.getServiceName(), null);

        addExtensionElement(fac, binding, soap12, BINDING_LOCAL_NAME, TRANSPORT, TRANSPORT_URI, STYLE, generationParams
            .getStyle());

        for (int i = 0; i < method.length; i++) {
            JMethod jmethod = method[i];

            if (jmethod.isPublic()) {
                OMElement operation = fac.createOMElement(OPERATION_LOCAL_NAME, wsdl);
                binding.addChild(operation);
                operation.declareNamespace(URI_WSDL12_SOAP, SOAP12_PREFIX);

                addExtensionElement(fac,
                                    operation,
                                    soap12,
                                    OPERATION_LOCAL_NAME,
                                    SOAP_ACTION,
                                    URN_PREFIX + COLON_SEPARATOR + jmethod.getSimpleName(),
                                    STYLE,
                                    generationParams.getStyle());
                operation.addAttribute(ATTRIBUTE_NAME, jmethod.getSimpleName(), null);

                OMElement input = fac.createOMElement(IN_PUT_LOCAL_NAME, wsdl);
                addExtensionElement(fac,
                                    input,
                                    soap12,
                                    SOAP_BODY,
                                    SOAP_USE,
                                    generationParams.getUse(),
                                    "namespace",
                                    generationParams.getTargetNamespace());
                operation.addChild(input);

                if (!jmethod.getReturnType().isVoidType()) {
                    OMElement output = fac.createOMElement(OUT_PUT_LOCAL_NAME, wsdl);
                    addExtensionElement(fac,
                                        output,
                                        soap12,
                                        SOAP_BODY,
                                        SOAP_USE,
                                        generationParams.getUse(),
                                        "namespace",
                                        generationParams.getTargetNamespace());
                    operation.addChild(output);
                }
            }
        }
    }

    private void addExtensionElement(OMFactory fac,
                                     OMElement element,
                                     OMNamespace namespace,
                                     String name,
                                     String att1Name,
                                     String att1Value,
                                     String att2Name,
                                     String att2Value) {
        OMElement soapbinding = fac.createOMElement(name, namespace);
        element.addChild(soapbinding);
        soapbinding.addAttribute(att1Name, att1Value, null);
        soapbinding.addAttribute(att2Name, att2Value, null);
    }

    private void addExtensionElement(OMFactory fac,
                                     OMElement element,
                                     OMNamespace namespace,
                                     String name,
                                     String att1Name,
                                     String att1Value) {
        OMElement soapbinding = fac.createOMElement(name, namespace);
        element.addChild(soapbinding);
        soapbinding.addAttribute(att1Name, att1Value, null);
    }

    private String generatePrefix() {
        return NAMESPACE_PREFIX + prefixCount++;
    }
}