From d47d4b15ea09f7a0b33ea9121198717bc00cc512 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Thu, 5 Apr 2012 17:34:00 +1000 Subject: [PATCH] Added ability to use a META-INF/jetty-webapp-context.xml file to apply a context configuration file to a webapp. Replaced more printStackTrace with log calls; more formatting. --- .../jetty/osgi/boot/OSGiAppProvider.java | 6 +- .../DefaultJettyAtJettyHomeHelper.java | 15 +++-- .../JettyContextHandlerServiceTracker.java | 13 ++-- .../webapp/WebBundleDeployerHelper.java | 65 +++++++++++++------ .../webapp/WebBundleTrackerCustomizer.java | 12 ++-- .../internal/DefaultFileLocatorHelper.java | 2 +- 6 files changed, 70 insertions(+), 43 deletions(-) diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/OSGiAppProvider.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/OSGiAppProvider.java index 34fd8c74db4..510c09e8b67 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/OSGiAppProvider.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/OSGiAppProvider.java @@ -349,7 +349,7 @@ public class OSGiAppProvider extends ScanningAppProvider implements AppProvider } catch (IOException e) { - e.printStackTrace(); + LOG.warn(e); return null; } } @@ -370,11 +370,11 @@ public class OSGiAppProvider extends ScanningAppProvider implements AppProvider } catch (IOException e) { - e.printStackTrace(); + LOG.warn(e); return null; } } - + public boolean isExtract() { return _extractWars; diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/serverfactory/DefaultJettyAtJettyHomeHelper.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/serverfactory/DefaultJettyAtJettyHomeHelper.java index d8491eeacbe..17ea8d22cc7 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/serverfactory/DefaultJettyAtJettyHomeHelper.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/serverfactory/DefaultJettyAtJettyHomeHelper.java @@ -167,18 +167,21 @@ public class DefaultJettyAtJettyHomeHelper { */ private static String getJettyConfigurationURLs(File jettyhome) { - String jettyetc = System.getProperty(SYS_PROP_JETTY_ETC_FILES,"etc/jetty.xml"); - StringTokenizer tokenizer = new StringTokenizer(jettyetc,";,", false); + String jettyetc = System.getProperty(SYS_PROP_JETTY_ETC_FILES, "etc/jetty.xml"); + StringTokenizer tokenizer = new StringTokenizer(jettyetc, ";,", false); StringBuilder res = new StringBuilder(); while (tokenizer.hasMoreTokens()) { String next = tokenizer.nextToken().trim(); if (!next.startsWith("/") && next.indexOf(':') == -1) { - try { + try + { next = new File(jettyhome, next).toURI().toURL().toString(); - } catch (MalformedURLException e) { - e.printStackTrace(); + } + catch (MalformedURLException e) + { + LOG.warn(e); continue; } } @@ -225,7 +228,7 @@ public class DefaultJettyAtJettyHomeHelper { } if (enUrls == null || !enUrls.hasMoreElements()) { - System.err.println("Unable to locate a jetty configuration file for " + etcFile); + LOG.warn("Unable to locate a jetty configuration file for " + etcFile); } if (enUrls != null) { diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/JettyContextHandlerServiceTracker.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/JettyContextHandlerServiceTracker.java index 26120c1850f..0838a4850ab 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/JettyContextHandlerServiceTracker.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/JettyContextHandlerServiceTracker.java @@ -24,6 +24,8 @@ import org.eclipse.jetty.osgi.boot.internal.serverfactory.DefaultJettyAtJettyHom import org.eclipse.jetty.osgi.boot.internal.serverfactory.IManagedJettyServerRegistry; import org.eclipse.jetty.osgi.boot.internal.serverfactory.ServerInstanceWrapper; import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.Scanner; import org.eclipse.jetty.webapp.WebAppContext; import org.osgi.framework.Bundle; @@ -52,7 +54,8 @@ import org.osgi.framework.ServiceReference; */ public class JettyContextHandlerServiceTracker implements ServiceListener { - + private static Logger __logger = Log.getLogger(WebBundleDeployerHelper.class.getName()); + /** New style: ability to manage multiple jetty instances */ private final IManagedJettyServerRegistry _registry; @@ -149,8 +152,7 @@ public class JettyContextHandlerServiceTracker implements ServiceListener } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + __logger.warn(e); } } } @@ -236,7 +238,7 @@ public class JettyContextHandlerServiceTracker implements ServiceListener } catch (Throwable e) { - e.printStackTrace(); + __logger.warn(e); } } else @@ -270,8 +272,7 @@ public class JettyContextHandlerServiceTracker implements ServiceListener } catch (Throwable e) { - // TODO Auto-generated catch block - e.printStackTrace(); + __logger.warn(e); } } } diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/WebBundleDeployerHelper.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/WebBundleDeployerHelper.java index 97d72cb2c38..a681396910e 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/WebBundleDeployerHelper.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/WebBundleDeployerHelper.java @@ -229,6 +229,11 @@ public class WebBundleDeployerHelper implements IWebBundleDeployerHelper try { + + + //apply any META-INF/context.xml file that is found to configure the webapp first + applyMetaInfContextXml (contributor, context); + // make sure we provide access to all the jetty bundles by going // through this bundle. OSGiWebappClassLoader composite = createWebappClassLoader(contributor); @@ -661,7 +666,7 @@ public class WebBundleDeployerHelper implements IWebBundleDeployerHelper } catch (FileNotFoundException e) { - e.printStackTrace(); + __logger.warn(e); } return null; } @@ -726,18 +731,15 @@ public class WebBundleDeployerHelper implements IWebBundleDeployerHelper } catch (SAXException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + __logger.warn(e); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + __logger.warn(e); } catch (Throwable e) { - // TODO Auto-generated catch block - e.printStackTrace(); + __logger.warn(e); } finally { @@ -802,21 +804,46 @@ public class WebBundleDeployerHelper implements IWebBundleDeployerHelper // know. OSGiWebappClassLoader webappClassLoader = new OSGiWebappClassLoader(_wrapper.getParentClassLoaderForWebapps(), new WebAppContext(), contributor, BUNDLE_CLASS_LOADER_HELPER); - /* - * DEBUG try { Class c = - * webappClassLoader.loadClass("org.glassfish.jsp.api.ResourceInjector" - * ); - * System.err.println("LOADED org.glassfish.jsp.api.ResourceInjector from " - * +c.getClassLoader()); } catch (Exception e) {e.printStackTrace();} - * try { Class c = - * webappClassLoader.loadClass("org.apache.jasper.xmlparser.ParserUtils" - * ); - * System.err.println("LOADED org.apache.jasper.xmlparser.ParserUtils from " - * +c.getClassLoader()); } catch (Exception e) {e.printStackTrace();} - */ return webappClassLoader; } + + protected void applyMetaInfContextXml (Bundle bundle, ContextHandler contextHandler) + throws Exception + { + if (bundle == null) + return; + if (contextHandler == null) + return; + + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + __logger.info("Context classloader = "+cl); + try + { + Thread.currentThread().setContextClassLoader(_wrapper.getParentClassLoaderForWebapps()); + + //find if there is a META-INF/context.xml file + URL contextXmlUrl = bundle.getEntry("/META-INF/jetty-webapp-context.xml"); + if (contextXmlUrl == null) + return; + + //Apply it just as the standard jetty ContextProvider would do + __logger.info("Applying "+contextXmlUrl+" to "+contextHandler); + + XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXmlUrl); + HashMap properties = new HashMap(); + properties.put("Server", _wrapper.getServer()); + xmlConfiguration.getProperties().putAll(properties); + xmlConfiguration.configure(contextHandler); + } + finally + { + Thread.currentThread().setContextClassLoader(cl); + } + } + + + /** * Set the property "this.bundle.install" to point to the location * of the bundle. Useful when is diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/WebBundleTrackerCustomizer.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/WebBundleTrackerCustomizer.java index 79938415305..137c49bb271 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/WebBundleTrackerCustomizer.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/internal/webapp/WebBundleTrackerCustomizer.java @@ -167,8 +167,7 @@ public class WebBundleTrackerCustomizer implements BundleTrackerCustomizer catch (Throwable e) { LOG.warn("Starting the web-bundle " + bundle.getSymbolicName() + " threw an exception.", e); - return true;// maybe it did not work maybe it did. safer to - // track this bundle. + return true;// maybe it did not work maybe it did. safer to track this bundle. } } else if (dic.get(OSGiWebappConstants.JETTY_CONTEXT_FILE_PATH) != null) @@ -189,8 +188,7 @@ public class WebBundleTrackerCustomizer implements BundleTrackerCustomizer } catch (Throwable e) { - // TODO Auto-generated catch block - e.printStackTrace(); + LOG.warn(e); } } return true; @@ -221,10 +219,8 @@ public class WebBundleTrackerCustomizer implements BundleTrackerCustomizer } catch (Throwable e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return true;// maybe it did not work maybe it did. safer to - // track this bundle. + LOG.warn(e); + return true;// maybe it did not work maybe it did. safer to track this bundle. } } } diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/internal/DefaultFileLocatorHelper.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/internal/DefaultFileLocatorHelper.java index 7858e574243..0e0ff3fbdae 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/internal/DefaultFileLocatorHelper.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/internal/DefaultFileLocatorHelper.java @@ -69,7 +69,7 @@ public class DefaultFileLocatorHelper implements BundleFileLocatorHelper // grab the MANIFEST.MF's url // and then do what it takes. URL url = bundle.getEntry("/META-INF/MANIFEST.MF"); - //System.err.println(url.toString() + " " + url.toURI() + " " + url.getProtocol()); + if (url.getProtocol().equals("file")) { // some osgi frameworks do use the file protocole directly in some