diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-09-09 22:24:55 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-09-09 22:24:55 +0000 |
commit | 8c7f53c15bf5b2dc64d96e1bee8b1f8cc632d48a (patch) | |
tree | 7ea9c24be52993fc36a414df4a52b1e1c35c985e /java | |
parent | a1bc5c955317ac6ba27cf44d4575dcdd0ffb654a (diff) |
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
Diffstat (limited to '')
11 files changed, 152 insertions, 65 deletions
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<URL> 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<InputStream>() { - 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<InputStream>() { + 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<String, String> 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<String, String> 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<ServiceDeclaration> 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<ServiceDeclaration> 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 Binary files differindex a4dc54170e..afee051f61 100644 --- a/java/sca/modules/extensibility-equinox/src/test/resources/test-bundle.jar +++ b/java/sca/modules/extensibility-equinox/src/test/resources/test-bundle.jar 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">
-
+
<component name="HelloWorld">
- <implementation.java class="hello.HelloWorldImpl"/>
+ <implementation.java class="hello.HelloWorldImpl" />
+ </component>
+ <component name="HelloWorldClient">
+ <implementation.java class="hello.HelloWorldClient" />
+ <reference name="helloWorld" target="HelloWorld"></reference>
</component>
</composite>
|