summaryrefslogtreecommitdiffstats
path: root/sandbox/old/contrib/runtime-webapp/webapp-api/src/main/java/org/apache/tuscany/runtime/webapp/TuscanyContextListener.java
blob: dce4fa73938ca0f2d71b30fe45010a3ee2927207 (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
/*
 * 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.runtime.webapp;

import java.net.URL;
import java.net.URI;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.tuscany.api.TuscanyRuntimeException;
import org.apache.tuscany.host.runtime.ShutdownException;
import static org.apache.tuscany.runtime.webapp.Constants.COMPOSITE_PARAM;
import static org.apache.tuscany.runtime.webapp.Constants.COMPONENT_PARAM;
import static org.apache.tuscany.runtime.webapp.Constants.ONLINE_PARAM;
import static org.apache.tuscany.runtime.webapp.Constants.RUNTIME_ATTRIBUTE;
import static org.apache.tuscany.runtime.webapp.Constants.APPLICATION_SCDL_PATH_DEFAULT;
import static org.apache.tuscany.runtime.webapp.Constants.APPLICATION_SCDL_PATH_PARAM;

/**
 * Launches a Tuscany runtime in a web application, loading information from servlet context parameters. This listener
 * manages one runtime per servlet context; the lifecycle of that runtime corresponds to the the lifecycle of the
 * associated servlet context.
 * <p/>
 * The runtime is launched in a child classloader of the web application, thereby providing isolation between
 * application and system artifacts. Application code only has access to the SCA API and may not reference Tuscany
 * system artifacts directly.
 * <p/>
 * The <code>web.xml</code> of a web application embedding Tuscany must have entries for this listener and {@link
 * TuscanySessionListener}. The latter notifies the runtime of session creation and expiration events through a
 * "bridging" contract, {@link WebappRuntime}. The <code>web.xml</code> may also optionally be configured with entries
 * for {@link TuscanyFilter} and {@link TuscanyServlet}. The former must be mapped to all urls that execute "unmanaged"
 * code which accesses the Tuscany runtime though the SCA API, for example, JSPs and Servlets. The latter forwards
 * service requests into the runtime, by default requests sent to URLs relative to the context path beginning with
 * <code>/services</code>.
 *
 * @version $Rev$ $Date$
 */
public class TuscanyContextListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        ClassLoader webappClassLoader = Thread.currentThread().getContextClassLoader();
        ServletContext servletContext = event.getServletContext();
        WebappUtil utils = getUtils(servletContext);
        try {
            // FIXME work this out from the servlet context
            String defaultComposite = "http://locahost/sca";
            URI compositeId = new URI(utils.getInitParameter(COMPOSITE_PARAM, defaultComposite));
            URI componentId = new URI(utils.getInitParameter(COMPONENT_PARAM, "webapp"));
            String scdlPath = utils.getInitParameter(APPLICATION_SCDL_PATH_PARAM, APPLICATION_SCDL_PATH_DEFAULT);
            URL scdl = servletContext.getResource(scdlPath);

            boolean online = Boolean.valueOf(utils.getInitParameter(ONLINE_PARAM, "true"));
            WebappRuntimeInfo info = new WebappRuntimeInfoImpl(servletContext,
                                                               servletContext.getResource("/WEB-INF/tuscany/"),
                                                               online);
            ClassLoader bootClassLoader = utils.getBootClassLoader(webappClassLoader);
            URL systemScdl = utils.getSystemScdl(bootClassLoader);

            WebappRuntime runtime = utils.getRuntime(bootClassLoader);
            runtime.setServletContext(servletContext);
            runtime.setRuntimeInfo(info);
            runtime.setHostClassLoader(webappClassLoader);
            runtime.setSystemScdl(systemScdl);
            runtime.initialize();
            servletContext.setAttribute(RUNTIME_ATTRIBUTE, runtime);

            runtime.deploy(compositeId, scdl, componentId);
        } catch (TuscanyRuntimeException e) {
            servletContext.log(e.getMessage(), e);
            e.printStackTrace();
            throw e;
        } catch (Throwable e) {
            servletContext.log(e.getMessage(), e);
            e.printStackTrace();
            throw new TuscanyInitException(e);
        }
    }

    protected WebappUtil getUtils(ServletContext servletContext) {
        return new WebappUtilImpl(servletContext);
    }

    public void contextDestroyed(ServletContextEvent event) {
        ServletContext servletContext = event.getServletContext();
        WebappRuntime runtime = (WebappRuntime) servletContext.getAttribute(RUNTIME_ATTRIBUTE);
        if (runtime == null) {
            return;
        }
        servletContext.removeAttribute(RUNTIME_ATTRIBUTE);
        try {
            runtime.destroy();
        } catch (ShutdownException e) {
            servletContext.log("Error destroying runtume", e);
        }
    }

}