com.samskivert.util.PropertiesUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of samskivert Show documentation
Show all versions of samskivert Show documentation
A collection of Java utilities.
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2012 Michael Bayne, et al.
// http://github.com/samskivert/samskivert/blob/master/COPYING
package com.samskivert.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import com.samskivert.io.StreamUtil;
/**
* Utility functions related to properties objects.
*/
public class PropertiesUtil
{
/**
* Extracts all properties from the supplied properties object with the specified prefix,
* removes the prefix from the key for those properties and inserts them into a new properties
* object which is then returned. This is useful for extracting properties from a global
* configuration object that must be passed to a service that expects it's own private
* properties (JDBC for example).
*
* The property file might look like so:
*
*
* my_happy_param=my_happy_value
* ...
* jdbc.driver=foo.bar.Driver
* jdbc.url=jdbc://blahblah
* jdbc.username=bob
* jdbc.password=is your uncle
* ...
* my_happy_other_param=my_happy_other_value
*
*
* This can be supplied to getSubProperties()
with a prefix of "jdbc"
* and the following properties would be returned:
*
*
* driver=foo.bar.Driver
* url=jdbc://blahblah
* username=bob
* password=is your uncle
*
*/
public static Properties getSubProperties (Properties source, String prefix)
{
Properties dest = new Properties();
extractSubProperties(source, dest, prefix);
return dest;
}
/**
* Like {@link #getSubProperties(Properties,String)} with the additional functionality of
* loading up defaults for the sub-properties which are identified by the
* defaultsPrefix
string.
*/
public static Properties getSubProperties (
Properties source, String prefix, String defaultsPrefix)
{
// first load up the defaults
Properties defs = new Properties();
extractSubProperties(source, defs, defaultsPrefix);
// now load up the desired properties
Properties dest = new Properties(defs);
extractSubProperties(source, dest, prefix);
return dest;
}
/**
* Returns a filtered version of the specified properties that first looks for a property
* starting with the given prefix, then looks for a property without the prefix. For example,
* passing the prefix "alt" and using the following properties:
*
*
* alt.texture = sand.png
* lighting = off
*
*
* ...would return "sand.png" for the property "texture" and "off" for the property "lighting".
* Unlike {@link #getSubProperties}, the object returned by this method references, rather than
* copies, the underlying properties. Only the {@link Properties#getProperty} methods are
* guaranteed to work correctly on the returned object.
*/
public static Properties getFilteredProperties (final Properties source, String prefix)
{
final String dprefix = prefix + ".";
return new Properties() {
@Override public String getProperty (String key) {
return getProperty(key, null);
}
@Override public String getProperty (String key, String defaultValue) {
return source.getProperty(dprefix + key, source.getProperty(key, defaultValue));
}
@Override public Enumeration> propertyNames () {
return new Enumeration