From 8c7f53c15bf5b2dc64d96e1bee8b1f8cc632d48a Mon Sep 17 00:00:00 2001 From: rfeng Date: Tue, 9 Sep 2008 22:24:55 +0000 Subject: Use bundle.getResources to discover services Run the test case within OSGi git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@693637 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/impl/ClassReferenceModelResolver.java | 2 +- .../equinox/EquinoxServiceDiscoverer.java | 126 +++++++++++++-------- .../equinox/EquinoxServiceDiscovererTestCase.java | 14 ++- .../src/test/resources/test-bundle.jar | Bin 11920 -> 1797 bytes .../sca/extensibility/ServiceDiscovery.java | 2 +- .../sca/node/equinox/launcher/EquinoxHost.java | 5 +- .../node/equinox/launcher/NodeLauncherUtil.java | 1 + .../src/test/java/hello/HelloWorldClient.java | 50 ++++++++ .../src/test/java/hello/HelloWorldImpl.java | 2 +- .../equinox/launcher/NodeLauncherTestCase.java | 7 -- .../src/test/resources/HelloWorld.composite | 8 +- 11 files changed, 152 insertions(+), 65 deletions(-) create mode 100644 java/sca/modules/node-launcher-equinox/src/test/java/hello/HelloWorldClient.java (limited to 'java') diff --git a/java/sca/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java b/java/sca/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java index b2bcdb7f54..962512a745 100644 --- a/java/sca/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java +++ b/java/sca/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java @@ -62,7 +62,7 @@ public class ClassReferenceModelResolver implements ModelResolver { // return Thread.currentThread().getContextClassLoader(); // } //}); - ClassLoader contextClassLoader = ServiceDiscovery.class.getClassLoader(); + ClassLoader contextClassLoader = ServiceDiscovery.getInstance().getServiceDiscoverer().getClass().getClassLoader(); cl = new ContributionClassLoader(contribution, contextClassLoader); contribution.setClassLoader(cl); } 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 19e212f355..fcc7f21483 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 @@ -28,6 +28,9 @@ import java.security.AccessController; import java.security.PrivilegedAction; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; +import java.util.Arrays; +import java.util.Collections; +import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -44,7 +47,7 @@ import org.osgi.framework.BundleContext; /** * A ServiceDiscoverer that find META-INF/services/... in installed bundles * - * @version $Rev: $ $Date: $ + * @version $Rev$ $Date$ */ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { private static final Logger logger = Logger.getLogger(EquinoxServiceDiscoverer.class.getName()); @@ -182,67 +185,90 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { serviceName = "META-INF/services/" + serviceName; for (Bundle bundle : context.getBundles()) { - final URL url = bundle.getEntry(serviceName); - if (url == null) { + if (bundle.getBundleId() == 0) { + // Skip system bundle as it has access to the application classloader continue; } - if (debug) { - logger.fine("Reading service provider file: " + url.toExternalForm()); - } + Enumeration urls = null; try { - // Allow privileged access to open URL stream. Add FilePermission to added to security - // policy file. - InputStream is; - try { - is = AccessController.doPrivileged(new PrivilegedExceptionAction() { - public InputStream run() throws IOException { - return url.openStream(); - } - }); - } catch (PrivilegedActionException e) { - throw (IOException)e.getException(); + // Use getResources to find resources on the classpath of the bundle + // Please note if there is an DynamicImport-Package=*, and another bundle + // exports the resource package, there is a possiblity that it doesn't + // find the containing entry + urls = bundle.getResources(serviceName); + if (urls == null) { + URL entry = bundle.getEntry(serviceName); + if (entry != null) { + urls = Collections.enumeration(Arrays.asList(entry)); + } + } + } catch (IOException e) { + logger.log(Level.SEVERE, e.getMessage(), e); + } + if (urls == null) { + continue; + } + while (urls.hasMoreElements()) { + final URL url = urls.nextElement(); + + if (debug) { + logger.fine("Reading service provider file: " + url.toExternalForm()); } - BufferedReader reader = null; try { - reader = new BufferedReader(new InputStreamReader(is)); - int count = 0; - while (true) { - String line = reader.readLine(); - if (line == null) - break; - line = line.trim(); - if (!line.startsWith("#") && !"".equals(line)) { - String reg = line.trim(); - if (debug) { - logger.fine("Registering service provider: " + reg); + // Allow privileged access to open URL stream. Add FilePermission to added to security + // policy file. + InputStream is; + try { + is = AccessController.doPrivileged(new PrivilegedExceptionAction() { + public InputStream run() throws IOException { + return url.openStream(); } + }); + } catch (PrivilegedActionException e) { + throw (IOException)e.getException(); + } + BufferedReader reader = null; + try { + reader = new BufferedReader(new InputStreamReader(is)); + int count = 0; + while (true) { + String line = reader.readLine(); + if (line == null) + break; + line = line.trim(); + if (!line.startsWith("#") && !"".equals(line)) { + String reg = line.trim(); + if (debug) { + logger.fine("Registering service provider: " + reg); + } - Map attributes = parseServiceDeclaration(reg); - String className = attributes.get("class"); - if (className == null) { - // Add a unique class name to prevent equals() from returning true - className = "_class_" + count; - count++; - } - ServiceDeclarationImpl descriptor = - new ServiceDeclarationImpl(bundle, url, className, attributes); - descriptors.add(descriptor); - if (firstOnly) { - return descriptors; + Map attributes = parseServiceDeclaration(reg); + String className = attributes.get("class"); + if (className == null) { + // Add a unique class name to prevent equals() from returning true + className = "_class_" + count; + count++; + } + ServiceDeclarationImpl descriptor = + new ServiceDeclarationImpl(bundle, url, className, attributes); + descriptors.add(descriptor); + if (firstOnly) { + return descriptors; + } } } - } - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - // Ignore + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + // Ignore + } } } + } catch (IOException e) { + logger.log(Level.SEVERE, e.getMessage(), e); } - } catch (IOException e) { - logger.log(Level.SEVERE, e.getMessage(), e); } } return descriptors; 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 f4c0056c05..3126365748 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 @@ -42,7 +42,7 @@ import org.osgi.framework.BundleContext; /** * Test the Equinox service discoverer. * - * @version $Rev: $ $Date: $ + * @version $Rev$ $Date$ */ public class EquinoxServiceDiscovererTestCase { private static EquinoxServiceDiscoverer discoverer; @@ -133,10 +133,20 @@ public class EquinoxServiceDiscovererTestCase { @Test public void testDiscovery() { Set descriptors = - discoverer.discover("org.apache.tuscany.sca.endpointresolver.EndpointResolverFactory", false); + discoverer.discover("test.TestService", false); Assert.assertEquals(1, descriptors.size()); descriptors = discoverer.discover("notthere", false); Assert.assertEquals(0, descriptors.size()); } + @Test + public void testDiscoveryFirst() { + Set descriptors = + discoverer.discover("test.TestService", true); + Assert.assertEquals(1, descriptors.size()); + descriptors = discoverer.discover("notthere", true); + Assert.assertEquals(0, descriptors.size()); + } + + } diff --git a/java/sca/modules/extensibility-equinox/src/test/resources/test-bundle.jar b/java/sca/modules/extensibility-equinox/src/test/resources/test-bundle.jar index a4dc54170e..afee051f61 100644 Binary files a/java/sca/modules/extensibility-equinox/src/test/resources/test-bundle.jar and b/java/sca/modules/extensibility-equinox/src/test/resources/test-bundle.jar differ diff --git a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java index c1bbfeb8e0..ce41a44068 100644 --- a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java +++ b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java @@ -55,7 +55,7 @@ public class ServiceDiscovery { return INSTANCE; } - private ServiceDiscoverer getServiceDiscoverer() { + public ServiceDiscoverer getServiceDiscoverer() { if (discoverer == null) { discoverer = new ContextClassLoaderServiceDiscoverer(); } diff --git a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxHost.java b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxHost.java index 2a6e24f6e0..06bc1907b4 100644 --- a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxHost.java +++ b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxHost.java @@ -100,7 +100,7 @@ public class EquinoxHost { props.put("osgi.contextClassLoaderParent", "boot"); // Set the extension bundle - props.put("osgi.framework.extensions", "org.apache.tuscany.sca.extensibility.equinox"); + props.put("osgi.framework.extensions", "org.apache.tuscany.sca.node.launcher.equinox"); // Set startup properties props.put(EclipseStarter.PROP_CLEAN, "true"); @@ -205,6 +205,9 @@ public class EquinoxHost { * @throws IOException */ private static String getBundleName(File file) throws IOException { + if (!file.exists()) { + return null; + } String bundleName = null; if (file.isDirectory()) { File mf = new File(file, "META-INF/MANIFEST.MF"); diff --git a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherUtil.java b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherUtil.java index 07da404d12..b422651244 100644 --- a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherUtil.java +++ b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherUtil.java @@ -250,6 +250,7 @@ final class NodeLauncherUtil { if (!entry.isDirectory() && entryName != null && entryName.length() > 0 && !entryName.startsWith(".") + && entryName.endsWith(".class") // Exclude resources from Export-Package && entryName.lastIndexOf("/") > 0) { String pkg = entryName.substring(0, entryName.lastIndexOf("/")).replace('/', '.') + version; packages.add(pkg); diff --git a/java/sca/modules/node-launcher-equinox/src/test/java/hello/HelloWorldClient.java b/java/sca/modules/node-launcher-equinox/src/test/java/hello/HelloWorldClient.java new file mode 100644 index 0000000000..11e008619c --- /dev/null +++ b/java/sca/modules/node-launcher-equinox/src/test/java/hello/HelloWorldClient.java @@ -0,0 +1,50 @@ +/* + * 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 hello; + +import org.osoa.sca.annotations.EagerInit; +import org.osoa.sca.annotations.Init; +import org.osoa.sca.annotations.Reference; +import org.osoa.sca.annotations.Scope; + +/** + * This client program shows how to create an SCA runtime, start it, + * and locate and invoke a SCA component + */ +@Scope("COMPOSITE") +@EagerInit +public class HelloWorldClient { + + private HelloWorld hw; + + @Reference + public void setHelloWorld(HelloWorld hw) { + this.hw = hw; + } + + @Init + public void hello() { + // Say hello + System.out.println("Contribution ClassLoader: " + getClass().getClassLoader()); + System.out.println("SCA API ClassLoader: " + Reference.class.getClassLoader()); + System.out.println(hw.hello("Equinox")); + } + +} diff --git a/java/sca/modules/node-launcher-equinox/src/test/java/hello/HelloWorldImpl.java b/java/sca/modules/node-launcher-equinox/src/test/java/hello/HelloWorldImpl.java index c9a7560b12..e51d3c79d9 100644 --- a/java/sca/modules/node-launcher-equinox/src/test/java/hello/HelloWorldImpl.java +++ b/java/sca/modules/node-launcher-equinox/src/test/java/hello/HelloWorldImpl.java @@ -24,7 +24,7 @@ package hello; */ public class HelloWorldImpl implements HelloWorld { public String hello(String name) { - System.out.println("Hello: " + name); + System.out.println("Name: " + name); return "Hello, " + name; } } diff --git a/java/sca/modules/node-launcher-equinox/src/test/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherTestCase.java b/java/sca/modules/node-launcher-equinox/src/test/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherTestCase.java index 569b82cf4c..b16045a103 100644 --- a/java/sca/modules/node-launcher-equinox/src/test/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherTestCase.java +++ b/java/sca/modules/node-launcher-equinox/src/test/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherTestCase.java @@ -19,9 +19,6 @@ package org.apache.tuscany.sca.node.equinox.launcher; -import hello.HelloWorld; - -import org.apache.tuscany.sca.node.SCAClient; import org.apache.tuscany.sca.node.SCANode; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -55,10 +52,6 @@ public class NodeLauncherTestCase { public void testLaunch() throws Exception { SCANode node = launcher.createNodeFromClassLoader("HelloWorld.composite", getClass().getClassLoader()); node.start(); - - HelloWorld hw = ((SCAClient)node).getService(HelloWorld.class, "HelloWorld"); - hw.hello("OSGi"); - node.stop(); } diff --git a/java/sca/modules/node-launcher-equinox/src/test/resources/HelloWorld.composite b/java/sca/modules/node-launcher-equinox/src/test/resources/HelloWorld.composite index 9e3299d691..0608e70ca5 100644 --- a/java/sca/modules/node-launcher-equinox/src/test/resources/HelloWorld.composite +++ b/java/sca/modules/node-launcher-equinox/src/test/resources/HelloWorld.composite @@ -22,9 +22,13 @@ targetNamespace="http://sample/composite" xmlns:sc="http://sample/composite" name="HelloWorld"> - + - + + + + + -- cgit v1.2.3