1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.eclipse.jetty.osgi.boot;
16
17 import java.io.File;
18 import java.io.FilenameFilter;
19 import java.io.IOException;
20
21 import org.eclipse.jetty.deploy.App;
22 import org.eclipse.jetty.deploy.AppProvider;
23 import org.eclipse.jetty.deploy.DeploymentManager;
24 import org.eclipse.jetty.deploy.providers.ContextProvider;
25 import org.eclipse.jetty.deploy.providers.ScanningAppProvider;
26 import org.eclipse.jetty.server.handler.ContextHandler;
27 import org.eclipse.jetty.util.resource.Resource;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class OSGiAppProvider extends ScanningAppProvider implements AppProvider
44 {
45
46 private boolean _extractWars = false;
47 private boolean _parentLoaderPriority = false;
48 private String _defaultsDescriptor;
49 private String _tldBundles;
50
51
52
53
54
55 private static class Filter implements FilenameFilter
56 {
57 OSGiAppProvider _enclosedInstance;
58
59 public boolean accept(File dir, String name)
60 {
61 if (!new File(dir,name).isDirectory())
62 {
63 String contextName = getDeployedAppName(name);
64 if (contextName != null)
65 {
66 App app = _enclosedInstance.getDeployedApps().get(contextName);
67 return app != null;
68 }
69 }
70 return false;
71 }
72 }
73
74
75
76
77
78
79
80 private static String getDeployedAppName(String contextFileName)
81 {
82 String lowername = contextFileName.toLowerCase();
83 if (lowername.endsWith(".xml"))
84 {
85 String contextName = contextFileName.substring(0,lowername.length() - ".xml".length());
86 return contextName;
87 }
88 return null;
89 }
90
91
92
93
94
95
96
97 public OSGiAppProvider()
98 {
99 super(new Filter());
100 ((Filter)super._filenameFilter)._enclosedInstance = this;
101 }
102
103
104
105
106
107
108
109 public OSGiAppProvider(File contextsDir) throws IOException
110 {
111 this();
112 setMonitoredDir(Resource.newResource(contextsDir.toURI()));
113 }
114
115
116
117
118
119
120 public ContextHandler createContextHandler(App app) throws Exception
121 {
122
123 if (app.getContextId() != null)
124 {
125 return app.getContextHandler();
126 }
127
128
129
130
131 throw new IllegalStateException("The App must be passed the " + "instance of the ContextHandler when it is construsted");
132 }
133
134
135
136
137 public void setDeploymentManager(DeploymentManager deploymentManager)
138 {
139
140 super.setDeploymentManager(deploymentManager);
141 }
142
143
144
145
146
147 public void addContext(ContextHandler context) throws Exception
148 {
149
150
151
152 App app = new App(getDeploymentManager(),this,context.getDisplayName(),context);
153 getDeployedApps().put(context.getDisplayName(),app);
154 getDeploymentManager().addApp(app);
155 }
156
157
158
159
160
161
162
163
164
165
166 @Override
167 protected App createApp(String filename)
168 {
169
170
171
172 String name = getDeployedAppName(filename);
173 if (name != null)
174 {
175 return getDeployedApps().get(name);
176 }
177 return null;
178 }
179
180 public void removeContext(ContextHandler context) throws Exception
181 {
182 App app = getDeployedApps().remove(context.getDisplayName());
183 if (app != null)
184 {
185 getDeploymentManager().removeApp(app);
186 }
187 }
188
189
190
191
192
193
194
195
196
197 public boolean isParentLoaderPriority()
198 {
199 return _parentLoaderPriority;
200 }
201
202
203
204
205
206
207
208
209 public void setParentLoaderPriority(boolean parentLoaderPriority)
210 {
211 _parentLoaderPriority = parentLoaderPriority;
212 }
213
214
215
216
217
218
219
220 public String getDefaultsDescriptor()
221 {
222 return _defaultsDescriptor;
223 }
224
225
226
227
228
229
230
231
232 public void setDefaultsDescriptor(String defaultsDescriptor)
233 {
234 _defaultsDescriptor = defaultsDescriptor;
235 }
236
237
238
239
240
241 public File getContextXmlDirAsFile()
242 {
243 try
244 {
245 Resource monitoredDir = getMonitoredDir();
246 if (monitoredDir == null)
247 return null;
248 return monitoredDir.getFile();
249 }
250 catch (IOException e)
251 {
252 e.printStackTrace();
253 return null;
254 }
255 }
256
257
258
259
260
261
262 public String getContextXmlDir()
263 {
264 try
265 {
266 Resource monitoredDir = getMonitoredDir();
267 if (monitoredDir == null)
268 return null;
269 return monitoredDir.getFile().toURI().toString();
270 }
271 catch (IOException e)
272 {
273 e.printStackTrace();
274 return null;
275 }
276 }
277
278
279
280
281
282
283
284
285
286
287
288
289
290 public void setContextXmlDir(String contextsDir)
291 {
292 setMonitoredDir(contextsDir);
293 }
294
295
296
297
298
299 public void setTldBundles(String tldBundles)
300 {
301 _tldBundles = tldBundles;
302 }
303
304
305
306
307
308 public String getTldBundles()
309 {
310 return _tldBundles;
311 }
312
313 }