From 6c7ad7c110656f47273dc23228b41e46023492f3 Mon Sep 17 00:00:00 2001 From: antelder Date: Sat, 18 Apr 2009 07:36:53 +0000 Subject: Create 1.5 branch git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@766260 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/osgi/runtime/KnopflerfishRuntime.java | 168 +++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 branches/sca-java-1.5/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/KnopflerfishRuntime.java (limited to 'branches/sca-java-1.5/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/KnopflerfishRuntime.java') diff --git a/branches/sca-java-1.5/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/KnopflerfishRuntime.java b/branches/sca-java-1.5/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/KnopflerfishRuntime.java new file mode 100644 index 0000000000..19041daf50 --- /dev/null +++ b/branches/sca-java-1.5/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/KnopflerfishRuntime.java @@ -0,0 +1,168 @@ +/* + * 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.sca.osgi.runtime; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.Hashtable; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleException; + +/** + * Implementation of an OSGi Runtime using Knopflerfish. + * + * @version $Rev$ $Date$ + */ +public class KnopflerfishRuntime extends OSGiRuntime { + + private static BundleContext bundleContext; + + private static KnopflerfishRuntime instance; + + + private static Class frameworkClass; + + private static Object framework; + + public static OSGiRuntime getInstance() throws Exception { + if (instance == null) { + frameworkClass = KnopflerfishRuntime.class.getClassLoader().loadClass("org.knopflerfish.framework.Framework"); + KnopflerfishRuntime runtime = new KnopflerfishRuntime(); + instance = runtime; + } + return instance; + } + + + + // FIXME: Knopflerfish does not expose the methods used for configuration as public methods + // It may be worth using the private methods in org.knopflerfish.framework.Main for + // configuring using reflection if security policies allow it. + // For now, a simple configuration routine reads sca.xargs from the directory in + // the classpath which contains framework.jar. The entries in init.xargs starting with + // -install are assumed to be single-line entries with full bundle location. + // + @Override + protected BundleContext startRuntime(boolean tuscanyRunningInOSGiContainer) throws Exception { + + if (bundleContext != null) + return bundleContext; + + + + System.setProperty("org.knopflerfish.framework.bundlestorage", "memory"); + + Constructor frameworkConstructor = frameworkClass.getConstructor(Object.class); + framework = frameworkConstructor.newInstance(new KnopflerfishRuntime()); + Method launchMethod = frameworkClass.getMethod("launch", long.class); + launchMethod.invoke(framework, 0); + Method getContextMethod = frameworkClass.getMethod("getSystemBundleContext"); + bundleContext = (BundleContext)getContextMethod.invoke(framework); + + System.setProperty("com.gatespace.bundle.cm.store", "knopflerfish.store"); + File xargsFile = null; + String classpath = System.getProperty("java.class.path"); + String[] classpathEntries = classpath.split(System.getProperty("path.separator")); + for (int i = 0; i < classpathEntries.length; i++) { + if (classpathEntries[i].endsWith("framework.jar")) { + String path = classpathEntries[i].substring(0, classpathEntries[i].length() - "framework.jar".length()); + path = path + "sca.xargs"; + xargsFile = new File(path); + if (!xargsFile.exists()) + xargsFile = null; + break; + } + } + if (xargsFile != null) { + BufferedReader reader = new BufferedReader(new FileReader(xargsFile)); + String line; + Hashtable bundles = new Hashtable(); + while ((line = reader.readLine()) != null) { + line = line.trim(); + if (line.startsWith("-install")) { + try { + + String bundleLocation = line.substring("-install".length()).trim(); + Bundle bundle = bundleContext.installBundle(bundleLocation); + bundles.put(bundleLocation, bundle); + + } catch (BundleException e) { + e.printStackTrace(); + } + + } + if (line.startsWith("-start")) { + + try { + String bundleLocation = line.substring("-start".length()).trim(); + Bundle bundle = bundles.get(bundleLocation); + bundle.start(); + } catch (BundleException e) { + e.printStackTrace(); + } + } + } + } + + + return bundleContext; + + } + + @Override + public BundleContext getBundleContext() { + return bundleContext; + } + + @Override + protected void setBundleContext(BundleContext bundleContext) { + super.setBundleContext(bundleContext); + KnopflerfishRuntime.bundleContext = bundleContext; + } + + @Override + public void shutdown() throws Exception { + + if (bundleContext == null) + return; + bundleContext = null; + instance = null; + if (framework != null) { + Method shutdownMethod = frameworkClass.getMethod("shutdown"); + try { + shutdownMethod.invoke(framework); + } catch (Exception e) { + // Ignore errors + } + framework = null; + } + super.shutdown(); + + } + + @Override + public boolean supportsBundleFragments() { + return true; + } +} -- cgit v1.2.3