Author: anil.saldhana(a)jboss.com
Date: 2011-03-09 13:56:11 -0500 (Wed, 09 Mar 2011)
New Revision: 804
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/util/StringUtil.java
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/SystemPropertyAsStringUnitTestCase.java
Log:
PLFED-138: allow default values
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/util/StringUtil.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/util/StringUtil.java 2011-03-09
18:55:24 UTC (rev 803)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/util/StringUtil.java 2011-03-09
18:56:11 UTC (rev 804)
@@ -27,7 +27,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-
/**
* Utility dealing with Strings
* @author Anil.Saldhana(a)redhat.com
@@ -43,8 +42,8 @@
public static boolean isNotNull(String str)
{
return str != null && !"".equals(str);
- }
-
+ }
+
/**
* Check whether the string is null or empty
* @param str
@@ -54,50 +53,75 @@
{
return str == null || str.isEmpty();
}
-
+
/**
+ * <p>
* Get the system property value if the string is of the format ${sysproperty}
+ * </p>
+ * <p>
+ * You can insert default value when the system property is not set, by
+ * separating it at the beginning with ::
+ * </p>
+ * <p>
+ * <b>Examples:</b>
+ * </p>
+ *
+ * <p>
+ * ${idp} should resolve to a value if the system property "idp" is set.
+ * </p>
+ * <p>
+ * ${idp::http://localhost:8080} will resolve to
http://localhost:8080 if the system
property "idp" is not set.
+ * </p>
* @param str
* @return
*/
- public static String getSystemPropertyAsString( String str )
+ public static String getSystemPropertyAsString(String str)
{
- if( str.contains( "${") )
- {
- Pattern pattern = Pattern.compile( "\\$\\{([^}]+)}" );
+ if (str.contains("${"))
+ {
+ Pattern pattern = Pattern.compile("\\$\\{([^}]+)}");
Matcher matcher = pattern.matcher(str);
- StringBuffer buffer = new StringBuffer();
- String sysPropertyValue = null;
+ StringBuffer buffer = new StringBuffer();
+ String sysPropertyValue = null;
- while (matcher.find())
+ while (matcher.find())
{
- sysPropertyValue = SecurityActions.getSystemProperty( matcher.group(1),
"" );
- if( sysPropertyValue.isEmpty() )
+ String subString = matcher.group(1);
+ String defaultValue = "";
+
+ //Look for default value
+ if (subString.contains("::"))
{
- throw new IllegalArgumentException( "System Property " +
matcher.group(1) + " is not set" );
- }
- matcher.appendReplacement(buffer,sysPropertyValue);
+ int index = subString.indexOf("::");
+ defaultValue = subString.substring(index + 2);
+ }
+ sysPropertyValue = SecurityActions.getSystemProperty(subString,
defaultValue);
+ if (sysPropertyValue.isEmpty())
+ {
+ throw new IllegalArgumentException("System Property " +
matcher.group(1) + " is not set");
+ }
+ matcher.appendReplacement(buffer, sysPropertyValue);
}
- matcher.appendTail(buffer);
+ matcher.appendTail(buffer);
str = buffer.toString();
}
return str;
}
-
+
/**
* Given a comma separated string, get the tokens as a {@link List}
* @param str
* @return
*/
- public static List<String> tokenize( String str )
+ public static List<String> tokenize(String str)
{
List<String> list = new ArrayList<String>();
StringTokenizer tokenizer = new StringTokenizer(str, ",");
- while( tokenizer.hasMoreTokens() )
+ while (tokenizer.hasMoreTokens())
{
- list.add( tokenizer.nextToken() );
+ list.add(tokenizer.nextToken());
}
return list;
}
Modified:
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/SystemPropertyAsStringUnitTestCase.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/SystemPropertyAsStringUnitTestCase.java 2011-03-09
18:55:24 UTC (rev 803)
+++
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/SystemPropertyAsStringUnitTestCase.java 2011-03-09
18:56:11 UTC (rev 804)
@@ -38,22 +38,33 @@
@Before
public void setup()
{
- System.setProperty( "test", "anil" );
- System.setProperty( "person", "marcus" );
+ System.setProperty("test", "anil");
+ System.setProperty("person", "marcus");
}
-
+
@Test
public void testSystemProperty() throws Exception
{
- assertEquals( "test" , StringUtil.getSystemPropertyAsString(
"test" ) );
- assertEquals( "test/test" , StringUtil.getSystemPropertyAsString(
"test/test" ) );
-
- assertEquals( "anil", StringUtil.getSystemPropertyAsString(
"${test}" ) );
- assertEquals( "test/anil", StringUtil.getSystemPropertyAsString(
"test/${test}" ) );
-
- assertEquals( "anil:anil:marcus//anil",
StringUtil.getSystemPropertyAsString( "${test}:${test}:${person}//${test}" ) );
+ assertEquals("test",
StringUtil.getSystemPropertyAsString("test"));
+ assertEquals("test/test",
StringUtil.getSystemPropertyAsString("test/test"));
+ assertEquals("anil",
StringUtil.getSystemPropertyAsString("${test}"));
+ assertEquals("test/anil",
StringUtil.getSystemPropertyAsString("test/${test}"));
+
+ assertEquals("anil:anil:marcus//anil",
StringUtil.getSystemPropertyAsString("${test}:${test}:${person}//${test}"));
+
//Test if any of the parantheses are not correctly closed
- assertEquals( "anil:anil:marcus//${test",
StringUtil.getSystemPropertyAsString( "${test}:${test}:${person}//${test" ) );
+ assertEquals("anil:anil:marcus//${test",
+
StringUtil.getSystemPropertyAsString("${test}:${test}:${person}//${test"));
+
+ //Test the default values
+ assertEquals("http://something",
StringUtil.getSystemPropertyAsString("${dummy::http://something}"));
+ assertEquals("http://something__hi",
+
StringUtil.getSystemPropertyAsString("${dummy::http://something}__${to::hi}"));
+ assertEquals("anil:anil:marcus//anilhi",
+
StringUtil.getSystemPropertyAsString("${test}:${test}:${person}//${test}${to::hi}"));
+ assertEquals("anil:anil:marcus//anilhihttp://something",
+ StringUtil
+
.getSystemPropertyAsString("${test}:${test}:${person}//${test}${to::hi}${dummy::http://something}"));
}
}
\ No newline at end of file