1 17 package org.apache.servicemix.sca.tuscany; 18 19 import java.util.List ; 20 21 import org.apache.tuscany.common.monitor.MonitorFactory; 22 import org.apache.tuscany.common.monitor.impl.NullMonitorFactory; 23 import org.apache.tuscany.core.builder.ContextFactoryBuilder; 24 import org.apache.tuscany.core.builder.impl.DefaultWireBuilder; 25 import org.apache.tuscany.core.config.ConfigurationException; 26 import org.apache.tuscany.core.config.ModuleComponentConfigurationLoader; 27 import org.apache.tuscany.core.context.AggregateContext; 28 import org.apache.tuscany.core.context.CoreRuntimeException; 29 import org.apache.tuscany.core.context.EventContext; 30 import org.apache.tuscany.core.context.SystemAggregateContext; 31 import org.apache.tuscany.core.runtime.RuntimeContext; 32 import org.apache.tuscany.core.runtime.RuntimeContextImpl; 33 import org.apache.tuscany.model.assembly.AssemblyModelContext; 34 import org.apache.tuscany.model.assembly.ModuleComponent; 35 import org.apache.tuscany.model.scdl.loader.SCDLModelLoader; 36 import org.osoa.sca.ModuleContext; 37 import org.osoa.sca.SCA; 38 import org.osoa.sca.ServiceRuntimeException; 39 40 public class TuscanyRuntime extends SCA { 41 private final TuscanyRuntime.Monitor monitor; 42 private final Object sessionKey = new Object (); 43 44 private final RuntimeContext runtime; 45 private final AggregateContext moduleContext; 46 47 private final ModuleComponent moduleComponent; 48 49 private static final String SYSTEM_MODULE_COMPONENT = "org.apache.tuscany.core.system"; 50 51 59 public TuscanyRuntime(String name, String uri) throws ConfigurationException { 60 this(name, uri, 61 Thread.currentThread().getContextClassLoader(), 62 new NullMonitorFactory()); 63 } 64 65 76 public TuscanyRuntime(String name, String uri, ClassLoader classLoader, MonitorFactory monitorFactory) throws ConfigurationException { 77 this.monitor = monitorFactory.getMonitor(TuscanyRuntime.Monitor.class); 78 79 AssemblyModelContext modelContext = BootstrapHelper.getModelContext(classLoader); 81 82 List <SCDLModelLoader> loaders = modelContext.getAssemblyLoader().getLoaders(); 84 List <ContextFactoryBuilder> configBuilders = BootstrapHelper.getBuilders(); 85 runtime = new RuntimeContextImpl(monitorFactory, loaders, configBuilders, new DefaultWireBuilder()); 86 runtime.start(); 87 monitor.started(runtime); 88 89 SystemAggregateContext systemContext = runtime.getSystemContext(); 91 ModuleComponentConfigurationLoader loader = BootstrapHelper.getConfigurationLoader(systemContext, modelContext); 92 ModuleComponent systemModuleComponent = loader.loadSystemModuleComponent(SYSTEM_MODULE_COMPONENT, SYSTEM_MODULE_COMPONENT); 93 AggregateContext context = BootstrapHelper.registerModule(systemContext, systemModuleComponent); 94 context.fireEvent(EventContext.MODULE_START, null); 95 96 AggregateContext rootContext = runtime.getRootContext(); 98 moduleComponent = loader.loadModuleComponent(name, uri); 99 moduleContext = BootstrapHelper.registerModule(rootContext, moduleComponent); 100 } 101 102 public ModuleComponent getModuleComponent() { 103 return moduleComponent; 104 } 105 106 public AggregateContext getModuleContext() { 107 return moduleContext; 108 } 109 110 113 @Override 114 public void start() { 115 setModuleContext((ModuleContext) moduleContext); 116 try { 117 moduleContext.fireEvent(EventContext.MODULE_START, null); 119 moduleContext.fireEvent(EventContext.REQUEST_START, null); 120 moduleContext.fireEvent(EventContext.SESSION_NOTIFY, sessionKey); 121 monitor.started(moduleContext); 122 } catch (CoreRuntimeException e) { 123 setModuleContext(null); 124 monitor.startFailed(moduleContext, e); 125 throw new ServiceRuntimeException(e); 127 } 128 } 129 130 133 @Override 134 public void stop() { 135 setModuleContext(null); 136 moduleContext.fireEvent(EventContext.REQUEST_END, null); 137 moduleContext.fireEvent(EventContext.SESSION_END, sessionKey); 138 moduleContext.fireEvent(EventContext.MODULE_STOP, null); 139 moduleContext.stop(); 140 monitor.stopped(moduleContext); 141 runtime.stop(); 142 monitor.stopped(runtime); 143 } 144 145 148 public static interface Monitor { 149 154 void started(AggregateContext ctx); 155 156 162 void startFailed(AggregateContext ctx, CoreRuntimeException e); 163 164 169 void stopped(AggregateContext ctx); 170 } 171 172 } | Popular Tags |