summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060518/java/sca/containers/container.java/src/main/java/org/apache/tuscany/container/java/loader/JavaImplementationLoader.java
blob: db3dd07c98d6136b41f9c234b83284b2bf10d879 (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
/**
 *
 * 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.java.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.java.assembly.JavaAssemblyFactory;
import org.apache.tuscany.container.java.assembly.JavaImplementation;
import org.apache.tuscany.core.config.ComponentTypeIntrospector;
import org.apache.tuscany.core.config.ConfigurationLoadException;
import org.apache.tuscany.core.config.InvalidRootElementException;
import org.apache.tuscany.core.config.JavaIntrospectionHelper;
import org.apache.tuscany.core.config.SidefileLoadException;
import org.apache.tuscany.core.config.processor.ProcessorUtils;
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 JavaImplementationLoader implements StAXElementLoader<JavaImplementation> {
    public static final QName IMPLEMENTATION_JAVA = new QName("http://www.osoa.org/xmlns/sca/0.9", "implementation.java");

    private StAXLoaderRegistry registry;
    private XMLInputFactory xmlFactory;

    private JavaAssemblyFactory factory;
    private ComponentTypeIntrospector introspector;

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

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

    @Autowire
    public void setFactory(JavaAssemblyFactory factory) {
        this.factory = factory;
        //FIXME JFM HACK
        introspector = ProcessorUtils.createCoreIntrospector(factory);
    }

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

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

    public JavaImplementation load(XMLStreamReader reader, LoaderContext loaderContext) throws XMLStreamException, ConfigurationLoadException {
        JavaImplementation javaImpl = factory.createJavaImplementation();
        String typeName = reader.getAttributeValue(null, "class");
        Class<?> implementationClass = getImplementationClass(loaderContext.getResourceLoader(), typeName);
        javaImpl.setImplementationClass(implementationClass);
        javaImpl.setComponentType(loadComponentType(loaderContext, implementationClass));
        return javaImpl;
    }

    protected Class<?> getImplementationClass(ResourceLoader resourceLoader, String typeName) throws ConfigurationLoadException {
        ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
        try {
            // set TCCL in case the application code needs it
            Thread.currentThread().setContextClassLoader(resourceLoader.getClassLoader());
            return resourceLoader.loadClass(typeName);
        } catch (ClassNotFoundException e) {
            throw new ConfigurationLoadException(e.getMessage(), e);
        } finally {
            Thread.currentThread().setContextClassLoader(oldCL);
        }
    }

    protected ComponentType loadComponentType(LoaderContext loaderContext, Class<?> implClass) throws ConfigurationLoadException, XMLStreamException {
        String baseName = JavaIntrospectionHelper.getBaseName(implClass);
        URL sidefile = implClass.getResource(baseName + ".componentType");
        if (sidefile == null) {
            return loadComponentTypeByIntrospection(implClass);
        } else {
            return loadComponentTypeFromSidefile(sidefile, loaderContext);
        }
    }

    protected ComponentType loadComponentTypeByIntrospection(Class<?> implClass) throws ConfigurationLoadException {
        return introspector.introspect(implClass);
    }

    protected ComponentType loadComponentTypeFromSidefile(URL sidefile, LoaderContext loaderContext) throws SidefileLoadException {
        try {
            XMLStreamReader reader;
            InputStream is;
            is = sidefile.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(sidefile.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(sidefile.toString());
            throw sfe;
        } catch (XMLStreamException e) {
            SidefileLoadException sfe = new SidefileLoadException(e.getMessage());
            sfe.setResourceURI(sidefile.toString());
            throw sfe;
        } catch (ConfigurationLoadException e) {
            SidefileLoadException sfe = new SidefileLoadException(e.getMessage());
            sfe.setResourceURI(sidefile.toString());
            throw sfe;
        }
    }
}