From f0fa1a8ac4ec439be118a7f446f2aef3a32dfc9c Mon Sep 17 00:00:00 2001 From: bdaniel Date: Tue, 28 Jun 2011 17:30:44 +0000 Subject: Commit patch for TUSCANY-3887 to add doPrivileged blocks to EquinoxServiceDiscoverer git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1140741 13f79535-47bb-0310-9956-ffa450edef68 --- .../equinox/EquinoxServiceDiscoverer.java | 169 +++++++++++---------- 1 file changed, 92 insertions(+), 77 deletions(-) (limited to 'sca-java-2.x/trunk/modules/extensibility-equinox/src/main/java/org/apache/tuscany') diff --git a/sca-java-2.x/trunk/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java b/sca-java-2.x/trunk/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java index 122729a2a4..5c801196ba 100644 --- a/sca-java-2.x/trunk/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java +++ b/sca-java-2.x/trunk/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/EquinoxServiceDiscoverer.java @@ -23,6 +23,8 @@ import java.io.IOException; import java.net.URL; import java.security.AccessController; import java.security.PrivilegedAction; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; import java.util.Collection; import java.util.Enumeration; import java.util.HashSet; @@ -128,8 +130,16 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { return javaClass; } - public Class loadClass(String className) throws ClassNotFoundException { - return bundle.loadClass(className); + public Class loadClass(final String className) throws ClassNotFoundException { + try { + return AccessController.doPrivileged(new PrivilegedExceptionAction>() { + public Class run() throws ClassNotFoundException { + return bundle.loadClass(className); + } + }); + } catch (PrivilegedActionException e) { + throw (ClassNotFoundException)e.getException(); + } } public URL getLocation() { @@ -205,7 +215,7 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { } - public ServiceDeclaration getServiceDeclaration(String name) throws IOException { + public ServiceDeclaration getServiceDeclaration(String name) { Collection declarations = getServiceDeclarations(name); if (declarations.isEmpty()) { return null; @@ -253,80 +263,85 @@ public class EquinoxServiceDiscoverer implements ServiceDiscoverer { return set; } - public Collection getServiceDeclarations(String serviceName) throws IOException { - boolean debug = logger.isLoggable(Level.FINE); - Collection descriptors = new HashSet(); - - // http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/xpath/XPathFactory.html - boolean isPropertyFile = "javax.xml.xpath.XPathFactory".equals(serviceName); - boolean isTuscanyService = serviceName.startsWith("org.apache.tuscany.sca."); - - if (serviceName.startsWith("/")) { - // If the service name starts with /, treat it as the entry name - serviceName = serviceName.substring(1); - } else { - // Use JDK SPI pattern - serviceName = "META-INF/services/" + serviceName; - } - - Set visited = new HashSet(); - //System.out.println(">>>> getServiceDeclarations()"); - for (Bundle bundle : getBundles(isTuscanyService)) { - // if (!isProviderBundle(bundle)) { - // continue; - // } - Enumeration urls = null; - try { - // Use getResources to find resources on the classpath of the bundle - // Please note there are cases that getResources will return null even - // the bundle containing such entries: - // 1. There is a match on Import-Package or DynamicImport-Package, and another - // bundle exports the resource package, there is a possiblity that it doesn't - // find the containing entry - // 2. The bundle cannot be resolved, then getResources will return null - urls = bundle.getResources(serviceName); - if (urls == null) { - URL entry = bundle.getEntry(serviceName); - if (entry != null) { - logger.warning("Unresolved resource " + serviceName + " found in bundle: " + toString(bundle)); - try { - bundle.start(); - } catch (BundleException e) { - logger.log(Level.SEVERE, "Bundle: " + bundle.getSymbolicName() + " - " + e.getMessage(), e); - } - // urls = Collections.enumeration(Arrays.asList(entry)); - } - } - } catch (IOException e) { - logger.log(Level.SEVERE, "Bundle: " + bundle.getSymbolicName() + " - " + e.getMessage(), e); - } - if (urls == null) { - continue; - } - while (urls.hasMoreElements()) { - final URL url = urls.nextElement(); - - if (!visited.add(url)) { - // The URL has already been processed - continue; - } - - if (debug) { - logger.fine("Reading service provider file: " + url.toExternalForm()); - } - try { - for (Map attributes : ServiceDeclarationParser.load(url, isPropertyFile)) { - String className = attributes.get("class"); - ServiceDeclarationImpl descriptor = - new ServiceDeclarationImpl(bundle, url, className, attributes); - descriptors.add(descriptor); - } - } catch (IOException e) { - logger.log(Level.SEVERE, "Bundle: " + bundle.getSymbolicName() + " - " + e.getMessage(), e); - } - } - } - return descriptors; + public Collection getServiceDeclarations(final String _serviceName) { + return AccessController.doPrivileged(new PrivilegedAction>() { + public Collection run() { + boolean debug = logger.isLoggable(Level.FINE); + Collection descriptors = new HashSet(); + + // http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/xpath/XPathFactory.html + boolean isPropertyFile = "javax.xml.xpath.XPathFactory".equals(_serviceName); + boolean isTuscanyService = _serviceName.startsWith("org.apache.tuscany.sca."); + + String serviceName; + if (_serviceName.startsWith("/")) { + // If the service name starts with /, treat it as the entry name + serviceName = _serviceName.substring(1); + } else { + // Use JDK SPI pattern + serviceName = "META-INF/services/" + _serviceName; + } + + Set visited = new HashSet(); + //System.out.println(">>>> getServiceDeclarations()"); + for (Bundle bundle : getBundles(isTuscanyService)) { + // if (!isProviderBundle(bundle)) { + // continue; + // } + Enumeration urls = null; + try { + // Use getResources to find resources on the classpath of the bundle + // Please note there are cases that getResources will return null even + // the bundle containing such entries: + // 1. There is a match on Import-Package or DynamicImport-Package, and another + // bundle exports the resource package, there is a possiblity that it doesn't + // find the containing entry + // 2. The bundle cannot be resolved, then getResources will return null + urls = bundle.getResources(serviceName); + if (urls == null) { + URL entry = bundle.getEntry(serviceName); + if (entry != null) { + logger.warning("Unresolved resource " + serviceName + " found in bundle: " + EquinoxServiceDiscoverer.toString(bundle)); + try { + bundle.start(); + } catch (BundleException e) { + logger.log(Level.SEVERE, "Bundle: " + bundle.getSymbolicName() + " - " + e.getMessage(), e); + } + // urls = Collections.enumeration(Arrays.asList(entry)); + } + } + } catch (IOException e) { + logger.log(Level.SEVERE, "Bundle: " + bundle.getSymbolicName() + " - " + e.getMessage(), e); + } + if (urls == null) { + continue; + } + while (urls.hasMoreElements()) { + final URL url = urls.nextElement(); + + if (!visited.add(url)) { + // The URL has already been processed + continue; + } + + if (debug) { + logger.fine("Reading service provider file: " + url.toExternalForm()); + } + try { + for (Map attributes : ServiceDeclarationParser.load(url, isPropertyFile)) { + String className = attributes.get("class"); + ServiceDeclarationImpl descriptor = + new ServiceDeclarationImpl(bundle, url, className, attributes); + descriptors.add(descriptor); + } + } catch (IOException e) { + logger.log(Level.SEVERE, "Bundle: " + bundle.getSymbolicName() + " - " + e.getMessage(), e); + } + } + } + return descriptors; + } + }); } public ClassLoader getContextClassLoader() { -- cgit v1.2.3