/** * Get an URI associated with the given configuration key. * * @param key The configuration key. * @return The associated URI. * * @throws ConversionException is thrown if the key maps to an * object that is not an URI. */ public URI getURI(final String key) { return get(URI.class, key); }
/** * Get an array of boolean primitives associated with the given * configuration key. If the key doesn't map to an existing object * an empty array is returned. * * @param key The configuration key. * @return The associated boolean array if the key is found. * * @throws ConversionException is thrown if the key maps to an * object that is not a list of booleans. */ public boolean[] getBooleanArray(final String key) { return (boolean[]) getArray(Boolean.TYPE, key); }
/** * Get a list of BigIntegers associated with the given configuration key. * If the key doesn't map to an existing object, the default value is * returned. * * @param key The configuration key. * @param defaultValue The default value. * @return The associated List of BigIntegers. * * @throws ConversionException is thrown if the key maps to an * object that is not a list of BigIntegers. */ public List<BigInteger> getBigIntegerList(final String key, final List<BigInteger> defaultValue) { return getList(BigInteger.class, key, defaultValue); }
@Override public Object getValue(String prefix, String key, Field field, Configuration configuration) throws DemoiselleConfigurationValueExtractorException { try{ return new DataConfiguration(configuration).getArray(field.getType().getComponentType(), prefix + key); } catch(Exception e){ throw new DemoiselleConfigurationValueExtractorException(e.getMessage(), e); } }
@Override public Object getValue(String prefix, String key, Field field, Configuration configuration) throws DemoiselleConfigurationValueExtractorException { try{ return new DataConfiguration(configuration).get(ClassUtils.primitiveToWrapper(field.getType()), prefix + key); } catch(Exception e){ throw new DemoiselleConfigurationValueExtractorException(e.getMessage(), e); } }
private static void logAccess(String key, Object defaultValue) { if (config.containsKey(key)) { log.debug("Found value {} for option {}.", config.getProperty(key), key); } else { log.debug("No value found for option {}. Returning default of {}.", key, defaultValue); } } }
DataConfiguration config = new DataConfiguration(new AppletConfiguration(applet)); Color color = config.getColor("color");
/** * Returns the date format specified by the user in the DATE_FORMAT_KEY * property, or the default format otherwise. * * @return the default date format */ private String getDefaultDateFormat() { return getString(DATE_FORMAT_KEY, DEFAULT_DATE_FORMAT); }
/** * (Re)create and populate the initial config. */ public static void init() { config = new DataConfiguration(new BaseConfiguration()); // Load maven project properties. InputStream stream = ClassLoader.getSystemClassLoader().getResourceAsStream(PROJECT_PROPS); if (stream != null) { loadResource(stream, PROJECT_PROPS); } // Load the configuration file directly if the path exists. String path = OptionConverter.getSystemProperty(PSL_CONFIG, PSL_CONFIG_DEFAULT); if ((new File(path)).isFile()) { loadResource(path); return; } // Try to get a resource URL from the system (if we have a property key instead of a path). stream = ClassLoader.getSystemClassLoader().getResourceAsStream(path); if (stream != null) { loadResource(stream, PSL_CONFIG); return; } log.debug( "PSL configuration {} file not found." + " Only default values will be used unless additional properties are specified.", path); }
/** * {@inheritDoc} This implementation checks for a defined data format in * the following order: * <ul> * <li>If a temporary date format is set for the current call, it is * used.</li> * <li>If a date format is specified in this configuration using the * {@code DATE_FORMAT_KEY} property, it is used.</li> * <li>Otherwise, the date format set for the original conversion * handler is used if available.</li> * </ul> */ @Override public String getDateFormat() { if (StringUtils.isNotEmpty(TEMP_DATE_FORMAT.get())) { return TEMP_DATE_FORMAT.get(); } if (containsKey(DATE_FORMAT_KEY)) { return getDefaultDateFormat(); } final DefaultConversionHandler orgHandler = getOriginalConversionHandler(); return (orgHandler != null) ? orgHandler.getDateFormat() : null; } }
public static void loadResource(String path) { try { PropertiesConfiguration props = new PropertiesConfiguration(); props.read(new FileReader(path)); config.append(props); } catch (IOException | ConfigurationException ex) { throw new RuntimeException("Failed to load config resource: " + path, ex); } log.debug("Configuration file loaded: {}", path); }
/** * Remove all properties from the configuration. */ public static void clear() { config.clear(); log.debug("Cleared all options in the configuration."); }
/** * Add a property to the configuration. * If it already exists then the value stated here will be added to the configuration entry. * For example, if the property: * * <pre> * resource.loader = file * </pre> * * is already present in the configuration and you call * * <pre> * addProperty("resource.loader", "classpath") * </pre> * * Then you will end up with a List like the following: * * <pre> * ["file", "classpath"] * </pre> * * @param key The key to add the property to. * @param value The value to add. */ public static void addProperty(String key, Object value) { config.addProperty(key, value); log.debug("Added {} to option {}.", value, key); }
/** * Get a property from the configuration. * Typically, a more specific method should be used. * * @param key The configuration key * * @return The associated Object (or null if undefined) */ public static Object getProperty(String key) { logAccess(key, ""); if (config.containsKey(key)) { return config.getProperty(key); } return null; }
public static String getString(String key, String defaultValue) { logAccess(key, defaultValue); return config.getString(key, defaultValue); }
public static void loadResource(InputStream stream, String resourceName) { try { PropertiesConfiguration props = new PropertiesConfiguration(); props.read(new InputStreamReader(stream)); config.append(props); } catch (IOException | ConfigurationException ex) { throw new RuntimeException("Failed to load config resource: " + resourceName, ex); } log.debug("Configuration stream loaded: {}", resourceName); }
/** * Get an URL associated with the given configuration key. * * @param key The configuration key. * @return The associated URL. * * @throws ConversionException is thrown if the key maps to an * object that is not an URL. */ public URL getURL(final String key) { return get(URL.class, key); }
/** * Get a list of Colors associated with the given configuration key. * If the key doesn't map to an existing object, the default value is * returned. * * @param key The configuration key. * @param defaultValue The default value. * @return The associated List of Colors. * * @throws ConversionException is thrown if the key maps to an * object that is not a list of Colors. */ public List<Color> getColorList(final String key, final List<Color> defaultValue) { return getList(Color.class, key, defaultValue); }
/** * Returns a new instance of the class whose name associated with the given configuration key. * The default constructor will be used. */ public static Object getNewObject(String key, String defaultValue) { logAccess(key, defaultValue); String className = config.getString(key, defaultValue); // It is not unusual for someone to want no object if the key does not exist. if (className == null) { return null; } return Reflection.newObject(className); }
/** * Get a Locale associated with the given configuration key. * * @param key The configuration key. * @return The associated Locale. * * @throws ConversionException is thrown if the key maps to an * object that is not a Locale. */ public Locale getLocale(final String key) { return get(Locale.class, key); }