diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-07-21 23:52:17 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2008-07-21 23:52:17 +0000 |
commit | ff69891d47964bc66f3288db72dd6923acf17c52 (patch) | |
tree | 437760994ff4617059edba1efeccbdb81d2c1d13 /java/sca/modules/extensibility-eclipse/src | |
parent | fea70a868259d29d62d7cfba7a331149fd3109b2 (diff) |
Add more comments and some minor clean up
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@678600 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/extensibility-eclipse/src')
-rw-r--r-- | java/sca/modules/extensibility-eclipse/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/java/sca/modules/extensibility-eclipse/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java b/java/sca/modules/extensibility-eclipse/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java index 7d10bd8fc5..ffe0407a68 100644 --- a/java/sca/modules/extensibility-eclipse/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java +++ b/java/sca/modules/extensibility-eclipse/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.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,11 @@ import org.osgi.framework.BundleContext; public class EquinoxServiceDiscoverer implements ServiceDiscoverer { private static final Logger logger = Logger.getLogger(EquinoxServiceDiscoverer.class.getName()); private BundleContext context; + private ClassLoader classLoader; public EquinoxServiceDiscoverer(BundleContext context) { this.context = context; + this.classLoader = new ClassLoaderImpl(); } public static class ServiceDeclarationImpl implements ServiceDeclaration { @@ -112,6 +116,72 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { } + public class ClassLoaderImpl extends SecureClassLoader { + + public ClassLoaderImpl() { + super(EquinoxServiceDiscoverer.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); + } + + } + private static String toString(Bundle b) { StringBuffer sb = new StringBuffer(); sb.append(b.getBundleId()).append(" ").append(b.getSymbolicName()); @@ -252,4 +322,12 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { } + /** + * This class loader can be set as the thread context class loader for non-OSGi code + * @return + */ + public ClassLoader getClassLoader() { + return classLoader; + } + } |