1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.mule.transport.servlet.jetty; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
|
20 | |
import org.mortbay.component.AbstractLifeCycle; |
21 | |
import org.mortbay.jetty.Handler; |
22 | |
import org.mortbay.jetty.HandlerContainer; |
23 | |
import org.mortbay.jetty.handler.ContextHandler; |
24 | |
import org.mortbay.jetty.handler.ContextHandlerCollection; |
25 | |
import org.mortbay.jetty.webapp.WebAppContext; |
26 | |
import org.mortbay.log.Log; |
27 | |
import org.mortbay.resource.Resource; |
28 | |
import org.mortbay.util.URIUtil; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 0 | public class WebAppDeployer extends AbstractLifeCycle |
35 | |
{ |
36 | |
private HandlerContainer _contexts; |
37 | |
private String _webAppDir; |
38 | |
private String _defaultsDescriptor; |
39 | |
private String[] _configurationClasses; |
40 | |
private boolean _extract; |
41 | |
private boolean _parentLoaderPriority; |
42 | |
private boolean _allowDuplicates; |
43 | |
private ArrayList _deployed; |
44 | |
private String[] serverClasses; |
45 | |
private String[] systemClasses; |
46 | |
|
47 | |
public String[] getConfigurationClasses() |
48 | |
{ |
49 | 0 | return _configurationClasses; |
50 | |
} |
51 | |
|
52 | |
public void setConfigurationClasses(String[] configurationClasses) |
53 | |
{ |
54 | 0 | _configurationClasses=configurationClasses; |
55 | 0 | } |
56 | |
|
57 | |
public HandlerContainer getContexts() |
58 | |
{ |
59 | 0 | return _contexts; |
60 | |
} |
61 | |
|
62 | |
public void setContexts(HandlerContainer contexts) |
63 | |
{ |
64 | 0 | _contexts=contexts; |
65 | 0 | } |
66 | |
|
67 | |
public String getDefaultsDescriptor() |
68 | |
{ |
69 | 0 | return _defaultsDescriptor; |
70 | |
} |
71 | |
|
72 | |
public void setDefaultsDescriptor(String defaultsDescriptor) |
73 | |
{ |
74 | 0 | _defaultsDescriptor=defaultsDescriptor; |
75 | 0 | } |
76 | |
|
77 | |
public boolean isExtract() |
78 | |
{ |
79 | 0 | return _extract; |
80 | |
} |
81 | |
|
82 | |
public void setExtract(boolean extract) |
83 | |
{ |
84 | 0 | _extract=extract; |
85 | 0 | } |
86 | |
|
87 | |
public boolean isParentLoaderPriority() |
88 | |
{ |
89 | 0 | return _parentLoaderPriority; |
90 | |
} |
91 | |
|
92 | |
public void setParentLoaderPriority(boolean parentPriorityClassLoading) |
93 | |
{ |
94 | 0 | _parentLoaderPriority=parentPriorityClassLoading; |
95 | 0 | } |
96 | |
|
97 | |
public String getWebAppDir() |
98 | |
{ |
99 | 0 | return _webAppDir; |
100 | |
} |
101 | |
|
102 | |
public void setWebAppDir(String dir) |
103 | |
{ |
104 | 0 | _webAppDir=dir; |
105 | 0 | } |
106 | |
|
107 | |
public boolean getAllowDuplicates() |
108 | |
{ |
109 | 0 | return _allowDuplicates; |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public void setAllowDuplicates(boolean allowDuplicates) |
117 | |
{ |
118 | 0 | _allowDuplicates=allowDuplicates; |
119 | 0 | } |
120 | |
|
121 | |
public String[] getServerClasses() |
122 | |
{ |
123 | 0 | return serverClasses; |
124 | |
} |
125 | |
|
126 | |
public void setServerClasses(String[] serverClasses) |
127 | |
{ |
128 | 0 | this.serverClasses = serverClasses; |
129 | 0 | } |
130 | |
|
131 | |
public String[] getSystemClasses() |
132 | |
{ |
133 | 0 | return systemClasses; |
134 | |
} |
135 | |
|
136 | |
public void setSystemClasses(String[] systemClasses) |
137 | |
{ |
138 | 0 | this.systemClasses = systemClasses; |
139 | 0 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
public void doStart() throws Exception |
146 | |
{ |
147 | 0 | _deployed=new ArrayList(); |
148 | |
|
149 | 0 | scan(); |
150 | |
|
151 | 0 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public void scan() throws Exception |
159 | |
{ |
160 | 0 | if (_contexts==null) |
161 | 0 | throw new IllegalArgumentException("No HandlerContainer"); |
162 | |
|
163 | 0 | Resource r=Resource.newResource(_webAppDir); |
164 | 0 | if (!r.exists()) |
165 | 0 | throw new IllegalArgumentException("No such webapps resource "+r); |
166 | |
|
167 | 0 | if (!r.isDirectory()) |
168 | 0 | throw new IllegalArgumentException("Not directory webapps resource "+r); |
169 | |
|
170 | 0 | String[] files=r.list(); |
171 | |
|
172 | 0 | files: for (int f=0; files!=null&&f<files.length; f++) |
173 | |
{ |
174 | 0 | String context=files[f]; |
175 | |
|
176 | 0 | if (context.equalsIgnoreCase("CVS/")||context.equalsIgnoreCase("CVS")||context.startsWith(".")) |
177 | 0 | continue; |
178 | |
|
179 | 0 | Resource app=r.addPath(r.encode(context)); |
180 | |
|
181 | 0 | if (context.toLowerCase().endsWith(".war")||context.toLowerCase().endsWith(".jar")) |
182 | |
{ |
183 | 0 | context=context.substring(0,context.length()-4); |
184 | 0 | Resource unpacked=r.addPath(context); |
185 | 0 | if (unpacked!=null&&unpacked.exists()&&unpacked.isDirectory()) |
186 | 0 | continue; |
187 | 0 | } |
188 | 0 | else if (!app.isDirectory()) |
189 | 0 | continue; |
190 | |
|
191 | 0 | if (context.equalsIgnoreCase("root")||context.equalsIgnoreCase("root/")) |
192 | 0 | context=URIUtil.SLASH; |
193 | |
else |
194 | 0 | context="/"+context; |
195 | 0 | if (context.endsWith("/")&&context.length()>0) |
196 | 0 | context=context.substring(0,context.length()-1); |
197 | |
|
198 | |
|
199 | 0 | if (!_allowDuplicates) |
200 | |
{ |
201 | 0 | Handler[] installed=_contexts.getChildHandlersByClass(ContextHandler.class); |
202 | 0 | for (int i=0; i<installed.length; i++) |
203 | |
{ |
204 | 0 | ContextHandler c=(ContextHandler)installed[i]; |
205 | |
|
206 | 0 | if (context.equals(c.getContextPath())) |
207 | 0 | continue files; |
208 | |
|
209 | |
try |
210 | |
{ |
211 | 0 | String path=null; |
212 | 0 | if (c instanceof WebAppContext) |
213 | 0 | path = Resource.newResource(((WebAppContext)c).getWar()).getFile().getAbsolutePath(); |
214 | 0 | else if (c.getBaseResource()!=null) |
215 | 0 | path = c.getBaseResource().getFile().getAbsolutePath(); |
216 | |
|
217 | 0 | if (path!=null && path.equals(app.getFile().getAbsolutePath())) |
218 | 0 | continue files; |
219 | |
} |
220 | 0 | catch (Exception e) |
221 | |
{ |
222 | 0 | Log.ignore(e); |
223 | 0 | } |
224 | |
} |
225 | |
} |
226 | |
|
227 | |
|
228 | 0 | WebAppContext wah=null; |
229 | 0 | if (_contexts instanceof ContextHandlerCollection && |
230 | |
WebAppContext.class.isAssignableFrom(((ContextHandlerCollection)_contexts).getContextClass())) |
231 | |
{ |
232 | |
try |
233 | |
{ |
234 | 0 | wah=(WebAppContext)((ContextHandlerCollection)_contexts).getContextClass().newInstance(); |
235 | |
} |
236 | 0 | catch (Exception e) |
237 | |
{ |
238 | 0 | throw new Error(e); |
239 | 0 | } |
240 | |
} |
241 | |
else |
242 | |
{ |
243 | 0 | wah=new WebAppContext(); |
244 | |
} |
245 | |
|
246 | |
|
247 | 0 | wah.setContextPath(context); |
248 | 0 | if (_configurationClasses!=null) |
249 | 0 | wah.setConfigurationClasses(_configurationClasses); |
250 | 0 | if (_defaultsDescriptor!=null) |
251 | 0 | wah.setDefaultsDescriptor(_defaultsDescriptor); |
252 | 0 | wah.setExtractWAR(_extract); |
253 | 0 | wah.setWar(app.toString()); |
254 | 0 | wah.setParentLoaderPriority(_parentLoaderPriority); |
255 | |
|
256 | 0 | if (serverClasses != null) |
257 | |
{ |
258 | 0 | wah.setServerClasses(serverClasses); |
259 | |
} |
260 | |
|
261 | 0 | if (systemClasses != null) |
262 | |
{ |
263 | 0 | wah.setSystemClasses(systemClasses); |
264 | |
} |
265 | |
|
266 | |
|
267 | 0 | _contexts.addHandler(wah); |
268 | 0 | _deployed.add(wah); |
269 | |
|
270 | 0 | if (_contexts.isStarted()) |
271 | 0 | _contexts.start(); |
272 | |
} |
273 | 0 | } |
274 | |
|
275 | |
public void doStop() throws Exception |
276 | |
{ |
277 | 0 | for (int i=_deployed.size();i-->0;) |
278 | |
{ |
279 | 0 | ContextHandler wac = (ContextHandler)_deployed.get(i); |
280 | 0 | wac.stop(); |
281 | 0 | } |
282 | 0 | } |
283 | |
} |