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 import java.util.Map.Entry;
21
22 import org.eclipse.jetty.deploy.App;
23 import org.eclipse.jetty.deploy.AppProvider;
24 import org.eclipse.jetty.deploy.DeploymentManager;
25 import org.eclipse.jetty.deploy.providers.ContextProvider;
26 import org.eclipse.jetty.deploy.providers.ScanningAppProvider;
27 import org.eclipse.jetty.server.handler.ContextHandler;
28 import org.eclipse.jetty.util.resource.Resource;
29 import org.eclipse.jetty.webapp.WebAppContext;
30 import org.osgi.framework.Bundle;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class OSGiAppProvider extends ScanningAppProvider implements AppProvider
47 {
48
49 private boolean _extractWars = true;
50 private boolean _parentLoaderPriority = false;
51 private String _defaultsDescriptor;
52 private String _tldBundles;
53
54
55
56
57
58 private static class Filter implements FilenameFilter
59 {
60 OSGiAppProvider _enclosedInstance;
61
62 public boolean accept(File dir, String name)
63 {
64 if (!new File(dir,name).isDirectory())
65 {
66 String contextName = getDeployedAppName(name);
67 if (contextName != null)
68 {
69 App app = _enclosedInstance.getDeployedApps().get(contextName);
70 return app != null;
71 }
72 }
73 return false;
74 }
75 }
76
77
78
79
80
81
82
83 private static String getDeployedAppName(String contextFileName)
84 {
85 String lowername = contextFileName.toLowerCase();
86 if (lowername.endsWith(".xml"))
87 {
88 String contextName = contextFileName.substring(0,lowername.length() - ".xml".length());
89 return contextName;
90 }
91 return null;
92 }
93
94
95
96
97
98
99
100
101 private String getContextHandlerAppName(ContextHandler context) {
102 String appName = context.getDisplayName();
103 if (appName == null || appName.length() == 0 || getDeployedApps().containsKey(appName)) {
104 if (context instanceof WebAppContext)
105 {
106 appName = ((WebAppContext)context).getContextPath();
107 if (getDeployedApps().containsKey(appName)) {
108 appName = "noDisplayName"+context.getClass().getSimpleName()+context.hashCode();
109 }
110 } else {
111 appName = "noDisplayName"+context.getClass().getSimpleName()+context.hashCode();
112 }
113 }
114 return appName;
115 }
116
117
118
119
120
121 public OSGiAppProvider()
122 {
123 super(new Filter());
124 ((Filter)super._filenameFilter)._enclosedInstance = this;
125 }
126
127
128
129
130
131
132
133 public OSGiAppProvider(File contextsDir) throws IOException
134 {
135 this();
136 setMonitoredDirResource(Resource.newResource(contextsDir.toURI()));
137 }
138
139
140
141
142
143
144 public ContextHandler createContextHandler(App app) throws Exception
145 {
146
147 if (app.getContextHandler() != null)
148 {
149 return app.getContextHandler();
150 }
151
152
153
154
155 throw new IllegalStateException("The App must be passed the " + "instance of the ContextHandler when it is construsted");
156 }
157
158
159
160
161 public void setDeploymentManager(DeploymentManager deploymentManager)
162 {
163
164 super.setDeploymentManager(deploymentManager);
165 }
166
167 private static String getOriginId(Bundle contributor, String pathInBundle)
168 {
169 return contributor.getSymbolicName() + "-" + contributor.getVersion().toString() +
170 (pathInBundle.startsWith("/") ? pathInBundle : "/" + pathInBundle);
171 }
172
173
174
175
176
177 public void addContext(Bundle contributor, String pathInBundle, ContextHandler context) throws Exception
178 {
179 addContext(getOriginId(contributor, pathInBundle), context);
180 }
181
182
183
184
185 public void addContext(String originId, ContextHandler context) throws Exception
186 {
187
188 if (context instanceof WebAppContext)
189 {
190 ((WebAppContext)context).setExtractWAR(isExtract());
191 }
192
193
194 App app = new App(getDeploymentManager(),this,originId,context);
195 String appName = getContextHandlerAppName(context);
196 getDeployedApps().put(appName,app);
197 getDeploymentManager().addApp(app);
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211 @Override
212 protected App createApp(String filename)
213 {
214
215
216
217 String name = getDeployedAppName(filename);
218 if (name != null)
219 {
220 return getDeployedApps().get(name);
221 }
222 return null;
223 }
224
225 public void removeContext(ContextHandler context) throws Exception
226 {
227 String appName = getContextHandlerAppName(context);
228 App app = getDeployedApps().remove(context.getDisplayName());
229 if (app == null) {
230
231
232 appName = null;
233 for (Entry<String,App> deployedApp : getDeployedApps().entrySet()) {
234 if (deployedApp.getValue().getContextHandler() == context) {
235 app = deployedApp.getValue();
236 appName = deployedApp.getKey();
237 break;
238 }
239 }
240 if (appName != null) {
241 getDeployedApps().remove(appName);
242 }
243 }
244 if (app != null)
245 {
246 getDeploymentManager().removeApp(app);
247 }
248 }
249
250
251
252
253
254
255
256
257
258 public boolean isParentLoaderPriority()
259 {
260 return _parentLoaderPriority;
261 }
262
263
264
265
266
267
268
269
270 public void setParentLoaderPriority(boolean parentLoaderPriority)
271 {
272 _parentLoaderPriority = parentLoaderPriority;
273 }
274
275
276
277
278
279
280
281 public String getDefaultsDescriptor()
282 {
283 return _defaultsDescriptor;
284 }
285
286
287
288
289
290
291
292
293 public void setDefaultsDescriptor(String defaultsDescriptor)
294 {
295 _defaultsDescriptor = defaultsDescriptor;
296 }
297
298
299
300
301
302 public File getContextXmlDirAsFile()
303 {
304 try
305 {
306 Resource monitoredDir = getMonitoredDirResource();
307 if (monitoredDir == null)
308 return null;
309 return monitoredDir.getFile();
310 }
311 catch (IOException e)
312 {
313 e.printStackTrace();
314 return null;
315 }
316 }
317
318
319
320
321
322
323 public String getContextXmlDir()
324 {
325 try
326 {
327 Resource monitoredDir = getMonitoredDirResource();
328 if (monitoredDir == null)
329 return null;
330 return monitoredDir.getFile().toURI().toString();
331 }
332 catch (IOException e)
333 {
334 e.printStackTrace();
335 return null;
336 }
337 }
338
339 public boolean isExtract()
340 {
341 return _extractWars;
342 }
343
344 public void setExtract(boolean extract)
345 {
346 _extractWars=extract;
347 }
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362 public void setContextXmlDir(String contextsDir)
363 {
364 setMonitoredDirName(contextsDir);
365 }
366
367
368
369
370
371 public void setTldBundles(String tldBundles)
372 {
373 _tldBundles = tldBundles;
374 }
375
376
377
378
379
380 public String getTldBundles()
381 {
382 return _tldBundles;
383 }
384
385 }