1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.spring.server.deployment.jetty;
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.mortbay.jetty.Server;
19 import org.mortbay.jetty.servlet.Context;
20 import org.mortbay.jetty.webapp.WebAppContext;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.springframework.context.ApplicationContext;
24
25
26
27
28
29
30
31
32
33
34
35 public class JettyWebApplicationContext extends ServiceApplicationContextModule {
36
37
38 private final Logger logger = LoggerFactory.getLogger(JettyWebApplicationContext.class);
39
40
41 protected Server jetty;
42
43
44 protected WebAppContext context;
45
46
47 protected ApplicationContext webServerContext;
48
49
50 protected String contextPath;
51
52
53
54
55 public JettyWebApplicationContext(ModuleId moduleId) {
56 super(moduleId);
57 }
58
59
60
61
62
63 protected void createInternal() {
64 Module module = (Module)webServerContext;
65 getDependsOn().add(module);
66 module.getDependOnThis().add(this);
67
68 jetty = (Server)webServerContext.getBean("jetty");
69 }
70
71
72
73
74 @Override
75 protected void startInternal() {
76
77
78 String contextPath = getContextPath();
79 if (contextPath == null) {
80 contextPath = "/" + getModuleId().getArtifactId();
81 setContextPath(contextPath);
82 }
83 if (!contextPath.startsWith("/")) {
84 contextPath = "/" + contextPath;
85 setContextPath(contextPath);
86 }
87
88 context = new WebAppContext(getWorkingLocation().toString(), contextPath);
89 if (logger.isDebugEnabled()) {
90 logger.debug("Started context: " + contextPath);
91 }
92
93 jetty.addHandler(context);
94
95 try {
96 context.start();
97 } catch (Exception e) {
98 throw new RuntimeException(e);
99 }
100 }
101
102
103
104
105 @Override
106 protected void stopInternal() {
107 jetty.removeHandler(context);
108 try {
109 context.stop();
110 } catch (Exception e) {
111 throw new RuntimeException(e);
112 }
113 context = null;
114 }
115
116
117
118
119 @Override
120 protected void destroyInternal() {
121 jetty = null;
122 }
123
124
125
126
127
128 protected String getContextFileName() {
129 return "WEB-INF/web-application.xml";
130 }
131
132
133
134
135
136 public ApplicationContext getWebServerContext() {
137 return webServerContext;
138 }
139
140
141
142
143
144 public void setWebServerContext(ApplicationContext webServerContext) {
145 this.webServerContext = webServerContext;
146 }
147
148
149
150
151 public String getContextPath() {
152 return contextPath;
153 }
154
155
156
157
158 public void setContextPath(String contextPath) {
159 this.contextPath = contextPath;
160 }
161
162
163
164
165
166
167 public Context getJettyContext() {
168 return context;
169 }
170
171 }