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