1 package org.eclipse.jetty.deploy.providers;
2
3 import java.io.File;
4 import java.io.FilenameFilter;
5 import java.io.IOException;
6 import java.net.MalformedURLException;
7
8 import org.eclipse.jetty.deploy.App;
9 import org.eclipse.jetty.deploy.WebAppDeployer;
10 import org.eclipse.jetty.deploy.util.FileID;
11 import org.eclipse.jetty.server.handler.ContextHandler;
12 import org.eclipse.jetty.util.URIUtil;
13 import org.eclipse.jetty.util.resource.Resource;
14 import org.eclipse.jetty.webapp.WebAppContext;
15
16
17
18
19
20
21
22
23
24 public class WebAppProvider extends ScanningAppProvider
25 {
26 private boolean _extractWars = false;
27 private boolean _parentLoaderPriority = false;
28 private String _defaultsDescriptor;
29 private Filter _filter;
30 private String[] _configurationClasses;
31
32 private static class Filter implements FilenameFilter
33 {
34 private File _contexts;
35
36 public boolean accept(File dir, String name)
37 {
38 if (!dir.exists())
39 return false;
40 String lowername = name.toLowerCase();
41
42 File file = new File(dir,name);
43
44 if (!file.isDirectory() && !lowername.endsWith(".war"))
45 return false;
46
47
48 if (file.isDirectory() &&
49 (new File(dir,name+".war").exists() ||
50 new File(dir,name+".WAR").exists()))
51 {
52 return false;
53 }
54
55
56 if (_contexts!=null)
57 {
58 String context=name;
59 if (!file.isDirectory())
60 context=context.substring(0,context.length()-4);
61 if (new File(_contexts,context+".xml").exists() ||
62 new File(_contexts,context+".XML").exists() )
63 {
64 return false;
65 }
66 }
67
68 return true;
69 }
70 }
71
72
73 public WebAppProvider()
74 {
75 super(new Filter());
76 _filter=(Filter)_filenameFilter;
77 setScanInterval(0);
78 }
79
80
81
82
83
84 public boolean isExtractWars()
85 {
86 return _extractWars;
87 }
88
89
90
91
92
93 public void setExtractWars(boolean extractWars)
94 {
95 _extractWars = extractWars;
96 }
97
98
99
100
101
102 public boolean isParentLoaderPriority()
103 {
104 return _parentLoaderPriority;
105 }
106
107
108
109
110
111 public void setParentLoaderPriority(boolean parentLoaderPriority)
112 {
113 _parentLoaderPriority = parentLoaderPriority;
114 }
115
116
117
118
119
120 public String getDefaultsDescriptor()
121 {
122 return _defaultsDescriptor;
123 }
124
125
126
127
128
129 public void setDefaultsDescriptor(String defaultsDescriptor)
130 {
131 _defaultsDescriptor = defaultsDescriptor;
132 }
133
134
135 public String getContextXmlDir()
136 {
137 return _filter._contexts==null?null:_filter._contexts.toString();
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151 public void setContextXmlDir(String contextsDir)
152 {
153 try
154 {
155 _filter._contexts=Resource.newResource(contextsDir).getFile();
156 }
157 catch (MalformedURLException e)
158 {
159 e.printStackTrace();
160 }
161 catch (IOException e)
162 {
163 throw new RuntimeException(e);
164 }
165 }
166
167
168
169
170
171
172 public void setConfigurationClasses(String[] configurations)
173 {
174 _configurationClasses = configurations==null?null:(String[])configurations.clone();
175 }
176
177
178
179
180
181 public String[] getConfigurationClasses()
182 {
183 return _configurationClasses;
184 }
185
186
187
188 public ContextHandler createContextHandler(final App app) throws Exception
189 {
190 Resource resource = Resource.newResource(app.getOriginId());
191 File file = resource.getFile();
192 if (!resource.exists())
193 throw new IllegalStateException("App resouce does not exist "+resource);
194
195 String context = file.getName();
196
197 if (file.isDirectory())
198 {
199
200 }
201 else if (FileID.isWebArchiveFile(file))
202 {
203
204 context = context.substring(0,context.length() - 4);
205 }
206 else
207 throw new IllegalStateException("unable to create ContextHandler for "+app);
208
209
210 if (context.equalsIgnoreCase("root") || context.equalsIgnoreCase("root/"))
211 context = URIUtil.SLASH;
212
213
214 if (context.charAt(0) != '/')
215 context = "/" + context;
216
217
218 if (context.endsWith("/") && context.length() > 0)
219 context = context.substring(0,context.length() - 1);
220
221 WebAppContext wah = new WebAppContext();
222 wah.setContextPath(context);
223 wah.setWar(file.getAbsolutePath());
224 if (_defaultsDescriptor != null)
225 wah.setDefaultsDescriptor(_defaultsDescriptor);
226 wah.setExtractWAR(_extractWars);
227 wah.setParentLoaderPriority(_parentLoaderPriority);
228 if (_configurationClasses != null)
229 wah.setConfigurationClasses(_configurationClasses);
230
231 return wah;
232 }
233
234 }