View Javadoc
1   /*
2    * Copyright (c) 2005 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL licence
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/copyleft/lesser.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.extend.server.deployment.tomcat;
14  
15  import org.abstracthorizon.extend.server.deployment.Module;
16  import org.abstracthorizon.extend.server.deployment.ModuleId;
17  import org.abstracthorizon.extend.support.spring.service.ServiceApplicationContextModule;
18  import org.apache.catalina.Context;
19  import org.apache.catalina.Host;
20  import org.apache.catalina.Loader;
21  import org.apache.catalina.core.StandardContext;
22  import org.apache.catalina.core.StandardEngine;
23  import org.apache.catalina.startup.Embedded;
24  import org.springframework.context.ApplicationContext;
25  
26  /**
27   * <p>
28   *   Module that represents a war module deployed with tomcat. Also it can define beans in
29   *   a same way as service application module (this is an extension of it).
30   * </p>
31   * <p>
32   *   It uses &quot;WEB-INF/web-application.xml&quot; for application context xml.
33   * </p>
34   * @author Daniel Sendula
35   */
36  public class TomcatWebApplicationContext extends ServiceApplicationContextModule {
37  
38      /** Reference to embedded tomcat */
39      protected Embedded tomcat;
40      
41      /** Reference to the Host this context is deploy under */
42      protected Host host;
43  
44      /** Context this module is running under */
45      protected Context context;
46  
47      /** Context of web server - tomcat's service application context */
48      protected ApplicationContext webServerContext;
49  
50      /** Context path for this module */
51      protected String contextPath;
52      
53      /**
54       * Empty constructor
55       */
56      public TomcatWebApplicationContext(ModuleId moduleId) {
57          super(moduleId);
58      }
59  
60      /**
61       * Obtains references to the embedded tomcat and to the host.
62       * Sets this module to be depended on tomcat's service application module. 
63       */
64      protected void createInternal() {
65          Module module = (Module)webServerContext;
66          getDependsOn().add(module);
67          module.getDependOnThis().add(this);
68  
69          tomcat = (Embedded)webServerContext.getBean("tomcat");
70  
71          StandardEngine engine = (StandardEngine)webServerContext.getBean("tomcat.engine");
72          ClassLoader classLoader = getClass().getClassLoader();
73          Loader loader = tomcat.createLoader(classLoader);
74          engine.setLoader(loader);
75  
76          host = (Host)webServerContext.getBean("tomcat.host");
77      }
78  
79      /**
80       * Creates and adds context to the obtained reference of the host.
81       */
82      @Override
83      protected void startInternal() {
84  
85          // TODO check protocol
86          String contextPath = getContextPath();
87          if (contextPath == null) {
88              contextPath = "/" + getModuleId().getArtifactId();
89              setContextPath(contextPath);
90          }
91          if (!contextPath.startsWith("/")) {
92              contextPath = "/" + contextPath;
93              setContextPath(contextPath);
94          }
95          
96          context = tomcat.createContext(contextPath, getWorkingLocation().getFile());
97  
98          ClassLoader classLoader = getClassLoader();
99          Loader loader = tomcat.createLoader(classLoader);
100         context.setLoader(loader);
101         StandardContext standardContext = (StandardContext)context;
102         standardContext.setConfigFile("deploy/tomcat.sar/");
103         standardContext.setDefaultWebXml("web.xml");
104 
105         host.addChild(context);
106 
107     }
108 
109     /**
110      * Removes context from the host.
111      */
112     @Override
113     protected void stopInternal() {
114         host.removeChild(context);
115         context = null;
116     }
117 
118     /**
119      * Removes all references of the embedded tomcat and the host
120      */
121     @Override
122     protected void destroyInternal() {
123         tomcat = null;
124         host = null;
125     }
126 
127     /**
128      * It uses &quot;WEB-INF/web-application.xml&quot; for application context xml.
129      * @return &quot;WEB-INF/web-application.xml&quot;
130      */
131     protected String getContextFileName() {
132         return "WEB-INF/web-application.xml";
133     }
134 
135     /**
136      * Tomcat's service application context
137      * @return tomcat's service application context
138      */
139     public ApplicationContext getWebServerContext() {
140         return webServerContext;
141     }
142 
143     /**
144      * Set's tomcat's service application context
145      * @param webServerContext tomcat's service application context
146      */
147     public void setWebServerContext(ApplicationContext webServerContext) {
148         this.webServerContext = webServerContext;
149     }
150 
151     /**
152      * @return Returns the contextPath.
153      */
154     public String getContextPath() {
155         return contextPath;
156     }
157 
158     /**
159      * @param contextPath The contextPath to set.
160      */
161     public void setContextPath(String contextPath) {
162         this.contextPath = contextPath;
163     }
164     
165     /**
166      * Returns tomcat context
167      * 
168      * @return tomcat context
169      */
170     public Context getTomcatContext() {
171         return context;
172     }
173 
174 }