KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > sca > tuscany > BootstrapHelper


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.sca.tuscany;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.xml.stream.XMLInputFactory;
23
24 import org.apache.tuscany.common.resource.ResourceLoader;
25 import org.apache.tuscany.common.resource.impl.ResourceLoaderImpl;
26 import org.apache.tuscany.core.builder.ContextFactoryBuilder;
27 import org.apache.tuscany.core.config.ConfigurationException;
28 import org.apache.tuscany.core.config.ModuleComponentConfigurationLoader;
29 import org.apache.tuscany.core.config.impl.ModuleComponentConfigurationLoaderImpl;
30 import org.apache.tuscany.core.config.impl.StAXModuleComponentConfigurationLoaderImpl;
31 import org.apache.tuscany.core.context.AggregateContext;
32 import org.apache.tuscany.core.context.EventContext;
33 import org.apache.tuscany.core.context.SystemAggregateContext;
34 import org.apache.tuscany.core.loader.StAXLoaderRegistry;
35 import org.apache.tuscany.core.loader.StAXUtil;
36 import org.apache.tuscany.core.system.assembly.impl.SystemAssemblyFactoryImpl;
37 import org.apache.tuscany.core.system.builder.SystemContextFactoryBuilder;
38 import org.apache.tuscany.core.system.builder.SystemEntryPointBuilder;
39 import org.apache.tuscany.core.system.builder.SystemExternalServiceBuilder;
40 import org.apache.tuscany.core.system.loader.SystemSCDLModelLoader;
41 import org.apache.tuscany.model.assembly.AssemblyFactory;
42 import org.apache.tuscany.model.assembly.AssemblyModelContext;
43 import org.apache.tuscany.model.assembly.ModuleComponent;
44 import org.apache.tuscany.model.assembly.impl.AssemblyModelContextImpl;
45 import org.apache.tuscany.model.assembly.loader.AssemblyModelLoader;
46 import org.apache.tuscany.model.scdl.loader.SCDLModelLoader;
47 import org.apache.tuscany.model.scdl.loader.impl.SCDLAssemblyModelLoaderImpl;
48
49 public class BootstrapHelper {
50     
51     /**
52      * Returns a default AssemblyModelContext.
53      *
54      * @param classLoader the classloader to use for application artifacts
55      * @return a default AssemblyModelContext
56      */

57     public static AssemblyModelContext getModelContext(ClassLoader JavaDoc classLoader) {
58         // Create an assembly model factory
59
AssemblyFactory modelFactory = new SystemAssemblyFactoryImpl();
60
61         // Create a default assembly model loader
62
List JavaDoc<SCDLModelLoader> scdlLoaders = new ArrayList JavaDoc<SCDLModelLoader>();
63         scdlLoaders.add(new SystemSCDLModelLoader());
64         AssemblyModelLoader modelLoader = new SCDLAssemblyModelLoaderImpl(scdlLoaders);
65
66         // Create a resource loader from the supplied classloader
67
ResourceLoader resourceLoader = new ResourceLoaderImpl(classLoader);
68
69         // Create an assembly model context
70
return new AssemblyModelContextImpl(modelFactory, modelLoader, resourceLoader);
71     }
72
73     /**
74      * Returns a default list of configuration builders.
75      *
76      * @return a default list of configuration builders
77      */

78     public static List JavaDoc<ContextFactoryBuilder> getBuilders() {
79         List JavaDoc<ContextFactoryBuilder> configBuilders = new ArrayList JavaDoc<ContextFactoryBuilder>();
80         configBuilders.add((new SystemContextFactoryBuilder()));
81         configBuilders.add(new SystemEntryPointBuilder());
82         configBuilders.add(new SystemExternalServiceBuilder());
83         return configBuilders;
84     }
85
86     private static final boolean useStax = true;
87     private static final String JavaDoc SYSTEM_LOADER_COMPONENT = "tuscany.loader";
88
89     /**
90      * Returns the default module configuration loader.
91      *
92      * @param systemContext the runtime's system context
93      * @param modelContext the model context the loader will use
94      * @return the default module configuration loader
95      */

96     public static ModuleComponentConfigurationLoader getConfigurationLoader(SystemAggregateContext systemContext, AssemblyModelContext modelContext) throws ConfigurationException {
97         if (useStax) {
98             // Bootstrap the StAX loader module
99
bootstrapStaxLoader(systemContext, modelContext);
100             return new StAXModuleComponentConfigurationLoaderImpl(modelContext, XMLInputFactory.newInstance(), systemContext.resolveInstance(StAXLoaderRegistry.class));
101         } else {
102             return new ModuleComponentConfigurationLoaderImpl(modelContext);
103         }
104     }
105
106     private static AggregateContext bootstrapStaxLoader(SystemAggregateContext systemContext, AssemblyModelContext modelContext) throws ConfigurationException {
107         AggregateContext loaderContext = (AggregateContext) systemContext.getContext(SYSTEM_LOADER_COMPONENT);
108         if (loaderContext == null) {
109             ModuleComponent loaderComponent = StAXUtil.bootstrapLoader(SYSTEM_LOADER_COMPONENT, modelContext);
110             loaderContext = registerModule(systemContext, loaderComponent);
111             loaderContext.fireEvent(EventContext.MODULE_START, null);
112         }
113         return loaderContext;
114     }
115
116     public static AggregateContext registerModule(AggregateContext parent, ModuleComponent component) throws ConfigurationException {
117         // register the component
118
parent.registerModelObject(component);
119
120         // Get the aggregate context representing the component
121
return (AggregateContext) parent.getContext(component.getName());
122     }
123 }
Popular Tags