summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/web-javascript/src/main/java/org/apache/tuscany/sca/web/javascript/DefaultJavascriptProxyFactoryExtensionPoint.java
blob: 402eb53bb0f61960c7b0145e8caf1783cef1c0db (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
/*
 * 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.web.javascript;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.xml.namespace.QName;

import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.core.FactoryExtensionPoint;
import org.apache.tuscany.sca.core.UtilityExtensionPoint;
import org.apache.tuscany.sca.extensibility.ServiceDeclaration;
import org.apache.tuscany.sca.extensibility.ServiceDiscovery;
import org.apache.tuscany.sca.monitor.Monitor;
import org.apache.tuscany.sca.monitor.MonitorFactory;

/**
 * Default extension point for javascript proxy factories
 * 
 * @version $Rev$ $Date$
 */
public class DefaultJavascriptProxyFactoryExtensionPoint implements JavascriptProxyFactoryExtensionPoint {
    private final Map<QName, JavascriptProxyFactory> factoriesByQName = new HashMap<QName, JavascriptProxyFactory>();
    private final Map<Class<?>, JavascriptProxyFactory> factoriesByType = new HashMap<Class<?>, JavascriptProxyFactory>();
    
    private Monitor monitor = null;
    private ExtensionPointRegistry registry;
    private boolean loaded = false;
    
    public DefaultJavascriptProxyFactoryExtensionPoint(ExtensionPointRegistry extensionPoints) {
        this.registry = extensionPoints;
        UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class);
        MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class);
        if (monitorFactory != null) {
                this.monitor = monitorFactory.createMonitor();
        }
    }

    /**
     * Report a exception.
     * 
     * @param problems
     * @param message
     * @param model
    */
    private void error(String message, Object model, Exception ex) {
        /*
        if (monitor != null) {
            Problem problem =
                monitor.createProblem(this.getClass().getName(),
                                      Messages.RESOURCE_BUNDLE,
                                      Severity.WARNING,
                                      model,
                                      message,
                                      (Object[])messageParameters);
            monitor.problem(problem);
        } 
        */     
    }
    
    public void addProxyFactory(JavascriptProxyFactory javascriptProxyfactory) {
        if (javascriptProxyfactory.getModelType() != null) {
            factoriesByType.put(javascriptProxyfactory.getModelType(), javascriptProxyfactory);
        }
        if (javascriptProxyfactory.getQName() != null) {
            factoriesByQName.put(javascriptProxyfactory.getQName(), javascriptProxyfactory);
        }
    }

    public void removeProxyFactory(JavascriptProxyFactory javascriptProxyfactory) {
        if (javascriptProxyfactory.getModelType() != null) {
            factoriesByType.remove(javascriptProxyfactory.getModelType());
        }
        if (javascriptProxyfactory.getQName() != null) {
            factoriesByQName.remove(javascriptProxyfactory.getQName());
        }
    }

    public JavascriptProxyFactory getProxyFactory(QName bindingName) {
        loadFactories();
        return factoriesByQName.get(bindingName);
    }

    public JavascriptProxyFactory getProxyFactory(Class<?> bindingType) {
        loadFactories();
        Class<?>[] classes = bindingType.getInterfaces();
        for (Class<?> c : classes) {
            JavascriptProxyFactory proxyFactory = factoriesByType.get(c);
            if (proxyFactory != null) {
                return proxyFactory;
            }
        }

        //here we didn't find the proxy factory for the biding
        JavascriptProxyFactory factory = null;
        if (bindingType.isInterface()) {
            // Dynamically load a factory class declared under META-INF/services 
            try {
                Class<?> factoryClass = registry.getServiceDiscovery().getServiceDeclaration(bindingType).getClass();
                if (factoryClass != null) {

                    try {
                        // Default empty constructor
                        Constructor<?> constructor = factoryClass.getConstructor();
                        factory = (JavascriptProxyFactory) constructor.newInstance();
                    } catch (NoSuchMethodException e) {

                        // Constructor taking the model factory extension point
                        Constructor<?> constructor = factoryClass.getConstructor(FactoryExtensionPoint.class);
                        factory = (JavascriptProxyFactory) constructor.newInstance(this);
                    }

                    // Cache the loaded factory
                    addProxyFactory(factory);
                }
            } catch (Exception e) {
                throw new IllegalArgumentException(e);
            }
        } else {

            // Call the newInstance static method on the factory abstract class
            try {
                factory = (JavascriptProxyFactory) ServiceDiscovery.getInstance().getServiceDeclaration(bindingType);
            } catch (Exception e) {
                throw new IllegalArgumentException(e);
            }

            // Cache the factory
            addProxyFactory(factory);
        }
    
        return factory;
    }
    
    /**
     * Private Utility methods
     */
    
    /**
     * Returns a QName object from a QName expressed as {ns}name
     * or ns#name.
     * 
     * @param qname
     * @return
     */
    private static QName getQName(String qname) {
        if (qname == null) {
            return null;
        }
        qname = qname.trim();
        if (qname.startsWith("{")) {
            int h = qname.indexOf('}');
            if (h != -1) {
                return new QName(qname.substring(1, h), qname.substring(h + 1));
            }
        } else {
            int h = qname.indexOf('#');
            if (h != -1) {
                return new QName(qname.substring(0, h), qname.substring(h + 1));
            }
        }
        throw new IllegalArgumentException("Invalid qname: "+qname);
    }    
    
    /**
     * Lazily load artifact processors registered in the extension point.
     */
    private synchronized void loadFactories() {
        if (loaded) {
            return;
        }

        // Get the proxy factories declarations
        Collection<ServiceDeclaration> factoryDeclarations = null;
        try {
            factoryDeclarations = ServiceDiscovery.getInstance().getServiceDeclarations(JavascriptProxyFactory.class);
        } catch (IOException e) {
            IllegalStateException ie = new IllegalStateException(e);
            error("IllegalStateException", factoryDeclarations, ie);
            throw ie;
        }

        for (ServiceDeclaration processorDeclaration : factoryDeclarations) {
            Map<String, String> attributes = processorDeclaration.getAttributes();

            // Load a StAX artifact processor

            // Get the model QName
            QName artifactType = getQName(attributes.get("qname"));

            // Get the model class name
            String modelTypeName = attributes.get("model");

            // Create a factory, and register it
            JavascriptProxyFactory proxyFactory = null;
            try {
                proxyFactory = (JavascriptProxyFactory) processorDeclaration.loadClass().newInstance();
            } catch (Exception e) {
                IllegalStateException ie = new IllegalStateException(e);
                error("IllegalStateException", proxyFactory, ie);
                throw ie;
            }

            addProxyFactory(proxyFactory);
        }

        loaded = true;
    }

}