summaryrefslogtreecommitdiffstats
path: root/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java
blob: 4ad9519ad8dabe83921d9ba96be83bb3fd5e915e (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * 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.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.host.http.DefaultServletHostExtensionPoint;
import org.apache.tuscany.sca.host.http.ServletHost;
import org.apache.tuscany.sca.host.http.ServletHostExtensionPoint;
import org.apache.tuscany.sca.node.Node;
import org.apache.tuscany.sca.node.NodeFactory;
import org.apache.tuscany.sca.node.configuration.NodeConfiguration;
import org.apache.tuscany.sca.node.impl.NodeImpl;

public class ServletHostHelper {
    public static final String DOMAIN_NAME_ATTR = "org.apache.tuscany.sca.domain.name";
    public static final String SCA_NODE_ATTRIBUTE = Node.class.getName();
    private static NodeFactory factory;

    private static URL getResource(ServletContext servletContext, String location) throws IOException {
        URI uri = URI.create(location);
        if (uri.isAbsolute()) {
            return uri.toURL();
        } else {
            String path = location;
            if (!path.startsWith("/")) {
                path = "/" + path;
            }
            URL url = servletContext.getResource(path);
            if (url != null && url.getProtocol().equals("jndi")) {
                //this is Tomcat case, we should use getRealPath
                File warRootFile = new File(servletContext.getRealPath(path));
                return warRootFile.toURI().toURL();
            } else {
                //this is Jetty case
                return url;
            }
        }
    }

    @SuppressWarnings("unchecked")
    private static NodeConfiguration getNodeConfiguration(ServletContext servletContext) throws IOException {
        NodeConfiguration configuration = null;
        String nodeConfigURI = (String) servletContext.getAttribute("node.configuration");
        if (nodeConfigURI != null) {
            URL url = getResource(servletContext, nodeConfigURI);
            configuration = factory.loadConfiguration(url.openStream(), url);
        } else {
            configuration = factory.createNodeConfiguration();
            configuration.setDomainURI(factory.getDomainURI());
            Enumeration<String> names = servletContext.getAttributeNames();
            while (names.hasMoreElements()) {
                String name = names.nextElement();
                if (name.startsWith("contribution.")) {
                    String contrib = (String) servletContext.getAttribute(name);
                    if (contrib != null) {
                        configuration.addContribution(getResource(servletContext, contrib));
                    }
                }
            }
            if (configuration.getContributions().isEmpty()) {
                // TODO: Which path should be the default root
                configuration.addContribution(getResource(servletContext, "/"));
            }
            URL composite = getResource(servletContext, "/WEB-INF/web.composite");
            if (composite != null) {
                configuration.getContributions().get(0).addDeploymentComposite(composite);
            }
            String nodeURI = (String) servletContext.getAttribute("node.uri");
            if (nodeURI == null) {
                nodeURI = new File(servletContext.getRealPath("/")).getName();
            }
            configuration.setURI(nodeURI);
            String domainURI = (String) servletContext.getAttribute("domain.uri");
            if (domainURI != null) {
                configuration.setDomainURI(domainURI);
            }
        }
        return configuration;
    }

    public static ServletHost init(final ServletContext servletContext) {
        Node node = (Node)servletContext.getAttribute(SCA_NODE_ATTRIBUTE);
        if (node == null) {
            try {
                String domainName = (String)servletContext.getAttribute(DOMAIN_NAME_ATTR);
                if (domainName != null) {
                    factory = NodeFactory.getInstance(domainName);
                } 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() {
                    public String getInitParameter(String name) {
                        return servletContext.getInitParameter(name);
                    }

                    public Enumeration<?> getInitParameterNames() {
                        return servletContext.getInitParameterNames();
                    }

                    public ServletContext getServletContext() {
                        return servletContext;
                    }

                    public String getServletName() {
                        return servletContext.getServletContextName();
                    }
                });
            } catch (ServletException e) {
                throw new RuntimeException(e);
            }
        }
        return getServletHost(node);
    }

    private static WebAppServletHost getServletHost(Node node) {
        NodeImpl nodeImpl = (NodeImpl)node;
        ExtensionPointRegistry eps = nodeImpl.getExtensionPoints();
        ServletHostExtensionPoint servletHosts = eps.getExtensionPoint(ServletHostExtensionPoint.class);
        List<ServletHost> hosts = servletHosts.getServletHosts();
        if (hosts == null || hosts.size() < 1) {
            throw new IllegalStateException("No ServletHost found");
        }
        for (ServletHost servletHost : hosts) {
            if ("webapp".equals(servletHost.getName())) {
                if(servletHost instanceof DefaultServletHostExtensionPoint.LazyServletHost) {
                    return (WebAppServletHost) ((DefaultServletHostExtensionPoint.LazyServletHost) servletHost).getServletHost();
                } else if(servletHost instanceof WebAppServletHost) {
                    return (WebAppServletHost) servletHost;
                }
            }
        }
        throw new IllegalStateException("No WebApp Servlet host is configured");
    }

    private static Node createNode(final ServletContext servletContext) throws ServletException {
        NodeConfiguration configuration;
        try {
            configuration = getNodeConfiguration(servletContext);
        } catch (IOException e) {
            throw new ServletException(e);
        }
        Node node = factory.createNode(configuration).start();
        return node;
    }

    public static void stop(ServletContext servletContext) {
        Node node = (Node)servletContext.getAttribute(ServletHostHelper.SCA_NODE_ATTRIBUTE);
        if (node != null) {
            node.stop();
            servletContext.setAttribute(ServletHostHelper.SCA_NODE_ATTRIBUTE, null);
        }
    }
}