summaryrefslogtreecommitdiffstats
path: root/sandbox/container.groovy/src/main/java/org/apache/tuscany/container/extension/loader/AbstractImplementationLoader.java
blob: ed325e087bb671869d726f5a93d519121740ea66 (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
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  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.extension.loader;

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

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

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.apache.tuscany.model.assembly.Implementation;
import org.osoa.sca.annotations.Scope;

/**
 * Abstract implementation loader.
 *
 */
@Scope("MODULE")
public abstract class AbstractImplementationLoader<T extends Implementation> implements StAXElementLoader<T> {

	// Injected Stax loader registry.
    protected StAXLoaderRegistry registry;

    // XML input factory.
    protected XMLInputFactory xmlFactory;

    /**
     * Initializes the XML input factory.
     *
     */
    public AbstractImplementationLoader() {
        xmlFactory = XMLInputFactory.newInstance();
    }

    /**
     * Injection method for Stax loader registry.
     * @param registry Stax loader registry.
     */
    @Autowire
    public void setRegistry(StAXLoaderRegistry registry) {
        this.registry = registry;
    }

    /**
     * Loads the Groovy implementation.
     *
     * @param Stax XML stream reader.
     * @param loaderContext Loader context.
     * @return Groovy implementation.
     */
	public T load(XMLStreamReader reader, LoaderContext loaderContext) throws XMLStreamException, ConfigurationLoadException {

		T assemblyObject = getAssemblyObject(reader, loaderContext);
		
		URL componentTypeFile = getSideFile(reader, loaderContext);
        ComponentType componentType = loadComponentType(componentTypeFile, loaderContext);

		assemblyObject.setComponentType(componentType);

		return assemblyObject;

	}
	
	/**
	 * Required to be implemented by the concrete classes.
	 * @return Implementation object.
	 */
	protected abstract T getAssemblyObject(XMLStreamReader reader, LoaderContext loaderContext);

    /**
     * Gets the side file.
     * 
     * @param reader Reader for the module file.
     * @param loaderContext Loader context.
     * @return Side file Url.
     * @throws MissingResourceException
     */
	protected abstract URL getSideFile(XMLStreamReader reader, LoaderContext loaderContext) throws MissingResourceException;

	/**
	 * Loads the SIDE file to get the component information.
	 *
	 * @param scriptFile SCript file name.
	 * @param loaderContext Loader context.
	 * @return Component information.
	 * @throws SidefileLoadException
	 * @throws MissingResourceException
	 */
    private ComponentType loadComponentType(URL componentTypeFile, LoaderContext loaderContext) throws SidefileLoadException, MissingResourceException{
    	
    	XMLStreamReader reader = null;
    	InputStream is = null;

        try {
            is = componentTypeFile.openStream();
            reader = xmlFactory.createXMLStreamReader(is);
            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);
        } 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;
        } finally {
            try {
            	if(reader != null) {
            		reader.close();
            	}
            } catch (XMLStreamException e) {
                // ignore
            }
            try {
            	if(is != null) {
            		is.close();
            	}
            } catch (IOException e) {
                // ignore
            }
        }
    }

}