From eab31f88e10a97c863c14eb9c18e9fe7e0ed096c Mon Sep 17 00:00:00 2001 From: rfeng Date: Fri, 19 Feb 2010 03:27:50 +0000 Subject: Start to add the support to load node factory configuration from external files git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@911689 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/extensibility/ClassLoaderContext.java | 4 + .../ContextClassLoaderServiceDiscoverer.java | 89 +--------------------- .../extensibility/ServiceDeclarationParser.java | 83 ++++++++++++++++++++ 3 files changed, 91 insertions(+), 85 deletions(-) (limited to 'sca-java-2.x/trunk/modules/extensibility/src') diff --git a/sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClassLoaderContext.java b/sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClassLoaderContext.java index e6ccb22ed5..e9407065b0 100644 --- a/sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClassLoaderContext.java +++ b/sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClassLoaderContext.java @@ -205,4 +205,8 @@ public class ClassLoaderContext { return loaders; } + public ClassLoader getClassLoader() { + return classLoader; + } + } diff --git a/sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ContextClassLoaderServiceDiscoverer.java b/sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ContextClassLoaderServiceDiscoverer.java index 767af937fd..4e997f4f8b 100644 --- a/sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ContextClassLoaderServiceDiscoverer.java +++ b/sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ContextClassLoaderServiceDiscoverer.java @@ -19,26 +19,18 @@ package org.apache.tuscany.sca.extensibility; -import static org.apache.tuscany.sca.extensibility.ServiceDeclarationParser.parseDeclaration; - -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.lang.ref.WeakReference; import java.net.URL; -import java.net.URLConnection; import java.security.AccessController; import java.security.PrivilegedAction; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; @@ -163,83 +155,10 @@ public class ContextClassLoaderServiceDiscoverer implements ServiceDiscoverer { 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 { - URLConnection connection = url.openConnection(); - // TUSCANY-2539 - // Don't cache connections by default to stop Tuscany locking contribution jar files - // done here as this is one of the first places we open a stream and the only way to - // set the default is to set it on an instance of URLConnection - connection.setDefaultUseCaches(false); - connection.setUseCaches(false); - return url.openStream(); - } - }); - } catch (PrivilegedActionException e) { - throw (IOException)e.getException(); - } - if (isPropertyFile) { - // Load as a property file - Properties props = new Properties(); - props.load(is); - is.close(); - for (Map.Entry e : props.entrySet()) { - Map attributes = new HashMap(); - String key = (String)e.getKey(); - String value = (String)e.getValue(); - // Unfortunately, the xalan file only has the classname - if (value == null || "".equals(value)) { - value = key; - key = ""; - } - if (!"".equals(key)) { - attributes.put(key, value); - attributes.put("uri", key); - } - attributes.putAll(parseDeclaration(value)); - ServiceDeclarationImpl descriptor = new ServiceDeclarationImpl(url, value, attributes); - descriptors.add(descriptor); - } - continue; - } - BufferedReader reader = null; - try { - reader = new BufferedReader(new InputStreamReader(is)); - int count = 0; - 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 = parseDeclaration(reg); - String className = attributes.get("class"); - if (className == null) { - // Add a unique class name to prevent equals() from returning true - className = "_class_" + count; - count++; - } - ServiceDeclarationImpl descriptor = new ServiceDeclarationImpl(url, className, attributes); - descriptors.add(descriptor); - } - } - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - // Ignore - } - } + for (Map attributes : ServiceDeclarationParser.load(url, isPropertyFile)) { + String className = attributes.get("class"); + ServiceDeclarationImpl descriptor = new ServiceDeclarationImpl(url, className, attributes); + descriptors.add(descriptor); } } } catch (IOException e) { diff --git a/sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclarationParser.java b/sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclarationParser.java index 993f6c6338..f546e44dfb 100644 --- a/sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclarationParser.java +++ b/sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclarationParser.java @@ -19,10 +19,21 @@ 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.net.URLConnection; +import java.security.AccessController; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Properties; import javax.xml.namespace.QName; @@ -288,5 +299,77 @@ public class ServiceDeclarationParser { } return new QName(qname); } + + public static Collection> load(final URL url, boolean isPropertyFile) throws IOException { + Collection> descriptors = new ArrayList>(); + + // 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 { + URLConnection connection = url.openConnection(); + // TUSCANY-2539 + // Don't cache connections by default to stop Tuscany locking contribution jar files + // done here as this is one of the first places we open a stream and the only way to + // set the default is to set it on an instance of URLConnection + connection.setDefaultUseCaches(false); + connection.setUseCaches(false); + return url.openStream(); + } + }); + } catch (PrivilegedActionException e) { + throw (IOException)e.getException(); + } + if (isPropertyFile) { + // Load as a property file + Properties props = new Properties(); + props.load(is); + is.close(); + for (Map.Entry e : props.entrySet()) { + Map attributes = new HashMap(); + String key = (String)e.getKey(); + String value = (String)e.getValue(); + // Unfortunately, the xalan file only has the classname + if (value == null || "".equals(value)) { + value = key; + key = ""; + } + if (!"".equals(key)) { + attributes.put(key, value); + attributes.put("uri", key); + } + attributes.putAll(parseDeclaration(value)); + descriptors.add(attributes); + } + return descriptors; + } + 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(); + + Map attributes = parseDeclaration(reg); + descriptors.add(attributes); + } + } + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + // Ignore + } + } + } + return descriptors; + } } -- cgit v1.2.3