summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DefaultTransformerExtensionPoint.java
blob: 39889b1e972c5943d604b591b76fa0195b9f21f7 (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
/*
 * 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.databinding;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.databinding.impl.DirectedGraph;
import org.apache.tuscany.sca.extensibility.ServiceDeclaration;
import org.apache.tuscany.sca.extensibility.ServiceDiscovery;

/**
 * @version $Rev$ $Date$
 */
public class DefaultTransformerExtensionPoint implements TransformerExtensionPoint {
    private static final Logger logger = Logger.getLogger(DefaultTransformerExtensionPoint.class.getName());
    private boolean loadedTransformers;

    private ExtensionPointRegistry registry;
    private final DirectedGraph<Object, Transformer> graph = new DirectedGraph<Object, Transformer>();

    public DefaultTransformerExtensionPoint(ExtensionPointRegistry registry) {
        this.registry = registry;
    }

    public void addTransformer(String sourceType, String resultType, int weight, Transformer transformer, boolean publicTransformer) {
        if (logger.isLoggable(Level.FINE)) {
            String className = transformer.getClass().getName();
            boolean lazy = false;
            boolean pull = (transformer instanceof PullTransformer);
            if (transformer instanceof LazyPullTransformer) {
                className = ((LazyPullTransformer)transformer).transformerDeclaration.getClassName();
                lazy = true;
            }
            if (transformer instanceof LazyPushTransformer) {
                className = ((LazyPushTransformer)transformer).transformerDeclaration.getClassName();
                lazy = true;
            }

            logger.fine("Adding transformer: " + className
                + ";source="
                + sourceType
                + ",target="
                + resultType
                + ",weight="
                + weight
                + ",type="
                + (pull ? "pull" : "push")
                + ",lazy="
                + lazy);
        }
        graph.addEdge(sourceType, resultType, transformer, weight, publicTransformer);
    }

    public void addTransformer(Transformer transformer, boolean publicTransformer) {
        addTransformer(transformer.getSourceDataBinding(),
                       transformer.getTargetDataBinding(),
                       transformer.getWeight(),
                       transformer, publicTransformer);
    }

    public boolean removeTransformer(String sourceType, String resultType) {
        return graph.removeEdge(sourceType, resultType);
    }

    public Transformer getTransformer(String sourceType, String resultType) {
        loadTransformers();

        DirectedGraph<Object, Transformer>.Edge edge = graph.getEdge(sourceType, resultType);
        return (edge == null) ? null : edge.getValue();
    }

    /**
     * Dynamically load transformers registered under META-INF/services.
     *
     */
    private synchronized void loadTransformers() {
        if (loadedTransformers) {
            return;
        }
        loadedTransformers = true;
        loadTransformers(PullTransformer.class);
        loadTransformers(PushTransformer.class);

    }

    /**
     * Dynamically load transformers registered under META-INF/services.
     *
     * @param transformerClass
     */
    private synchronized void loadTransformers(Class<?> transformerClass) {

        // Get the transformer service declarations
        Collection<ServiceDeclaration> transformerDeclarations;

        try {
            transformerDeclarations = registry.getServiceDiscovery().getServiceDeclarations(transformerClass.getName());

        } catch (IOException e) {
            throw new IllegalStateException(e);
        }

        // Load transformers
        for (ServiceDeclaration transformerDeclaration : transformerDeclarations) {
            Map<String, String> attributes = transformerDeclaration.getAttributes();

            String source = attributes.get("source");
            String target = attributes.get("target");
            int weight = Integer.valueOf(attributes.get("weight"));
            String b = attributes.get("public");
            boolean pub = true;
            if (b != null) {
                pub = Boolean.valueOf(b);
            }

            // Create a transformer wrapper and register it
            Transformer transformer;
            if (transformerClass == PullTransformer.class) {
                transformer = new LazyPullTransformer(source, target, weight, transformerDeclaration);
            } else {
                transformer = new LazyPushTransformer(source, target, weight, transformerDeclaration);
            }
            addTransformer(transformer, pub);
        }
    }

    /**
     * A transformer facade allowing transformers to be lazily loaded
     * and initialized.
     */
    private class LazyPullTransformer implements PullTransformer<Object, Object> {

        private String source;
        private String target;
        private int weight;
        private ServiceDeclaration transformerDeclaration;
        private PullTransformer<Object, Object> transformer;

        public LazyPullTransformer(String source, String target, int weight, ServiceDeclaration transformerDeclaration) {
            this.source = source;
            this.target = target;
            this.weight = weight;
            this.transformerDeclaration = transformerDeclaration;
        }

        /**
         * Load and instantiate the transformer class.
         *
         * @return The transformer.
         */
        @SuppressWarnings("unchecked")
        private PullTransformer<Object, Object> getTransformer() {
            if (transformer == null) {
                try {
                    Class<PullTransformer<Object, Object>> transformerClass =
                        (Class<PullTransformer<Object, Object>>)transformerDeclaration.loadClass();
                    try {
                        Constructor<PullTransformer<Object, Object>> constructor = transformerClass.getConstructor();
                        transformer = constructor.newInstance();
                    } catch (NoSuchMethodException e) {
                        Constructor<PullTransformer<Object, Object>> constructor =
                            transformerClass.getConstructor(ExtensionPointRegistry.class);
                        transformer = constructor.newInstance(registry);
                    }
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return transformer;
        }

        public String getSourceDataBinding() {
            return source;
        }

        public String getTargetDataBinding() {
            return target;
        }

        public int getWeight() {
            return weight;
        }

        public Object transform(Object source, TransformationContext context) {
            return getTransformer().transform(source, context);
        }

        @Override
        public String toString() {
            StringBuffer sb = new StringBuffer(super.toString());
            sb.append(";className=").append(transformerDeclaration.getClassName());
            return sb.toString();
        }
    }

    /**
     * A transformer facade allowing transformers to be lazily loaded
     * and initialized.
     */
    private class LazyPushTransformer implements PushTransformer<Object, Object> {

        private String source;
        private String target;
        private int weight;
        private ServiceDeclaration transformerDeclaration;
        private PushTransformer<Object, Object> transformer;

        public LazyPushTransformer(String source, String target, int weight, ServiceDeclaration transformerDeclaration) {
            this.source = source;
            this.target = target;
            this.weight = weight;
            this.transformerDeclaration = transformerDeclaration;
        }

        /**
         * Load and instantiate the transformer class.
         *
         * @return The transformer.
         */
        @SuppressWarnings("unchecked")
        private PushTransformer<Object, Object> getTransformer() {
            if (transformer == null) {
                try {
                    Class<PushTransformer<Object, Object>> transformerClass =
                        (Class<PushTransformer<Object, Object>>)transformerDeclaration.loadClass();
                    try {
                        Constructor<PushTransformer<Object, Object>> constructor = transformerClass.getConstructor();
                        transformer = constructor.newInstance();
                    } catch (NoSuchMethodException e) {
                        Constructor<PushTransformer<Object, Object>> constructor =
                            transformerClass.getConstructor(ExtensionPointRegistry.class);
                        transformer = constructor.newInstance(registry);
                    }
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return transformer;
        }

        public String getSourceDataBinding() {
            return source;
        }

        public String getTargetDataBinding() {
            return target;
        }

        public int getWeight() {
            return weight;
        }

        public void transform(Object source, Object sink, TransformationContext context) {
            getTransformer().transform(source, sink, context);
        }

        @Override
        public String toString() {
            StringBuffer sb = new StringBuffer(super.toString());
            sb.append(";className=").append(transformerDeclaration.getClassName());
            return sb.toString();
        }
    }

    //FIXME The following methods should be on a different class from
    // extension point

    public List<Transformer> getTransformerChain(String sourceType, String resultType) {
        loadTransformers();

        String source = sourceType;
        String result = resultType;
        List<Transformer> transformers = new ArrayList<Transformer>();
        // First check if there is a direct path, if yes, use it regardless of the weight
        DirectedGraph<Object, Transformer>.Edge link = graph.getEdge(sourceType, resultType);
        if (link != null) {
            transformers.add(link.getValue());
        } else {
            DirectedGraph<Object, Transformer>.Path path = graph.getShortestPath(source, result);
            if (path == null) {
                return null;
            }
            for (DirectedGraph<Object, Transformer>.Edge edge : path.getEdges()) {
                transformers.add(edge.getValue());
            }
        }
        return transformers;
    }

    @Override
    public String toString() {
        loadTransformers();

        return graph.toString();
    }

}