Configurations can add server/system classes

This commit is contained in:
Greg Wilkins 2015-11-27 13:58:16 +11:00
parent 2972354db7
commit 722eb60ce5
8 changed files with 76 additions and 23 deletions

View File

@ -99,7 +99,9 @@ public class AnnotationConfiguration extends AbstractConfiguration
public AnnotationConfiguration()
{
super(new String[]{WebXmlConfiguration.class.getName(),MetaInfConfiguration.class.getName(),FragmentConfiguration.class.getName(),PlusConfiguration.class.getName()},
new String[]{JettyWebXmlConfiguration.class.getName()});
new String[]{JettyWebXmlConfiguration.class.getName()},
new String[]{"org.eclipse.jetty.util.annotation."},
new String[]{"-org.eclipse.jetty.util.annotation."});
}
/**

View File

@ -63,7 +63,9 @@ public class EnvConfiguration extends AbstractConfiguration
public EnvConfiguration()
{
super(new String[]{WebXmlConfiguration.class.getName(),MetaInfConfiguration.class.getName(),FragmentConfiguration.class.getName()},
new String[]{PlusConfiguration.class.getName(),JettyWebXmlConfiguration.class.getName()});
new String[]{PlusConfiguration.class.getName(),JettyWebXmlConfiguration.class.getName()},
new String[]{"org.eclipse.jetty.jndi."},
new String[]{"-org.eclipse.jetty.jndi."});
}
public void setJettyEnvXml (URL url)

View File

@ -30,6 +30,7 @@ import java.net.URL;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.Descriptor;
import org.eclipse.jetty.webapp.FragmentDescriptor;
import org.eclipse.jetty.webapp.Origin;
@ -56,6 +57,8 @@ public class PlusDescriptorProcessorTest
public void setUp() throws Exception
{
context = new WebAppContext();
context.setConfigurations(new Configuration[]{new PlusConfiguration(),new EnvConfiguration()});
context.preConfigure();
context.setClassLoader(new WebAppClassLoader(Thread.currentThread().getContextClassLoader(), context));
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(context.getClassLoader());

View File

@ -28,6 +28,7 @@ import org.eclipse.jetty.plus.jndi.EnvEntry;
import org.eclipse.jetty.plus.jndi.NamingEntry;
import org.eclipse.jetty.plus.jndi.NamingEntryUtil;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.MetaData;
import org.eclipse.jetty.webapp.WebAppClassLoader;
import org.eclipse.jetty.webapp.WebAppContext;
@ -49,6 +50,8 @@ public class TestConfiguration
WebAppContext wac = new WebAppContext();
wac.setServer(server);
wac.setClassLoader(new WebAppClassLoader(Thread.currentThread().getContextClassLoader(), wac));
wac.addSystemClass("org.eclipse.jetty.jndi.");
wac.prependServerClass("-org.eclipse.jetty.jndi.");
MetaData metaData = new MetaData();

View File

@ -27,25 +27,31 @@ public class AbstractConfiguration implements Configuration
{
private final List<String> _after;
private final List<String> _before;
private final List<String> _system;
private final List<String> _server;
protected AbstractConfiguration()
{
_after=Collections.emptyList();
_before=Collections.emptyList();
_system=Collections.emptyList();
_server=Collections.emptyList();
}
protected AbstractConfiguration(String[] before,String[] after)
{
_after=Collections.unmodifiableList(after==null?Collections.emptyList():Arrays.asList(after));
_before=Collections.unmodifiableList(before==null?Collections.emptyList():Arrays.asList(before));
this(before,after,null,null);
}
protected AbstractConfiguration(List<String> after,List<String> before)
protected AbstractConfiguration(String[] before,String[] after,String[] systemClasses,String[] serverClasses)
{
_after=Collections.unmodifiableList(after==null?Collections.emptyList():new ArrayList<>(after));
_before=Collections.unmodifiableList(before==null?Collections.emptyList():new ArrayList<>(before));
_after=Collections.unmodifiableList(after==null?Collections.emptyList():Arrays.asList(after));
_before=Collections.unmodifiableList(before==null?Collections.emptyList():Arrays.asList(before));
_system=Collections.unmodifiableList(systemClasses==null?Collections.emptyList():Arrays.asList(systemClasses));
_server=Collections.unmodifiableList(serverClasses==null?Collections.emptyList():Arrays.asList(serverClasses));
}
@Override
public String getName()
{
@ -64,6 +70,18 @@ public class AbstractConfiguration implements Configuration
return _before;
}
@Override
public List<String> getSystemClasses()
{
return _system;
}
@Override
public List<String> getServerClasses()
{
return _server;
}
public void preConfigure(WebAppContext context) throws Exception
{
}

View File

@ -61,6 +61,20 @@ public interface Configuration
*/
public default List<String> getAfterThis(){ return Collections.emptyList(); }
/* ------------------------------------------------------------------------------- */
/** Get the system classes associated with this Configuration.
* @return A list of class/package names. Exclusions are prepended to the system classes and
* inclusions are appended.
*/
public default List<String> getSystemClasses() { return Collections.emptyList(); }
/* ------------------------------------------------------------------------------- */
/** Get the system classes associated with this Configuration.
* @return A list of class/package names. Exclusions are prepended to the system classes and
* inclusions are appended.
*/
public default List<String> getServerClasses() { return Collections.emptyList(); }
/* ------------------------------------------------------------------------------- */
/** Set up for configuration.
* <p>

View File

@ -35,7 +35,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.management.RuntimeErrorException;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration.Dynamic;
import javax.servlet.ServletSecurityElement;
@ -98,7 +97,8 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
private String[] __dftProtectedTargets = {"/web-inf", "/meta-inf"};
/* default configurations.
/*
* default configurations.
* Order is determined by Topological sort in preconfigure
*/
public static final String[] DEFAULT_CONFIGURATION_CLASSES =
@ -122,9 +122,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
"org.xml.", // needed by javax.xml
"org.w3c.", // needed by javax.xml
"org.eclipse.jetty.jmx.", // webapp cannot change jmx classes
"org.eclipse.jetty.util.annotation.", // webapp cannot change jmx annotations
"org.eclipse.jetty.continuation.", // webapp cannot change continuation classes
"org.eclipse.jetty.jndi.", // webapp cannot change naming classes
"org.eclipse.jetty.jaas.", // webapp cannot change jaas classes
"org.eclipse.jetty.websocket.", // webapp cannot change / replace websocket classes
"org.eclipse.jetty.util.log.", // webapp should use server log
@ -143,9 +141,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
public final static String[] __dftServerClasses =
{
"-org.eclipse.jetty.jmx.", // don't hide jmx classes
"-org.eclipse.jetty.util.annotation.", // don't hide jmx annotation
"-org.eclipse.jetty.continuation.", // don't hide continuation classes
"-org.eclipse.jetty.jndi.", // don't hide naming classes
"-org.eclipse.jetty.jaas.", // don't hide jaas classes
"-org.eclipse.jetty.servlets.", // don't hide jetty servlets
"-org.eclipse.jetty.servlet.DefaultServlet", // don't hide default servlet
@ -455,7 +451,11 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
Map<String,Configuration> map = new HashMap<>();
TopologicalSort<Configuration> sort = new TopologicalSort<>();
for (Configuration c:_configurations)
map.put(c.getName(),c);
{
Configuration duplicate = map.put(c.getName(),c);
if (duplicate!=null)
LOG.warn("Duplicate configuration in {} for {} and {}",this,duplicate,c);
}
for (Configuration c:_configurations)
{
for (String b:c.getBeforeThis())
@ -481,6 +481,26 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
// Setup server classes
loadServerClasses();
// Setup Configuration classes
for (Configuration configuration:_configurations)
{
configuration.getSystemClasses().forEach(c->
{
if (c.startsWith("-"))
prependSystemClass(c);
else
addSystemClass(c);
});
configuration.getServerClasses().forEach(c->
{
if (c.startsWith("-"))
prependServerClass(c);
else
addServerClass(c);
});
}
// Configure classloader
_ownClassLoader=false;
if (getClassLoader()==null)
@ -1029,7 +1049,6 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
_configurations.addAll(Arrays.asList(configurations));
}
/* ------------------------------------------------------------ */
public void addConfiguration(Configuration... configuration)
{
@ -1040,7 +1059,6 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
_configurations.add(c);
}
/* ------------------------------------------------------------ */
/**
* The default descriptor is a web.xml format file that is applied to the context before the standard WEB-INF/web.xml

View File

@ -65,13 +65,6 @@ public class WebInfConfiguration extends AbstractConfiguration
super();
}
public WebInfConfiguration(List<String> after, List<String> before)
{
super(after,before);
// TODO Auto-generated constructor stub
}
@Override
public void preConfigure(final WebAppContext context) throws Exception
{