diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-14 17:09:50 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-14 17:09:50 +0000 |
commit | 1d0ca0d129756c9aea37ad5165e19c1ba0a8faf0 (patch) | |
tree | 1e50ed81911036f74e7af603e3fca12e62138c42 | |
parent | b9c1e93951e71d729ea283e96f39729033465c57 (diff) |
Resolve relative path of the contribution locations against the node.xml
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@825196 13f79535-47bb-0310-9956-ffa450edef68
5 files changed, 67 insertions, 30 deletions
diff --git a/java/sca/modules/common-java/src/main/java/org/apache/tuscany/sca/common/java/io/IOHelper.java b/java/sca/modules/common-java/src/main/java/org/apache/tuscany/sca/common/java/io/IOHelper.java index f43ff4538b..faa29fa978 100644 --- a/java/sca/modules/common-java/src/main/java/org/apache/tuscany/sca/common/java/io/IOHelper.java +++ b/java/sca/modules/common-java/src/main/java/org/apache/tuscany/sca/common/java/io/IOHelper.java @@ -24,6 +24,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.JarURLConnection; +import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; @@ -93,6 +94,21 @@ public class IOHelper { return createURI(url.toString()); } + public static URL normalize(URL url) { + // Make sure the trailing / is added to the file directory URL so that + // URLClassLoader can load classes from that + try { + File file = toFile(url); + if (file != null) { + return file.getAbsoluteFile().toURI().toURL(); + } else { + return toURI(url).toURL(); + } + } catch (MalformedURLException e) { + throw new IllegalArgumentException(e); + } + } + /** * Returns the File object representing the given URL. * diff --git a/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java index 2ff16eeac7..4ad9519ad8 100644 --- a/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java +++ b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java @@ -21,7 +21,6 @@ package org.apache.tuscany.sca.host.webapp; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.net.URI; import java.net.URL; import java.util.Enumeration; @@ -45,19 +44,6 @@ public class ServletHostHelper { public static final String SCA_NODE_ATTRIBUTE = Node.class.getName(); private static NodeFactory factory; - private static InputStream openStream(ServletContext servletContext, String location) throws IOException { - URI uri = URI.create(location); - if (uri.isAbsolute()) { - return uri.toURL().openStream(); - } else { - String path = location; - if (!path.startsWith("/")) { - path = "/" + path; - } - return servletContext.getResourceAsStream(path); - } - } - private static URL getResource(ServletContext servletContext, String location) throws IOException { URI uri = URI.create(location); if (uri.isAbsolute()) { @@ -82,17 +68,18 @@ public class ServletHostHelper { @SuppressWarnings("unchecked") private static NodeConfiguration getNodeConfiguration(ServletContext servletContext) throws IOException { NodeConfiguration configuration = null; - String nodeConfigURI = servletContext.getInitParameter("node.configuration"); + String nodeConfigURI = (String) servletContext.getAttribute("node.configuration"); if (nodeConfigURI != null) { - configuration = factory.loadConfiguration(openStream(servletContext, nodeConfigURI)); + URL url = getResource(servletContext, nodeConfigURI); + configuration = factory.loadConfiguration(url.openStream(), url); } else { configuration = factory.createNodeConfiguration(); configuration.setDomainURI(factory.getDomainURI()); - Enumeration<String> names = servletContext.getInitParameterNames(); + Enumeration<String> names = servletContext.getAttributeNames(); while (names.hasMoreElements()) { String name = names.nextElement(); if (name.startsWith("contribution.")) { - String contrib = servletContext.getInitParameter(name); + String contrib = (String) servletContext.getAttribute(name); if (contrib != null) { configuration.addContribution(getResource(servletContext, contrib)); } @@ -106,12 +93,12 @@ public class ServletHostHelper { if (composite != null) { configuration.getContributions().get(0).addDeploymentComposite(composite); } - String nodeURI = servletContext.getInitParameter("node.uri"); + String nodeURI = (String) servletContext.getAttribute("node.uri"); if (nodeURI == null) { nodeURI = new File(servletContext.getRealPath("/")).getName(); } configuration.setURI(nodeURI); - String domainURI = servletContext.getInitParameter("domain.uri"); + String domainURI = (String) servletContext.getAttribute("domain.uri"); if (domainURI != null) { configuration.setDomainURI(domainURI); } @@ -129,6 +116,11 @@ public class ServletHostHelper { } else { factory = NodeFactory.newInstance(); } + for (Enumeration<String> e = servletContext.getInitParameterNames(); e.hasMoreElements();) { + String name = e.nextElement(); + String value = servletContext.getInitParameter(name); + servletContext.setAttribute(name, value); + } node = createNode(servletContext); servletContext.setAttribute(SCA_NODE_ATTRIBUTE, node); getServletHost(node).init(new ServletConfig() { diff --git a/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java index 2a65aefc15..31fe4aa322 100644 --- a/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java +++ b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java @@ -20,6 +20,7 @@ package org.apache.tuscany.sca.host.webapp; import java.io.IOException; +import java.util.Enumeration; import javax.servlet.Filter; import javax.servlet.FilterConfig; @@ -47,6 +48,11 @@ public class TuscanyServletFilter implements Filter { public void init(final FilterConfig config) throws ServletException { context = config.getServletContext(); + for (Enumeration<String> e = config.getInitParameterNames(); e.hasMoreElements();) { + String name = e.nextElement(); + String value = config.getInitParameter(name); + context.setAttribute(name, value); + } servletHost = ServletHostHelper.init(context); } diff --git a/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java b/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java index a92dbdb03b..24dac505ef 100644 --- a/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java +++ b/java/sca/modules/node-api/src/main/java/org/apache/tuscany/sca/node/NodeFactory.java @@ -245,7 +245,8 @@ public abstract class NodeFactory extends DefaultNodeConfigurationFactory { public Node createNode(URL configurationURL) { try { InputStream is = openStream(configurationURL); - return createNode(is); + NodeConfiguration configuration = loadConfiguration(is, configurationURL); + return createNode(configuration); } catch (IOException e) { throw new ServiceRuntimeException(e); } @@ -258,13 +259,8 @@ public abstract class NodeFactory extends DefaultNodeConfigurationFactory { * @return a new SCA node */ public Node createNode(InputStream is) { - try { - NodeConfiguration configuration = loadConfiguration(is); - is.close(); - return createNode(configuration); - } catch (IOException e) { - throw new ServiceRuntimeException(e); - } + NodeConfiguration configuration = loadConfiguration(is, null); + return createNode(configuration); } /** @@ -442,7 +438,7 @@ public abstract class NodeFactory extends DefaultNodeConfigurationFactory { * @param configuration The input stream of the XML document * @return The node configuration */ - public abstract NodeConfiguration loadConfiguration(InputStream xml); + public abstract NodeConfiguration loadConfiguration(InputStream xml, URL base); } diff --git a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java index 00d8dc7978..6182c5ca2d 100644 --- a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java +++ b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java @@ -29,6 +29,7 @@ import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.security.AccessController; import java.security.PrivilegedAction; @@ -54,6 +55,7 @@ import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint; import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; import org.apache.tuscany.sca.assembly.builder.DeployedCompositeBuilder; +import org.apache.tuscany.sca.common.java.io.IOHelper; import org.apache.tuscany.sca.common.xml.stax.StAXHelper; import org.apache.tuscany.sca.contribution.Artifact; import org.apache.tuscany.sca.contribution.Contribution; @@ -166,7 +168,7 @@ public class NodeFactoryImpl extends NodeFactory { } @Override - public NodeConfiguration loadConfiguration(InputStream xml) { + public NodeConfiguration loadConfiguration(InputStream xml, URL base) { try { init(); StAXHelper helper = StAXHelper.getInstance(extensionPoints); @@ -177,11 +179,36 @@ public class NodeFactoryImpl extends NodeFactory { reader.nextTag(); NodeConfiguration config = (NodeConfiguration)processor.read(reader); xml.close(); + if (base != null && config != null) { + // Resolve the contribution location against the node.xml + for (ContributionConfiguration c : config.getContributions()) { + String location = c.getLocation(); + if (location != null) { + URL url = new URL(base, location); + url = IOHelper.normalize(url); + c.setLocation(url.toString()); + } + } + } return config; } catch (Throwable e) { throw new ServiceRuntimeException(e); } } + + private File toFile(URL url) { + if("file".equalsIgnoreCase(url.getProtocol())) { + try { + return new File(url.toURI()); + } catch(URISyntaxException e) { + return new File(url.getPath()); + } catch(IllegalArgumentException e) { + // Hack for file:./a.txt or file:../a/c.wsdl + return new File(url.getPath()); + } + } + return null; + } public Map<Object, Node> getNodes() { return nodes; |