title: Embedding Jetty 9.3 with modular XmlConfiguration
body: <p>I am migrating from Jetty 8.1.17 to Jetty 9.3.9. Our application <strong><em>embeds</em></strong> Jetty. Previously we had a single XML configuration file <code>jetty.xml</code> which contained everything we needed.</p>
  
  <p>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 <code>jetty.xml</code>, <code>jetty-http.xml</code>, <code>jetty-https.xml</code> and <code>jetty-ssl.xml</code> in my <code>$JETTY_HOME/etc</code>; these are pretty much copies of those from the 9.3.9 distribution. This seems to work well when I use <code>start.jar</code> but not through my own code which embeds Jetty.</p>
  
  <p>Ideally I would like to be able to scan for any jetty xml files in the <code>$JETTY_HOME/etc</code> 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 <code>&lt;ref id="x"/&gt;</code> dependencies between them etc.</p>
  
  <p>My initial attempt is based on <a href="https://stackoverflow.com/questions/17817375/how-can-i-programmatically-start-a-jetty-server-with-multiple-configuration-file">How can I programmatically start a jetty server with multiple configuration files?</a> and looks like:</p>
  
  <pre><code>final List&lt;Object&gt; 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();
          }
      }
  }
  </code></pre>
  
  <p>However, I get Exceptions at startup if <code>jetty-https.xml</code> is loaded before <code>jetty-ssl.xml</code> or if I place a reference in <code>jetty.xml</code> to an object from a sub-configuration <code>jetty-blah.xml</code> which has not been loaded first.</p>
  
  <p>It seems to me like Jetty manages to do this okay itself when you call <code>java -jar start.jar</code>, so what am I missing to get Jetty to not care about what order the config files are parsed in? </p>