summaryrefslogtreecommitdiffstats
path: root/branches/sca-java-0.99/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java
blob: 4e0d3d88c595d2e859c47b918f5f2695a7ecfc39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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);
        }
    }

}