summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/assembly/src/main/java/org/apache/tuscany/sca/interfacedef/impl/InterfaceContractMapperImpl.java
blob: d04c8b4d97756124c60659321dcdc3f569809007 (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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*
 * 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.interfacedef.impl;

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

import javax.xml.namespace.QName;

import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint;
import org.apache.tuscany.sca.assembly.builder.ContractBuilder;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.interfacedef.Compatibility;
import org.apache.tuscany.sca.interfacedef.DataType;
import org.apache.tuscany.sca.interfacedef.IncompatibleInterfaceContractException;
import org.apache.tuscany.sca.interfacedef.Interface;
import org.apache.tuscany.sca.interfacedef.InterfaceContract;
import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.interfacedef.util.Audit;
import org.apache.tuscany.sca.interfacedef.util.XMLType;
import org.apache.tuscany.sca.policy.ExtensionType;

/**
 * @version $Rev$ $Date$
 */
public class InterfaceContractMapperImpl implements InterfaceContractMapper {

    protected ExtensionPointRegistry registry;
    protected BuilderExtensionPoint builders;
    protected ContractBuilder contractBuilder;

    public InterfaceContractMapperImpl(ExtensionPointRegistry registry){
        this.registry = registry;
        this.builders = registry.getExtensionPoint(BuilderExtensionPoint.class);
    }

    public boolean isCompatible(DataType source, DataType target, boolean passByValue) {
        return isCompatible(source, target, passByValue, null);   
    }

    public boolean isCompatible(DataType source, DataType target, boolean passByValue, Audit audit) {
        if (source == target) {
            return true;
        }
        if (!passByValue) {
            if (source == null || target == null) {
                if (audit != null){
                    audit.append("One of either the source or target data types is null for");
                }
                return false;
            }
            // For local case
            return target.getPhysical().isAssignableFrom(source.getPhysical());
        } else {
            // For remote interfaces where the target is represented with WSDL 
            // the source will have been converted to WSDL so we rely on JAXB mappings
            // being the same in both cases and just compare the type names directly. 
            // TODO - is this right?
            XMLType sourceLogicalType = null;

            // There is some nesting of data types (when GeneratedDataTypes or arrays are used) so
            // dig a bit deeper to find the real data type. Use a loop since for a multidimensional 
            // array, we might need to go more than one level deep.
            while (source.getLogical() instanceof DataType<?>) {
                source = (DataType<?>)source.getLogical();
            }            
            sourceLogicalType = (XMLType)source.getLogical();

            XMLType targetLogicalType = null;
            while (target.getLogical() instanceof DataType<?>) {
                target = (DataType<?>)target.getLogical();
            }            
            targetLogicalType = (XMLType)target.getLogical();

            // The logical type is null in some cases. This is when the 
            // runtime can't determine the XML type for a particular type, for
            // example for a non-JAXB Java bean. This makes interface checking
            // rather lenient with errors being detected at runtime
            if (sourceLogicalType.getTypeName() == null ||
                targetLogicalType.getTypeName() == null) {
                return true;
            }

            boolean match = sourceLogicalType.getTypeName().equals(targetLogicalType.getTypeName());

            if (!match){

                QName anyType = new QName("http://www.w3.org/2001/XMLSchema", "anyType");
                if (sourceLogicalType.getTypeName().equals(anyType) || 
                    targetLogicalType.getTypeName().equals(anyType)){
                    // special case where a Java interface uses a generic type, e.g.
                    // public OMElement getGreetings(OMElement om)
                    // while the provided WSDL uses a specific type. So we assume
                    // that xsd:anyType matched anything
                    match = true;
                } else {
                    if (audit != null){
                        audit.append("Operation argument types source = " + 
                                     sourceLogicalType.getTypeName() + 
                                     " target = " + 
                                     targetLogicalType.getTypeName() +
                        " don't match for");
                    }
                }
            }

            return match; 
        }

    }

    /**
     * Check that two interface contracts are equal. The contracts are equal if the two contracts have the 
     * same set of operations, with each operation having the same signature, both for forward and callback
     * interfaces
     * @param source
     * @param target
     * @return
     */
    public boolean isMutuallyCompatible(InterfaceContract source, InterfaceContract target) {
        ExtensionType ext = source.getInterface().getExtensionType();
        InterfaceContract sourceContract = null;

        // Are the forward interfaces equal?
        if (isMutuallyCompatible(source.getInterface(), target.getInterface())) {
            // Is there a Callback interface?
            if (source.getCallbackInterface() == null && target.getCallbackInterface() == null) {
                return true;
            } else {
                if (isMutuallyCompatible(source.getCallbackInterface(), target.getCallbackInterface())) {
                    return true;
                } // end if
            } // end if
        } // end if
        return false;
    } // end method isEqual

    /**
     * Check that two interfaces are equal. The interfaces are equal if the two interfaces have the 
     * same set of operations, with each operation having the same signature. 
     * @param source
     * @param target
     * @return
     */
    public boolean isMutuallyCompatible(Interface source, Interface target) {
        if (source == target) {
            // Shortcut for performance
            return true;
        } // end if
        if (source == null || target == null) {
            return false;
        } // end if

        if (source.isDynamic() || target.isDynamic()) {
            return true;
        }

        if (source.isRemotable() != target.isRemotable()) {
            return false;
        }
        if (source.getOperations().size() != target.getOperations().size()) {
            return false;
        }

        for (Operation operation : source.getOperations()) {
            Operation targetOperation = getOperation(target.getOperations(), operation.getName());
            if (targetOperation == null) {
                return false;
            }
            if (!isCompatible(operation, targetOperation, Compatibility.SUBSET)) {
                return false;
            }
        }
        return true;
    } // end method isEqual

    public boolean isCompatible(Operation source, Operation target, Compatibility compatibilityType) {
        return isCompatible(source, target, compatibilityType, true);
    }

    public boolean isCompatible(Operation source, Operation target, Compatibility compatibilityType, boolean byValue) {
        return isCompatible(source, target, compatibilityType, true, null);
    }

    public boolean isCompatible(Operation source, Operation target, Compatibility compatibilityType, boolean byValue, Audit audit) {
        if (source == target) {
            return true;
        }

        if (source.isDynamic() || target.isDynamic()) {
            return true;
        }

        // Check name
        if (!source.getName().equals(target.getName())) {
            if (audit != null){
                audit.append("operation names are not the same source = " +
                             source.getName() + 
                             " target = " +
                             target.getName());
                audit.appendSeperator();
            }
            return false;
        }

        if (source.getInterface().isRemotable() != target.getInterface().isRemotable()) {
            if (audit != null){
                audit.append("Interfaces have different remote settings source = " +
                             source.getName() + 
                             " target = " +
                             target.getName());
                audit.appendSeperator();
            }            
            return false;
        }

        if (source.isNonBlocking() != target.isNonBlocking()) {
            if (audit != null){
                audit.append("operations one-way not the same, source = " +
                             source.isNonBlocking() + 
                             " target = " +
                             target.isNonBlocking());
                audit.appendSeperator();
            }            
            return false;
        }

        boolean passByValue = (source.getInterface().isRemotable()) && byValue;

        // FIXME: We need to deal with wrapped<-->unwrapped conversion

        List<DataType> sourceOutputType = source.getOutputType().getLogical();
        List<DataType> targetOutputType = target.getOutputType().getLogical();

        List<DataType> sourceInputType = source.getInputType().getLogical();
        List<DataType> targetInputType = target.getInputType().getLogical();
        
        if (source.isInputWrapperStyle() && source.getInputWrapper() != null) {
            sourceInputType = source.getInputWrapper().getUnwrappedType().getLogical();
        }
        
        if (source.isOutputWrapperStyle() && source.getOutputWrapper() != null) {
            sourceOutputType = source.getOutputWrapper().getUnwrappedType().getLogical();
        }

        if (target.isInputWrapperStyle() && target.getInputWrapper() != null) {
            targetInputType = target.getInputWrapper().getUnwrappedType().getLogical();
        }
        
        if (target.isOutputWrapperStyle() && target.getOutputWrapper() != null) {        
            targetOutputType = target.getOutputWrapper().getUnwrappedType().getLogical();
        }

        if ( sourceOutputType.size() != targetOutputType.size()) {
            if (audit != null){
                audit.append("different number of output types");
                audit.appendSeperator();
            } 
            return false;
        }

        for ( int i=0; i < sourceOutputType.size(); i++) {
            if (!isCompatible(targetOutputType.get(i), sourceOutputType.get(i), passByValue, audit)) {
                if (audit != null){
                    audit.append(" output types");
                    audit.appendSeperator();
                } 
                return false;
            }
        }

        if (sourceInputType.size() != targetInputType.size()) {
            if (audit != null){
                audit.append("different number of input types");
                audit.appendSeperator();
            } 
            return false;
        }

        int size = sourceInputType.size();
        for (int i = 0; i < size; i++) {
            if (!isCompatible(sourceInputType.get(i), targetInputType.get(i), passByValue, audit)) {
                if (audit != null){
                    audit.append(" input types");
                    audit.appendSeperator();
                } 
                return false;
            }
        }

        // Check fault types
        for (DataType targetFaultType : target.getFaultTypes()) {
            // Source fault types must be the same or superset of target fault
            // types
            boolean found = true;
            for (DataType sourceFaultType : source.getFaultTypes()) {
                found = false;
                if (isCompatible(targetFaultType, sourceFaultType, passByValue, audit)) {
                    // Target fault type can be covered by the source fault type
                    found = true;
                    break;
                }
            }
            if (!found) {
                if (audit != null){
                    audit.append("Fault types incompatible");
                    audit.appendSeperator();
                } 
                return false;
            }
        }

        return true;
    }    
    public boolean isCompatibleByReference(Operation source, Operation target, Compatibility compatibilityType) {
        return isCompatible(source, target, compatibilityType, false);
    }

    public boolean isCompatibleByValue(Operation source, Operation target, Compatibility compatibilityType) {
        return isCompatible(source, target, compatibilityType, true);
    }

    @Override
    public boolean isCompatibleWithoutUnwrapByValue(Operation source, Operation target, Compatibility compatibilityType) {
        if (!source.isInputWrapperStyle() == target.isInputWrapperStyle()) {
            return false; 
        } else if (!source.isOutputWrapperStyle() == target.isOutputWrapperStyle()) {
            return false; 
        } else {
            return isCompatible(source, target, compatibilityType, true);
        }
    }

    // FIXME: How to improve the performance for the lookup
    private Operation getOperation(List<Operation> operations, String name) {
        for (Operation op : operations) {
            if (op.getName().equals(name)) {
                return op;
            }
        }
        return null;
    }

    /*
     * this variant of the checkCompatibility method is intended to supersede the one without an audit argument
     * Presence of both method variants indicates a state of partial development
     */
    public boolean checkCompatibility(InterfaceContract source,
                                      InterfaceContract target, 
                                      Compatibility compatibility,
                                      boolean ignoreCallback, 
                                      boolean silent, 
                                      Audit audit)
        throws IncompatibleInterfaceContractException {

        if (source == target) {
            // Shortcut for performance
            return true;
        }

        if (source == null || target == null) {
            return false;
        }

        if (source.getInterface() == target.getInterface()) {
            return ignoreCallback
            || isCallbackCompatible(source, target, silent, audit);
        }

        if (source.getInterface() == null || target.getInterface() == null) {
            return false;
        }

        if (source.getInterface().isDynamic()
            || target.getInterface().isDynamic()) {
            return ignoreCallback
            || isCallbackCompatible(source, target, silent, audit);
        }

        if (source.getInterface().isRemotable() != target.getInterface()
            .isRemotable()) {
            if (!silent) {
                audit.append("Remotable settings do not match: "+ source + "," + target); // TODO see if serialization is sufficient
                audit.appendSeperator();
                throw new IncompatibleInterfaceContractException(
                                                                 "Remotable settings do not match", source, target);

            } else {
                return false;
            }
        }

        for (Operation operation : source.getInterface().getOperations()) {
            Operation targetOperation = map(target.getInterface(), operation);
            if (targetOperation == null) {
                if (!silent) {
                    audit.append("Operation " + operation.getName()+ " not found on target"); 
                    audit.appendSeperator();
                    throw new IncompatibleInterfaceContractException(
                                                                     "Operation " + operation.getName()
                                                                     + " not found on target", source, target);
                } else {
                    return false;
                }
            }

            if (!silent) {
                if (audit == null)
                    audit = new Audit();
                if (!isCompatible(operation, targetOperation,
                                  Compatibility.SUBSET, true, audit)) {
                    audit.append("Operations called " + operation.getName()+ " are not compatible"); 
                    audit.appendSeperator();
                    throw new IncompatibleInterfaceContractException(
                                                                     "Operations called " + operation.getName()
                                                                     + " are not compatible " + audit, source,
                                                                     target);
                }
            } else {
                if (!isCompatible(operation, targetOperation,
                                  Compatibility.SUBSET)) {
                    return false;
                }
            }
        }

        return ignoreCallback || isCallbackCompatible(source, target, silent, audit);
    }

    /*
     * The old checkCompatibility operation without auditing. This just delegates to the new one for the time
     * being while there are still calls that don't provide and audit object. In the medium term when the calls have
     * been converted to sue the new opetion directly this should be removed. 
     */
    public boolean checkCompatibility(InterfaceContract source,
                                      InterfaceContract target,
                                      Compatibility compatibility,
                                      boolean ignoreCallback,
                                      boolean silent) 
        throws IncompatibleInterfaceContractException {

        // create dummy audit object.
        Audit audit = new Audit();

        return checkCompatibility(source, 
                                  target, 
                                  compatibility, 
                                  ignoreCallback, 
                                  silent,
                                  audit);
    }



    protected boolean isCallbackCompatible(InterfaceContract source, InterfaceContract target, boolean silent, Audit audit)
        throws IncompatibleInterfaceContractException {
        if (source.getCallbackInterface() == null && target.getCallbackInterface() == null) {
            return true;
        }
        if (source.getCallbackInterface() == null || target.getCallbackInterface() == null) {
            if (!silent) {
                audit.append("Callback interface doesn't match as one of the callback interfaces is null");
                audit.appendSeperator();
                throw new IncompatibleInterfaceContractException("Callback interface doesn't match as one of the callback interfaces is null", source, target);
            } else {
                return false;
            }
        }

        for (Operation operation : source.getCallbackInterface().getOperations()) {
            Operation targetOperation =
                getOperation(target.getCallbackInterface().getOperations(), operation.getName());
            if (targetOperation == null) {
                if (!silent) {
                    audit.append("Callback operation not found on target " + operation.getName());
                    audit.appendSeperator();
                    throw new IncompatibleInterfaceContractException("Callback operation not found on target", source,
                                                                     target, null, targetOperation);
                } else {
                    return false;
                }
            }
            if (!source.getCallbackInterface().isRemotable()) {
                // FIXME: for remotable operation, only compare name for now
                if (!operation.equals(targetOperation)) {
                    if (!silent) {
                        audit.append("Target callback operation is not compatible " + operation.getName());
                        audit.appendSeperator();
                        throw new IncompatibleInterfaceContractException("Target callback operation is not compatible",
                                                                         source, target, operation, targetOperation);
                    } else {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public boolean isCompatibleSubset(Interface source, Interface target) {
        if (source == target) {
            // Shortcut for performance
            return true;
        }
        if (source == null || target == null) {
            return false;
        }

        if (source.isDynamic() || target.isDynamic()) {
            return true;
        }

        if (source.isRemotable() != target.isRemotable()) {
            return false;
        }

        for (Operation operation : source.getOperations()) {
            Operation targetOperation = getOperation(target.getOperations(), operation.getName());
            if (targetOperation == null) {
                return false;
            }
            if (!isCompatible(operation, targetOperation, Compatibility.SUBSET)) {
                return false;
            }
        }
        return true;
    }

    /*
     * the variant of isCompatibleSubset with the audit parameter is intended to supersede the other
     * -- the presence of both indicates a partial development state
     */
    public boolean isCompatibleSubset(InterfaceContract source, InterfaceContract target, Audit audit) {

        try {
            return checkCompatibility(source, target, Compatibility.SUBSET, false, false, audit);
        } catch (IncompatibleInterfaceContractException e) {
            return false;
        }
    }

    public boolean isCompatibleSubset(InterfaceContract source, InterfaceContract target) {

        try {
            return checkCompatibility(source, target, Compatibility.SUBSET, false, false);
        } catch (IncompatibleInterfaceContractException e) {
            return false;
        }
    }

    /**
     * @see org.apache.tuscany.sca.interfacedef.InterfaceContractMapper#map(org.apache.tuscany.sca.interfacedef.Interface,
     *      org.apache.tuscany.sca.interfacedef.Operation)
     */
    public Operation map(Interface target, Operation source) {
        // TODO: How to handle the case that source operation is dynamic?
        if (target == null || target.isDynamic()) {
            return source;
        } else if (target.isRemotable()) {
            for (Operation op : target.getOperations()) {
                if (op.getName().equals(source.getName())) {
                    return op;
                }
            }
            return null;
        } else {
            for (Operation op : target.getOperations()) {
                if (isCompatible(source, op, Compatibility.SUBSET)) {
                    return op;
                }
            }
            return null;
        }
    }

}