diff options
Diffstat (limited to 'branches/sca-android-r643746/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java')
-rw-r--r-- | branches/sca-android-r643746/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java | 436 |
1 files changed, 436 insertions, 0 deletions
diff --git a/branches/sca-android-r643746/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java b/branches/sca-android-r643746/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java new file mode 100644 index 0000000000..a7358590de --- /dev/null +++ b/branches/sca-android-r643746/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java @@ -0,0 +1,436 @@ +/*
+ * 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.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;
+
+/**
+ * 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 class name 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<ClassLoader> 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<ClassLoader>();
+ 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<ServiceDeclaration> getServiceDeclarations(String name)
+ throws IOException {
+
+ Set<ServiceDeclaration> classSet = new HashSet<ServiceDeclaration>();
+
+ 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<ServiceDeclaration> 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<ServiceDeclaration> classSet = new HashSet<ServiceDeclaration>();
+
+ 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/<name>
+ * 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<ClassLoader, Set<URL>> getServiceResources(
+ final String name) throws IOException {
+
+ Hashtable<ClassLoader, Set<URL>> resourceTable = new Hashtable<ClassLoader, Set<URL>>();
+
+ HashSet<URL> allURLs = new HashSet<URL>();
+ for (final ClassLoader classLoader : registeredClassLoaders) {
+ HashSet<URL> urls = new HashSet<URL>();
+ 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<URL> urlList;
+ try {
+ // FIXME J2 Security - promote this to callers of this method
+ urlList = AccessController
+ .doPrivileged(new PrivilegedExceptionAction<ArrayList<URL>>() {
+ public ArrayList<URL> 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<String, String> parseServiceDeclaration(String declaration) {
+ Map<String, String> attributes = new HashMap<String, String>();
+ 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<ServiceDeclaration> classSet,
+ boolean findAllClasses) throws IOException {
+
+ if ("org.apache.tuscany.sca.contribution.processor.PackageProcessor"
+ .equals(name)) {
+
+ for (String reg : new String[] {
+ "org.apache.tuscany.sca.contribution.processor.impl.FolderContributionProcessor;type=application/vnd.tuscany.folder",
+ "org.apache.tuscany.sca.contribution.processor.impl.JarContributionProcessor;type=application/x-compressed",
+ "org.apache.tuscany.sca.contribution.processor.impl.DexContributionProcessor;type=application/x-dex" }) {
+
+ Map<String, String> attributes = parseServiceDeclaration(reg);
+ String className = attributes.get("class");
+ ServiceDeclaration serviceClass = new ServiceDeclaration(
+ className, classLoader, attributes);
+ classSet.add(serviceClass);
+
+ }
+
+ } else if ("org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor"
+ .equals(name)) {
+
+ for (String reg : new String[] {
+ "org.apache.tuscany.sca.implementation.java.xml.JavaImplementationProcessor;qname=http://www.osoa.org/xmlns/sca/1.0#implementation.java,model=org.apache.tuscany.sca.implementation.java.JavaImplementation"
+ }) {
+
+ Map<String, String> attributes = parseServiceDeclaration(reg);
+ String className = attributes.get("class");
+ ServiceDeclaration serviceClass = new ServiceDeclaration(
+ className, classLoader, attributes);
+ classSet.add(serviceClass);
+
+ }
+
+ } else if ("org.apache.tuscany.sca.provider.PolicyProviderFactory"
+ .equals(name)) {
+
+ for (String reg : new String[] {
+ "org.apache.tuscany.sca.policy.logging.jdk.JDKLoggingPolicyProviderFactory;model=org.apache.tuscany.sca.policy.logging.jdk.JDKLoggingPolicy"
+ }) {
+
+ Map<String, String> attributes = parseServiceDeclaration(reg);
+ String className = attributes.get("class");
+ ServiceDeclaration serviceClass = new ServiceDeclaration(
+ className, classLoader, attributes);
+ classSet.add(serviceClass);
+
+ }
+
+ } else if ("org.apache.tuscany.sca.provider.BindingProviderFactory"
+ .equals(name)) {
+
+ for (String reg : new String[] {
+ "org.apache.tuscany.sca.binding.sca.impl.RuntimeSCABindingProviderFactory;model=org.apache.tuscany.sca.assembly.SCABinding"
+
+ }) {
+
+ Map<String, String> attributes = parseServiceDeclaration(reg);
+ String className = attributes.get("class");
+ ServiceDeclaration serviceClass = new ServiceDeclaration(
+ className, classLoader, attributes);
+ classSet.add(serviceClass);
+
+ }
+
+ } else if ("org.apache.tuscany.sca.contribution.resolver.ModelResolver"
+ .equals(name)) {
+
+ for (String reg : new String[] {
+ "org.apache.tuscany.sca.contribution.resource.impl.ArtifactModelResolver;model=org.apache.tuscany.sca.contribution.Artifact",
+ "org.apache.tuscany.sca.assembly.xml.CompositeModelResolver;model=org.apache.tuscany.sca.assembly.Composite",
+ "org.apache.tuscany.sca.assembly.xml.ComponentTypeModelResolver;model=org.apache.tuscany.sca.assembly.ComponentType",
+ "org.apache.tuscany.sca.assembly.xml.ConstrainingTypeModelResolver;model=org.apache.tuscany.sca.assembly.ConstrainingType",
+ "org.apache.tuscany.sca.contribution.java.impl.ClassReferenceModelResolver;model=org.apache.tuscany.sca.contribution.resolver.ClassReference",
+ "org.apache.tuscany.sca.contribution.xml.ContributionModelResolver;model=org.apache.tuscany.sca.contribution.Contribution"
+ /*"org.apache.tuscany.sca.contribution.java.impl.ClassReferenceModelResolver;model=org.apache.tuscany.sca.contribution.resolver.ClassReference",
+ "org.apache.tuscany.sca.contribution.resolver.impl.ResourceReferenceModelResolver;model=org.apache.tuscany.sca.contribution.resolver.ResourceReference",
+ "org.apache.tuscany.sca.assembly.xml.ComponentTypeModelResolver;model=org.apache.tuscany.sca.assembly.ComponentType",
+ "org.apache.tuscany.sca.assembly.xml.ConstrainingTypeModelResolver;model=org.apache.tuscany.sca.assembly.ConstrainingType",
+ "org.apache.tuscany.sca.assembly.xml.CompositeModelResolver;model=org.apache.tuscany.sca.assembly.Composite",
+ "org.apache.tuscany.sca.interfacedef.wsdl.xml.XSDModelResolver;model=org.apache.tuscany.sca.interfacedef.wsdl.XSDefinition",
+ "org.apache.tuscany.sca.interfacedef.wsdl.xml.WSDLModelResolver;model=org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition",
+ "org.apache.tuscany.sca.contribution.osgi.impl.OSGiBundleReferenceModelResolver;model=org.apache.tuscany.sca.contribution.osgi.BundleReference"*/
+ }) {
+
+ Map<String, String> attributes = parseServiceDeclaration(reg);
+ String className = attributes.get("class");
+ ServiceDeclaration serviceClass = new ServiceDeclaration(
+ className, classLoader, attributes);
+ classSet.add(serviceClass);
+
+ }
+
+ } else if ("org.apache.tuscany.sca.core.ModuleActivator"
+ .equals(name)) {
+
+ for (String reg : new String[] {
+ "org.apache.tuscany.sca.core.databinding.module.DataBindingModuleActivator",
+ "org.apache.tuscany.sca.implementation.java.module.JavaRuntimeModuleActivator"
+ /*
+ * "org.apache.tuscany.sca.contribution.java.impl.ClassReferenceModelResolver;model=org.apache.tuscany.sca.contribution.resolver.ClassReference",
+ * "org.apache.tuscany.sca.contribution.resolver.impl.ResourceReferenceModelResolver;model=org.apache.tuscany.sca.contribution.resolver.ResourceReference",
+ * "org.apache.tuscany.sca.assembly.xml.ComponentTypeModelResolver;model=org.apache.tuscany.sca.assembly.ComponentType",
+ * "org.apache.tuscany.sca.assembly.xml.ConstrainingTypeModelResolver;model=org.apache.tuscany.sca.assembly.ConstrainingType",
+ * "org.apache.tuscany.sca.assembly.xml.CompositeModelResolver;model=org.apache.tuscany.sca.assembly.Composite",
+ * "org.apache.tuscany.sca.interfacedef.wsdl.xml.XSDModelResolver;model=org.apache.tuscany.sca.interfacedef.wsdl.XSDefinition",
+ * "org.apache.tuscany.sca.interfacedef.wsdl.xml.WSDLModelResolver;model=org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition",
+ * "org.apache.tuscany.sca.contribution.osgi.impl.OSGiBundleReferenceModelResolver;model=org.apache.tuscany.sca.contribution.osgi.BundleReference"
+ */
+ }) {
+
+ Map<String, String> attributes = parseServiceDeclaration(reg);
+ String className = attributes.get("class");
+ ServiceDeclaration serviceClass = new ServiceDeclaration(
+ className, classLoader, attributes);
+ classSet.add(serviceClass);
+
+ }
+
+ } else {
+ 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<URL> urlList;
+ try {
+ urlList = AccessController
+ .doPrivileged(new PrivilegedExceptionAction<ArrayList<URL>>() {
+ public ArrayList<URL> 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<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));
+ 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");
+ 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;
+ }
+
+ }
+ }
+
+}
|