1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.osgi.boot;
20
21 import java.io.File;
22 import java.net.URL;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Dictionary;
26 import java.util.Enumeration;
27 import java.util.HashMap;
28 import java.util.List;
29
30 import org.eclipse.jetty.deploy.App;
31 import org.eclipse.jetty.deploy.AppProvider;
32 import org.eclipse.jetty.deploy.DeploymentManager;
33 import org.eclipse.jetty.osgi.boot.internal.serverfactory.ServerInstanceWrapper;
34 import org.eclipse.jetty.osgi.boot.internal.webapp.OSGiWebappClassLoader;
35 import org.eclipse.jetty.osgi.boot.utils.BundleFileLocatorHelperFactory;
36 import org.eclipse.jetty.server.handler.ContextHandler;
37 import org.eclipse.jetty.util.ArrayUtil;
38 import org.eclipse.jetty.util.component.AbstractLifeCycle;
39 import org.eclipse.jetty.util.Loader;
40 import org.eclipse.jetty.util.log.Log;
41 import org.eclipse.jetty.util.log.Logger;
42 import org.eclipse.jetty.util.resource.JarResource;
43 import org.eclipse.jetty.util.resource.Resource;
44 import org.eclipse.jetty.webapp.WebAppContext;
45 import org.eclipse.jetty.xml.XmlConfiguration;
46 import org.osgi.framework.Bundle;
47 import org.osgi.framework.BundleContext;
48 import org.osgi.framework.ServiceReference;
49 import org.osgi.service.packageadmin.PackageAdmin;
50
51
52
53
54
55
56
57
58
59
60
61 public abstract class AbstractWebAppProvider extends AbstractLifeCycle implements AppProvider
62 {
63 private static final Logger LOG = Log.getLogger(AbstractWebAppProvider.class);
64
65 public static String __defaultConfigurations[] = {
66 "org.eclipse.jetty.osgi.boot.OSGiWebInfConfiguration",
67 "org.eclipse.jetty.webapp.WebXmlConfiguration",
68 "org.eclipse.jetty.webapp.MetaInfConfiguration",
69 "org.eclipse.jetty.webapp.FragmentConfiguration",
70 "org.eclipse.jetty.webapp.JettyWebXmlConfiguration"
71 };
72
73 public static void setDefaultConfigurations (String[] defaultConfigs)
74 {
75 __defaultConfigurations = defaultConfigs;
76 }
77
78 public static String[] getDefaultConfigurations ()
79 {
80 List<String> configs = ArrayUtil.asMutableList(__defaultConfigurations);
81 if (annotationsAvailable())
82 {
83
84 int i = configs.indexOf("org.eclipse.jetty.webapp.JettyWebXmlConfiguration");
85 configs.add(i, "org.eclipse.jetty.osgi.annotations.AnnotationConfiguration");
86 }
87
88 if (jndiAvailable())
89 {
90
91 int i = configs.indexOf("org.eclipse.jetty.webapp.FragmentConfiguration");
92 configs.add(++i, "org.eclipse.jetty.plus.webapp.EnvConfiguration");
93 configs.add(++i, "org.eclipse.jetty.plus.webapp.PlusConfiguration");
94 }
95
96 return configs.toArray(new String[configs.size()]);
97 }
98
99 private static boolean annotationsAvailable()
100 {
101 boolean result = false;
102 try
103 {
104 Loader.loadClass(AbstractWebAppProvider.class,"org.eclipse.jetty.annotations.AnnotationConfiguration");
105 result = true;
106 LOG.debug("Annotation support detected");
107 }
108 catch (ClassNotFoundException e)
109 {
110 result = false;
111 LOG.debug("No annotation support detected");
112 }
113
114 return result;
115 }
116
117
118 private static boolean jndiAvailable()
119 {
120 try
121 {
122 Loader.loadClass(AbstractWebAppProvider.class, "org.eclipse.jetty.plus.jndi.Resource");
123 Loader.loadClass(AbstractWebAppProvider.class, "org.eclipse.jetty.plus.webapp.EnvConfiguration");
124 LOG.debug("JNDI support detected");
125 return true;
126 }
127 catch (ClassNotFoundException e)
128 {
129 LOG.debug("No JNDI support detected");
130 return false;
131 }
132 }
133
134
135 private boolean _parentLoaderPriority;
136
137 private String _defaultsDescriptor;
138
139 private boolean _extractWars = true;
140
141 private String _tldBundles;
142
143 private DeploymentManager _deploymentManager;
144
145 private String[] _configurationClasses;
146
147 private ServerInstanceWrapper _serverWrapper;
148
149
150
151
152
153
154
155 public class OSGiApp extends AbstractOSGiApp
156 {
157 private String _contextPath;
158 private String _webAppPath;
159 private WebAppContext _webApp;
160
161 public OSGiApp(DeploymentManager manager, AppProvider provider, Bundle bundle, String originId)
162 {
163 super(manager, provider, bundle, originId);
164 }
165
166 public OSGiApp(DeploymentManager manager, AppProvider provider, Bundle bundle, Dictionary properties, String originId)
167 {
168 super(manager, provider, bundle, properties, originId);
169 }
170
171 public void setWebAppContext (WebAppContext webApp)
172 {
173 _webApp = webApp;
174 }
175
176 public String getContextPath()
177 {
178 return _contextPath;
179 }
180
181 public void setContextPath(String contextPath)
182 {
183 this._contextPath = contextPath;
184 }
185
186 public String getBundlePath()
187 {
188 return _webAppPath;
189 }
190
191 public void setWebAppPath(String path)
192 {
193 this._webAppPath = path;
194 }
195
196
197 public ContextHandler createContextHandler()
198 throws Exception
199 {
200 if (_webApp != null)
201 {
202 configureWebApp();
203 return _webApp;
204 }
205
206 createWebApp();
207 return _webApp;
208 }
209
210
211
212 protected void createWebApp ()
213 throws Exception
214 {
215 _webApp = newWebApp();
216 configureWebApp();
217 }
218
219 protected WebAppContext newWebApp ()
220 {
221 WebAppContext webApp = new WebAppContext();
222 webApp.setAttribute(OSGiWebappConstants.WATERMARK, OSGiWebappConstants.WATERMARK);
223
224
225 String[] targets = webApp.getProtectedTargets();
226 String[] updatedTargets = null;
227 if (targets != null)
228 {
229 updatedTargets = new String[targets.length+OSGiWebappConstants.DEFAULT_PROTECTED_OSGI_TARGETS.length];
230 System.arraycopy(targets, 0, updatedTargets, 0, targets.length);
231 }
232 else
233 updatedTargets = new String[OSGiWebappConstants.DEFAULT_PROTECTED_OSGI_TARGETS.length];
234 System.arraycopy(OSGiWebappConstants.DEFAULT_PROTECTED_OSGI_TARGETS, 0, updatedTargets, targets.length, OSGiWebappConstants.DEFAULT_PROTECTED_OSGI_TARGETS.length);
235 webApp.setProtectedTargets(updatedTargets);
236
237 return webApp;
238 }
239
240
241 public void configureWebApp()
242 throws Exception
243 {
244
245 _webApp.setContextPath(_contextPath);
246
247
248 _webApp.setAttribute(OSGiWebappConstants.OSGI_BUNDLECONTEXT, _bundle.getBundleContext());
249
250 String overrideBundleInstallLocation = (String)_properties.get(OSGiWebappConstants.JETTY_BUNDLE_INSTALL_LOCATION_OVERRIDE);
251 File bundleInstallLocation =
252 (overrideBundleInstallLocation == null
253 ? BundleFileLocatorHelperFactory.getFactory().getHelper().getBundleInstallLocation(_bundle)
254 : new File(overrideBundleInstallLocation));
255 URL url = null;
256 Resource rootResource = Resource.newResource(BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(bundleInstallLocation.toURI().toURL()));
257
258 if (rootResource.exists()&& !rootResource.isDirectory() && !rootResource.toString().startsWith("jar:"))
259 {
260 Resource jarResource = JarResource.newJarResource(rootResource);
261 if (jarResource.exists() && jarResource.isDirectory())
262 rootResource = jarResource;
263 }
264
265
266 if (_webAppPath == null || _webAppPath.length() == 0 || ".".equals(_webAppPath))
267 {
268 url = bundleInstallLocation.toURI().toURL();
269 }
270 else
271 {
272
273 if (_webAppPath.startsWith("/") || _webAppPath.startsWith("file:"))
274 {
275 url = new File(_webAppPath).toURI().toURL();
276 }
277 else if (bundleInstallLocation != null && bundleInstallLocation.isDirectory())
278 {
279 url = new File(bundleInstallLocation, _webAppPath).toURI().toURL();
280 }
281 else if (bundleInstallLocation != null)
282 {
283 Enumeration<URL> urls = BundleFileLocatorHelperFactory.getFactory().getHelper().findEntries(_bundle, _webAppPath);
284 if (urls != null && urls.hasMoreElements())
285 url = urls.nextElement();
286 }
287 }
288
289 if (url == null)
290 {
291 throw new IllegalArgumentException("Unable to locate " + _webAppPath
292 + " in "
293 + (bundleInstallLocation != null ? bundleInstallLocation.getAbsolutePath() : "unlocated bundle '" + _bundle.getSymbolicName()+ "'"));
294 }
295
296
297
298 _webApp.setWar(BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(url).toString());
299
300
301 _webApp.setParentLoaderPriority(isParentLoaderPriority());
302 _webApp.setExtractWAR(isExtract());
303 if (getConfigurationClasses() != null)
304 _webApp.setConfigurationClasses(getConfigurationClasses());
305 else
306 _webApp.setConfigurationClasses(getDefaultConfigurations());
307
308 if (getDefaultsDescriptor() != null)
309 _webApp.setDefaultsDescriptor(getDefaultsDescriptor());
310
311
312
313 String tmp = (String)_properties.get(OSGiWebappConstants.JETTY_EXTRA_CLASSPATH);
314 if (tmp != null)
315 _webApp.setExtraClasspath(tmp);
316
317
318 tmp = (String)_properties.get(OSGiWebappConstants.JETTY_WEB_XML_PATH);
319 if (tmp != null && tmp.trim().length() != 0)
320 {
321 File webXml = getFile (tmp, bundleInstallLocation);
322 if (webXml != null && webXml.exists())
323 _webApp.setDescriptor(webXml.getAbsolutePath());
324 }
325
326
327 tmp = (String)_properties.get(OSGiWebappConstants.JETTY_DEFAULT_WEB_XML_PATH);
328 if (tmp != null)
329 {
330 File defaultWebXml = getFile (tmp, bundleInstallLocation);
331 if (defaultWebXml != null)
332 {
333 if (defaultWebXml.exists())
334 _webApp.setDefaultsDescriptor(defaultWebXml.getAbsolutePath());
335 else
336 LOG.warn(defaultWebXml.getAbsolutePath()+" does not exist");
337 }
338 }
339
340
341
342
343 String requireTldBundles = (String)_properties.get(OSGiWebappConstants.REQUIRE_TLD_BUNDLE);
344 String pathsToTldBundles = getPathsToRequiredBundles(requireTldBundles);
345
346
347
348
349 OSGiWebappClassLoader webAppLoader = new OSGiWebappClassLoader(_serverWrapper.getParentClassLoaderForWebapps(), _webApp, _bundle);
350
351 if (pathsToTldBundles != null)
352 webAppLoader.addClassPath(pathsToTldBundles);
353 _webApp.setClassLoader(webAppLoader);
354
355
356
357
358 applyMetaInfContextXml(rootResource, overrideBundleInstallLocation);
359
360 _webApp.setAttribute(OSGiWebappConstants.REQUIRE_TLD_BUNDLE, requireTldBundles);
361
362
363
364 _webApp.setAttribute(OSGiWebappConstants.RFC66_OSGI_BUNDLE_CONTEXT, _bundle.getBundleContext());
365
366
367
368
369
370 _webApp.setAttribute("org.springframework.osgi.web." + BundleContext.class.getName(), _bundle.getBundleContext());
371
372
373
374
375
376 _webApp.setAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE, _bundle);
377 }
378
379 protected String getPathsToRequiredBundles (String requireTldBundles)
380 throws Exception
381 {
382 if (requireTldBundles == null) return null;
383
384 ServiceReference ref = _bundle.getBundleContext().getServiceReference(org.osgi.service.packageadmin.PackageAdmin.class.getName());
385 PackageAdmin packageAdmin = (ref == null) ? null : (PackageAdmin)_bundle.getBundleContext().getService(ref);
386 if (packageAdmin == null)
387 throw new IllegalStateException("Unable to get PackageAdmin reference to locate required Tld bundles");
388
389 StringBuilder paths = new StringBuilder();
390 String[] symbNames = requireTldBundles.split(", ");
391
392 for (String symbName : symbNames)
393 {
394 Bundle[] bs = packageAdmin.getBundles(symbName, null);
395 if (bs == null || bs.length == 0)
396 {
397 throw new IllegalArgumentException("Unable to locate the bundle '" + symbName
398 + "' specified by "
399 + OSGiWebappConstants.REQUIRE_TLD_BUNDLE
400 + " in manifest of "
401 + (_bundle == null ? "unknown" : _bundle.getSymbolicName()));
402 }
403
404 File f = BundleFileLocatorHelperFactory.getFactory().getHelper().getBundleInstallLocation(bs[0]);
405 if (paths.length() > 0) paths.append(", ");
406 paths.append(f.toURI().toURL().toString());
407 LOG.debug("getPathsToRequiredBundles: bundle path=" + bs[0].getLocation() + " uri=" + f.toURI());
408 }
409
410 return paths.toString();
411 }
412
413
414 protected void applyMetaInfContextXml(Resource rootResource, String overrideBundleInstallLocation)
415 throws Exception
416 {
417 if (_bundle == null) return;
418 if (_webApp == null) return;
419
420 ClassLoader cl = Thread.currentThread().getContextClassLoader();
421 LOG.debug("Context classloader = " + cl);
422 try
423 {
424
425 Thread.currentThread().setContextClassLoader(_webApp.getClassLoader());
426
427
428
429 URL contextXmlUrl = _bundle.getEntry("/META-INF/jetty-webapp-context.xml");
430
431 if (contextXmlUrl == null)
432 {
433
434 if (_properties != null)
435 {
436 String tmp = (String)_properties.get(OSGiWebappConstants.JETTY_CONTEXT_FILE_PATH);
437 if (tmp != null)
438 {
439 String[] filenames = tmp.split(",;");
440 if (filenames != null && filenames.length > 0)
441 {
442 String filename = filenames[0];
443 String jettyHome = (String)getServerInstanceWrapper().getServer().getAttribute(OSGiServerConstants.JETTY_HOME);
444 if (jettyHome == null)
445 jettyHome = System.getProperty(OSGiServerConstants.JETTY_HOME);
446 Resource res = findFile(filename, jettyHome, overrideBundleInstallLocation, _bundle);
447 if (res != null)
448 contextXmlUrl = res.getURL();
449 }
450 }
451 }
452 }
453 if (contextXmlUrl == null) return;
454
455
456 LOG.info("Applying " + contextXmlUrl + " to " + _webApp);
457
458 XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXmlUrl);
459 HashMap properties = new HashMap();
460 properties.put("Server", getDeploymentManager().getServer());
461 properties.put(OSGiWebappConstants.JETTY_BUNDLE_ROOT, rootResource.toString());
462 properties.put(OSGiServerConstants.JETTY_HOME, getDeploymentManager().getServer().getAttribute(OSGiServerConstants.JETTY_HOME));
463 xmlConfiguration.getProperties().putAll(properties);
464 xmlConfiguration.configure(_webApp);
465 }
466 finally
467 {
468 Thread.currentThread().setContextClassLoader(cl);
469 }
470 }
471
472 private File getFile (String file, File bundleInstall)
473 {
474 if (file == null)
475 return null;
476
477 if (file.startsWith("/") || file.startsWith("file:/"))
478 return new File(file);
479 else
480 {
481
482
483 File f = new File (bundleInstall, file);
484 if (f.exists()) return f;
485 String jettyHome = (String)getDeploymentManager().getServer().getAttribute(OSGiServerConstants.JETTY_HOME);
486 if (jettyHome != null)
487 return new File(jettyHome, file);
488 }
489
490 return null;
491 }
492 }
493
494
495 public AbstractWebAppProvider (ServerInstanceWrapper wrapper)
496 {
497 _serverWrapper = wrapper;
498 }
499
500
501
502
503
504
505
506
507
508 public boolean isParentLoaderPriority()
509 {
510 return _parentLoaderPriority;
511 }
512
513
514
515
516
517
518
519 public void setParentLoaderPriority(boolean parentLoaderPriority)
520 {
521 _parentLoaderPriority = parentLoaderPriority;
522 }
523
524
525
526
527
528
529
530 public String getDefaultsDescriptor()
531 {
532 return _defaultsDescriptor;
533 }
534
535
536
537
538
539
540
541 public void setDefaultsDescriptor(String defaultsDescriptor)
542 {
543 _defaultsDescriptor = defaultsDescriptor;
544 }
545
546
547
548 public boolean isExtract()
549 {
550 return _extractWars;
551 }
552
553
554
555 public void setExtract(boolean extract)
556 {
557 _extractWars = extract;
558 }
559
560
561
562
563
564
565
566 public void setTldBundles(String tldBundles)
567 {
568 _tldBundles = tldBundles;
569 }
570
571
572
573
574
575
576
577 public String getTldBundles()
578 {
579 return _tldBundles;
580 }
581
582
583
584
585
586 public void setConfigurationClasses(String[] configurations)
587 {
588 _configurationClasses = configurations == null ? null : (String[]) configurations.clone();
589 }
590
591
592
593
594
595 public String[] getConfigurationClasses()
596 {
597 return _configurationClasses;
598 }
599
600
601 public void setServerInstanceWrapper(ServerInstanceWrapper wrapper)
602 {
603 _serverWrapper = wrapper;
604 }
605
606 public ServerInstanceWrapper getServerInstanceWrapper()
607 {
608 return _serverWrapper;
609 }
610
611
612
613
614
615 public DeploymentManager getDeploymentManager()
616 {
617 return _deploymentManager;
618 }
619
620
621
622
623
624 public void setDeploymentManager(DeploymentManager deploymentManager)
625 {
626 _deploymentManager = deploymentManager;
627 }
628
629
630
631 public ContextHandler createContextHandler(App app) throws Exception
632 {
633 if (app == null)
634 return null;
635 if (!(app instanceof OSGiApp))
636 throw new IllegalStateException(app+" is not a BundleApp");
637
638
639 ContextHandler ch = ((OSGiApp)app).createContextHandler();
640 return ch;
641 }
642
643
644
645 public static String getOriginId(Bundle contributor, String path)
646 {
647 return contributor.getSymbolicName() + "-" + contributor.getVersion().toString() + (path.startsWith("/") ? path : "/" + path);
648 }
649
650 }