summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/trunk/modules/extensibility
diff options
context:
space:
mode:
authorrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2010-02-19 03:27:50 +0000
committerrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2010-02-19 03:27:50 +0000
commiteab31f88e10a97c863c14eb9c18e9fe7e0ed096c (patch)
tree182f006b80d2aded574f367a0a0b65db41250c78 /sca-java-2.x/trunk/modules/extensibility
parent5cf2cf2306522c7bd3b420e7ce6e362ce3b743f1 (diff)
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
Diffstat (limited to 'sca-java-2.x/trunk/modules/extensibility')
-rw-r--r--sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ClassLoaderContext.java4
-rw-r--r--sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ContextClassLoaderServiceDiscoverer.java89
-rw-r--r--sca-java-2.x/trunk/modules/extensibility/src/main/java/org/apache/tuscany/sca/extensibility/ServiceDeclarationParser.java83
3 files changed, 91 insertions, 85 deletions
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<InputStream>() {
- 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<Object, Object> e : props.entrySet()) {
- Map<String, String> attributes = new HashMap<String, String>();
- 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<String, String> 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<String, String> 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<Map<String, String>> load(final URL url, boolean isPropertyFile) throws IOException {
+ Collection<Map<String, String>> descriptors = new ArrayList<Map<String, String>>();
+
+ // Allow privileged access to open URL stream. Add FilePermission to added to security
+ // policy file.
+ InputStream is;
+ try {
+ is = AccessController.doPrivileged(new PrivilegedExceptionAction<InputStream>() {
+ 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<Object, Object> e : props.entrySet()) {
+ Map<String, String> attributes = new HashMap<String, String>();
+ 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<String, String> attributes = parseDeclaration(reg);
+ descriptors.add(attributes);
+ }
+ }
+ } finally {
+ if (reader != null) {
+ try {
+ reader.close();
+ } catch (IOException e) {
+ // Ignore
+ }
+ }
+ }
+ return descriptors;
+ }
}