summaryrefslogtreecommitdiffstats
path: root/sandbox/samples/temp/implementation-extension/src/main/java/sample/impl/SampleImplementationProcessor.java
blob: 6017030d35e4ccfb006e21beff23d73747db98b6 (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
/*
 * 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 sample.impl;

import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
import static sample.impl.SampleImplementation.QN;
import static sample.impl.ImplUtil.clazz;
import static sample.impl.ImplUtil.definition;
import static sample.impl.ImplUtil.implementation;
import static sample.impl.ImplUtil.interfaze;
import static sample.impl.ImplUtil.qname;
import static sample.impl.ImplUtil.reference;
import static sample.impl.ImplUtil.service;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import javax.wsdl.PortType;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import org.apache.tuscany.sca.assembly.AssemblyFactory;
import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor;
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.ClassReference;
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.databinding.xml.DOMDataBinding;
import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException;
import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory;
import org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition;
import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory;
import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface;
import org.apache.tuscany.sca.interfacedef.wsdl.WSDLObject;

import sample.api.Java;
import sample.api.WSDL;

/**
 * StAX artifact processor for Sample implementations.
 * 
 * @version $Rev$ $Date$
 */
public class SampleImplementationProcessor extends BaseStAXArtifactProcessor implements StAXArtifactProcessor<SampleImplementation> {
    final AssemblyFactory af;
    final JavaInterfaceFactory jif;
    final WSDLFactory wf;

    public SampleImplementationProcessor(final ExtensionPointRegistry ep) {
        final FactoryExtensionPoint fep = ep.getExtensionPoint(FactoryExtensionPoint.class);
        this.af = fep.getFactory(AssemblyFactory.class);
        this.jif = fep.getFactory(JavaInterfaceFactory.class);
        this.wf = fep.getFactory(WSDLFactory.class);
    }

    public QName getArtifactType() {
        return QN;
    }

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

    public SampleImplementation read(final XMLStreamReader r, final ProcessorContext ctx) throws ContributionReadException, XMLStreamException {
        // Read the component implementation element
        final SampleImplementation impl = implementation(r.getAttributeValue(null, "class"));
        while(r.hasNext() && !(r.next() == END_ELEMENT && QN.equals(r.getName())))
            ;
        return impl;
    }

    public void resolve(final SampleImplementation impl, final ModelResolver res, final ProcessorContext ctx) throws ContributionResolveException {
        try {
            // Resolve and introspect the implementation class
            impl.clazz = resolve(impl.name, res, ctx);

            for(final Annotation a: impl.clazz.getAnnotations()) {
                if(a instanceof Java)
                    impl.getServices().add(service(clazz(a), jif, af));
                else if(a instanceof WSDL)
                    impl.getServices().add(service(resolve(qname(a), res, ctx, wf), wf, af));
            }

            for(Field f: impl.clazz.getDeclaredFields()) {
                for(final Annotation a: f.getAnnotations()) {
                    if(a instanceof Java)
                        impl.getReferences().add(reference(f.getName(), clazz(a), jif, af));
                    else if(a instanceof WSDL)
                        impl.getReferences().add(reference(f.getName(), resolve(qname(a), res, ctx, wf), wf, af));
                }
            }

            impl.setUnresolved(false);
        } catch(InvalidInterfaceException e) {
            throw new ContributionResolveException(e);
        }
    }

    public void write(final SampleImplementation impl, final XMLStreamWriter w, final ProcessorContext ctx) throws ContributionWriteException, XMLStreamException {
        writeStart(w, QN.getNamespaceURI(), QN.getLocalPart(), new XAttr("class", impl.name));
        writeEnd(w);
    }

    /**
     * Resolve a Java class.
     */
    static Class<?> resolve(final String name, final ModelResolver res, final ProcessorContext ctx) throws ContributionResolveException {
        final ClassReference cr = res.resolveModel(ClassReference.class, new ClassReference(name), ctx);
        if(cr.getJavaClass() != null)
            return cr.getJavaClass();
        throw new ContributionResolveException(new ClassNotFoundException(name));
    }

    /**
     * Resolve a WSDL interface.
     */
    static WSDLInterface resolve(final QName name, final ModelResolver res, final ProcessorContext ctx, final WSDLFactory wif) throws ContributionResolveException {
        final WSDLInterface wi = res.resolveModel(WSDLInterface.class, interfaze(name, wif), ctx);
        if(!wi.isUnresolved())
            return domBound(wi);

        final WSDLDefinition wd = res.resolveModel(WSDLDefinition.class, definition(wi.getName(), wif), ctx);
        if(wd.isUnresolved())
            throw new ContributionResolveException("Couldn't find " + name.getNamespaceURI());

        WSDLObject<PortType> pt = wd.getWSDLObject(PortType.class, name);
        if(pt == null)
            throw new ContributionResolveException("Couldn't find " + name);
        try {
            final WSDLInterface nwi = wif.createWSDLInterface(pt.getElement(), wd, res, ctx.getMonitor());
            nwi.setWsdlDefinition(wd);
            res.addModel(nwi, ctx);
            return domBound(nwi);
        } catch(InvalidInterfaceException e) {
            throw new ContributionResolveException(e);
        }
    }

    /**
     * Return a WSDL interface configured to use a DOM databinding. 
     */
    static WSDLInterface domBound(WSDLInterface wi) throws ContributionResolveException {
        try {
            final WSDLInterface domwi = (WSDLInterface)wi.clone();
            domwi.resetDataBinding(DOMDataBinding.NAME);
            return domwi;
        } catch(CloneNotSupportedException e) {
            throw new ContributionResolveException(e);
        }
    }
}