MetricsConfig extends SubsetConfiguration { static MetricsConfig loadFirst(String prefix, String... fileNames) { for (String fname : fileNames) { try { PropertiesConfiguration pcf = new PropertiesConfiguration(); FileHandler fh = new FileHandler(pcf); fh.setFileName(fname); fh.load(); Configuration cf = pcf.interpolatedConfiguration(); LOG.info("Loaded properties from {}", fname); if (LOG.isDebugEnabled()) { LOG.debug("Properties: {}", toString(cf)); } MetricsConfig mc = new MetricsConfig(cf, prefix); LOG.debug("Metrics Config: {}", mc); return mc; } catch (ConfigurationException e) { if (e.getMessage().startsWith("Could not locate")) { LOG.debug("Could not locate file {}", fname, e); continue; } throw new MetricsConfigException(e); } } LOG.warn("Cannot locate configuration: tried " + Joiner.on(",").join(fileNames)); return new MetricsConfig(new PropertiesConfiguration(), prefix); } MetricsConfig(Configuration c, String prefix); @Override MetricsConfig subset(String prefix); @Override Object getPropertyInternal(String key); @Override String toString();  }
@Test public void testLoadFirst() throws Exception { String filename = getTestFilename("hadoop-metrics2-p1"); new ConfigBuilder().add("p1.foo", "p1foo").save(filename); MetricsConfig mc = MetricsConfig.create("p1"); MetricsConfig mc2 = MetricsConfig.create("p1", "na1", "na2", filename); Configuration expected = new ConfigBuilder().add("foo", "p1foo").config; assertEq(expected, mc); assertEq(expected, mc2); }