From 2410d8844182e057fd59ea4139b9a7d61413bbea Mon Sep 17 00:00:00 2001 From: rfeng Date: Tue, 8 Jul 2008 23:10:31 +0000 Subject: Make the ServiceDiscovery pluggable and add support for OSGi-based service discoverer git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@675040 13f79535-47bb-0310-9956-ffa450edef68 --- .../extensibility/ClasspathServiceDiscover.java | 237 ++++++++++++++++++ .../sca/extensibility/ServiceDeclaration.java | 118 ++------- .../sca/extensibility/ServiceDiscoverer.java | 34 +++ .../sca/extensibility/ServiceDiscovery.java | 273 ++++++--------------- .../ClasspathServiceDiscovererTestCase.java | 61 +++++ 5 files changed, 427 insertions(+), 296 deletions(-) create mode 100644 java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscover.java create mode 100644 java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscoverer.java create mode 100644 java/sca/modules/extensibility/src/test/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscovererTestCase.java (limited to 'java/sca/modules/extensibility/src') diff --git a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscover.java b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscover.java new file mode 100644 index 0000000000..c83a7d9172 --- /dev/null +++ b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscover.java @@ -0,0 +1,237 @@ +/* + * 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; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.ref.WeakReference; +import java.net.URL; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.StringTokenizer; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + */ +public class ClasspathServiceDiscover implements ServiceDiscoverer { + + public class ServiceDeclarationImpl implements ServiceDeclaration { + private URL url; + private String className; + private Class javaClass; + private Map attributes; + + public ServiceDeclarationImpl(URL url, String className, Map attributes) { + super(); + this.url = url; + this.className = className; + this.attributes = attributes; + } + + public Map getAttributes() { + return attributes; + } + + public String getClassName() { + return className; + } + + public URL getLocation() { + return url; + } + + public Class loadClass() throws ClassNotFoundException { + if (className == null) { + return null; + } + if (javaClass == null) { + javaClass = loadClass(className); + } + return javaClass; + } + + public Class loadClass(String className) throws ClassNotFoundException { + return getClassLoader().loadClass(className); + } + + private ClasspathServiceDiscover getOuterType() { + return ClasspathServiceDiscover.this; + } + + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("ClassLoader: ").append(getClassLoader()); + sb.append(" Attributes: ").append(attributes); + return sb.toString(); + } + + public URL getResource(final String name) { + return AccessController.doPrivileged(new PrivilegedAction() { + public URL run() { + return getClassLoader().getResource(name); + } + }); + } + + } + + private WeakReference classLoaderReference; + private static final Logger logger = Logger.getLogger(ClasspathServiceDiscover.class.getName()); + + public ClasspathServiceDiscover() { + // ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + ClassLoader classLoader = ClasspathServiceDiscover.class.getClassLoader(); + this.classLoaderReference = new WeakReference(classLoader); + } + + public ClasspathServiceDiscover(ClassLoader classLoader) { + this.classLoaderReference = new WeakReference(classLoader); + } + + protected List getResources(final String name) throws IOException { + try { + return AccessController.doPrivileged(new PrivilegedExceptionAction>() { + public List run() throws IOException { + return Collections.list(getClassLoader().getResources(name)); + } + }); + } catch (PrivilegedActionException e) { + throw (IOException)e.getException(); + } + } + + private ClassLoader getClassLoader() { + return classLoaderReference.get(); + } + + /** + * Parse a service declaration in the form class;attr=value,attr=value and + * return a map of attributes + * + * @param declaration + * @return a map of attributes + */ + protected static Map parseServiceDeclaration(String declaration) { + Map attributes = new HashMap(); + int index = declaration.indexOf(';'); + if (index != -1) { + attributes.put("class", declaration.substring(0, index).trim()); + declaration = declaration.substring(index); + } else { + int j = declaration.indexOf('='); + if (j == -1) { + attributes.put("class", declaration.trim()); + return attributes; + } else { + declaration = ";" + declaration; + } + } + StringTokenizer tokens = new StringTokenizer(declaration); + for (; tokens.hasMoreTokens();) { + String key = tokens.nextToken("=").substring(1).trim(); + if (key == null) + break; + String value = tokens.nextToken(",").substring(1).trim(); + if (value == null) + break; + attributes.put(key, value); + } + return attributes; + } + + public Set discover(String serviceName) { + Set descriptors = new HashSet(); + + String name = "META-INF/services/" + serviceName; + boolean debug = logger.isLoggable(Level.FINE); + try { + for (final URL url : getResources(name)) { + if (debug) { + logger.fine("Reading service provider file: " + url.toExternalForm()); + } + + // 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(url, className, attributes); + descriptors.add(descriptor); + } + } + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + // Ignore + } + } + } + } + } catch (IOException e) { + logger.log(Level.SEVERE, e.getMessage(), e); + } + return descriptors; + + } + +} diff --git a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclaration.java b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclaration.java index 4b25f2adc2..403b6aa7a7 100644 --- a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclaration.java +++ b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclaration.java @@ -19,7 +19,6 @@ package org.apache.tuscany.sca.extensibility; -import java.lang.ref.WeakReference; import java.net.URL; import java.util.Map; @@ -30,107 +29,40 @@ import java.util.Map; * * @version $Rev$ $Date$ */ -public class ServiceDeclaration { - - private WeakReference classLoader; - - private String className; - - private Map attributes; - - /** - * Service declaration constructor - * - * @param className Service implementation class name - * @param classLoader ClassLoader corresponding to this service - * implementation - * @param attributes Optional attributes for this service declaration - */ - public ServiceDeclaration(String className, ClassLoader classLoader, Map attributes) { - - this.className = className; - this.classLoader = new WeakReference(classLoader); - this.attributes = attributes; - } - +public interface ServiceDeclaration { /** - * Load this service implementation class - * - * @return Class + * Load a java class in the same context as the service definition + * @param className The class name + * @return The loaded class * @throws ClassNotFoundException */ - @SuppressWarnings("unchecked") - public Class loadClass() throws ClassNotFoundException { - - return Class.forName(className, true, classLoader.get()); - } - + Class loadClass(String className) throws ClassNotFoundException; /** - * Load another class using the ClassLoader of this service implementation - * - * @param anotherClassName - * @return Class - * @throws ClassNotFoundException + * Get the java class for the service impl + * @return The java class */ - public Class loadClass(String anotherClassName) throws ClassNotFoundException { - - return Class.forName(anotherClassName, true, classLoader.get()); - } - - /** - * Return the resource corresponding to this service implementation class - * - * @return resource URL - */ - public URL getResource() { - return classLoader.get().getResource(className); - } - + Class loadClass() throws ClassNotFoundException; /** - * ClassLoader associated with this service declaration - * - * @return ClassLoader + * Get all attributes (name=value pairs) defined for the given entry + * @return All attributes keyed by name */ - public ClassLoader getClassLoader() { - return classLoader.get(); - } - + Map getAttributes(); + + URL getLocation(); + + String getClassName(); + + URL getResource(String name); + /** - * Service implementation class corresponding to this declaration - * - * @return The Service implementation class corresponding to this declaration + * The service descriptor might be hashed + * @param obj Another object + * @return */ - public String getClassName() { - return className; - } - + boolean equals(Object obj); /** - * Attributes specified for this declaration - * - * @return attributes + * The service descriptor might be hashed + * @return */ - public Map getAttributes() { - return attributes; - } - - /** - * Equals method used to ensure that each service declaration is stored only - * once in a set of declarations. - */ - @Override - public boolean equals(Object o) { - if (!(o instanceof ServiceDeclaration)) - return false; - ServiceDeclaration s = (ServiceDeclaration)o; - if (!className.equals(s.className)) - return false; - else if (!classLoader.equals(s.classLoader)) - return false; - else if (attributes == null) - return s.attributes == null; - else - return attributes.equals(s.attributes); - - } - + int hashCode(); } diff --git a/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscoverer.java b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscoverer.java new file mode 100644 index 0000000000..31f2cf600e --- /dev/null +++ b/java/sca/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscoverer.java @@ -0,0 +1,34 @@ +/* + * 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; + +import java.util.Set; + +/** + * + */ +public interface ServiceDiscoverer { + /** + * Discover the service descriptors by name + * @param serviceName + * @return A set of service descriptors + */ + Set discover(String serviceName); +} 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 45a4494f24..412774c83b 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 @@ -19,23 +19,9 @@ package org.apache.tuscany.sca.extensibility; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.URL; -import java.security.AccessController; -import java.security.PrivilegedActionException; -import java.security.PrivilegedExceptionAction; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; -import java.util.Hashtable; -import java.util.Map; import java.util.Set; -import java.util.StringTokenizer; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -50,9 +36,10 @@ import java.util.logging.Logger; public class ServiceDiscovery { private static final Logger logger = Logger.getLogger(ServiceDiscovery.class.getName()); - private static ServiceDiscovery instance; + private final static ServiceDiscovery instance = new ServiceDiscovery(); - private HashSet registeredClassLoaders; + private static ServiceDiscoverer discoverer; + private Set registeredClassLoaders = new HashSet(); /** * Get an instance of Service discovery, one instance is created per @@ -61,15 +48,29 @@ public class ServiceDiscovery { * @return */ public static ServiceDiscovery getInstance() { - - if (instance == null) { - instance = new ServiceDiscovery(); - instance.registeredClassLoaders = new HashSet(); - instance.registeredClassLoaders.add(ServiceDiscovery.class.getClassLoader()); - } +// +// if (instance == null) { +// instance = new ServiceDiscovery(); +// instance.registeredClassLoaders = new HashSet(); +// instance.registeredClassLoaders.add(ServiceDiscovery.class.getClassLoader()); +// } return instance; } - + + public static ServiceDiscoverer getServiceDiscoverer() { + if (discoverer == null) { + discoverer = new ClasspathServiceDiscover(); + } + return discoverer; + } + + public static void setServiceDiscoverer(ServiceDiscoverer sd) { + if (discoverer != null) { + throw new IllegalStateException("The ServiceDiscoverer cannot be reset"); + } + discoverer = sd; + } + /** * Register a ClassLoader with this discovery mechanism. Tuscany extension * ClassLoaders are registered here. @@ -96,14 +97,10 @@ public class ServiceDiscovery { * @return set of service declarations * @throws IOException */ - public synchronized Set getServiceDeclarations(String name) throws IOException { - - Set classSet = new HashSet(); - - for (ClassLoader classLoader : registeredClassLoaders) { - getServiceClasses(classLoader, name, classSet, true); - } - return classSet; + public Set getServiceDeclarations(String name) throws IOException { + // Set classSet = new HashSet(); + Set services = getServiceDiscoverer().discover(name); + return services; } /** @@ -126,185 +123,55 @@ public class ServiceDiscovery { * @throws IOException * @throws ClassNotFoundException */ - public synchronized Class loadFirstServiceClass(Class serviceInterface) throws IOException, ClassNotFoundException { + public Class loadFirstServiceClass(Class serviceInterface) throws IOException, ClassNotFoundException { + Set services = getServiceDiscoverer().discover(serviceInterface.getName()); + if(services.isEmpty()) { + return null; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - Set classSet = new HashSet(); - for (ClassLoader classLoader : registeredClassLoaders) { - getServiceClasses(classLoader, serviceInterface.getName(), classSet, false); - if (classSet.size() > 0) - break; - } - if (classSet.size() > 0) - return classSet.iterator().next().loadClass(); - else - return null; - } - /** - * Returns a unique list of resource URLs of name META-INF/services/ - * Each URL is associated with the first ClassLoader that it was visible - * from - * - * @param name Name of resource - * @return Table of URLs with associated ClassLoaders - * @throws IOException - */ - public synchronized Hashtable> getServiceResources(final String name) throws IOException { - - Hashtable> resourceTable = new Hashtable>(); - - HashSet allURLs = new HashSet(); - for (final ClassLoader classLoader : registeredClassLoaders) { - HashSet urls = new HashSet(); - resourceTable.put(classLoader, urls); - boolean debug = logger.isLoggable(Level.FINE); - if (debug) { - logger.fine("Discovering service resources using class loader " + classLoader); - } - // Allow privileged access to read META-INF/services/*. Add FilePermission to added to security policy file. - ArrayList urlList; - try { - // FIXME J2 Security - promote this to callers of this method - urlList = AccessController.doPrivileged(new PrivilegedExceptionAction>() { - public ArrayList run() throws IOException { - return Collections.list(classLoader.getResources("META-INF/services/" + name)); - } - }); - } catch (PrivilegedActionException e) { - throw (IOException)e.getException(); - } - - for (URL url : urlList) { - if (allURLs.contains(url)) - continue; - urls.add(url); - } - allURLs.addAll(urls); - } - return resourceTable; - } - /** - * Parse a service declaration in the form class;attr=value,attr=value and - * return a map of attributes - * - * @param declaration - * @return a map of attributes - */ - private Map parseServiceDeclaration(String declaration) { - Map attributes = new HashMap(); - int index = declaration.indexOf(';'); - if (index != -1) { - attributes.put("class", declaration.substring(0, index).trim()); - declaration = declaration.substring(index); - } else { - int j = declaration.indexOf('='); - if (j == -1) { - attributes.put("class", declaration.trim()); - return attributes; - } else { - declaration = ";" + declaration; - } - } - StringTokenizer tokens = new StringTokenizer(declaration); - for (; tokens.hasMoreTokens();) { - String key = tokens.nextToken("=").substring(1).trim(); - if (key == null) - break; - String value = tokens.nextToken(",").substring(1).trim(); - if (value == null) - break; - attributes.put(key, value); - } - return attributes; - } - /** - * Load the service class whose name specified in a configuration file - * - * @param classLoader - * @param name The name of the service class - * @param classSet Populate this set with classes extends/implements the - * service class - * @throws IOException - */ - private void getServiceClasses(final ClassLoader classLoader, - final String name, - Set classSet, - boolean findAllClasses) throws IOException { - - boolean debug = logger.isLoggable(Level.FINE); - if (debug) { - logger.fine("Discovering service providers using class loader " + classLoader); - } - // Allow privileged access to read META-INF/services/*. Add FilePermission to added to - // security policy file. - ArrayList urlList; - try { - urlList = AccessController.doPrivileged(new PrivilegedExceptionAction>() { - public ArrayList run() throws IOException { - return Collections.list(classLoader.getResources("META-INF/services/" + name)); - } - }); - } catch (PrivilegedActionException e) { - throw (IOException)e.getException(); - } - - for (final URL url : urlList) { - if (debug) { - logger.fine("Reading service provider file: " + url.toExternalForm()); - } - - // 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)); - 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"); - ServiceDeclaration serviceClass = new ServiceDeclaration(className, classLoader, attributes); - classSet.add(serviceClass); - - if (!findAllClasses) - break; - } - } - } finally { - if (reader != null) - reader.close(); - if (is != null) { - try { - is.close(); - } catch (IOException ioe) { - } - } - } - if (!findAllClasses && classSet.size() > 0) - break; } + return services.iterator().next().loadClass(); } } diff --git a/java/sca/modules/extensibility/src/test/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscovererTestCase.java b/java/sca/modules/extensibility/src/test/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscovererTestCase.java new file mode 100644 index 0000000000..cf203cce7a --- /dev/null +++ b/java/sca/modules/extensibility/src/test/java/org/apache/tuscany/sca/extensibility/ClasspathServiceDiscovererTestCase.java @@ -0,0 +1,61 @@ +/* + * 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; + +import java.util.Set; + +import junit.framework.Assert; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test Case for ClasspathServiceDiscover + */ +public class ClasspathServiceDiscovererTestCase { + private static ClasspathServiceDiscover discover; + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + discover = new ClasspathServiceDiscover(ClasspathServiceDiscover.class.getClassLoader()); + } + + @Test + public void testDiscovery() { + Set discriptors = + discover.discover("org.apache.tuscany.sca.core.ModuleActivatorExtensionPoint"); + Assert.assertEquals(1, discriptors.size()); + discriptors = + discover.discover("notthere"); + Assert.assertEquals(0, discriptors.size()); + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + +} -- cgit v1.2.3