summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/branches/sca-java-1.6.2/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/impl/SCDLProcessor.java
blob: c91618c24dedb35c5a6d22a3b0d308e8afba1fa7 (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
/*
 * 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.extension.helper.impl;

import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

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.assembly.ComponentType;
import org.apache.tuscany.sca.assembly.Implementation;
import org.apache.tuscany.sca.assembly.Property;
import org.apache.tuscany.sca.assembly.Reference;
import org.apache.tuscany.sca.assembly.Service;
import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint;
import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
import org.apache.tuscany.sca.contribution.service.ContributionReadException;
import org.apache.tuscany.sca.contribution.service.ContributionWriteException;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.extension.helper.utils.AbstractStAXArtifactProcessor;
import org.apache.tuscany.sca.extension.helper.utils.DynamicImplementation;
import org.osoa.sca.ServiceRuntimeException;

/**
 * An SCDL ArtifactProcessor which uses the Implementation class getters/setters
 * to define the SCDL attributes.
 *
 * @version $Rev$ $Date$
 */
public class SCDLProcessor extends AbstractStAXArtifactProcessor<Implementation> {

    protected QName scdlQName;
    protected Class<Implementation> implementationClass;
    protected ExtensionPointRegistry registry;
    protected ModelFactoryExtensionPoint factories;

    protected Map<String, Method> attributeSetters;
    protected Method elementTextSetter;

    public SCDLProcessor(AssemblyFactory assemblyFactory, QName scdlQName, Class<Implementation> implementationClass, ExtensionPointRegistry registry, ModelFactoryExtensionPoint factories) {
        super(assemblyFactory);
        this.scdlQName = scdlQName;
        this.implementationClass = implementationClass;
        this.registry = registry;
        this.factories = factories;
        initAttributes();
    }

    protected void initAttributes() {
        attributeSetters = new HashMap<String, Method>();
        Set<Method> methods = new HashSet<Method>(Arrays.asList(implementationClass.getMethods()));
        methods.removeAll(Arrays.asList(DynamicImplementation.class.getMethods()));
        for (Method m : methods) {
            if ("setElementText".equals(m.getName())) {
                elementTextSetter = m;
            } else if ((m.getName().startsWith("set"))) {
                attributeSetters.put(getFieldName(m), m);
            }
        }
    }

    /**
     * Remove get/set from method name, set 1st char to lowercase and
     * remove any trailing underscore character
     */
    protected String getFieldName(Method m) {
        StringBuilder sb = new StringBuilder(m.getName().substring(3));
        sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
        String name = sb.toString();
        if (name.endsWith("_")) {
            name = name.substring(0,name.length()-1);
        }
        return name;
    }

    private Object[] getImplConstrArgs() {
        Constructor[] cs = implementationClass.getConstructors();
        if (cs.length != 1) {
            throw new IllegalArgumentException("Implementation class must have a single constructor: "+ implementationClass.getName());
        }
        List args = new ArrayList();
        for (Class c : cs[0].getParameterTypes()) {
            Object o = factories.getFactory(c);
            if (o == null) {
                o = registry.getExtensionPoint(c);
            }
            args.add(o);
        }
        return args.toArray();
    }

    
    public QName getArtifactType() {
        return scdlQName;
    }

    public Class<Implementation> getModelType() {
        Class clazz;
        if (Implementation.class.isAssignableFrom(implementationClass)) {
            clazz = implementationClass;
        } else {
            clazz = PojoImplementation.class;
        }
        return clazz;
    }

    public Implementation read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException {
        Object impl;
        try {
            impl = implementationClass.getConstructors()[0].newInstance(getImplConstrArgs());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        for (String attribute : attributeSetters.keySet()) {
            String value = reader.getAttributeValue(null, attribute);
            if (value != null && value.length() > 0) {
                try {
                    attributeSetters.get(attribute).invoke(impl, new Object[] {value});
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }

        if (elementTextSetter != null) {
            try {
                String value = reader.getElementText();
                if (value != null && value.length() > 0) {
                    elementTextSetter.invoke(impl, value);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        while (!(reader.getEventType() == END_ELEMENT && scdlQName.equals(reader.getName())) && reader.hasNext()) {
            reader.next();
        }
        
        if (!(impl instanceof Implementation)) {
            impl = new PojoImplementation(impl);
        }

        return (Implementation)impl;
    }

    public void write(Implementation arg0, XMLStreamWriter arg1) throws ContributionWriteException, XMLStreamException {
    }

    @Override
    protected void addSideFileComponentType(String name, Implementation impl, ModelResolver resolver) {
//    protected void addSideFileComponentType(Implementation impl, ModelResolver resolver) {

        ComponentType componentType;
        try {
            componentType = getComponentType(resolver, impl);
        } catch (Exception e) {
            throw new ServiceRuntimeException(e);
        }

        if (componentType != null && !componentType.isUnresolved()) {
            for (Reference reference : componentType.getReferences()) {
                impl.getReferences().add(reference);
            }
            for (Service service : componentType.getServices()) {
                impl.getServices().add(service);
            }
            for (Property property : componentType.getProperties()) {
                impl.getProperties().add(property);
            }
            if (componentType.getConstrainingType() != null) {
                impl.setConstrainingType(componentType.getConstrainingType());
            }
        }
    }

    ComponentType getComponentType(ModelResolver resolver, Implementation impl) throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        for (Method m : getGetters()) {
            Object io;
            if (impl instanceof PojoImplementation) {
                io = ((PojoImplementation) impl).getUserImpl();
            } else {
                io = impl;
            }
            String value = (String) m.invoke(io, new Object[] {});
            if (value != null) {
                value = value.substring(0, value.lastIndexOf('.'));
                ComponentType componentType = assemblyFactory.createComponentType();
                componentType.setUnresolved(true);
                componentType.setURI(value + ".componentType");
                componentType = resolver.resolveModel(ComponentType.class, componentType);
                if (!componentType.isUnresolved()) {
                    return componentType;
                }
            }
        }
        return null;
    }

    private List<Method> getGetters() {
        List<Method> ms = new ArrayList<Method>();
        for (Method setter : attributeSetters.values()) {
            String s = getFieldName(setter);
            for (Method m : implementationClass.getMethods()) {
                String name = m.getName();
                if (name.length() > 3 && name.startsWith("get")) {
                    if (s.endsWith(name.substring(4))) {
                        ms.add(m);
                    }
                }
            }
        }
        return ms;
    }
}