summaryrefslogtreecommitdiffstats
path: root/sandbox/rfeng/runtime/embedded/src/main/java/org/apache/tuscany/api/SCARuntime.java
blob: 8fb945a28a660882792600975ec7422e41aeed5d (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
/*
 * 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.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;

import org.osoa.sca.ComponentContext;

/**
 * SCARuntime is used to start a Tuscany SCA runtime.
 */
public abstract class SCARuntime {

    private static SCARuntime instance;

    /**
     * Read the service name from a configuration file
     * 
     * @param classLoader
     * @param name The name of the service class
     * @return A class name which extends/implements the service class
     * @throws IOException
     */
    private static String getServiceName(ClassLoader classLoader, String name) throws IOException {
        InputStream is = classLoader.getResourceAsStream("META-INF/services/" + name);
        if (is == null) {
            return null;
        }
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(is));
            while (true) {
                String line = reader.readLine();
                if (line == null) {
                    break;
                } else if (!line.startsWith("#")) {
                    return line.trim();
                }
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
        return null;
    }

    /**
     * Returns a SCARuntime instance. If the system property
     * "org.apache.tuscany.api.SCARuntime" is set, its value is used as the name
     * of the implementation class. Otherwise, if the resource
     * "META-INF/services/org.apache.tuscany.api.SCARuntime" can be loaded from
     * the supplied classloader. Otherwise, it will use
     * "org.apache.tuscany.runtime.embedded.DefaultSCARuntime" as the default.
     * The named class is loaded from the supplied classloader and instantiated
     * using its default (no-arg) constructor.
     * 
     * @return
     */
    private static SCARuntime newInstance(final ClassLoader classLoader) {

        try {
            final String name = SCARuntime.class.getName();
            String className = AccessController.doPrivileged(new PrivilegedAction<String>() {
                public String run() {
                    return System.getProperty(name);
                }
            });

            if (className == null) {
                className = getServiceName(classLoader, name);
            }
            if (className == null) {
                className = "org.apache.tuscany.runtime.embedded.DefaultSCARuntime";
            }
            Class cls = Class.forName(className, true, classLoader);
            return (SCARuntime)cls.newInstance(); // NOPMD lresende
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    /**
     * Get an instance of SCA Runtime
     * 
     * @return The instance
     */
    public static synchronized SCARuntime getInstance() { // NOPMD
        if (instance != null) {
            return instance;
        }
        ClassLoader classLoader = SCARuntime.class.getClassLoader();
        instance = newInstance(classLoader);
        return instance;
    }

    /**
     * Start the Tuscany runtime using default SCDLs
     */
    public static void start() {
        try {
            getInstance().startup(null, null, null, null);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    /**
     * Start the SCA Runtime with the given SCDLs
     * 
     * @param system The URL for the system SCDL
     * @param extensions An array of URLs for extensions
     * @param application The URL for the application SCDL
     */
    public static void start(URL system, URL[] extensions, URL application, String compositePath) {
        try {
            getInstance().startup(system, extensions, application, compositePath);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    /**
     * Start the SCA Runtime with the given SCDL
     * 
     * @param application The URL for the application SCDL
     */
    public static void start(URL application, String compositePath) {
        try {
            getInstance().startup(null, null, application, compositePath);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    /**
     * Start the SCA Runtime with the given SCDL
     * 
     * @param compositePath The path of the application SCDL
     */
    public static void start(String compositePath) {
        try {
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            URL applicationURL = cl.getResource(compositePath);
            getInstance().startup(null, null, applicationURL, compositePath);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    /**
     * Start the SCA Runtime with the given SCDL
     * 
     * @param compositePath The path of the system SCDL
     * @param compositePath The path of the application SCDL
     */
    public static void start(String system, String compositePath) {
        try {
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            URL systemURL = cl.getResource(system);
            URL applicationURL = cl.getResource(compositePath);
            getInstance().startup(systemURL, null, applicationURL, compositePath);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    /**
     * Get the ComponentContext by name
     * 
     * @param componentName
     * @return
     */
    public static ComponentContext getComponentContext(String componentName) {
        return getInstance().getContext(componentName);
    }

    /**
     * Get access to a system service
     * 
     * @param serviceName
     * @return
     */
    protected abstract Object getSystemService(String serviceName);

    /**
     * Stop the SCA Runtime
     */
    public static void stop() {
        try {
            getInstance().shutdown();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        } finally {
            instance = null;
        }
    }

    /**
     * Look up the ComponentContext by name
     * 
     * @param componentName
     * @return
     */
    protected abstract ComponentContext getContext(String componentName);

    /**
     * Start up the runtime
     * 
     * @param system The URL of the SCDL for tuscany system composite
     * @param extensions The URLs of the SCDLs for tuscany extension composites
     * @param application The URL of the SCDL for the application composite
     * @param compositePath The path of the application composite relative to
     *            the application URL
     * @throws Exception
     */
    protected abstract void startup(URL system, URL[] extensions, URL application, String compositePath)
        throws Exception;

    /**
     * Shutdown the runtime
     * 
     * @throws Exception
     */
    protected abstract void shutdown() throws Exception;
}