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 | 436 +++++++++++++++++++++ 2 files changed, 570 insertions(+) create mode 100644 sandbox/mobile-android/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclaration.java create mode 100644 sandbox/mobile-android/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java (limited to 'sandbox/mobile-android/extensibility/src/main/java') diff --git a/sandbox/mobile-android/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclaration.java b/sandbox/mobile-android/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclaration.java new file mode 100644 index 0000000000..ff7d215d2e --- /dev/null +++ b/sandbox/mobile-android/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 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; + } + + /** + * 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 The Service implementation class corresponding to this declaration + */ + 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/sandbox/mobile-android/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java b/sandbox/mobile-android/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDiscovery.java new file mode 100644 index 0000000000..a7358590de --- /dev/null +++ b/sandbox/mobile-android/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 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( + 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 { + + 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 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 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 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 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 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 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 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; + } + + } + } + +} -- cgit v1.2.3