summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/tags/java-M1-final/java/sca/containers/container.rhino/src/main/java/org/apache/tuscany/container/rhino/loader/JavaScriptImplementationLoader.java
blob: e28776049cb3dd35d6d00e87e24115c4ea50104b (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
/**
 *
 * Copyright 2005 The Apache Software Foundation
 *
 *  Licensed 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.container.rhino.loader;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.apache.tuscany.common.resource.ResourceLoader;
import org.apache.tuscany.container.rhino.assembly.JavaScriptImplementation;
import org.apache.tuscany.core.config.ConfigurationLoadException;
import org.apache.tuscany.core.config.InvalidRootElementException;
import org.apache.tuscany.core.config.MissingResourceException;
import org.apache.tuscany.core.config.SidefileLoadException;
import org.apache.tuscany.core.loader.LoaderContext;
import org.apache.tuscany.core.loader.StAXElementLoader;
import org.apache.tuscany.core.loader.StAXLoaderRegistry;
import org.apache.tuscany.core.loader.assembly.AssemblyConstants;
import org.apache.tuscany.core.system.annotation.Autowire;
import org.apache.tuscany.model.assembly.ComponentType;
import org.osoa.sca.annotations.Destroy;
import org.osoa.sca.annotations.Init;
import org.osoa.sca.annotations.Scope;

/**
 * @version $Rev$ $Date$
 */
@Scope("MODULE")
public class JavaScriptImplementationLoader implements StAXElementLoader<JavaScriptImplementation> {

    public static final QName IMPLEMENTATION_JS = new QName("http://org.apache.tuscany/xmlns/js/0.9", "implementation.js");

    protected StAXLoaderRegistry registry;

    private XMLInputFactory xmlFactory;

    public JavaScriptImplementationLoader() {
        // todo make this a reference to a system service
        xmlFactory = XMLInputFactory.newInstance();
    }

    @Autowire
    public void setRegistry(StAXLoaderRegistry registry) {
        this.registry = registry;
    }

    @Init(eager = true)
    public void start() {
        registry.registerLoader(IMPLEMENTATION_JS, this);
    }

    @Destroy
    public void stop() {
        registry.unregisterLoader(IMPLEMENTATION_JS, this);
    }

    public JavaScriptImplementation load(XMLStreamReader reader, LoaderContext loaderContext) throws XMLStreamException, ConfigurationLoadException {
        String scriptFile = reader.getAttributeValue(null, "scriptFile");
        String script = loadScript(scriptFile, loaderContext.getResourceLoader());
        ComponentType componentType = loadComponentType(scriptFile, loaderContext);

        JavaScriptImplementation jsImpl = new JavaScriptImplementation();
        jsImpl.setComponentType(componentType);
        jsImpl.setScriptFile(scriptFile);
        jsImpl.setScript(script);
        jsImpl.setResourceLoader(loaderContext.getResourceLoader());
        jsImpl.setTypeHelper(registry.getContext().getTypeHelper());
        return jsImpl;
    }

    protected String loadScript(String scriptFile, ResourceLoader resourceLoader) throws ConfigurationLoadException {
        URL url = resourceLoader.getResource(scriptFile);
        if (url == null) {
            throw new ConfigurationLoadException(scriptFile);
        }
        InputStream inputStream;
        try {
            inputStream = url.openStream();
        } catch (IOException e) {
            throw new ConfigurationLoadException(scriptFile, e);
        }
        try {
            StringBuilder sb = new StringBuilder(1024);
            int n;
            while ((n = inputStream.read()) != -1) {
                sb.append((char) n);
            }
            return sb.toString();
        } catch (IOException e) {
            throw new ConfigurationLoadException(scriptFile, e);
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }

    protected ComponentType loadComponentType(String scriptFile, LoaderContext loaderContext) throws SidefileLoadException, MissingResourceException{
        String sidefile = scriptFile.substring(0, scriptFile.lastIndexOf('.')) + ".componentType";
        URL componentTypeFile = loaderContext.getResourceLoader().getResource(sidefile);
        if (componentTypeFile == null) {
            throw new MissingResourceException(sidefile);
        }

        try {
            XMLStreamReader reader;
            InputStream is;
                is = componentTypeFile.openStream();
            try {
                reader = xmlFactory.createXMLStreamReader(is);
                try {
                    reader.nextTag();
                    if (!AssemblyConstants.COMPONENT_TYPE.equals(reader.getName())) {
                        InvalidRootElementException e = new InvalidRootElementException(AssemblyConstants.COMPONENT_TYPE, reader.getName());
                        e.setResourceURI(componentTypeFile.toString());
                        throw e;
                    }
                    return (ComponentType) registry.load(reader, loaderContext);
                } finally {
                    try {
                        reader.close();
                    } catch (XMLStreamException e) {
                        // ignore
                    }
                }
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        } catch (IOException e) {
            SidefileLoadException sfe = new SidefileLoadException(e.getMessage());
            sfe.setResourceURI(componentTypeFile.toString());
            throw sfe;
        } catch (XMLStreamException e) {
            SidefileLoadException sfe = new SidefileLoadException(e.getMessage());
            sfe.setResourceURI(componentTypeFile.toString());
            throw sfe;
        } catch (ConfigurationLoadException e) {
            SidefileLoadException sfe = new SidefileLoadException(e.getMessage());
            sfe.setResourceURI(componentTypeFile.toString());
            throw sfe;
        }
    }
}