System.getProperty except that the {@link SecurityException} is
* hidden.
* @param key The key to search for.
* @param def The default value to return.
* @return the string value of the system property, or the default value if there is no property
* with that key.
* @since 1.1
*/
public static String getSystemProperty( final String key, final String def )
{
try
{
return System.getProperty( key, def );
}
catch ( final Throwable e )
{
// MS-Java throws com.ms.security.SecurityExceptionEx
log.debug( "Was not allowed to read system property \"{0}\".", key );
return def;
}
}
/**
* Creates an object for the className value of the key.
*
* @param props
* @param key
* @param defaultValue
* @return Object that was created
*/
public static value is "true", then {@code true} is returned. If value is
* "false", then {@code true} is returned. Otherwise, default is returned.
*
* Case of value is unimportant.
* @param value
* @param defaultValue
* @return Object
*/
public static boolean toBoolean( final String value, final boolean defaultValue )
{
if ( value == null )
{
return defaultValue;
}
final String trimmedVal = value.trim();
if ( "true".equalsIgnoreCase( trimmedVal ) )
{
return true;
}
if ( "false".equalsIgnoreCase( trimmedVal ) )
{
return false;
}
return defaultValue;
}
/**
* Converts to int.
*
* @param value
* @param defaultValue
* @return int
*/
public static int toInt( final String value, final int defaultValue )
{
if ( value != null )
{
final String s = value.trim();
try
{
return Integer.parseInt(s);
}
catch ( final NumberFormatException e )
{
log.error( "[{0}] is not in proper int form.", s, e );
}
}
return defaultValue;
}
/**
* @param value
* @param defaultValue
* @return long
*/
public static long toFileSize( final String value, final long defaultValue )
{
if ( value == null )
{
return defaultValue;
}
String s = value.trim().toUpperCase();
long multiplier = 1;
int index;
if ( ( index = s.indexOf( "KB" ) ) != -1 )
{
multiplier = 1024;
s = s.substring( 0, index );
}
else if ( ( index = s.indexOf( "MB" ) ) != -1 )
{
multiplier = 1024 * 1024;
s = s.substring( 0, index );
}
else if ( ( index = s.indexOf( "GB" ) ) != -1 )
{
multiplier = 1024 * 1024 * 1024;
s = s.substring( 0, index );
}
try
{
return Long.parseLong(s) * multiplier;
}
catch ( final NumberFormatException e )
{
log.error( "[{0}] is not in proper int form.", s);
log.error( "[{0}] not in expected format", value, e );
}
return defaultValue;
}
/**
* Find the value corresponding to key in props. Then perform variable
* substitution on the found value.
*
* @param key
* @param props
* @return substituted string
*/
public static String findAndSubst( final String key, final Properties props )
{
final String value = props.getProperty( key );
if ( value == null )
{
return null;
}
try
{
return substVars( value, props );
}
catch ( final IllegalArgumentException e )
{
log.error( "Bad option value [{0}]", value, e );
return value;
}
}
/**
* Instantiate an object given a class name. Check that the className is a subclass
* of superClass. If that test fails or the object could not be instantiated, then
* defaultValue is returned.
*
* @param className The fully qualified class name of the object to instantiate.
* @param defaultValue The object to return in case of non-fulfillment
* @return instantiated object
*/
public static val from the values of keys found in the
* system properties.
*
* The variable substitution delimiters are ${ and } .
*
* For example, if the System properties contains "key=value", then the call
*
*
* String s = OptionConverter.substituteVars( "Value of key is ${key}." );
*
*
* will set the variable s to "Value of key is value.".
*
* If no value could be found for the specified key, then the props parameter is
* searched, if the value could not be found there, then substitution defaults to the empty
* string.
*
* For example, if system properties contains no value for the key "inexistentKey", then the call
*
*
* String s = OptionConverter.subsVars( "Value of inexistentKey is [${inexistentKey}]" );
*
*
* will set s to "Value of inexistentKey is []"
*
* An {@link java.lang.IllegalArgumentException}is thrown if val contains a start
* delimiter "${" which is not balanced by a stop delimiter "}".
*
* Author Avy Sharell *
* @param val The string on which variable substitution is performed. * @param props * @return String * @throws IllegalArgumentException ifval is malformed.
*/
public static String substVars( final String val, final Properties props )
throws IllegalArgumentException
{
final StringBuilder sbuf = new StringBuilder();
int i = 0;
int j;
int k;
while ( true )
{
j = val.indexOf( DELIM_START, i );
if ( j == -1 )
{
if ( i == 0 )
{
return val;
}
sbuf.append( val.substring( i ) );
return sbuf.toString();
}
sbuf.append(val, i, j);
k = val.indexOf( DELIM_STOP, j );
if ( k == -1 )
{
throw new IllegalArgumentException( '"' + val + "\" has no closing brace. Opening brace at position "
+ j + '.' );
}
j += DELIM_START_LEN;
final String key = val.substring( j, k );
// first try in System properties
String replacement = getSystemProperty( key, null );
// then try props parameter
if ( replacement == null && props != null )
{
replacement = props.getProperty( key );
}
if ( replacement != null )
{
sbuf.append( replacement );
}
i = k + DELIM_STOP_LEN;
}
}
}