From bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a Mon Sep 17 00:00:00 2001 From: dims Date: Tue, 17 Jun 2008 00:23:01 +0000 Subject: Move Tuscany from Incubator to top level. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/extensibility/ServiceDeclaration.java | 134 +++++++++++ .../sca/extensibility/ServiceDiscovery.java | 257 +++++++++++++++++++++ 2 files changed, 391 insertions(+) create mode 100644 branches/sca-java-1.2/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclaration.java create mode 100644 branches/sca-java-1.2/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java (limited to 'branches/sca-java-1.2/modules/extensibility/src/main/java/org/apache/tuscany') diff --git a/branches/sca-java-1.2/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclaration.java b/branches/sca-java-1.2/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclaration.java new file mode 100644 index 0000000000..cb8f6ac18d --- /dev/null +++ b/branches/sca-java-1.2/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclaration.java @@ -0,0 +1,134 @@ +/* + * 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.lang.ref.WeakReference; +import java.net.URL; +import java.util.Map; + +/** + * Service declaration using J2SE Jar service provider spec Classes specified + * inside this declaration are loaded using the classloader used to read the + * configuration file corresponding to this declaration. + */ +public class ServiceDeclaration { + + private WeakReference classLoader; + + private String className; + + private Map attributes; + + /** + * Service declaration constructor + * + * @param className Service implementation classname + * @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; + } + + /** + * Load this service implementation class + * + * @return Class + * @throws ClassNotFoundException + */ + @SuppressWarnings("unchecked") + public Class loadClass() throws ClassNotFoundException { + + return Class.forName(className, true, classLoader.get()); + } + + /** + * Load another class using the classloader of this service implementation + * + * @param anotherClassName + * @return Class + * @throws ClassNotFoundException + */ + 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); + } + + /** + * Classloader associated with this service declaration + * + * @return classloader + */ + public ClassLoader getClassLoader() { + return classLoader.get(); + } + + /** + * Service implementation class corresponding to this declaration + * + * @return classname + */ + public String getClassName() { + return className; + } + + /** + * Attributes specified for this declaration + * + * @return attributes + */ + 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); + + } + +} diff --git a/branches/sca-java-1.2/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java b/branches/sca-java-1.2/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java new file mode 100644 index 0000000000..e74eb0a7e3 --- /dev/null +++ b/branches/sca-java-1.2/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java @@ -0,0 +1,257 @@ +/* + * 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.net.URL; +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; + +/** + * Service discovery for Tuscany based on J2SE Jar service provider spec. + * Services are described using configuration files in META-INF/services. + * Service description specifies a classname followed by optional properties. + * Multi-classloader environments are supported through a classloader + * registration API + */ +public class ServiceDiscovery { + private final static Logger logger = Logger.getLogger(ServiceDiscovery.class.getName()); + + private static ServiceDiscovery instance; + + private HashSet registeredClassLoaders; + + /** + * Get an instance of Service discovery, one instance is created per + * classloader that this class is loaded from + * + * @return + */ + public static ServiceDiscovery getInstance() { + + if (instance == null) { + instance = new ServiceDiscovery(); + instance.registeredClassLoaders = new HashSet(); + instance.registeredClassLoaders.add(ServiceDiscovery.class.getClassLoader()); + } + return instance; + } + + /** + * Register a classloader with this discovery mechanism. Tuscany extension + * classloaders are registered here. + * + * @param classLoader + */ + public void registerClassLoader(ClassLoader classLoader) { + registeredClassLoaders.add(classLoader); + } + + /** + * Get all service declarations for this name + * + * @param name + * @return set of service declarations + * @throws IOException + */ + public Set getServiceDeclarations(String name) throws IOException { + + Set classSet = new HashSet(); + + for (ClassLoader classLoader : registeredClassLoaders) { + getServiceClasses(classLoader, name, classSet, true); + } + return classSet; + } + + /** + * Get all service declarations for this interface + * + * @param serviceInterface + * @return set of service declarations + * @throws IOException + */ + public Set getServiceDeclarations(Class serviceInterface) throws IOException { + + return getServiceDeclarations(serviceInterface.getName()); + } + + /** + * Load one service implementation class for this interface + * + * @param serviceInterface + * @return service implementation class + * @throws IOException + * @throws ClassNotFoundException + */ + public Class loadFirstServiceClass(Class serviceInterface) throws IOException, ClassNotFoundException { + + 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 Hashtable> getServiceResources(String name) throws IOException { + + Hashtable> resourceTable = new Hashtable>(); + + HashSet allURLs = new HashSet(); + for (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); + } + for (URL url : Collections.list(classLoader.getResources("META-INF/services/" + name))) { + 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(ClassLoader classLoader, + 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); + } + for (URL url : Collections.list(classLoader.getResources("META-INF/services/" + name))) { + if (debug) { + logger.fine("Reading service provider file: " + url.toExternalForm()); + } + InputStream is = url.openStream(); + 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; + } + } + +} -- cgit v1.2.3