1 17 package org.apache.servicemix.sca.tuscany; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 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 57 public static AssemblyModelContext getModelContext(ClassLoader classLoader) { 58 AssemblyFactory modelFactory = new SystemAssemblyFactoryImpl(); 60 61 List <SCDLModelLoader> scdlLoaders = new ArrayList <SCDLModelLoader>(); 63 scdlLoaders.add(new SystemSCDLModelLoader()); 64 AssemblyModelLoader modelLoader = new SCDLAssemblyModelLoaderImpl(scdlLoaders); 65 66 ResourceLoader resourceLoader = new ResourceLoaderImpl(classLoader); 68 69 return new AssemblyModelContextImpl(modelFactory, modelLoader, resourceLoader); 71 } 72 73 78 public static List <ContextFactoryBuilder> getBuilders() { 79 List <ContextFactoryBuilder> configBuilders = new ArrayList <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 SYSTEM_LOADER_COMPONENT = "tuscany.loader"; 88 89 96 public static ModuleComponentConfigurationLoader getConfigurationLoader(SystemAggregateContext systemContext, AssemblyModelContext modelContext) throws ConfigurationException { 97 if (useStax) { 98 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 parent.registerModelObject(component); 119 120 return (AggregateContext) parent.getContext(component.getName()); 122 } 123 } | Popular Tags |