From 8a34bbab4753a11146ce0d76b4f1391c474ff61f Mon Sep 17 00:00:00 2001 From: rfeng Date: Wed, 8 Sep 2010 05:56:06 +0000 Subject: Fix the base URI display git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@993615 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/host/webapp/WebAppContributionScanner.java | 83 ++++++++++++++++++++++ .../tuscany/sca/host/webapp/WebAppHelper.java | 47 ++++++++---- .../tuscany/sca/host/webapp/WebAppServletHost.java | 5 +- .../tuscany/sca/node/impl/NodeFactoryImpl.java | 1 - 4 files changed, 121 insertions(+), 15 deletions(-) create mode 100644 sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppContributionScanner.java (limited to 'sca-java-2.x/trunk/modules') diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppContributionScanner.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppContributionScanner.java new file mode 100644 index 0000000000..835783612a --- /dev/null +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppContributionScanner.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.host.webapp; + +import java.net.URI; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.ServletContext; + +import org.apache.tuscany.sca.contribution.Artifact; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.ContributionFactory; +import org.apache.tuscany.sca.contribution.PackageType; +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.scanner.ContributionScanner; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; + +/** + * + */ +public class WebAppContributionScanner implements ContributionScanner { + private ServletContext servletContext; + private ContributionFactory contributionFactory; + + public WebAppContributionScanner(ExtensionPointRegistry registry) { + super(); + this.servletContext = registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(ServletContext.class); + this.contributionFactory = + registry.getExtensionPoint(FactoryExtensionPoint.class).getFactory(ContributionFactory.class); + } + + public String getContributionType() { + return PackageType.WAR; + } + + public List scan(Contribution contribution) throws ContributionReadException { + try { + List artifacts = new ArrayList(); + URL location = new URL(contribution.getLocation()); + URL root = servletContext.getResource("/"); + URI relative = root.toURI().relativize(location.toURI()); + String path = relative.getPath(); + if (!path.startsWith("/")) { + path = "/" + path; + } + for (Object file : servletContext.getResourcePaths(path)) { + Artifact artifact = contributionFactory.createArtifact(); + String name = (String)file; + // Remove leading / + name = name.substring(1); + artifact.setURI(name); + URL artifactURL = new URL(location, name); + artifact.setLocation(artifactURL.toString()); + artifacts.add(artifact); + } + return artifacts; + } catch (Exception e) { + throw new ContributionReadException(e); + } + } + +} diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java index 655e19c88c..47fd852f57 100644 --- a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java @@ -31,6 +31,7 @@ import javax.servlet.ServletContext; import javax.servlet.ServletException; import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; import org.apache.tuscany.sca.host.http.ServletHost; import org.apache.tuscany.sca.host.http.ServletHostExtensionPoint; import org.apache.tuscany.sca.node.Node; @@ -72,6 +73,13 @@ public class WebAppHelper { } } } + + private static String[] parse(String listOfValues) { + if (listOfValues == null) { + return null; + } + return listOfValues.split("(\\s|,)+"); + } @SuppressWarnings("unchecked") private static NodeConfiguration getNodeConfiguration(ServletContext servletContext) throws IOException, @@ -88,21 +96,31 @@ public class WebAppHelper { String name = names.nextElement(); if (name.equals(CONTRIBUTION) || name.startsWith(CONTRIBUTION + ".")) { // We need to have a way to select one or more folders within the webapp as the contributions - String contrib = (String)servletContext.getAttribute(name); - if (contrib != null) { - File f = new File(getResource(servletContext, contrib).toURI()); - configuration.addContribution(f.toURI().toURL()); + String listOfValues = (String)servletContext.getAttribute(name); + if (listOfValues != null) { + for (String path : parse(listOfValues)) { + if ("".equals(path)) { + continue; + } + File f = new File(getResource(servletContext, path).toURI()); + configuration.addContribution(f.toURI().toURL()); + } } } else if (name.equals(CONTRIBUTIONS) || name.startsWith(CONTRIBUTIONS + ".")) { - String contrib = (String)servletContext.getAttribute(name); - if (contrib != null) { - File f = new File(getResource(servletContext, contrib).toURI()); - if (f.isDirectory()) { - for (File n : f.listFiles()) { - configuration.addContribution(n.toURI().toURL()); + String listOfValues = (String)servletContext.getAttribute(name); + if (listOfValues != null) { + for (String path : parse(listOfValues)) { + if ("".equals(path)) { + continue; + } + File f = new File(getResource(servletContext, path).toURI()); + if (f.isDirectory()) { + for (File n : f.listFiles()) { + configuration.addContribution(n.toURI().toURL()); + } + } else { + configuration.addContribution(f.toURI().toURL()); } - } else { - configuration.addContribution(f.toURI().toURL()); } } } @@ -163,7 +181,12 @@ public class WebAppHelper { } else { factory = NodeFactory.newInstance(); } + + // Add ServletContext as a utility ExtensionPointRegistry registry = factory.getExtensionPointRegistry(); + UtilityExtensionPoint utilityExtensionPoint = registry.getExtensionPoint(UtilityExtensionPoint.class); + utilityExtensionPoint.addUtility(ServletContext.class, servletContext); + ServletHostExtensionPoint servletHosts = registry.getExtensionPoint(ServletHostExtensionPoint.class); servletHosts.setWebApp(true); diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java index 65ea14fbf0..3a779c9abb 100644 --- a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java @@ -110,7 +110,7 @@ public class WebAppServletHost implements ServletHost { // as they are fixed by the Web container servlets.put(suri, servlet); - URL url = getURLMapping(suri, securityContext); + URL url = getURLMapping(pathURI.toString(), securityContext); logger.info("Added Servlet mapping: " + url); return url.toString(); } @@ -162,7 +162,8 @@ public class WebAppServletHost implements ServletHost { scheme = "http"; } int portNumber = uri.getPort(); - if (portNumber == -1) { + if (portNumber == -1 && uri.getScheme() == null) { + // Only set the default port number if the scheme is not present portNumber = defaultPortNumber; } diff --git a/sca-java-2.x/trunk/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java b/sca-java-2.x/trunk/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java index 3c34c5ce25..5497acd6cf 100644 --- a/sca-java-2.x/trunk/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java +++ b/sca-java-2.x/trunk/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java @@ -33,7 +33,6 @@ import java.net.URL; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; -- cgit v1.2.3