1
2
3
4
5
6
7
8
9
10
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
28
29
30
31
32
33
34
35
36 public class TomcatWebApplicationContext extends ServiceApplicationContextModule {
37
38
39 protected Embedded tomcat;
40
41
42 protected Host host;
43
44
45 protected Context context;
46
47
48 protected ApplicationContext webServerContext;
49
50
51 protected String contextPath;
52
53
54
55
56 public TomcatWebApplicationContext(ModuleId moduleId) {
57 super(moduleId);
58 }
59
60
61
62
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
81
82 @Override
83 protected void startInternal() {
84
85
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
111
112 @Override
113 protected void stopInternal() {
114 host.removeChild(context);
115 context = null;
116 }
117
118
119
120
121 @Override
122 protected void destroyInternal() {
123 tomcat = null;
124 host = null;
125 }
126
127
128
129
130
131 protected String getContextFileName() {
132 return "WEB-INF/web-application.xml";
133 }
134
135
136
137
138
139 public ApplicationContext getWebServerContext() {
140 return webServerContext;
141 }
142
143
144
145
146
147 public void setWebServerContext(ApplicationContext webServerContext) {
148 this.webServerContext = webServerContext;
149 }
150
151
152
153
154 public String getContextPath() {
155 return contextPath;
156 }
157
158
159
160
161 public void setContextPath(String contextPath) {
162 this.contextPath = contextPath;
163 }
164
165
166
167
168
169
170 public Context getTomcatContext() {
171 return context;
172 }
173
174 }