summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/runtime/RuntimeContextImpl.java
blob: 044958b9bfac1b1ff947967adc7c7fee8aef646d (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
 * 
 * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 * 
 * 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.runtime;

import java.util.ArrayList;
import java.util.List;

import org.apache.tuscany.common.monitor.MonitorFactory;
import org.apache.tuscany.common.monitor.impl.NullMonitorFactory;
import org.apache.tuscany.core.builder.BuilderConfigException;
import org.apache.tuscany.core.builder.HierarchicalWireBuilder;
import org.apache.tuscany.core.builder.RuntimeConfigurationBuilder;
import org.apache.tuscany.core.builder.WireBuilder;
import org.apache.tuscany.core.builder.impl.AssemblyVisitor;
import org.apache.tuscany.core.builder.impl.DefaultWireBuilder;
import org.apache.tuscany.core.config.ConfigurationException;
import org.apache.tuscany.core.context.AbstractContext;
import org.apache.tuscany.core.context.AggregateContext;
import org.apache.tuscany.core.context.AutowireContext;
import org.apache.tuscany.core.context.AutowireResolutionException;
import org.apache.tuscany.core.context.ConfigurationContext;
import org.apache.tuscany.core.context.CoreRuntimeException;
import org.apache.tuscany.core.context.EventException;
import org.apache.tuscany.core.context.QualifiedName;
import org.apache.tuscany.core.context.RuntimeEventListener;
import org.apache.tuscany.core.context.ScopeContext;
import org.apache.tuscany.core.context.SystemAggregateContext;
import org.apache.tuscany.core.context.TargetException;
import org.apache.tuscany.core.context.impl.AggregateContextImpl;
import org.apache.tuscany.core.context.impl.EventContextImpl;
import org.apache.tuscany.core.invocation.spi.ProxyFactory;
import org.apache.tuscany.core.system.context.SystemAggregateContextImpl;
import org.apache.tuscany.core.system.context.SystemScopeStrategy;
import org.apache.tuscany.model.assembly.Aggregate;
import org.apache.tuscany.model.assembly.Extensible;
import org.apache.tuscany.model.scdl.loader.SCDLModelLoader;

/**
 * Implementation of a RuntimeContext that forms the foundation for a Tuscany environment.
 * 
 * @version $Rev$ $Date$
 */
public class RuntimeContextImpl extends AbstractContext implements RuntimeContext {

    private final List<RuntimeConfigurationBuilder> builders;

    private final List<SCDLModelLoader> loaders;

    // the top-level wire builder in the runtime
    private final HierarchicalWireBuilder wireBuilder;

    private final List<RuntimeEventListener> listeners = new ArrayList(1);

    private final AggregateContext rootContext;

    private final SystemAggregateContext systemContext;

    private final MonitorFactory monitorFactory;
    
    /**
     * Default constructor that creates a runtime with a NullMonitorFactory and no builders.
     */
    public RuntimeContextImpl() {
        this(new NullMonitorFactory(), null, null, null);
    }

    /**
     * Constructor for creating a runtime with a specified MonitorFactory and pre-defined builders.
     * 
     * @param monitorFactory the default {@link MonitorFactory} for this runtime
     * @param builders a list of builders automatically made available; may be null
     * @param wireBuilder the top-level hierarchical wire builder for the runtime; if not specified, a default
     *        implementation will be used
     */
    public RuntimeContextImpl(MonitorFactory monitorFactory, List<SCDLModelLoader> loaders,
            List<RuntimeConfigurationBuilder> builders, HierarchicalWireBuilder wireBuilder) {
        super(RUNTIME);
        this.monitorFactory = monitorFactory;
        this.builders = (builders == null) ? new ArrayList(1) : builders;
        this.loaders = (loaders == null) ? new ArrayList(1) : loaders;
        this.wireBuilder = (wireBuilder == null) ? new DefaultWireBuilder() : wireBuilder;

        rootContext = new AggregateContextImpl(ROOT, this, this, new RuntimeScopeStrategy(), new EventContextImpl(), this, monitorFactory);
        systemContext = new SystemAggregateContextImpl(SYSTEM, this, this, new SystemScopeStrategy(), new EventContextImpl(), this, monitorFactory);
    }

    /**
     * Specialized constructor that allows the default implementations of the root and system contexts to be
     * overridden.
     * 
     * @param monitorFactory the default {@link MonitorFactory} for this runtime
     * @param rootContext the context to use for the root of the user context tree
     * @param systemContext the context to use for the root of the system context tree
     * @param builders a list of builders automatically made available; may be null
     * @param wireBuilder the top-level hierarchical wire builder for the runtime; if not specified, a default
     *        implementation will be used
     */
    public RuntimeContextImpl(MonitorFactory monitorFactory, AggregateContext rootContext, SystemAggregateContext systemContext,
            List<SCDLModelLoader> loaders, List<RuntimeConfigurationBuilder> builders, HierarchicalWireBuilder wireBuilder) {
        super(RUNTIME);
        this.rootContext = rootContext;
        this.systemContext = systemContext;
        this.monitorFactory = monitorFactory;
        this.loaders = (loaders == null) ? new ArrayList(1) : loaders;
        this.builders = (builders == null) ? new ArrayList(1) : builders;
        this.wireBuilder = (wireBuilder == null) ? new DefaultWireBuilder() : wireBuilder;
    }

    public void start() throws CoreRuntimeException {
        if (lifecycleState == RUNNING) {
            return;
        }
        systemContext.start();
        rootContext.start();
        lifecycleState = RUNNING;
    }

    public void stop() throws CoreRuntimeException {
        if (lifecycleState == STOPPED) {
            return;
        }
        rootContext.stop();
        systemContext.stop();
        lifecycleState = STOPPED;
    }

    public void addBuilder(RuntimeConfigurationBuilder builder) {
        assert (builder != null) : "Builder was null";
        builders.add(builder);
    }

    public void addBuilder(WireBuilder builder) {
        assert (builder != null) : "Builder was null";
        wireBuilder.addWireBuilder(builder);
    }

    public void addLoader(SCDLModelLoader loader) {
        assert (loader != null) : "Loader was null";
        loaders.add(loader);
    }

    public AggregateContext getContext(String ctxName) {
        checkRunning();
        if (ROOT.equals(ctxName)) {
            return rootContext;
        } else if (SYSTEM.equals(ctxName)) {
            return systemContext;
        }
        return (AggregateContext) rootContext.getContext(ctxName);
    }

    public AggregateContext getRootContext() {
        checkRunning();
        return rootContext;
    }

    public SystemAggregateContext getSystemContext() {
        checkRunning();
        return systemContext;
    }

    public MonitorFactory getMonitorFactory() {
        return monitorFactory;
    }

    public void registerModelObject(Extensible model) throws ConfigurationException {
        assert (model != null) : "Model was null";
        // note do not configure or build model object since the root context will perform a call back
        rootContext.registerModelObject(model);
    }

    public void registerModelObjects(List<Extensible> models) throws ConfigurationException {
        for (Extensible model : models) {
            registerModelObject(model);
        }
    }

    public void registerListener(RuntimeEventListener listener) {
        assert (listener != null) : "Listener cannot be null";
        listeners.add(listener);
    }

    public void fireEvent(int eventType, Object message) throws EventException {
        checkRunning();
        for (RuntimeEventListener listener : listeners) {
            listener.onEvent(eventType, message);
        }
    }

    public AggregateContext getParent() {
        return null; // there is no parent
    }

    public Object locateService(String serviceName) {
        return null;
    }

    public Object locateInstance(String serviceName) {
        return null;
    }

    public Object getInstance(QualifiedName qName) throws TargetException {
        return getSystemContext().getInstance(qName);
    }

    public Object getInstance(QualifiedName qName, boolean notify) throws TargetException {
        return getInstance(qName);
    }

    // ----------------------------------
    // ConfigurationContext methods
    // ----------------------------------

    public synchronized void build(AggregateContext parent, Extensible model) throws BuilderConfigException {
        AssemblyVisitor visitor = new AssemblyVisitor(parent, builders);
        visitor.start(model);
    }

    public void configure(Extensible model) throws ConfigurationException {
    }

    public void wire(ProxyFactory sourceFactory, ProxyFactory targetFactory, Class targetType, boolean downScope,
            ScopeContext targetScopeContext) throws BuilderConfigException {
        wireBuilder.connect(sourceFactory, targetFactory, targetType, downScope, targetScopeContext);
    }

    public void wire(ProxyFactory targetFactory, Class targetType, ScopeContext targetScopeContext) throws BuilderConfigException {
        wireBuilder.completeTargetChain(targetFactory, targetType, targetScopeContext);
    }

    // ----------------------------------
    // AutowireContext methods
    // ----------------------------------

    public <T> T resolveInstance(Class<T> instanceInterface) throws AutowireResolutionException {
        if (MonitorFactory.class.equals(instanceInterface)) {
            return instanceInterface.cast(monitorFactory);
        } else if (ConfigurationContext.class.equals(instanceInterface)) {
            return instanceInterface.cast(this);
        } else if (AutowireContext.class.equals(instanceInterface)) {
            return instanceInterface.cast(this);
        } else if (RuntimeContext.class.equals(instanceInterface)) {
            return instanceInterface.cast(this);
        } else {
            // autowire to system components
            return instanceInterface.cast(getSystemContext().resolveInstance(instanceInterface));
        }
    }

    // ----------------------------------
    // InstanceContext methods
    // ----------------------------------

    public Object getImplementationInstance() throws TargetException {
        return this;
    }

    public Object getImplementationInstance(boolean notify) throws TargetException {
        return this;
    }
    
    public Aggregate getAggregate() {
        return systemContext.getAggregate();
    }

    // ----------------------------------
    // Private methods
    // ----------------------------------

    private void checkRunning() {
        if (lifecycleState != RUNNING) {
            throw new IllegalStateException("Context must be in RUNNING state");
        }
    }
}