summaryrefslogtreecommitdiffstats
path: root/java/sca/modules/extensibility-osgi
diff options
context:
space:
mode:
authorrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2008-07-21 22:29:57 +0000
committerrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2008-07-21 22:29:57 +0000
commitfea70a868259d29d62d7cfba7a331149fd3109b2 (patch)
tree2a802e781935b8e0749d3240ff161f576d6d57ba /java/sca/modules/extensibility-osgi
parentf1d04ae9c2fa7e3f55dab43187b5015e17533e51 (diff)
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
Diffstat (limited to 'java/sca/modules/extensibility-osgi')
-rw-r--r--java/sca/modules/extensibility-osgi/src/main/java/org/apache/tuscany/sca/extensibility/osgi/OSGiServiceDiscoverer.java78
1 files changed, 78 insertions, 0 deletions
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<URL> 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<URL> urlSet = new HashSet<URL>();
+
+ for (Bundle bundle : context.getBundles()) {
+ Enumeration<URL> 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<ServiceDeclaration> discover(String serviceName, boolean firstOnly) {
boolean debug = logger.isLoggable(Level.FINE);