From 5f01e2428b929e840fabe6c9383661f419492fff Mon Sep 17 00:00:00 2001 From: Jesse McConnell Date: Fri, 13 May 2011 20:22:12 +0000 Subject: [PATCH] Bug 345729 add binding that can allow for global management of server and system classes for webapp contexts, added 2 gettings on webappcontext for getting default server and system class settings. git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@3143 7e9141cc-0065-0410-87d8-b60c137991c4 --- .../WebappClasspathPatternBinding.java | 86 +++++++++++ .../WebappClasspathPatternBindingTest.java | 145 ++++++++++++++++++ .../resources/binding-test-contexts-1.xml | 78 ++++++++++ .../resources/binding-test-contexts-2.xml | 63 ++++++++ .../eclipse/jetty/webapp/WebAppContext.java | 11 ++ 5 files changed, 383 insertions(+) create mode 100644 jetty-deploy/src/main/java/org/eclipse/jetty/deploy/bindings/WebappClasspathPatternBinding.java create mode 100644 jetty-deploy/src/test/java/org/eclipse/jetty/deploy/bindings/WebappClasspathPatternBindingTest.java create mode 100644 jetty-deploy/src/test/resources/binding-test-contexts-1.xml create mode 100644 jetty-deploy/src/test/resources/binding-test-contexts-2.xml diff --git a/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/bindings/WebappClasspathPatternBinding.java b/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/bindings/WebappClasspathPatternBinding.java new file mode 100644 index 00000000000..7b2229d3522 --- /dev/null +++ b/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/bindings/WebappClasspathPatternBinding.java @@ -0,0 +1,86 @@ +package org.eclipse.jetty.deploy.bindings; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jetty.deploy.App; +import org.eclipse.jetty.deploy.AppLifeCycle; +import org.eclipse.jetty.deploy.graph.Node; +import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.webapp.WebAppContext; + +/** + * This binding allows a user to manage a server wide policy of server + * and system classes as they exist for the webapp context. These settings + * can alter and override any that might have been set during context handler + * creation. + * + */ +public class WebappClasspathPatternBinding implements AppLifeCycle.Binding +{ + private List _serverClasses = new ArrayList(); + private List _systemClasses = new ArrayList(); + + /** + * if true, this binding will replace server and system classes instead + * of merging with whatever is in the ContextHandler when processBinding + * is invoked. + */ + private boolean _override = false; + + public void setOverride( boolean override ) + { + _override = override; + } + + public void setServerClasses(String[] serverClasses) + { + for ( String entry : serverClasses ) + { + _serverClasses.add(entry); + } + } + + public void setSystemClasses(String[] systemClasses) + { + for (String entry : systemClasses) + { + _systemClasses.add(entry); + } + } + + public String[] getBindingTargets() + { + return new String[] { "deploying" }; + } + + public void processBinding(Node node, App app) throws Exception + { + ContextHandler handler = app.getContextHandler(); + if (handler == null) + { + throw new NullPointerException("No Handler created for App: " + app); + } + + if (handler instanceof WebAppContext) + { + WebAppContext webapp = (WebAppContext)handler; + + if ( !_override ) + { + for ( String entry : webapp.getServerClasses() ) + { + _serverClasses.add(entry); + } + for ( String entry : webapp.getSystemClasses() ) + { + _systemClasses.add(entry); + } + } + + webapp.setServerClasses( _serverClasses.toArray(new String[_serverClasses.size()]) ); + webapp.setSystemClasses( _systemClasses.toArray(new String[_systemClasses.size()]) ); + } + } + +} diff --git a/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/bindings/WebappClasspathPatternBindingTest.java b/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/bindings/WebappClasspathPatternBindingTest.java new file mode 100644 index 00000000000..4583a3576f0 --- /dev/null +++ b/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/bindings/WebappClasspathPatternBindingTest.java @@ -0,0 +1,145 @@ +// ======================================================================== +// Copyright (c) Webtide LLC +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.apache.org/licenses/LICENSE-2.0.txt +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +package org.eclipse.jetty.deploy.bindings; + +import junit.framework.Assert; + +import org.eclipse.jetty.deploy.providers.ScanningAppProvider; +import org.eclipse.jetty.deploy.test.XmlConfiguredJetty; +import org.eclipse.jetty.toolchain.test.TestingDir; +import org.eclipse.jetty.webapp.WebAppContext; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +/** + * Tests {@link ScanningAppProvider} as it starts up for the first time. + */ +public class WebappClasspathPatternBindingTest +{ + @Rule + public TestingDir testdir = new TestingDir(); + private static XmlConfiguredJetty jetty; + + @Before + public void setupEnvironment() throws Exception + { + jetty = new XmlConfiguredJetty(testdir); + jetty.addConfiguration("jetty.xml"); +// jetty.addConfiguration("jetty-deploymgr-contexts.xml"); + + // Setup initial context + jetty.copyContext("foo.xml","foo.xml"); + jetty.copyWebapp("foo-webapp-1.war","foo.war"); + + // Should not throw an Exception + // jetty.load(); + + // Start it + // jetty.start(); + } + + @After + public void teardownEnvironment() throws Exception + { + // Stop jetty. + jetty.stop(); + } + + @Test + public void testServerAndSystemClassesOverride() throws Exception + { + jetty.addConfiguration("binding-test-contexts-1.xml"); + jetty.load(); + jetty.start(); + + WebAppContext context = jetty.getWebAppContexts().get(0); + + Assert.assertNotNull(context); + Assert.assertEquals(context.getDefaultServerClasses().length, context.getServerClasses().length - 1); // added a pattern + Assert.assertEquals(context.getDefaultSystemClasses().length,context.getSystemClasses().length + 1); // removed a patter + + boolean fooPackage = false; + + // we are inserting the foo package into the server classes + for (String entry : context.getServerClasses()) + { + if ("org.eclipse.foo.".equals(entry)) + { + fooPackage = true; + } + } + + Assert.assertTrue(fooPackage); + + boolean jndiPackage = false; + + // this test overrides and we removed the jndi from the list so it + // should test false + for (String entry : context.getSystemClasses()) + { + if ("org.eclipse.jetty.jndi.".equals(entry)) + { + jndiPackage = true; + } + } + + Assert.assertFalse(jndiPackage); + } + + @Test + public void testServerAndSystemClassesModification() throws Exception + { + jetty.addConfiguration("binding-test-contexts-2.xml"); + jetty.load(); + jetty.start(); + + WebAppContext context = jetty.getWebAppContexts().get(0); + + Assert.assertNotNull(context); + Assert.assertEquals(context.getDefaultServerClasses().length,context.getServerClasses().length - 1); // added a pattern + Assert.assertEquals(context.getDefaultSystemClasses().length,context.getSystemClasses().length - 1); // added a pattern + + boolean testPackageServer = false; + + + for (String entry : context.getSystemClasses()) + { + if ("com.foo.test.".equals(entry)) + { + testPackageServer = true; + } + } + + Assert.assertTrue(testPackageServer); + + boolean testPackageSystem = false; + + + for (String entry : context.getSystemClasses()) + { + if ("com.foo.test.".equals(entry)) + { + testPackageSystem = true; + } + } + + Assert.assertTrue(testPackageSystem); + } + +} + diff --git a/jetty-deploy/src/test/resources/binding-test-contexts-1.xml b/jetty-deploy/src/test/resources/binding-test-contexts-1.xml new file mode 100644 index 00000000000..940b880f948 --- /dev/null +++ b/jetty-deploy/src/test/resources/binding-test-contexts-1.xml @@ -0,0 +1,78 @@ + + + + + + + -org.eclipse.jetty.continuation. + -org.eclipse.jetty.jndi. + -org.eclipse.jetty.plus.jaas. + -org.eclipse.jetty.websocket. + -org.eclipse.jetty.servlet.DefaultServlet + org.eclipse.jetty. + org.eclipse.foo. + + + + java. + javax. + org.xml. + org.w3c. + org.apache.commons.logging + org.eclipse.jetty.continuation + org.eclipse.jetty.plus.jaas. + org.eclipse.jetty.websocket + org.eclipse.jetty.servlet.DefaultServlet + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + /contexts + 1 + + + + /xml-configured-jetty.properties + + + + + + + + /webapps + 1 + /contexts + + + + + + + + + diff --git a/jetty-deploy/src/test/resources/binding-test-contexts-2.xml b/jetty-deploy/src/test/resources/binding-test-contexts-2.xml new file mode 100644 index 00000000000..665fd4ab316 --- /dev/null +++ b/jetty-deploy/src/test/resources/binding-test-contexts-2.xml @@ -0,0 +1,63 @@ + + + + + + + org.foo.test. + + + + com.foo.test. + + + + + + + + + + + + + + + + + + + + + + + + + + + + /contexts + 1 + + + + /xml-configured-jetty.properties + + + + + + + + /webapps + 1 + /contexts + + + + + + + + + diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java index f7115a58a19..f59a5684c7e 100644 --- a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java @@ -777,6 +777,17 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL return __dftConfigurationClasses; } + /* ------------------------------------------------------------ */ + public String[] getDefaultServerClasses () + { + return __dftServerClasses; + } + + /* ------------------------------------------------------------ */ + public String[] getDefaultSystemClasses () + { + return __dftSystemClasses; + } /* ------------------------------------------------------------ */ protected void loadConfigurations()