KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
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 JavaDoc sessionKey = new Object JavaDoc();
43
44     private final RuntimeContext runtime;
45     private final AggregateContext moduleContext;
46     
47     private final ModuleComponent moduleComponent;
48
49     private static final String JavaDoc SYSTEM_MODULE_COMPONENT = "org.apache.tuscany.core.system";
50
51     /**
52      * Construct a runtime using a null MonitorFactory.
53      *
54      * @param name the name of the module component
55      * @param uri the URI to assign to the module component
56      * @throws ConfigurationException if there was a problem loading the SCA configuration
57      * @see TuscanyRuntime#TuscanyRuntime(String, String, org.apache.tuscany.common.monitor.MonitorFactory)
58      */

59     public TuscanyRuntime(String JavaDoc name, String JavaDoc uri) throws ConfigurationException {
60         this(name, uri,
61              Thread.currentThread().getContextClassLoader(),
62              new NullMonitorFactory());
63     }
64
65     /**
66      * Construct a runtime containing a single module component with the
67      * specified name. The module definition is loaded from a "/sca.module"
68      * resource found on the classpath of the current Thread context classloader.
69      *
70      * @param name the name of the module component
71      * @param uri the URI to assign to the module component
72      * @param classLoader the class loader to use for the assembly
73      * @param monitorFactory the MonitorFactory for this runtime
74      * @throws ConfigurationException if there was a problem loading the SCA configuration
75      */

76     public TuscanyRuntime(String JavaDoc name, String JavaDoc uri, ClassLoader JavaDoc classLoader, MonitorFactory monitorFactory) throws ConfigurationException {
77         this.monitor = monitorFactory.getMonitor(TuscanyRuntime.Monitor.class);
78
79         // Create an assembly model context
80
AssemblyModelContext modelContext = BootstrapHelper.getModelContext(classLoader);
81
82         // Create a runtime context and start it
83
List JavaDoc<SCDLModelLoader> loaders = modelContext.getAssemblyLoader().getLoaders();
84         List JavaDoc<ContextFactoryBuilder> configBuilders = BootstrapHelper.getBuilders();
85         runtime = new RuntimeContextImpl(monitorFactory, loaders, configBuilders, new DefaultWireBuilder());
86         runtime.start();
87         monitor.started(runtime);
88
89         // Load and start the system configuration
90
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         // Load the SCDL configuration of the application module
97
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     /**
111      * Start the runtime and associate the module context with the calling thread.
112      */

113     @Override JavaDoc
114     public void start() {
115         setModuleContext((ModuleContext) moduleContext);
116         try {
117             //moduleContext.start();
118
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             //FIXME throw a better exception
126
throw new ServiceRuntimeException(e);
127         }
128     }
129
130     /**
131      * Disassociate the module context from the current thread and shut down the runtime.
132      */

133     @Override JavaDoc
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     /**
146      * Monitor interface for a TuscanyRuntime.
147      */

148     public static interface Monitor {
149         /**
150          * Event emitted after the runtime has been started.
151          *
152          * @param ctx the runtime's module component context
153          */

154         void started(AggregateContext ctx);
155
156         /**
157          * Event emitted when an attempt to start the runtime failed.
158          *
159          * @param ctx the runtime's module component context
160          * @param e the exception that caused the failure
161          */

162         void startFailed(AggregateContext ctx, CoreRuntimeException e);
163
164         /**
165          * Event emitted after the runtime has been stopped.
166          *
167          * @param ctx the runtime's module component context
168          */

169         void stopped(AggregateContext ctx);
170     }
171
172 }
Popular Tags