/** * {@inheritDoc} */ public String get(final String key, final String defolt) { String result = get(key); if (result == null) { return defolt; } else { return result; } }
/** * {@inheritDoc} */ @Override public BigDecimal getBigDecimal(final String key) { return getBigDecimal(key, new BigDecimal("0.0")); }
/** * {@inheritDoc} */ @Override public BigInteger getBigInteger(final String key) { return getBigInteger(key, BigInteger.ZERO); }
/** * {@inheritDoc} */ @Override public String[] getStringArray(final String key) { String list = get(key); if (list == null) { return new String[0]; } return parseStringArray(list); }
/** * {@inheritDoc} */ @Override public boolean getBoolean(final String key, final boolean defaultValue) { return containsKey(key) ? getBoolean(key) : defaultValue; }
/** * {@inheritDoc} */ @Override public void addProperty(final String key, final Object value) { if (containsKey(key)) { String newValue = get(key) + ',' + (value == null ? "" : value); addOrModifyProperty(key, newValue); } else { addOrModifyProperty(key, value == null ? null : value.toString()); } }
recordMessage("Loading parameters"); File cwd = new File("."); String workingDir; recordMessage("Working directory is " + workingDir); loadTop(resourceName); if (getBoolean(USE_SYSTEM_PROPERTIES)) { recordMessage("Loading from system properties"); load(System.getProperties(), "System Properties", true); } while (substitute()); if (getBoolean(DUMP)) { log(getDebuggingInfo()); log(getMessages()); clearMessages(); Properties systemProperties = getSubProperties(SYSTEM_PARAMETERS_PREFIX, true); System.getProperties().putAll(systemProperties);
/** * Loading of "top level" resources is different to the general recursive case, since it is only at the top level * that we check for the includeAfter parameter. * * @param resourceName the path of the resource to load from. */ @SuppressWarnings("checkstyle:emptyblock") private void loadTop(final String resourceName) { try { resources.push(resourceName); load(resourceName); // Now check for INCLUDE_AFTER resources String includes = get(INCLUDE_AFTER); if (includes != null) { // First, do substitution on the INCLUDE_AFTER do { } while (substitute(INCLUDE_AFTER)); // Now split and process String[] includeAfter = getString(INCLUDE_AFTER).split(","); backing.remove(INCLUDE_AFTER); for (String after : includeAfter) { loadTop(after); } } } finally { resources.pop(); } }
backing.put(aKey, ""); booleanBacking.remove(aKey); recordMessage("WARNING: Recursive substitution detected on parameter " + aKey); String history = locations.get(aKey); locations.put(aKey, history + "recursion detected, using null value; " + history); int start = findStartVariable(value); madeChange = madeChange || substitute(variableName); String variableValue = get(variableName);
@Test public void testSimpleProperties() { Assert.assertNull("Missing properties should be null", config.get( "simple.nonExistantPropertyKey")); Assert.assertEquals("Incorrect default value for missing property", "defaultValue", config. getString("simple.nonExistantPropertyKey", "defaultValue")); assertPropertyEquals(EMPTY_PROPERTY_KEY, ""); assertPropertyEquals(STRING_PROPERTY_KEY, "simplePropertyValue"); assertPropertyEquals(INT_PROPERTY_KEY, "123"); assertPropertyEquals(BOOLEAN_TRUE_PROPERTY_KEY, "true"); assertPropertyEquals(BOOLEAN_FALSE_PROPERTY_KEY, "false"); assertPropertyEquals("simple.listPropertyKey", "item1,item2,item3"); }
/** * Load the properties from the given Properties object, recording the origin on those properties as being from the * given location. * * @param properties the properties to load from * @param location the location where the parameter was defined. * @param overwriteOnly if true, only properties that are already defined will be loaded */ private void load(final Properties properties, final String location, final boolean overwriteOnly) { for (Map.Entry<Object, Object> entry : properties.entrySet()) { String key = (String) entry.getKey(); String already = get(key); if (overwriteOnly && already == null && !INCLUDE.equals(key)) { continue; } String value = (String) entry.getValue(); load(key, value, location); } }
/** * {@inheritDoc} */ @Override public byte getByte(final String key) { return getByte(key, (byte) 0); }
/** * {@inheritDoc} */ @Override public float getFloat(final String key) { return getFloat(key, 0.0f); }
/** * {@inheritDoc} */ @Override public double getDouble(final String key) { return getDouble(key, 0.0); }
@Test public void testGetInt() { Assert.assertEquals("Incorrect int value for " + INT_PROPERTY_KEY, INT_PROPERTY_VAL, config.getInt(INT_PROPERTY_KEY)); Assert.assertEquals("Incorrect int value for missing key", 0, config.getInt(MISSING_PROPERTY_KEY)); Assert.assertEquals("Incorrect default int value for missing key", MISSING_PROPERTY_VAL, config.getInt(MISSING_PROPERTY_KEY, MISSING_PROPERTY_VAL)); Assert.assertEquals("Incorrect default integer value for missing key", Integer.valueOf(MISSING_PROPERTY_VAL), config.getInteger(MISSING_PROPERTY_KEY, Integer.valueOf(MISSING_PROPERTY_VAL))); }
/** * {@inheritDoc} */ @Override public List getList(final String key) { return getList(key, new ArrayList(1)); }
/** * @return the configuration to use. */ private static synchronized Configuration loadConfiguration() { // Find if there are classes implementing the ConfigurationLoader SPI. ServiceLoader<ConfigurationLoader> loaders = ServiceLoader.load(ConfigurationLoader.class); Iterator<ConfigurationLoader> iterator = loaders.iterator(); // Use a CompositeConfiguration if there are custom ConfigurationLoader implementations. if (iterator.hasNext()) { CompositeConfiguration compositeConfig = new CompositeConfiguration(new MapConfiguration(new HashMap<String, Object>())); while (iterator.hasNext()) { compositeConfig.addConfiguration(iterator.next().getConfiguration()); } //Add the base WComponents configuration last so it is overridden. compositeConfig.addConfiguration(new DefaultInternalConfiguration()); return compositeConfig; } else { // If no custom ConfigurationLoader is defined, return the default configuration. return new DefaultInternalConfiguration(); } }
/** * {@inheritDoc} */ @Override public int getInt(final String key) { return getInt(key, 0); }
@Test public void testGetBoolean() { Assert.assertEquals("Incorrect boolean value for " + BOOLEAN_TRUE_PROPERTY_KEY, true, config.getBoolean(BOOLEAN_TRUE_PROPERTY_KEY)); Assert.assertEquals("Incorrect boolean value for " + BOOLEAN_FALSE_PROPERTY_KEY, false, config.getBoolean(BOOLEAN_FALSE_PROPERTY_KEY)); Assert.assertEquals("Incorrect boolean value for missing key", false, config.getBoolean(MISSING_PROPERTY_KEY)); Assert.assertEquals("Incorrect default boolean value for missing key", true, config.getBoolean(MISSING_PROPERTY_KEY, true)); Assert.assertEquals("Incorrect default boolean value for missing key", Boolean.TRUE, config.getBoolean(MISSING_PROPERTY_KEY, Boolean.TRUE)); }
/** * {@inheritDoc} */ @Override public Boolean getBoolean(final String key, final Boolean defaultValue) { return containsKey(key) ? Boolean.valueOf(getBoolean(key)) : defaultValue; }