diff options
Diffstat (limited to 'branches/sca-java-0.99/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java')
-rw-r--r-- | branches/sca-java-0.99/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/branches/sca-java-0.99/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java b/branches/sca-java-0.99/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java new file mode 100644 index 0000000000..4e0d3d88c5 --- /dev/null +++ b/branches/sca-java-0.99/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java @@ -0,0 +1,116 @@ +/* + * 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.util.HashMap; +import java.util.Map; +import java.util.logging.Logger; + +import javax.servlet.Servlet; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; + +import org.apache.tuscany.sca.host.http.DefaultResourceServlet; +import org.apache.tuscany.sca.host.http.ServletHost; +import org.apache.tuscany.sca.host.http.ServletMappingException; + +/** + * ServletHost impl singleton thats shared between the SCADomain + * instance and the TuscanyServlet instance. + * TODO: using a static singleton seems a big hack but how + * should it be shared? Need some way for TuscanyServlet + * to pull it out of the SCADomain instance. + * + */ +public class WebAppServletHost implements ServletHost { + private static final Logger logger = Logger.getLogger(WebAppServletHost.class.getName()); + private static WebAppServletHost instance = new WebAppServletHost(); + + private Map<String, Servlet> servlets; + + private WebAppServletHost() { + servlets = new HashMap<String, Servlet>(); + } + + public void addServletMapping(String path, Servlet servlet) throws ServletMappingException { + URI pathURI = URI.create(path); + + // Ignore registrations of our default resource servlet, as resources + // are already served by the web container + if (servlet instanceof DefaultResourceServlet) { + //TODO maybe ignore registration of the servlet only if it's + // mapped to "/" and still honor other registrations, to do this + // we will need a way to determine what's the default servlet + // in the web container that we are running in. + return; + } + + // For webapps just use the path and ignore the host and port + path = pathURI.getPath(); + if (!path.startsWith("/")) { + path = '/' + path; + } + servlets.put(path, servlet); + logger.info("addServletMapping: " + path); + } + + public Servlet removeServletMapping(String path) throws ServletMappingException { + URI pathURI = URI.create(path); + path = pathURI.getPath(); + if (!path.startsWith("/")) { + path = '/' + path; + } + // for webapps just use the path and ignore the host and port + return servlets.remove(path); + } + + public Servlet getServlet(String path) { + if (path == null) { + return null; + } + if (!path.startsWith("/")) { + path = '/' + path; + } + Servlet servlet = servlets.get(path); + if (servlet != null) { + return servlet; + } + for (String servletPath : servlets.keySet()) { + if (servletPath.endsWith("*")) { + if (path.startsWith(servletPath.substring(0, servletPath.length() - 1))) { + return servlets.get(servletPath); + } + } + } + return null; + } + + public static WebAppServletHost getInstance() { + return instance; + } + + public void init(ServletConfig config) throws ServletException { + for (Servlet servlet : servlets.values()) { + servlet.init(config); + } + } + +} |