summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060518/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXUtil.java
blob: f9ac2b05593f4669bd9fe64cae3748a16d9e8eca (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
 *
 *  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.core.loader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.apache.tuscany.core.config.ComponentTypeIntrospector;
import org.apache.tuscany.core.config.ConfigurationException;
import org.apache.tuscany.core.config.ConfigurationLoadException;
import org.apache.tuscany.core.config.impl.Java5ComponentTypeIntrospector;
import org.apache.tuscany.core.config.processor.ProcessorUtils;
import org.apache.tuscany.core.loader.assembly.ComponentLoader;
import org.apache.tuscany.core.loader.assembly.EntryPointLoader;
import org.apache.tuscany.core.loader.assembly.InterfaceJavaLoader;
import org.apache.tuscany.core.loader.assembly.ModuleFragmentLoader;
import org.apache.tuscany.core.loader.assembly.ModuleLoader;
import org.apache.tuscany.core.loader.impl.StAXLoaderRegistryImpl;
import org.apache.tuscany.core.loader.impl.StringParserPropertyFactory;
import org.apache.tuscany.core.loader.system.SystemBindingLoader;
import org.apache.tuscany.core.loader.system.SystemImplementationLoader;
import org.apache.tuscany.core.system.assembly.SystemAssemblyFactory;
import org.apache.tuscany.core.system.assembly.SystemImplementation;
import org.apache.tuscany.core.system.assembly.impl.SystemAssemblyFactoryImpl;
import org.apache.tuscany.model.assembly.AssemblyContext;
import org.apache.tuscany.model.assembly.Component;
import org.apache.tuscany.model.assembly.EntryPoint;
import org.apache.tuscany.model.assembly.Module;
import org.apache.tuscany.model.assembly.ModuleComponent;
import org.apache.tuscany.model.assembly.Multiplicity;
import org.apache.tuscany.model.assembly.OverrideOption;
import org.apache.tuscany.model.assembly.Scope;

/**
 * @version $Rev$ $Date$
 */
public final class StAXUtil {
    private static final Map<String, Multiplicity> MULTIPLICITY = new HashMap<String, Multiplicity>(4);
    private static final Map<String, OverrideOption> OVERRIDE_OPTIONS = new HashMap<String, OverrideOption>(3);

    static {
        MULTIPLICITY.put("0..1", Multiplicity.ZERO_ONE);
        MULTIPLICITY.put("1..1", Multiplicity.ONE_ONE);
        MULTIPLICITY.put("0..n", Multiplicity.ZERO_N);
        MULTIPLICITY.put("1..n", Multiplicity.ONE_N);

        OVERRIDE_OPTIONS.put("no", OverrideOption.NO);
        OVERRIDE_OPTIONS.put("may", OverrideOption.MAY);
        OVERRIDE_OPTIONS.put("must", OverrideOption.MUST);
    }

    private StAXUtil() {
    }

    public static void skipToEndElement(XMLStreamReader reader) throws XMLStreamException {
        int depth = 0;
        while (true) {
            int event = reader.next();
            if (event == XMLStreamConstants.START_ELEMENT) {
                depth++;
            } else if (event == XMLStreamConstants.END_ELEMENT) {
                if (depth == 0) {
                    return;
                }
                depth--;
            }
        }
    }

    public static Multiplicity multiplicity(String multiplicity, Multiplicity def) {
        return multiplicity == null ? def : MULTIPLICITY.get(multiplicity);
    }

    public static OverrideOption overrideOption(String overrideOption, OverrideOption def) {
        return overrideOption == null ? def : OVERRIDE_OPTIONS.get(overrideOption);
    }

    public static ModuleComponent bootstrapLoader(String name, AssemblyContext context) throws ConfigurationLoadException {
        SystemAssemblyFactory factory = new SystemAssemblyFactoryImpl();
        ComponentTypeIntrospector introspector = ProcessorUtils.createCoreIntrospector(factory);
        Module module = factory.createModule();
        module.setName("org.apache.tuscany.core.system.loader");

        List<Component> components = module.getComponents();

        // bootstrap the minimal set of loaders needed to read the system module files
        // all others should be defined in the system.module file
        components.add(bootstrapLoader(factory, introspector, ModuleLoader.class));
        components.add(bootstrapLoader(factory, introspector, ModuleFragmentLoader.class));
        Component propFactory = factory.createSystemComponent("org.apache.tuscany.core.system.loader.DefaultPropertyFactory", StAXPropertyFactory.class, StringParserPropertyFactory.class, Scope.MODULE);
        introspector.introspect(StAXPropertyFactory.class);
        components.add(propFactory);
        components.add(bootstrapLoader(factory, introspector, ComponentLoader.class));
        components.add(bootstrapLoader(factory, introspector, EntryPointLoader.class));
        components.add(bootstrapLoader(factory, introspector, InterfaceJavaLoader.class));
        components.add(bootstrapLoader(factory, introspector, SystemImplementationLoader.class));
        components.add(bootstrapLoader(factory, introspector, SystemBindingLoader.class));
        // do not add additional loaders above - they should be in the system.module file

        // bootstrap the registries needed by the bootstrap loaders above
        bootstrapService(factory, introspector, module, StAXLoaderRegistry.class, StAXLoaderRegistryImpl.class);
        bootstrapService(factory, introspector, module, SystemAssemblyFactory.class, SystemAssemblyFactoryImpl.class);
        bootstrapService(factory, introspector, module, ComponentTypeIntrospector.class, Java5ComponentTypeIntrospector.class);

        ModuleComponent mc = factory.createModuleComponent();
        mc.setName(name);
        mc.setImplementation(module);
        mc.initialize(context);
        return mc;
    }

    private static Component bootstrapLoader(SystemAssemblyFactory factory, ComponentTypeIntrospector introspector, Class<?> loaderClass) {
        SystemImplementation implementation = factory.createSystemImplementation();
        implementation.setImplementationClass(loaderClass);
        try {
            implementation.setComponentType(introspector.introspect(loaderClass));
        } catch (ConfigurationException e) {
            throw (AssertionError) new AssertionError("Invalid bootstrap loader").initCause(e);
        }
        Component component = factory.createSimpleComponent();
        component.setName(loaderClass.getName());
        component.setImplementation(implementation);
        return component;
    }

    private static <T> void bootstrapService(SystemAssemblyFactory factory, ComponentTypeIntrospector introspector, Module module, Class<T> service, Class<? extends T> impl) {
        String epName = service.getName();
        String compName = impl.getName();

        Component component = factory.createSystemComponent(compName, service, impl, Scope.MODULE);
        try {
            component.getImplementation().setComponentType(introspector.introspect(impl));
        } catch (ConfigurationException e) {
            throw (AssertionError) new AssertionError("Invalid bootstrap loader").initCause(e);
        }

        EntryPoint entryPoint = factory.createSystemEntryPoint(epName, service, compName);

        module.getComponents().add(component);
        module.getEntryPoints().add(entryPoint);
    }
}