diff options
Diffstat (limited to 'sca-java-1.x/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org')
5 files changed, 685 insertions, 0 deletions
diff --git a/sca-java-1.x/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/EquinoxRuntime.java b/sca-java-1.x/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/EquinoxRuntime.java new file mode 100644 index 0000000000..b693a9ec4a --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/EquinoxRuntime.java @@ -0,0 +1,106 @@ +/* + * 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.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.osgi.framework.BundleContext; + +public class EquinoxRuntime extends OSGiRuntime { + + + private static BundleContext bundleContext; + + private static EquinoxRuntime instance; + + private static Class eclipseStarterClass; + + public static OSGiRuntime getInstance() throws Exception { + if (instance == null) { + EquinoxRuntime runtime = new EquinoxRuntime(); + runtime.startRuntime(); + instance = runtime; + } + return instance; + } + + + protected BundleContext startRuntime() throws Exception { + + if (bundleContext != null) + return bundleContext; + + eclipseStarterClass = EquinoxRuntime.class.getClassLoader().loadClass("org.eclipse.core.runtime.adaptor.EclipseStarter"); + Method startupMethod = eclipseStarterClass.getMethod("startup", String [].class, Runnable.class); + + // Equinox version 3.2 upwards have a startup method which returns BundleContext + if (startupMethod.getReturnType() == BundleContext.class) { + bundleContext = (BundleContext) startupMethod.invoke(null, new String[] {"-clean", "-console"}, null ); + } + else { + + // Older versions of Equinox dont have a public method to obtain system bundlecontext + // Extract bundleContext from the private field 'context'. We are assuming that + // there is no access restriction + Method mainMethod = eclipseStarterClass.getMethod("main", String [].class); + mainMethod.invoke(null, (Object)new String[] {"-clean", "-console"}); + + Field contextField = eclipseStarterClass.getDeclaredField("context"); + contextField.setAccessible(true); + bundleContext = (BundleContext) contextField.get(null); + + } + + + return bundleContext; + + } + + @Override + public BundleContext getBundleContext() { + return bundleContext; + } + + @Override + public void shutdown() throws Exception { + + if (bundleContext == null) + return; + bundleContext = null; + instance = null; + if (eclipseStarterClass != null) { + Method shutdownMethod = eclipseStarterClass.getMethod("shutdown"); + try { + shutdownMethod.invoke(eclipseStarterClass); + } catch (Exception e) { + // Ignore errors. + } + } + } + + + @Override + public boolean supportsBundleFragments() { + return false; + } + + + +} diff --git a/sca-java-1.x/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/FelixRuntime.java b/sca-java-1.x/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/FelixRuntime.java new file mode 100644 index 0000000000..529f796310 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/FelixRuntime.java @@ -0,0 +1,193 @@ +/* + * 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.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; + +public class FelixRuntime extends OSGiRuntime implements BundleActivator { + + private static BundleContext bundleContext; + + private static FelixRuntime instance; + + private static Class felixClass; + private static Object felix; + + public static OSGiRuntime getInstance() throws Exception { + if (instance == null) { + FelixRuntime runtime = new FelixRuntime(); + runtime.startRuntime(); + instance = runtime; + } + return instance; + } + + + private static void deleteDirectory(File dir) { + + File[] files = dir.listFiles(); + for (int i = 0; i < files.length; i++) { + if (files[i].isDirectory()) + deleteDirectory(files[i]); + else + files[i].delete(); + } + dir.delete(); + + } + + private BundleContext startRuntime() throws Exception { + + if (bundleContext != null) + return bundleContext; + + + ClassLoader cl = FelixRuntime.class.getClassLoader(); + + Class felixMainClass = cl.loadClass("org.apache.felix.main.Main"); + felixClass = cl.loadClass("org.apache.felix.framework.Felix"); + Method propsMethod = felixMainClass.getMethod("loadConfigProperties"); + Properties props = (Properties)propsMethod.invoke(null); + + File profileDir = new File(".felix"); + if (profileDir.isDirectory()) + deleteDirectory(profileDir); + else + profileDir.delete(); + profileDir.mkdir(); + profileDir.deleteOnExit(); + + props.put("felix.cache.profiledir", profileDir.getAbsolutePath()); + props.put("felix.embedded.execution", "true"); + props.put("org.osgi.framework.system.packages", + "org.osgi.framework; version=1.3.0," + + "org.osgi.service.packageadmin; version=1.2.0, " + + "org.osgi.service.startlevel; version=1.0.0, " + + "org.osgi.service.url; version=1.0.0, " + + "org.osoa.sca.annotations; version=1.0.0, " + + "org.osoa.sca; version=1.0.0"); + + + try { + Constructor felixConstructor = felixClass.getConstructor(Map.class, List.class); + List<BundleActivator> activators = new ArrayList<BundleActivator>(); + felix = felixConstructor.newInstance(props, activators); + ((Bundle)felix).start(); + bundleContext = ((Bundle)felix).getBundleContext(); + + + } catch (Exception e) { + + // This is the older Felix API which has been retained temporarily to avoid build break + // TODO: Remove these once Felix 1.0.0 is released. + + Class propertyResolverClass = cl.loadClass("org.apache.felix.framework.util.MutablePropertyResolver"); + Class propertyResolverImplClass = cl.loadClass("org.apache.felix.framework.util.MutablePropertyResolverImpl"); + + Constructor implConstructor = propertyResolverImplClass.getConstructor(Map.class); + Object mutableProps = implConstructor.newInstance(props); + + + try { + Constructor felixConstructor = felixClass.getConstructor(propertyResolverClass, List.class); + List<BundleActivator> activators = new ArrayList<BundleActivator>(); + felix = felixConstructor.newInstance(mutableProps, activators); + ((Bundle)felix).start(); + bundleContext = ((Bundle)felix).getBundleContext(); + } catch (Exception e1) { + + + felix = felixClass.newInstance(); + Method startMethod = felixClass.getMethod("start", propertyResolverClass, List.class); + List<BundleActivator> activators = new ArrayList<BundleActivator>(); + BundleActivator activator = new FelixRuntime(); + activators.add(activator); + startMethod.invoke(felix, mutableProps, activators); + + synchronized (activator) { + int retries = 0; + while (bundleContext == null && retries++ < 10) { + activator.wait(1000); + } + } + } + } + + return bundleContext; + + } + + public void start(BundleContext context) throws Exception { + + bundleContext = context; + synchronized (this) { + this.notify(); + } + } + + public void stop(BundleContext context) throws Exception { + bundleContext = null; + } + + + + @Override + public BundleContext getBundleContext() { + return bundleContext; + } + + + @Override + public void shutdown() throws Exception { + + if (bundleContext == null) + return; + bundleContext = null; + instance = null; + + if (felix instanceof Bundle) { + ((Bundle)felix).stop(); + } + else if (felix != null) { + Method shutdownMethod = felixClass.getMethod("shutdown"); + try { + shutdownMethod.invoke(felix); + } catch (Exception e) { + // Ignore errors + } + felix = null; + } + } + + @Override + public boolean supportsBundleFragments() { + return false; + } + +} diff --git a/sca-java-1.x/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/KnopflerfishRuntime.java b/sca-java-1.x/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/KnopflerfishRuntime.java new file mode 100644 index 0000000000..a553c00cc0 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/KnopflerfishRuntime.java @@ -0,0 +1,156 @@ +/* + * 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; + +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) { + KnopflerfishRuntime runtime = new KnopflerfishRuntime(); + runtime.startRuntime(); + 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. + // + private BundleContext startRuntime() throws Exception { + + if (bundleContext != null) + return bundleContext; + + + + System.setProperty("org.knopflerfish.framework.bundlestorage", "memory"); + + frameworkClass = KnopflerfishRuntime.class.getClassLoader().loadClass("org.knopflerfish.framework.Framework"); + 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<String, Bundle> bundles = new Hashtable<String, Bundle>(); + 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 + 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; + } + + } + + @Override + public boolean supportsBundleFragments() { + return true; + } +} diff --git a/sca-java-1.x/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntime.java b/sca-java-1.x/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntime.java new file mode 100644 index 0000000000..773200cc46 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntime.java @@ -0,0 +1,183 @@ +/* + * 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.InputStream; +import java.lang.reflect.Method; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleException; +import org.osgi.service.packageadmin.PackageAdmin; + +public abstract class OSGiRuntime { + private static final Logger logger = Logger.getLogger(OSGiRuntime.class.getName()); + private BundleContext bundleContext; + + public abstract BundleContext getBundleContext(); + + protected abstract void shutdown() throws Exception; + + public abstract boolean supportsBundleFragments(); + + private PackageAdmin packageAdmin; + + private static OSGiRuntime instance; + + /** + * System property org.apache.tuscany.implementation.osgi.runtime.OSGiRuntime can be set to the + * name of the OSGiRuntime class (eg. EquinoxRuntime). If set, start this runtime and return the + * system bundlecontext. If not set, start Equinox/Felix/Knopflerfish (in that order) from the + * classpath. + * + * @throws BundleException + */ + public synchronized static OSGiRuntime getRuntime() throws Exception { + if (instance != null) { + return instance; + } + String runtimeClassName = System.getProperty(OSGiRuntime.class.getName()); + + if (runtimeClassName != null) { + try { + Class<?> runtimeClass = OSGiRuntime.class.getClassLoader().loadClass(runtimeClassName); + Method method = runtimeClass.getMethod("getInstance"); + instance = (OSGiRuntime)method.invoke(null); + return instance; + + } catch (Exception e) { + throw new BundleException("Could not start OSGi runtime " + runtimeClassName, e); + } + } + + try { + instance = EquinoxRuntime.getInstance(); + return instance; + + } catch (ClassNotFoundException e) { + // Ignore + } catch (Throwable e) { + logger.log(Level.SEVERE, e.getMessage(), e); + } + + try { + instance = FelixRuntime.getInstance(); + return instance; + } catch (ClassNotFoundException e) { + // Ignore + } catch (Throwable e) { + logger.log(Level.SEVERE, e.getMessage(), e); + } + + try { + instance = KnopflerfishRuntime.getInstance(); + return instance; + } catch (ClassNotFoundException e) { + // Ignore + } catch (Throwable e) { + logger.log(Level.SEVERE, e.getMessage(), e); + } + + throw new BundleException("Could not start OSGi runtime from the classpath"); + } + + private void initialize() { + if (bundleContext == null) + bundleContext = getBundleContext(); + + if (bundleContext != null) { + + org.osgi.framework.ServiceReference packageAdminReference = + bundleContext.getServiceReference("org.osgi.service.packageadmin.PackageAdmin"); + if (packageAdminReference != null) { + + packageAdmin = (PackageAdmin)bundleContext.getService(packageAdminReference); + } + } + + } + + public Bundle findBundle(String bundleSymbolicName, String bundleVersion) { + + initialize(); + + if (bundleContext != null) { + Bundle[] installedBundles = bundleContext.getBundles(); + for (Bundle bundle : installedBundles) { + if (bundleSymbolicName.equals(bundle.getSymbolicName()) && (bundleVersion == null || bundleVersion + .equals(bundle.getHeaders().get("Bundle-Version")))) + return bundle; + } + + } + return null; + } + + public Bundle findBundle(String bundleLocation) { + + initialize(); + + if (bundleContext != null) { + Bundle[] installedBundles = bundleContext.getBundles(); + for (Bundle bundle : installedBundles) { + if (bundle.getLocation().equals(bundleLocation)) + return bundle; + } + + } + return null; + } + + public Bundle installBundle(String bundleLocation, InputStream inputStream) { + + initialize(); + + try { + if (bundleContext != null) { + Bundle bundle = findBundle(bundleLocation); + if (bundle != null) + return bundle; + if (inputStream == null) + bundle = bundleContext.installBundle(bundleLocation); + else + bundle = bundleContext.installBundle(bundleLocation, inputStream); + + if (bundle != null && packageAdmin != null) + packageAdmin.refreshPackages(null); + + return bundle; + } + } catch (BundleException e) { + } + return null; + } + + /** + * @return the instance + */ + public synchronized static void stop() throws Exception { + if (instance != null) { + instance.shutdown(); + instance = null; + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java b/sca-java-1.x/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java new file mode 100644 index 0000000000..3f2064b621 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java @@ -0,0 +1,47 @@ +/* + * 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 org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.ModuleActivator; + +/** + * @version $Rev$ $Date$ + */ +public class OSGiRuntimeModuleActivator implements ModuleActivator { + + /** + * @see org.apache.tuscany.sca.core.ModuleActivator#start(org.apache.tuscany.sca.core.ExtensionPointRegistry) + */ + public void start(ExtensionPointRegistry registry) { + } + + /** + * @see org.apache.tuscany.sca.core.ModuleActivator#stop(org.apache.tuscany.sca.core.ExtensionPointRegistry) + */ + public void stop(ExtensionPointRegistry registry) { + try { + OSGiRuntime.stop(); + } catch (Exception e) { + // Ignore the exception + } + } + +} |