summaryrefslogtreecommitdiffstats
path: root/sandbox/ant/tuscany-container-rhino/src/main/java/org/apache/tuscany/container/rhino/e4x/E4XPolicyBuilder.java
blob: 985a0efcc8cf79828498e4f36c527e4e3240abbf (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
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed 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.container.rhino.e4x;

import java.util.List;

import javax.wsdl.Input;
import javax.wsdl.Message;
import javax.wsdl.Operation;
import javax.wsdl.PortType;
import javax.xml.namespace.QName;

import org.apache.tuscany.container.rhino.assembly.JavaScriptImplementation;
import org.apache.tuscany.core.builder.BuilderException;
import org.apache.tuscany.core.builder.SourcePolicyBuilder;
import org.apache.tuscany.core.builder.TargetPolicyBuilder;
import org.apache.tuscany.core.builder.system.PolicyBuilderRegistry;
import org.apache.tuscany.core.system.annotation.Autowire;
import org.apache.tuscany.core.wire.TargetInvocationConfiguration;
import org.apache.tuscany.core.wire.WireSourceConfiguration;
import org.apache.tuscany.core.wire.WireTargetConfiguration;
import org.apache.tuscany.model.assembly.AtomicComponent;
import org.apache.tuscany.model.assembly.AtomicImplementation;
import org.apache.tuscany.model.assembly.ConfiguredReference;
import org.apache.tuscany.model.assembly.ConfiguredService;
import org.apache.tuscany.model.assembly.Part;
import org.apache.tuscany.model.assembly.Service;
import org.apache.tuscany.model.assembly.ServiceContract;
import org.apache.tuscany.model.types.wsdl.WSDLServiceContract;
import org.osoa.sca.annotations.Init;

import commonj.sdo.helper.TypeHelper;

/**
 */
@org.osoa.sca.annotations.Scope("MODULE")
public class E4XPolicyBuilder implements SourcePolicyBuilder, TargetPolicyBuilder {

    private PolicyBuilderRegistry builderRegistry;

    public E4XPolicyBuilder() {
    }

    @Init(eager = true)
    public void init() {
        builderRegistry.registerSourceBuilder(this);
        builderRegistry.registerTargetBuilder(this);
    }

    @Autowire
    public void setBuilderRegistry(PolicyBuilderRegistry builderRegistry) {
        this.builderRegistry = builderRegistry;
    }

    public void build(ConfiguredReference service, List<WireSourceConfiguration> wireSourceConfigurations) throws BuilderException {
    }

    public void build(ConfiguredService service, WireTargetConfiguration wireTargetConfiguration) throws BuilderException {
        Part part = service.getPart();
        if (part instanceof AtomicComponent) {
            AtomicImplementation implementation = ((AtomicComponent) part).getImplementation();
            if (implementation instanceof JavaScriptImplementation) {
                JavaScriptImplementation javaScriptImplementation = (JavaScriptImplementation) implementation;
                for (TargetInvocationConfiguration configuration : wireTargetConfiguration.getInvocationConfigurations().values()) {
                    TypeHelper typeHelper = javaScriptImplementation.getTypeHelper();
                    ClassLoader classLoader = javaScriptImplementation.getResourceLoader().getClassLoader();
                    String methodName = configuration.getMethod().getName();
                    QName responseQN = getElementQName(javaScriptImplementation, methodName);
                    if (responseQN != null) {
                        configuration.addInterceptor(new E4XInterceptor(responseQN, typeHelper, classLoader));
                    }
                }
            }
        }
    }

    protected QName getElementQName(JavaScriptImplementation javaScriptImplementation, String methodName) {
        for (Service service : javaScriptImplementation.getComponentType().getServices()) {
            ServiceContract sc = service.getServiceContract();
            if (sc instanceof WSDLServiceContract) {
                PortType pt = ((WSDLServiceContract) sc).getPortType();
                for (Object o : pt.getOperations()) {
                    Operation operation = (Operation) o;
                    if (methodName.equals(operation.getName())) {
                        Input input = operation.getInput();
                        if (input != null) {
                            Message message = input.getMessage();
                            if (message != null) {
                                List parts = message.getOrderedParts(null);
                                if (parts != null && parts.size() > 0) {
                                    javax.wsdl.Part part = (javax.wsdl.Part) parts.get(0);
                                    return part.getElementName();
                                }
                            }
                        }
                    }
                }
            }
        }
        return null;
    }

}