1

I'm trying a self-executable WAR package with Jetty. It configures with web.xml by default. If a run-time option is given, I wanted to override web.xml by Java code-level configuration with ServletContextHandler#addServlet, #addEventListener, and ...

Can I ignore web.xml while loading a WAR package?


% java -jar foobar.jar  # Use web.xml
% java -jar foobar.jar --customize=something  # Use Java code to configure

// Example

WebAppContext webapp = new WebAppContext();
webapp.setWar(warLocation.toExternalForm());
webapp.setContextPath("/");
if ( /* has run-time options */ ) {
  webapp.setWar(warLocation.toExternalForm()); // But, no load web.xml!
  // Emulates web.xml.
  webapp.addEventListener(...);
  webapp.setInitParameter("resteasy.role.based.security", "true");
  webapp.addFilter(...);
} else {
  webapp.setWar(warLocation.toExternalForm()); // Loading web.xml.
}

Additional Question:

Before server.start() is called, classes under WEB-INF/ are not loaded. Can I do some configuration webapp.something() with some classes under WEB-INF/? (E.g. extend WebInfConfiguration or do a similar class-loading that WebInfConfiguration does?)

For example, I'd like to do something like:

  • webapp.addEventListener(new SomeClassUnderWebInf()));
  • webapp.addEventListener(someInjector.inject(SomeClassUnderWebInf.class));

before server.start().

Dai MIKURUBE
  • 125
  • 1
  • 1
  • 13
  • Ask "additional question" as a different question on stackoverflow, don't complicate the original question here. (it also makes it better for those coming after you to find relevant questions and answers) – Joakim Erdfelt Jul 02 '15 at 15:44
  • Ah, making sense. Thanks for your suggestion. I'd make another question! – Dai MIKURUBE Jul 02 '15 at 18:29
  • Posted another question at http://stackoverflow.com/questions/31192270/jetty-accessing-classes-under-web-inf-before. – Dai MIKURUBE Jul 02 '15 at 18:43

1 Answers1

1

Handle the WebAppContext Configuration yourself.

Eg:

private static class SelfConfiguration extends AbstractConfiguration
{
    @Override
    public void configure(WebAppContext context) throws Exception
    {
        // Emulates web.xml.
        webapp.addEventListener(...);
        webapp.setInitParameter("resteasy.role.based.security", "true");
        webapp.addFilter(...);
    }
}

public static void main(String[] args) throws Exception
{
    Server server = new Server(8080);

    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    if (useWebXml)
    {
        webapp.setConfigurationClasses(WebAppContext.getDefaultConfigurationClasses());
    } 
    else 
    {
        webapp.setConfigurations(new Configuration[] { 
            new SelfConfiguration() 
        });
    }
    webapp.setWar("path/to/my/test.war");
    webapp.setParentLoaderPriority(true);
    server.setHandler(webapp);
    server.start();
    server.join();
}
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Thanks! Though I cannot try it immediately, I'd try soon. – Dai MIKURUBE Jun 11 '15 at 22:39
  • Thanks, Joakim. After a long interval, I could start Jetty server without web.xml configuration. But, I have another problem. Does anyone know how to manually load classes under WEB-INF/ ? – Dai MIKURUBE Jul 01 '15 at 23:41