@ -22,9 +22,11 @@ import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
@@ -22,9 +22,11 @@ import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
* Helper class for resolving placeholders in texts . Usually applied to file paths .
*
* < p > A text may contain < code > $ { . . . } < / code > placeholders , to be resolved as system properties : e . g .
* < code > $ { user . dir } < / code > .
* < code > $ { user . dir } < / code > . Default values can be supplied using the ":" separator between key
* and value .
*
* @author Juergen Hoeller
* @author Dave Syer
* @see # PLACEHOLDER_PREFIX
* @see # PLACEHOLDER_SUFFIX
* @see System # getProperty ( String )
@ -38,12 +40,14 @@ public abstract class SystemPropertyUtils {
@@ -38,12 +40,14 @@ public abstract class SystemPropertyUtils {
/** Suffix for system property placeholders: "}" */
public static final String PLACEHOLDER_SUFFIX = "}" ;
/** Value separator for system property placeholders: "} " */
/** Value separator for system property placeholders: ": " */
public static final String VALUE_SEPARATOR = ":" ;
private static final PropertyPlaceholderHelper helper =
new PropertyPlaceholderHelper ( PLACEHOLDER_PREFIX , PLACEHOLDER_SUFFIX , VALUE_SEPARATOR , false ) ;
private static final PropertyPlaceholderHelper strictHelper = new PropertyPlace holderH elper( PLACEHOLDER_PREFIX ,
PLACEHOLDER_SUFFIX , VALUE_SEPARATOR , false ) ;
private static final PropertyPlaceholderHelper nonStrictHelper = new PropertyPlaceholderHelper ( PLACEHOLDER_PREFIX ,
PLACEHOLDER_SUFFIX , VALUE_SEPARATOR , true ) ;
/ * *
* Resolve $ { . . . } placeholders in the given text , replacing them with corresponding system property values .
@ -51,31 +55,61 @@ public abstract class SystemPropertyUtils {
@@ -51,31 +55,61 @@ public abstract class SystemPropertyUtils {
* @return the resolved String
* @see # PLACEHOLDER_PREFIX
* @see # PLACEHOLDER_SUFFIX
*
* @throws IllegalArgumentException if there is an unresolvable placeholder
* /
public static String resolvePlaceholders ( final String text ) {
return helper . replacePlaceholders ( text , new PlaceholderResolver ( ) {
public String resolvePlaceholder ( String placeholderName ) {
String propVal = null ;
try {
propVal = System . getProperty ( placeholderName ) ;
if ( propVal = = null ) {
// Fall back to searching the system environment.
propVal = System . getenv ( placeholderName ) ;
}
if ( propVal = = null ) {
System . err . println ( "Could not resolve placeholder '" + placeholderName + "' in [" + text +
"] as system property: neither system property nor environment variable found" ) ;
}
return resolvePlaceholders ( text , false ) ;
}
/ * *
* Resolve $ { . . . } placeholders in the given text , replacing them with corresponding system property values .
* Unresolvable placeholders with no default value are ignored and passed through unchanged if the
* flag is set to true .
*
* @param text the String to resolve
* @param ignoreUnresolvablePlaceholders flag to determine is unresolved placeholders are ignored
* @return the resolved String
* @see # PLACEHOLDER_PREFIX
* @see # PLACEHOLDER_SUFFIX
*
* @throws IllegalArgumentException if there is an unresolvable placeholder and the flag is false
*
* /
public static String resolvePlaceholders ( final String text , boolean ignoreUnresolvablePlaceholders ) {
if ( ignoreUnresolvablePlaceholders ) {
return nonStrictHelper . replacePlaceholders ( text , new PlaceholderResolverImplementation ( text ) ) ;
}
return strictHelper . replacePlaceholders ( text , new PlaceholderResolverImplementation ( text ) ) ;
}
private static final class PlaceholderResolverImplementation implements PlaceholderResolver {
private final String text ;
private PlaceholderResolverImplementation ( String text ) {
this . text = text ;
}
public String resolvePlaceholder ( String placeholderName ) {
String propVal = null ;
try {
propVal = System . getProperty ( placeholderName ) ;
if ( propVal = = null ) {
// Fall back to searching the system environment.
propVal = System . getenv ( placeholderName ) ;
}
catch ( Throwable ex ) {
System . err . println ( "Could not resolve placeholder '" + placeholderName + "' in [" + text +
"] as system property: " + ex ) ;
if ( propVal = = null ) {
System . err . println ( "Could not resolve placeholder '" + placeholderName + "' in [" + text
+ "] as system property: neither system property nor environment variable found" ) ;
}
return propVal ;
} catch ( Throwable ex ) {
System . err . println ( "Could not resolve placeholder '" + placeholderName + "' in [" + text
+ "] as system property: " + ex ) ;
}
} ) ;
return propVal ;
}
}
}