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
This commit is contained in:
Jesse McConnell 2011-05-13 20:22:12 +00:00
parent dbcdcc6ce6
commit 5f01e2428b
5 changed files with 383 additions and 0 deletions

View File

@ -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<String> _serverClasses = new ArrayList<String>();
private List<String> _systemClasses = new ArrayList<String>();
/**
* 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()]) );
}
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,78 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Array id="serverClasses" type="java.lang.String">
<Item>-org.eclipse.jetty.continuation.</Item>
<Item>-org.eclipse.jetty.jndi.</Item>
<Item>-org.eclipse.jetty.plus.jaas.</Item>
<Item>-org.eclipse.jetty.websocket.</Item>
<Item>-org.eclipse.jetty.servlet.DefaultServlet</Item>
<Item>org.eclipse.jetty.</Item>
<Item>org.eclipse.foo.</Item>
</Array>
<Array id="systemClasses" type="java.lang.String">
<Item>java.</Item>
<Item>javax.</Item>
<Item>org.xml.</Item>
<Item>org.w3c.</Item>
<Item>org.apache.commons.logging</Item>
<Item>org.eclipse.jetty.continuation</Item>
<Item>org.eclipse.jetty.plus.jaas.</Item>
<Item>org.eclipse.jetty.websocket</Item>
<Item>org.eclipse.jetty.servlet.DefaultServlet</Item>
</Array>
<Call name="addLifeCycle">
<Arg>
<New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager">
<Set name="contexts">
<Ref id="Contexts" />
</Set>
<Ref id="DeploymentManager">
<Call name="addLifeCycleBinding">
<Arg>
<New class="org.eclipse.jetty.deploy.bindings.WebappClasspathPatternBinding">
<Set name="override">true</Set>
<Set name="serverClasses"><Ref id="serverClasses"/></Set>
<Set name="systemClasses"><Ref id="systemClasses"/></Set>
</New>
</Arg>
</Call>
</Ref>
<!-- Providers of Apps -->
<Set name="appProviders">
<Array type="org.eclipse.jetty.deploy.AppProvider">
<Item>
<New class="org.eclipse.jetty.deploy.providers.ContextProvider">
<Set name="monitoredDirName"><SystemProperty name="jetty.home" />/contexts</Set>
<Set name="scanInterval">1</Set>
<Set name="configurationManager">
<New class="org.eclipse.jetty.deploy.FileConfigurationManager">
<Set name="file">
<Property name="test.targetdir" default="target" />/xml-configured-jetty.properties
</Set>
</New>
</Set>
</New>
</Item>
<Item>
<New class="org.eclipse.jetty.deploy.providers.WebAppProvider">
<Set name="monitoredDirName"><SystemProperty name="jetty.home" />/webapps</Set>
<Set name="scanInterval">1</Set>
<Set name="contextXmlDir"><SystemProperty name="jetty.home" />/contexts</Set>
</New>
</Item>
</Array>
</Set>
</New>
</Arg>
</Call>
</Configure>

View File

@ -0,0 +1,63 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Array id="serverClasses" type="java.lang.String">
<Item>org.foo.test.</Item>
</Array>
<Array id="systemClasses" type="java.lang.String">
<Item>com.foo.test.</Item>
</Array>
<Call name="addLifeCycle">
<Arg>
<New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager">
<Set name="contexts">
<Ref id="Contexts" />
</Set>
<Ref id="DeploymentManager">
<Call name="addLifeCycleBinding">
<Arg>
<New class="org.eclipse.jetty.deploy.bindings.WebappClasspathPatternBinding">
<Set name="serverClasses"><Ref id="serverClasses"/></Set>
<Set name="systemClasses"><Ref id="systemClasses"/></Set>
</New>
</Arg>
</Call>
</Ref>
<!-- Providers of Apps -->
<Set name="appProviders">
<Array type="org.eclipse.jetty.deploy.AppProvider">
<Item>
<New class="org.eclipse.jetty.deploy.providers.ContextProvider">
<Set name="monitoredDirName"><SystemProperty name="jetty.home" />/contexts</Set>
<Set name="scanInterval">1</Set>
<Set name="configurationManager">
<New class="org.eclipse.jetty.deploy.FileConfigurationManager">
<Set name="file">
<Property name="test.targetdir" default="target" />/xml-configured-jetty.properties
</Set>
</New>
</Set>
</New>
</Item>
<Item>
<New class="org.eclipse.jetty.deploy.providers.WebAppProvider">
<Set name="monitoredDirName"><SystemProperty name="jetty.home" />/webapps</Set>
<Set name="scanInterval">1</Set>
<Set name="contextXmlDir"><SystemProperty name="jetty.home" />/contexts</Set>
</New>
</Item>
</Array>
</Set>
</New>
</Arg>
</Call>
</Configure>

View File

@ -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()