From 2e51df13006ebb09b2359c0bb281c7cbb60c813b Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Sat, 6 Sep 2008 15:43:27 +0000 Subject: Minor code cleanup. Added class comments and ref/date tags. Moved test class to src/test/java. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@692666 13f79535-47bb-0310-9956-ffa450edef68 --- .../equinox/BundleFileWrapperFactory.java | 6 +- .../equinox/EquinoxHookConfigurator.java | 8 +- .../equinox/EquinoxServiceDiscoverer.java | 2 + .../equinox/EquinoxServiceDiscoveryActivator.java | 2 + .../extensibility/equinox/URLClassLoadingHook.java | 30 +----- .../extensibility/equinox/EquinoxHostTestCase.java | 96 ++++++++++++++++++ .../equinox/EquinoxServiceDiscovererTestCase.java | 6 +- .../sca/extensibility/equinox/TestEquinoxHost.java | 109 +++++++++++++++++++++ 8 files changed, 228 insertions(+), 31 deletions(-) create mode 100644 java/sca/modules/extensibility-equinox/src/test/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxHostTestCase.java create mode 100644 java/sca/modules/extensibility-equinox/src/test/java/org/apache/tuscany/sca/extensibility/equinox/TestEquinoxHost.java (limited to 'java/sca') diff --git a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/BundleFileWrapperFactory.java b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/BundleFileWrapperFactory.java index 3b369bb54b..ccf2c9b3d6 100644 --- a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/BundleFileWrapperFactory.java +++ b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/BundleFileWrapperFactory.java @@ -28,8 +28,10 @@ import org.eclipse.osgi.baseadaptor.hooks.BundleFileFactoryHook; import org.eclipse.osgi.baseadaptor.hooks.BundleFileWrapperFactoryHook; /** - * Bundle file wrapper factory that converts plain jars into OSGi bundles - */ + * A bundle file wrapper that we can use later to adjust to different bundle schemes. + * + * @version $Rev: $ $Date: $ +*/ public class BundleFileWrapperFactory implements BundleFileWrapperFactoryHook, BundleFileFactoryHook { public BundleFile createBundleFile(Object content, BaseData data, boolean base) throws IOException { diff --git a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxHookConfigurator.java b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxHookConfigurator.java index d5027805c1..d04f1f9a8a 100644 --- a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxHookConfigurator.java +++ b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxHookConfigurator.java @@ -23,11 +23,17 @@ import org.eclipse.osgi.baseadaptor.HookConfigurator; import org.eclipse.osgi.baseadaptor.HookRegistry; /** - * Hook Configurator for Equinox + * Hook Configurator for Equinox. + * + * @version $Rev: $ $Date: $ */ public class EquinoxHookConfigurator implements HookConfigurator { + public void addHooks(HookRegistry registry) { + + // Register our BundleFileWrapperFactory and ClassLoading hooks registry.addBundleFileWrapperFactoryHook(new BundleFileWrapperFactory()); + registry.addClassLoadingHook(new URLClassLoadingHook()); } } diff --git a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java index dd3b3c56e9..a07a6ed810 100644 --- a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java +++ b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java @@ -45,6 +45,8 @@ import org.osgi.framework.BundleContext; /** * The ServiceDiscoverer that find META-INF/services/... in installed bundles + * + * @version $Rev: $ $Date: $ */ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { private static final Logger logger = Logger.getLogger(EquinoxServiceDiscoverer.class.getName()); diff --git a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoveryActivator.java b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoveryActivator.java index 0d00c1c3dd..3454b0b07f 100644 --- a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoveryActivator.java +++ b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoveryActivator.java @@ -25,6 +25,8 @@ import org.osgi.framework.BundleContext; /** * The Bundle Activator that creates the Equinox-based service discoverer + * + * @version $Rev: $ $Date: $ */ public class EquinoxServiceDiscoveryActivator implements BundleActivator { private static BundleContext bundleContext; diff --git a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/URLClassLoadingHook.java b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/URLClassLoadingHook.java index 51c42350b2..e9c1a7bd80 100644 --- a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/URLClassLoadingHook.java +++ b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/URLClassLoadingHook.java @@ -32,67 +32,45 @@ import org.eclipse.osgi.framework.adaptor.BundleProtectionDomain; import org.eclipse.osgi.framework.adaptor.ClassLoaderDelegate; /** - * + * A URLClassLoadingHook that enables Tuscany bundles to load classes from non-OSGi third + * party JARs. + * + * @version $Rev: $ $Date: $ */ public class URLClassLoadingHook implements ClassLoadingHook { - /* (non-Javadoc) - * @see org.eclipse.osgi.baseadaptor.hooks.ClassLoadingHook#addClassPathEntry(java.util.ArrayList, java.lang.String, org.eclipse.osgi.baseadaptor.loader.ClasspathManager, org.eclipse.osgi.baseadaptor.BaseData, java.security.ProtectionDomain) - */ public boolean addClassPathEntry(ArrayList cpEntries, String cp, ClasspathManager hostmanager, BaseData sourcedata, ProtectionDomain sourcedomain) { - // TODO Auto-generated method stub return false; } - /* (non-Javadoc) - * @see org.eclipse.osgi.baseadaptor.hooks.ClassLoadingHook#createClassLoader(java.lang.ClassLoader, org.eclipse.osgi.framework.adaptor.ClassLoaderDelegate, org.eclipse.osgi.framework.adaptor.BundleProtectionDomain, org.eclipse.osgi.baseadaptor.BaseData, java.lang.String[]) - */ public BaseClassLoader createClassLoader(ClassLoader parent, ClassLoaderDelegate delegate, BundleProtectionDomain domain, BaseData data, String[] bundleclasspath) { - // TODO Auto-generated method stub return null; } - /* (non-Javadoc) - * @see org.eclipse.osgi.baseadaptor.hooks.ClassLoadingHook#findLibrary(org.eclipse.osgi.baseadaptor.BaseData, java.lang.String) - */ public String findLibrary(BaseData data, String libName) { - // TODO Auto-generated method stub return null; } - /* (non-Javadoc) - * @see org.eclipse.osgi.baseadaptor.hooks.ClassLoadingHook#getBundleClassLoaderParent() - */ public ClassLoader getBundleClassLoaderParent() { - // TODO Auto-generated method stub return null; } - /* (non-Javadoc) - * @see org.eclipse.osgi.baseadaptor.hooks.ClassLoadingHook#initializedClassLoader(org.eclipse.osgi.baseadaptor.loader.BaseClassLoader, org.eclipse.osgi.baseadaptor.BaseData) - */ public void initializedClassLoader(BaseClassLoader baseClassLoader, BaseData data) { - // TODO Auto-generated method stub - } - /* (non-Javadoc) - * @see org.eclipse.osgi.baseadaptor.hooks.ClassLoadingHook#processClass(java.lang.String, byte[], org.eclipse.osgi.baseadaptor.loader.ClasspathEntry, org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry, org.eclipse.osgi.baseadaptor.loader.ClasspathManager) - */ public byte[] processClass(String name, byte[] classbytes, ClasspathEntry classpathEntry, BundleEntry entry, ClasspathManager manager) { - // TODO Auto-generated method stub return null; } diff --git a/java/sca/modules/extensibility-equinox/src/test/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxHostTestCase.java b/java/sca/modules/extensibility-equinox/src/test/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxHostTestCase.java new file mode 100644 index 0000000000..7602b02488 --- /dev/null +++ b/java/sca/modules/extensibility-equinox/src/test/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxHostTestCase.java @@ -0,0 +1,96 @@ +/* + * 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.extensibility.equinox; + +import java.util.Dictionary; +import java.util.Enumeration; + +import junit.framework.Assert; + +import org.junit.Test; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; + +/** + * Test start/stop the Equinox runtime. + * + * @version $Rev: $ $Date: $ + */ +public class EquinoxHostTestCase { + @Test + public void testStartThenStop() { + TestEquinoxHost host = new TestEquinoxHost(); + BundleContext context = host.start(); + Assert.assertNotNull(context); + for (Bundle b : context.getBundles()) { + System.out.println(toString(b, false)); + } + host.stop(); + } + + @Test + public void testStartTwice() { + TestEquinoxHost host = new TestEquinoxHost(); + host.start(); + try { + host.start(); + Assert.fail(); + } catch (IllegalStateException e) { + Assert.assertTrue(IllegalStateException.class.isInstance(e.getCause())); + } finally { + host.stop(); + } + } + + public static String toString(Bundle b, boolean verbose) { + StringBuffer sb = new StringBuffer(); + sb.append(b.getBundleId()).append(" ").append(b.getSymbolicName()); + int s = b.getState(); + if ((s & Bundle.UNINSTALLED) != 0) { + sb.append(" UNINSTALLED"); + } + if ((s & Bundle.INSTALLED) != 0) { + sb.append(" INSTALLED"); + } + if ((s & Bundle.RESOLVED) != 0) { + sb.append(" RESOLVED"); + } + if ((s & Bundle.STARTING) != 0) { + sb.append(" STARTING"); + } + if ((s & Bundle.STOPPING) != 0) { + sb.append(" STOPPING"); + } + if ((s & Bundle.ACTIVE) != 0) { + sb.append(" ACTIVE"); + } + + sb.append(" ").append(b.getLocation()); + if (verbose) { + Dictionary dict = b.getHeaders(); + Enumeration keys = dict.keys(); + while (keys.hasMoreElements()) { + Object key = keys.nextElement(); + sb.append(" ").append(key).append("=").append(dict.get(key)); + } + } + return sb.toString(); + } +} diff --git a/java/sca/modules/extensibility-equinox/src/test/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscovererTestCase.java b/java/sca/modules/extensibility-equinox/src/test/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscovererTestCase.java index 7d10c39bf9..f4c0056c05 100644 --- a/java/sca/modules/extensibility-equinox/src/test/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscovererTestCase.java +++ b/java/sca/modules/extensibility-equinox/src/test/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscovererTestCase.java @@ -40,12 +40,14 @@ import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; /** + * Test the Equinox service discoverer. * + * @version $Rev: $ $Date: $ */ public class EquinoxServiceDiscovererTestCase { private static EquinoxServiceDiscoverer discoverer; private static Bundle testBundle; - private static EquinoxOSGiHost host; + private static TestEquinoxHost host; private static String getState(Bundle b) { StringBuffer sb = new StringBuffer(); @@ -77,7 +79,7 @@ public class EquinoxServiceDiscovererTestCase { */ @BeforeClass public static void setUpBeforeClass() throws Exception { - host = new EquinoxOSGiHost(); + host = new TestEquinoxHost(); BundleContext context = host.start(); InputStream is = EquinoxServiceDiscovererTestCase.class.getResourceAsStream("/test-bundle.jar"); diff --git a/java/sca/modules/extensibility-equinox/src/test/java/org/apache/tuscany/sca/extensibility/equinox/TestEquinoxHost.java b/java/sca/modules/extensibility-equinox/src/test/java/org/apache/tuscany/sca/extensibility/equinox/TestEquinoxHost.java new file mode 100644 index 0000000000..41202d3d1b --- /dev/null +++ b/java/sca/modules/extensibility-equinox/src/test/java/org/apache/tuscany/sca/extensibility/equinox/TestEquinoxHost.java @@ -0,0 +1,109 @@ +/* + * 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.extensibility.equinox; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.core.runtime.adaptor.EclipseStarter; +import org.eclipse.core.runtime.adaptor.LocationManager; +import org.osgi.framework.BundleContext; + +/** + * A test host that starts/stops Equinox. + * + * @version $Rev: $ $Date: $ + */ +public class TestEquinoxHost { + + private final static String systemPackages = + "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.osgi.util.tracker; version=1.3.2, " + + "javax.xml, " + + "javax.xml.datatype, " + + "javax.xml.namespace, " + + "javax.xml.parsers, " + + "javax.xml.transform, " + + "javax.xml.transform.dom, " + + "javax.xml.transform.sax, " + + "javax.xml.transform.stream, " + + "javax.xml.validation, " + + "javax.xml.xpath, " + // Force the classes to be imported from the system bundle + + "javax.xml.stream, " + + "javax.xml.stream.util, " + + "javax.sql," + + "org.w3c.dom, " + + "org.xml.sax, " + + "org.xml.sax.ext, " + + "org.xml.sax.helpers, " + + "javax.security.auth, " + + "javax.security.cert, " + + "javax.security.auth.login, " + + "javax.security.auth.callback, " + + "javax.naming, " + + "javax.naming.spi, " + + "javax.naming.directory, " + + "javax.management, " + + "javax.imageio, " + + "sun.misc, " + + "javax.net, " + + "javax.net.ssl, " + + "javax.crypto, " + + "javax.rmi, " + + "javax.transaction, " + + "javax.transaction.xa"; + + public BundleContext start() { + try { + return startup(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + + public void stop() { + try { + shutdown(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + + private BundleContext startup() throws Exception { + String args[] = {}; + Map props = new HashMap(); + props.put("org.osgi.framework.system.packages", systemPackages); + props.put(EclipseStarter.PROP_CLEAN, "true"); + props.put(LocationManager.PROP_INSTANCE_AREA, new File("target/workspace").toURI().toString()); + props.put(LocationManager.PROP_INSTALL_AREA, new File("target/eclipse").toURI().toString()); + EclipseStarter.setInitialProperties(props); + BundleContext context = EclipseStarter.startup(args, null); + return context; + } + + private void shutdown() throws Exception { + EclipseStarter.shutdown(); + } + +} -- cgit v1.2.3