From fea70a868259d29d62d7cfba7a331149fd3109b2 Mon Sep 17 00:00:00 2001 From: rfeng Date: Mon, 21 Jul 2008 22:29:57 +0000 Subject: Fix the a set of classloading related issues such as ClassNotFoundException, ClassCastException and VerifyError git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@678589 13f79535-47bb-0310-9956-ffa450edef68 --- .../extensibility/osgi/OSGiServiceDiscoverer.java | 78 ++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'java/sca/modules/extensibility-osgi') diff --git a/java/sca/modules/extensibility-osgi/src/main/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscoverer.java b/java/sca/modules/extensibility-osgi/src/main/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscoverer.java index 1d26d2891a..e90ded0901 100644 --- a/java/sca/modules/extensibility-osgi/src/main/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscoverer.java +++ b/java/sca/modules/extensibility-osgi/src/main/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscoverer.java @@ -28,6 +28,8 @@ import java.security.AccessController; import java.security.PrivilegedAction; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; +import java.security.SecureClassLoader; +import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; @@ -48,9 +50,77 @@ import org.osgi.framework.BundleContext; public class OSGiServiceDiscoverer implements ServiceDiscoverer { private static final Logger logger = Logger.getLogger(OSGiServiceDiscoverer.class.getName()); private BundleContext context; + private ClassLoader classLoader; public OSGiServiceDiscoverer(BundleContext context) { this.context = context; + this.classLoader = new ClassLoaderImpl(); + } + + public class ClassLoaderImpl extends SecureClassLoader { + + public ClassLoaderImpl() { + super(OSGiServiceDiscoverer.class.getClassLoader()); + } + + /** + * Open a back-door to expose the META-INF/services resources + */ + @Override + protected URL findResource(String name) { + int index = name.lastIndexOf('/'); + if (index == -1) { + return null; + } + String path = name.substring(0, index); + if (path.startsWith("/")) { + path = path.substring(1); + } + + if (!path.startsWith("META-INF/services")) { + return null; + } + + for (Bundle bundle : context.getBundles()) { + URL url = bundle.getEntry(name); + if (url != null) { + return url; + } + } + + return null; + } + + /** + * Open a back-door to expose the META-INF/services resources + */ + @Override + protected Enumeration findResources(String name) throws IOException { + int index = name.lastIndexOf('/'); + if (index == -1) { + return null; + } + String path = name.substring(0, index); + String file = name.substring(index + 1); + if (path.startsWith("/")) { + path = path.substring(1); + } + + if (!path.startsWith("META-INF/services")) { + return null; + } + + Set urlSet = new HashSet(); + + for (Bundle bundle : context.getBundles()) { + Enumeration urls = bundle.findEntries(path, file, false); + if (urls != null) { + urlSet.addAll(Collections.list(urls)); + } + } + return Collections.enumeration(urlSet); + } + } public static class ServiceDeclarationImpl implements ServiceDeclaration { @@ -173,6 +243,14 @@ public class OSGiServiceDiscoverer implements ServiceDiscoverer { return attributes; } + /** + * This class loader can be set as the thread context class loader for non-OSGi code + * @return + */ + public ClassLoader getClassLoader() { + return classLoader; + } + @SuppressWarnings("unchecked") public Set discover(String serviceName, boolean firstOnly) { boolean debug = logger.isLoggable(Level.FINE); -- cgit v1.2.3