summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/branches/2.0/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/InterfaceContractProcessor.java
blob: 3cdc956cfd0f7e4b831d628a1e72c6ac8f7737ed (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
/*
 * 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.sca.assembly.xml;

import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
import static org.apache.tuscany.sca.assembly.xml.Constants.SCA11_TUSCANY_NS;

import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import org.apache.tuscany.sca.contribution.processor.ContributionReadException;
import org.apache.tuscany.sca.contribution.processor.ContributionResolveException;
import org.apache.tuscany.sca.contribution.processor.ContributionWriteException;
import org.apache.tuscany.sca.contribution.processor.ProcessorContext;
import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.core.FactoryExtensionPoint;
import org.apache.tuscany.sca.interfacedef.DataType;
import org.apache.tuscany.sca.interfacedef.Interface;
import org.apache.tuscany.sca.interfacedef.InterfaceContract;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl;
import org.apache.tuscany.sca.interfacedef.impl.InterfaceImpl;
import org.apache.tuscany.sca.interfacedef.impl.OperationImpl;
import org.apache.tuscany.sca.interfacedef.impl.TuscanyInterfaceContractImpl;
import org.apache.tuscany.sca.interfacedef.util.XMLType;

/**
 * Processor for reading/writing the Tuscany interface model in order to 
 * support distributed interface matching
 *
 */
public class InterfaceContractProcessor extends BaseAssemblyProcessor implements StAXArtifactProcessor<InterfaceContract>, Constants{
    
    String INTERFACE_CONTRACT = "interfaceContract";
    QName INTERFACE_CONTRACT_QNAME = new QName(SCA11_TUSCANY_NS, INTERFACE_CONTRACT);
    String INTERFACE = "interface";
    QName INTERFACE_QNAME = new QName(SCA11_TUSCANY_NS, INTERFACE);
    String CALLBACK_INTERFACE = "callbackInterface";
    QName CALLBACK_INTERFACE_QNAME = new QName(SCA11_TUSCANY_NS, CALLBACK_INTERFACE);
    String OPERATION = "operation";
    QName OPERATION_QNAME = new QName(SCA11_TUSCANY_NS, OPERATION);
    String INPUT = "input"; 
    QName INPUT_QNAME = new QName(SCA11_TUSCANY_NS, INPUT);
    String OUTPUT = "output";
    QName OUTPUT_QNAME = new QName(SCA11_TUSCANY_NS, OUTPUT);
    String FAULT = "fault";
    QName FAULT_QNAME = new QName(SCA11_TUSCANY_NS, FAULT);
    String DATATYPE = "dataType";
    QName DATATYPE_QNAME = new QName(SCA11_TUSCANY_NS, DATATYPE);
    String GENERIC = "generic";
    QName GENERIC_QNAME = new QName(SCA11_TUSCANY_NS, GENERIC);
    String LOGICAL_COLLECTION = "logicalCollection";
    QName LOGICAL_COLLECTION_QNAME = new QName(SCA11_TUSCANY_NS, LOGICAL_COLLECTION);
    String LOGICAL_XMLTYPE = "logicalXMLType";
    QName LOGICAL_XMLTYPE_QNAME = new QName(SCA11_TUSCANY_NS, LOGICAL_XMLTYPE);
    String LOGICAL_TYPE = "logicalType";
    QName LOGICAL_TYPE_QNAME = new QName(SCA11_TUSCANY_NS, LOGICAL_TYPE);
    String PHYSICAL = "physical";
    QName PHYSICAL_QNAME = new QName(SCA11_TUSCANY_NS, PHYSICAL);
    String XMLTYPE = "xmlType";
    QName XMLTYPE_QNAME = new QName(SCA11_TUSCANY_NS, XMLTYPE);
    
    String NO_TYPE = "NoType";
    
    enum Iof {
        UNSET,
        INPUT,
        OUTPUT,
        FAULT
    }
    
    enum CharacterTarget {
        UNSET,
        GENERIC,
        PHYSICAL,
        XMLTYPE
    }
    
    public InterfaceContractProcessor(ExtensionPointRegistry registry) {
        super(registry.getExtensionPoint(FactoryExtensionPoint.class), 
              null /*registry.getExtensionPoint(StAXArtifactProcessor.class)*/);
    }

    public InterfaceContract read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException {
        
        TuscanyInterfaceContractImpl interfaceContract = new TuscanyInterfaceContractImpl();
        
        try {
            InterfaceImpl iface = null;
            QName name = null;
            Operation operation = null;
            List<DataType> inputs = null;
            List<DataType> outputs = null;
            List<DataType> faults = null;
            XMLType logicalXMLType = null;
            
            Iof iof = Iof.UNSET;
            CharacterTarget characterTarget = CharacterTarget.UNSET;
            
            boolean logicalCollection = false;            
          
            DataType dataType = null;
            
            while (reader.hasNext()) {
                int event = reader.getEventType();
                
                switch (event) {
                    case START_ELEMENT:
                        name = reader.getName();
                        if (INTERFACE_CONTRACT_QNAME.equals(name)){
                        } else if (INTERFACE_QNAME.equals(name)){
                            iface = new InterfaceImpl();
                            interfaceContract.setInterface(iface);
                            iface.setRemotable(getBoolean(reader, "isRemotable"));
                        } else if (CALLBACK_INTERFACE_QNAME.equals(name)){
                            iface = new InterfaceImpl();
                            interfaceContract.setCallbackInterface(iface);
                            iface.setRemotable(getBoolean(reader, "isRemotable"));
                        } else if (OPERATION_QNAME.equals(name)) {
                            operation = new OperationImpl();
                            iface.getOperations().add(operation);
                            
                            operation.setName(getString(reader, "name"));
                            operation.setDynamic(getBoolean(reader, "isDynamic"));
                            operation.setNonBlocking(getBoolean(reader, "isNonBlocking"));
                            operation.setInputWrapperStyle(getBoolean(reader, "isInputWrapperStyle"));
                            operation.setOutputWrapperStyle(getBoolean(reader, "isOutputWrapperStyle"));
                            
                            inputs = new ArrayList<DataType>();
                            DataType inputType = new DataTypeImpl<List<DataType>>(null, null);
                            inputType.setLogical(inputs);
                            operation.setInputType(inputType);
                            
                            outputs = new ArrayList<DataType>();
                            DataType outputType = new DataTypeImpl<List<DataType>>(null, null);
                            outputType.setLogical(outputs);
                            operation.setOutputType(outputType);
                            
                            faults = new ArrayList<DataType>();
                            operation.setFaultTypes(faults);
                        } else if (INPUT_QNAME.equals(name)) {
                            iof = Iof.INPUT;
                        } else if (OUTPUT_QNAME.equals(name)) {
                            iof = Iof.OUTPUT;
                        } else if (FAULT_QNAME.equals(name)) {
                            iof = Iof.FAULT;
                        } else if (DATATYPE_QNAME.equals(name)){
                            DataType newDataType = new DataTypeImpl<XMLType>(null, null);
                            newDataType.setDataBinding(getString(reader, "dataBinding"));
                            if (logicalCollection) {
                                dataType.setLogical(newDataType);
                                dataType = newDataType;
                            } else if (iof == Iof.INPUT) {
                                inputs.add(newDataType);
                                dataType = newDataType;
                            } else if (iof == Iof.OUTPUT){
                                outputs.add(newDataType);
                                dataType = newDataType;
                            } else if (iof == Iof.FAULT){
                                faults.add(newDataType);
                                dataType = newDataType;
                            } 
                        } else if (GENERIC_QNAME.equals(name)){
                            characterTarget = CharacterTarget.GENERIC;
                        } else if (PHYSICAL_QNAME.equals(name)){
                            characterTarget = CharacterTarget.PHYSICAL;
                        } else if (LOGICAL_COLLECTION_QNAME.equals(name)){
                            logicalCollection = true;
                        } else if (LOGICAL_XMLTYPE_QNAME.equals(name)){
                            characterTarget = CharacterTarget.XMLTYPE;
                            logicalXMLType = new XMLType(null, null);
                            dataType.setLogical(logicalXMLType);
                        } else if (LOGICAL_TYPE_QNAME.equals(name)){
                            // is this ever used?
                        } else if (XMLTYPE_QNAME.equals(name)){
                            // is this ever used?
                        } else {
                            System.out.println("Unexpected element " + name);
                        }
                        break;
                    case XMLStreamConstants.CHARACTERS:
                        if (characterTarget == CharacterTarget.GENERIC){
                            String generic = reader.getText();
                            // Not sure what to do with this as we may not have the actual type
                        } else if (characterTarget == CharacterTarget.PHYSICAL){
                            String physical = reader.getText();
                            // Not sure what to do with this as we may not have the actual type
                        } else if (characterTarget == CharacterTarget.XMLTYPE) {
                            String xmlType = reader.getText();
                            if (!xmlType.equals(NO_TYPE)){
                                int splitPoint = xmlType.indexOf("}");
                                String namespace = xmlType.substring(1, splitPoint);
                                String localname = xmlType.substring(splitPoint + 1);
                                QName typeName = new QName(namespace, localname);
                                logicalXMLType.setTypeName(typeName);
                            }
                        }
                        characterTarget = CharacterTarget.UNSET;
                        break;
                    case END_ELEMENT:
                        name = reader.getName();
                        if (INPUT_QNAME.equals(name) ||
                            OUTPUT_QNAME.equals(name) ||
                            FAULT_QNAME.equals(name)) {
                            iof = Iof.UNSET;
                        } else if (LOGICAL_COLLECTION_QNAME.equals(name)) {
                            logicalCollection = false;
                        }
                }
    
                // Read the next element
                if (reader.hasNext()) {
                    reader.next();
                }
            }
        } catch (XMLStreamException e) {
            ContributionReadException ex = new ContributionReadException(e);
            //error(monitor, "XMLStreamException", reader, ex);
        }                    
        
        return interfaceContract;
    }

    public void write(InterfaceContract interfaceContract, XMLStreamWriter writer, ProcessorContext context)
        throws ContributionWriteException, XMLStreamException {

        if (interfaceContract == null || interfaceContract.getInterface() == null) {
            return;
        }
        
        writer.writeStartElement(Constants.SCA11_TUSCANY_NS, INTERFACE_CONTRACT);
        writer.writeStartElement(Constants.SCA11_TUSCANY_NS, INTERFACE);
        writeInterface(interfaceContract.getInterface(), writer, context);
        
        if (interfaceContract.getCallbackInterface() != null){
            writer.writeStartElement(Constants.SCA11_TUSCANY_NS, CALLBACK_INTERFACE);
            writeInterface(interfaceContract.getCallbackInterface(), writer, context);
        }
        writer.writeEndElement();

    }

    private void writeInterface(Interface iface, XMLStreamWriter writer, ProcessorContext context) throws XMLStreamException {

        
        writer.writeAttribute("isRemotable", String.valueOf(iface.isRemotable()));
       
        for (Operation operation : iface.getOperations()){
            writer.writeStartElement(Constants.SCA11_TUSCANY_NS, OPERATION);
            writer.writeAttribute("name", operation.getName());
            writer.writeAttribute("isDynamic", String.valueOf(operation.isDynamic()));
            writer.writeAttribute("isNonBlocking", String.valueOf(operation.isNonBlocking()));
            writer.writeAttribute("isInputWrapperStyle", String.valueOf(operation.isInputWrapperStyle()));
            writer.writeAttribute("isOutputWrapperStyle", String.valueOf(operation.isOutputWrapperStyle()));

            List<DataType> outputTypes = operation.getOutputType().getLogical();
            List<DataType> inputTypes = operation.getInputType().getLogical();
            List<DataType> faultTypes = operation.getFaultTypes();

            if (operation.isInputWrapperStyle() && operation.getInputWrapper() != null) {
                inputTypes = operation.getInputWrapper().getUnwrappedType().getLogical();
            }
            if (operation.isOutputWrapperStyle() && operation.getOutputWrapper() != null) {
                outputTypes = operation.getOutputWrapper().getUnwrappedType().getLogical();
            }
          
            writer.writeStartElement(Constants.SCA11_TUSCANY_NS, INPUT);
            writeDataTypes(inputTypes, writer);
            writer.writeEndElement();
            
            writer.writeStartElement(Constants.SCA11_TUSCANY_NS, OUTPUT);
            writeDataTypes(outputTypes, writer);
            writer.writeEndElement();
            
            writer.writeStartElement(Constants.SCA11_TUSCANY_NS, FAULT);
            writeDataTypes(faultTypes, writer);
            writer.writeEndElement();

            writer.writeEndElement(); 
        }
        writer.writeEndElement();        
    }
    
    private void writeDataTypes(List<DataType> dataTypes, XMLStreamWriter writer) throws XMLStreamException {
        for(DataType dataType : dataTypes){
            writeDataType(dataType, writer);
        }
    }
    
    private void writeDataType(DataType<?> dataType, XMLStreamWriter writer)  throws XMLStreamException {
        writer.writeStartElement(Constants.SCA11_TUSCANY_NS, DATATYPE);
        if (dataType.getDataBinding() != null){
            writer.writeAttribute("dataBinding", dataType.getDataBinding());
        }
       
        if (dataType.getGenericType() != null){
            writer.writeStartElement(Constants.SCA11_TUSCANY_NS, GENERIC);
            writer.writeCharacters(dataType.getGenericType().toString());
            writer.writeEndElement();  
        }
        
        if (dataType.getLogical() instanceof DataType){
            writer.writeStartElement(Constants.SCA11_TUSCANY_NS, LOGICAL_COLLECTION);
            writeDataType((DataType<?>)dataType.getLogical(), writer);
        } else if (dataType.getLogical() instanceof XMLType){
            writer.writeStartElement(Constants.SCA11_TUSCANY_NS, LOGICAL_XMLTYPE);
            writer.writeStartElement(Constants.SCA11_TUSCANY_NS, XMLTYPE);
            XMLType xmlType = (XMLType)dataType.getLogical();
            if (xmlType.getTypeName() != null){
                writer.writeCharacters(xmlType.getTypeName().toString());
            } else {
                writer.writeCharacters("NoType");
            }
            writer.writeEndElement();  
        } else {
            writer.writeStartElement(Constants.SCA11_TUSCANY_NS, LOGICAL_TYPE);
            writer.writeCharacters(dataType.getLogical().toString());
        }
        writer.writeEndElement();
       
        if (dataType.getPhysical() != null){
            writer.writeStartElement(Constants.SCA11_TUSCANY_NS, PHYSICAL);
            writer.writeCharacters(dataType.getPhysical().getName());
            writer.writeEndElement();
        }
        
        writer.writeEndElement();

    }

    public void resolve(InterfaceContract interfaceContract, ModelResolver resolver, ProcessorContext context)
        throws ContributionResolveException {
        // do nothing
    }

    public QName getArtifactType() {
        // these internal interface contracts aren't found in the composite file
        return null;
    }

    public Class<InterfaceContract> getModelType() {
        return InterfaceContract.class;
    }

}