Q:

How to add server classes for all contexts in Jetty?

I have jetty configured to use slf4j and logback. version 8.x
According to this place http://dev.eclipse.org/mhonarc/lists/jetty-users/msg02859.html you can add those classes in the list of server classes of jetty per context, so the web application can't see the slf4j server classes.
I don't want to do this for each war I deploy. Is it possible to configure only once all the classes AND resources ($JETTY_HOME/resources/logback.xml is visible to webapps by default) of the server that are not to be available in the classloader of deployed webapps ?

A:

Yes, you can have a class that participates in the deployment lifecycle of webapps.
Example Class:
package example.deploy.logging.config;

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;

public class LoggingConfigBinding implements AppLifeCycle.Binding
{
    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;
            webapp.addSystemClass("org.slf4j.");
        }
    }
}

Then you'll have a piece of XML that loads it into the DeploymentManager
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <Ref id="DeploymentManager">
    <Call name="addLifeCycleBinding">
      <Arg>
        <New class="example.deploy.logging.config.LoggingConfigBinding">
        </New>
      </Arg>
    </Call>
  </Ref>
</Configure>

For a more complete example see Sifting Logs in Logback on Jetty (note: logback is a slf4j logging implementation).

