summaryrefslogtreecommitdiffstats
path: root/sandbox/rfeng/runtime/embedded/src/main/java/org/apache/tuscany/runtime/embedded/SimpleRuntimeImpl.java
blob: 990fa0107f8163c75b0d39f0993d1e1201d71985 (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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.runtime.embedded;

import static org.apache.tuscany.runtime.embedded.SimpleRuntimeInfo.DEFAULT_COMPOSITE;

import java.net.URI;
import java.net.URL;
import java.util.Collection;

import org.apache.tuscany.api.annotation.LogLevel;
import org.apache.tuscany.core.component.SimpleWorkContext;
import org.apache.tuscany.core.implementation.PojoWorkContextTunnel;
import org.apache.tuscany.core.implementation.system.model.SystemCompositeImplementation;
import org.apache.tuscany.core.runtime.AbstractRuntime;
import org.apache.tuscany.spi.builder.BuilderException;
import org.apache.tuscany.spi.component.AtomicComponent;
import org.apache.tuscany.spi.component.Component;
import org.apache.tuscany.spi.component.ComponentException;
import org.apache.tuscany.spi.component.SCAObject;
import org.apache.tuscany.spi.component.ScopeContainer;
import org.apache.tuscany.spi.component.ScopeRegistry;
import org.apache.tuscany.spi.component.TargetResolutionException;
import org.apache.tuscany.spi.component.WorkContext;
import org.apache.tuscany.spi.loader.LoaderException;
import org.apache.tuscany.spi.model.ComponentDefinition;
import org.apache.tuscany.spi.model.CompositeImplementation;
import org.apache.tuscany.spi.model.Scope;
import org.apache.tuscany.spi.resolver.ResolutionException;

/**
 * @version $Rev$ $Date$
 */
public class SimpleRuntimeImpl extends AbstractRuntime<SimpleRuntimeInfo> implements SimpleRuntime {
    private ScopeContainer<URI> container;

    public SimpleRuntimeImpl(SimpleRuntimeInfo runtimeInfo) {
        super(SimpleRuntimeInfo.class);
        ClassLoader hostClassLoader = ClassLoader.getSystemClassLoader();
        setHostClassLoader(hostClassLoader);
        setApplicationScdl(runtimeInfo.getApplicationSCDL());
        setSystemScdl(runtimeInfo.getSystemSCDL());
        setRuntimeInfo(runtimeInfo);
    }

    public interface SimpleMonitor {
        @LogLevel("SEVERE")
        void runError(Exception e);
    }

    protected Collection<Component> deployExtension(Component parent,
                                                    URI name,
                                                    URL extensionSCDL,
                                                    ClassLoader systemClassLoader) throws LoaderException,
        BuilderException, ComponentException, ResolutionException {

        SystemCompositeImplementation impl = new SystemCompositeImplementation();
        impl.setScdlLocation(extensionSCDL);
        impl.setClassLoader(systemClassLoader);
        ComponentDefinition<SystemCompositeImplementation> definition 
            = new ComponentDefinition<SystemCompositeImplementation>(name, impl);

        Collection<Component> components = getDeployer().deploy(parent, definition);
        for (Component component : components) {
            component.start();
        }
        return components;
    }

    @SuppressWarnings("unchecked")
    public Component start() throws Exception {
        initialize();

        ScopeRegistry scopeRegistry = getScopeRegistry();
        container = scopeRegistry.getScopeContainer(Scope.COMPOSITE);

        /*
        int i = 0;
        for (URL ext : runtimeInfo.getExtensionSCDLs()) {
            URI uri = URI.create("/extensions/extension" + (i++));
            deployExtension(null, uri, ext, runtimeInfo.getClassLoader());
        }
        */
        
        CompositeImplementation impl = new CompositeImplementation();
        impl.setScdlLocation(applicationScdl);
        impl.setClassLoader(runtimeInfo.getClassLoader());

        ComponentDefinition<CompositeImplementation> definition 
            = new ComponentDefinition<CompositeImplementation>(DEFAULT_COMPOSITE, impl);
        Collection<Component> components = getDeployer().deploy(null, definition);
        for (Component component : components) {
            component.start();
        }
        container.startContext(DEFAULT_COMPOSITE, DEFAULT_COMPOSITE);
        getWorkContext().setIdentifier(Scope.COMPOSITE, DEFAULT_COMPOSITE);
        WorkContext workContext = new SimpleWorkContext();
        workContext.setIdentifier(Scope.COMPOSITE, DEFAULT_COMPOSITE);
        PojoWorkContextTunnel.setThreadWorkContext(workContext);
        return componentManager.getComponent(definition.getUri());
    }

    @SuppressWarnings("deprecation")
    public <T> T getSystemService(Class<T> type, String name) throws TargetResolutionException {
        SCAObject child = componentManager.getComponent(URI.create(name));
        if (child == null) {
            return null;
        }
        AtomicComponent service = (AtomicComponent)child;
        return type.cast(service.getTargetInstance());
    }

    @Override
    public void destroy() {
        container.stopContext(DEFAULT_COMPOSITE);
        getWorkContext().setIdentifier(Scope.COMPOSITE, null);
        super.destroy();
    }

}