0

I am migrating from Jetty 8.1.17 to Jetty 9.3.9. Our application embeds Jetty. Previously we had a single XML configuration file jetty.xml which contained everything we needed.

I felt that with Jetty 9.3.9 it would be much nicer to use the modular approach that they suggest, so far I have jetty.xml, jetty-http.xml, jetty-https.xml and jetty-ssl.xml in my $JETTY_HOME/etc; these are pretty much copies of those from the 9.3.9 distribution. This seems to work well when I use start.jar but not through my own code which embeds Jetty.

Ideally I would like to be able to scan for any jetty xml files in the $JETTY_HOME/etc folder and load the configuration. However for embedded mode I have not found a way to do that without explicitly defining the order that those files should be loaded in, due to <ref id="x"/> dependencies between them etc.

My initial attempt is based on How can I programmatically start a jetty server with multiple configuration files? and looks like:

final List<Object> configuredObjects = new ArrayList();
XmlConfiguration last = null;
for(final Path confFile : configFiles) {
    logger.info("[loading jetty configuration : {}]", confFile.toString());
    try(final InputStream is = Files.newInputStream(confFile)) {
        final XmlConfiguration configuration = new XmlConfiguration(is);
        if (last != null) {
            configuration.getIdMap().putAll(last.getIdMap());
        }
        configuredObjects.add(configuration.configure());
        last = configuration;
    }
}

Server server = null;
// For all objects created by XmlConfigurations, start them if they are lifecycles.
for (final Object configuredObject : configuredObjects) {
    if(configuredObject instanceof Server) {
        server = (Server)configuredObject;
    }

    if (configuredObject instanceof LifeCycle) {
        final LifeCycle lc = (LifeCycle)configuredObject;
        if (!lc.isRunning()) {
            lc.start();
        }
    }
}

However, I get Exceptions at startup if jetty-https.xml is loaded before jetty-ssl.xml or if I place a reference in jetty.xml to an object from a sub-configuration jetty-blah.xml which has not been loaded first.

It seems to me like Jetty manages to do this okay itself when you call java -jar start.jar, so what am I missing to get Jetty to not care about what order the config files are parsed in?

Community
  • 1
  • 1
adamretter
  • 3,885
  • 2
  • 23
  • 43

1 Answers1

0

Order is extremely important when loading the Jetty XML files.

That's the heart of what the entire start.jar and its module system is about, have an appropriate set of properties, the server classpath is sane, and ensuring proper load order of the XML.

Note: its not possible to have everything in ${jetty.home}/etc loaded at the same time, as you will get conflicts on alternate implementations of common technologies (something start.jar also manages for you)

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Is there any way that I can easily reuse/call function code from start.jar in my embedded app, where I can present it with a list of XML files and it can figure out the order etc for me? – adamretter Jun 15 '16 at 16:39
  • you can't have start.jar work with just xml files. it has to work with modules. – Joakim Erdfelt Jun 15 '16 at 16:40
  • you are embedded, declare the xml load order in your embedded code and use it. no need to be dynamic about it. – Joakim Erdfelt Jun 15 '16 at 16:41
  • Well yes we embed Jetty. But it is useful to us if people can modify the Jetty config by just adding/changing XML files in our `jetty/etc` folder. Could we perhaps adopt the module mechanism from start.jar as well? – adamretter Jun 15 '16 at 16:50
  • the `start.jar` uses a `${jetty.base}` to handle modification needs to `${jetty.home}` content. (Note: do **not** change/edit/modify/delete content in the `${jetty.home}` directory) – Joakim Erdfelt Jun 15 '16 at 17:05
  • Can I not just set $jetty.base = $jetty.home, and somehow use functions from start.jar to determine what to load? – adamretter Jun 15 '16 at 17:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114770/discussion-between-joakim-erdfelt-and-adamretter). – Joakim Erdfelt Jun 15 '16 at 17:48