Settings implements Serializable { public synchronized void loadFromClassPath(String location) { Preconditions.checkNotNull(location); Properties p = new Properties(); try (InputStream is = Resources.getResource(location).openStream()) { p.load(is); } catch (IOException e) { throw new IllegalStateException( String.format("problem loading %s from the classpath: %s", location, e.getMessage()), e ); } substituteVariables(p); putAll(location, p); locationsLoaded.add(String.format("[classpath]: %s ", location)); } Settings(); synchronized void loadFromClassPath(String location); synchronized void loadFromProperties(Properties p); synchronized void loadFromFile(String location); Object set(String source, String setting, @Nullable String value); Object set(String setting, @Nullable String value); static KeyIteratorFilter startsWith(String prefix); static KeyIteratorFilter endsWith(String prefix); static KeyIteratorFilter contains(String string); static KeyIteratorFilter notContains(String string); static KeyIteratorFilter matches(String regex); static KeyIteratorFilter[] join(KeyIteratorFilter filter,
                                           KeyIteratorFilter... filters); Keys keys(); Keys keys(@Nullable KeyIteratorFilter... filter); MappedSettings<T> map(@Nullable String prefix, Class<T> type); MappedSettings<T> map(@Nullable String prefix,
                                     Class<T> type,
                                     KeyIteratorFilter... filters); boolean isDefined(String key); @Nullable String getString(String key); @Nullable Integer getInteger(String key); @Nullable Long getLong(String key); @Nullable Double getDouble(String key); Boolean getBoolean(String key); @Nullable BigDecimal getBigDecimal(String key); String getString(String key, String defaultValue); Long getLong(String key, @Nullable Long defaultValue); Integer getInteger(String key, @Nullable Integer defaultValue); Double getDouble(String key, @Nullable Double defaultValue); @Nullable BigDecimal getBigDecimal(String key, @Nullable BigDecimal defaultValue); Boolean getBoolean(String key, @Nullable Boolean defaultValue); Properties asProperties(); Properties asProperties(@Nullable String prefixForCutoff); Map<String, String> asMap(); Map<String, String> asMap(@Nullable String prefixForCutoff); synchronized List<String> getLocationsLoaded(); void addListener(SettingsListener l); void removeListener(SettingsListener l); @Override String toString(); static SettingsBuilder builder(); static final String OVERRIDES_FILE_SYSTEM_PROPERTY; }
@Test public void testLoadFromClassPath() { Settings p = new Settings(); p.loadFromClassPath("com/chillenious/common/util/p1.properties"); Assert.assertEquals("bar", p.getString("foo")); Assert.assertEquals("fender", p.getString("guitar")); p = Settings.builder() .addFromClassPath("com/chillenious/common/util/p1.properties") .build(); Assert.assertEquals("bar", p.getString("foo")); Assert.assertEquals("fender", p.getString("guitar")); }