summaryrefslogtreecommitdiffstats
path: root/sandbox/old/contrib/implementation-script/container.jsr223/src/main/java/org/apache/tuscany/extension/script/ScriptComponent.java
blob: 874e978e77ffc0db09e771bf8e7b9064d4655e25 (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
/*
 * 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.extension.script;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.apache.tuscany.spi.ObjectCreationException;
import org.apache.tuscany.spi.ObjectFactory;
import org.apache.tuscany.spi.component.InstanceWrapper;
import org.apache.tuscany.spi.component.TargetResolutionException;
import org.apache.tuscany.spi.component.WorkContext;
import org.apache.tuscany.spi.extension.AtomicComponentExtension;
import org.apache.tuscany.spi.model.Operation;
import org.apache.tuscany.spi.model.physical.PhysicalOperationDefinition;
import org.apache.tuscany.spi.wire.ProxyService;
import org.apache.tuscany.spi.wire.TargetInvoker;
import org.apache.tuscany.spi.wire.Wire;

public class ScriptComponent extends AtomicComponentExtension {

    private ScriptImplementation impl;
    protected Map<String, List<Wire>> wires = new HashMap<String, List<Wire>>();

    protected ScriptComponent(URI name,
                              ScriptImplementation implementation,
                              ProxyService proxyService,
                              WorkContext workContext,
                              URI groupId,
                              int initLevel) {
        super(name, proxyService, workContext, groupId, initLevel);
        impl = implementation;
    }

    public TargetInvoker createTargetInvoker(String targetName, Operation operation) {
        return new ScriptInvoker(operation.getName(), this, scopeContainer, workContext);
    }

    @SuppressWarnings("unchecked")
    public InstanceWrapper<?> createInstanceWrapper() throws ObjectCreationException {
        try {

            Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); // TODO: classloader?

            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByExtension(impl.getScriptLanguage());
            if (engine == null) {
                throw new ObjectCreationException("no script engine found for language: " + impl.getScriptLanguage());
            }

            Reader reader;
            if (impl.getInlineSrc() == null) {
                URL url = impl.getClassLoader().getResource(impl.getScriptName());
                reader = new InputStreamReader(url.openStream());
            } else {
                reader = new StringReader(impl.getInlineSrc());
            }
            
            engine.eval(reader);

            return new InstanceWrapperBase(engine);

        } catch (ScriptException e) {
            throw new ObjectCreationException(e);
        } catch (IOException e) {
            throw new ObjectCreationException(e);
        }
    }

    // TODO: move all the rest to SPI extension 

    @Deprecated
    public Object createInstance() throws ObjectCreationException {
        throw new UnsupportedOperationException();
    }

    public Object getAssociatedTargetInstance() throws TargetResolutionException {
        // TODO Auto-generated method stub
        return null;
    }

    public Object getTargetInstance() throws TargetResolutionException {
        // TODO Auto-generated method stub
        return null;
    }

    public void attachCallbackWire(Wire wire) {
        // TODO Auto-generated method stub

    }

    public void attachWire(Wire wire) {
        assert wire.getSourceUri().getFragment() != null;
        String referenceName = wire.getSourceUri().getFragment();
        List<Wire> wireList = wires.get(referenceName);
        if (wireList == null) {
            wireList = new ArrayList<Wire>();
            wires.put(referenceName, wireList);
        }
        wireList.add(wire);
//        Member member = referenceSites.get(referenceName);
//        if (member != null) {
//            injectors.add(createInjector(member, wire));
//        }
//        // cycle through constructor param names as well
//        for (int i = 0; i < constructorParamNames.size(); i++) {
//            if (referenceName.equals(constructorParamNames.get(i))) {
//                ObjectFactory[] initializerFactories = instanceFactory.getInitializerFactories();
//                initializerFactories[i] = createWireFactory(constructorParamTypes.get(i), wire);
//                break;
//            }
//        }
//        //TODO error if ref not set on constructor or ref site

    }


    public void attachWires(List<Wire> attachWires) {
        assert attachWires.size() > 0;
        assert attachWires.get(0).getSourceUri().getFragment() != null;
        String referenceName = attachWires.get(0).getSourceUri().getFragment();
        List<Wire> wireList = wires.get(referenceName);
        if (wireList == null) {
            wireList = new ArrayList<Wire>();
            wires.put(referenceName, wireList);
        }
        wireList.addAll(attachWires);
//        Member member = referenceSites.get(referenceName);
//        if (member == null) {
//            if (constructorParamNames.contains(referenceName)) {
//                // injected on the constructor
//                throw new UnsupportedOperationException();
//            } else {
//                throw new NoAccessorException(referenceName);
//            }
//        }
//
//        Class<?> type = attachWires.get(0).getSourceContract().getInterfaceClass();
//        if (type == null) {
//            throw new NoMultiplicityTypeException("Java interface must be specified for multiplicity", referenceName);
//        }
//        injectors.add(createMultiplicityInjector(member, type, wireList));

    }

    public List<Wire> getWires(String name) {
        return wires.get(name);
    }

    public TargetInvoker createTargetInvoker(String targetName, PhysicalOperationDefinition operation) {
        throw new UnsupportedOperationException();
    }

    protected <B> ObjectFactory<B> createWireFactory(Class<B> interfaze, Wire wire) {
        return new WireObjectFactory<B>(interfaze, wire, proxyService);
    }

}