diff options
author | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
---|---|---|
committer | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
commit | bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a (patch) | |
tree | 38a92061c0793434c4be189f1d70c3458b6bc41d /sandbox/rfeng/runtime/embedded/src/main/java/org/apache/tuscany/api/SCARuntime.java |
Move Tuscany from Incubator to top level.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sandbox/rfeng/runtime/embedded/src/main/java/org/apache/tuscany/api/SCARuntime.java')
-rw-r--r-- | sandbox/rfeng/runtime/embedded/src/main/java/org/apache/tuscany/api/SCARuntime.java | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/sandbox/rfeng/runtime/embedded/src/main/java/org/apache/tuscany/api/SCARuntime.java b/sandbox/rfeng/runtime/embedded/src/main/java/org/apache/tuscany/api/SCARuntime.java new file mode 100644 index 0000000000..8fb945a28a --- /dev/null +++ b/sandbox/rfeng/runtime/embedded/src/main/java/org/apache/tuscany/api/SCARuntime.java @@ -0,0 +1,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; +} |