Author: anil.saldhana(a)jboss.com
Date: 2011-02-23 11:36:01 -0500 (Wed, 23 Feb 2011)
New Revision: 769
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: add a method to StringUtil to pick a system property if needed
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-02-23
02:27:19 UTC (rev 768)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/util/StringUtil.java 2011-02-23
16:36:01 UTC (rev 769)
@@ -24,6 +24,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
@@ -60,14 +62,26 @@
*/
public static String getSystemPropertyAsString( String str )
{
- if( str.startsWith( "${") && str.endsWith( "}" ))
- {
- int len = str.length();
- str = str.substring( 2, len -1 );
- String sysPropertyValue = SecurityActions.getSystemProperty(str, ""
);
- if( sysPropertyValue.isEmpty() )
- throw new IllegalArgumentException( "System Property " + str +
" is not set" );
- str = sysPropertyValue;
+ if( str.contains( "${") )
+ {
+ Pattern pattern = Pattern.compile( "\\$\\{([^}]+)}" );
+ Matcher matcher = pattern.matcher(str);
+
+ StringBuffer buffer = new StringBuffer();
+ String sysPropertyValue = null;
+
+ while (matcher.find())
+ {
+ sysPropertyValue = SecurityActions.getSystemProperty( matcher.group(1),
"" );
+ if( sysPropertyValue.isEmpty() )
+ {
+ throw new IllegalArgumentException( "System Property " +
matcher.group(1) + " is not set" );
+ }
+ matcher.appendReplacement(buffer,sysPropertyValue);
+ }
+
+ matcher.appendTail(buffer);
+ str = buffer.toString();
}
return str;
}
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-02-23
02:27:19 UTC (rev 768)
+++
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/SystemPropertyAsStringUnitTestCase.java 2011-02-23
16:36:01 UTC (rev 769)
@@ -23,6 +23,7 @@
import static org.junit.Assert.assertEquals;
+import org.junit.Before;
import org.junit.Test;
import org.picketlink.identity.federation.core.util.StringUtil;
@@ -34,13 +35,25 @@
*/
public class SystemPropertyAsStringUnitTestCase
{
+ @Before
+ public void setup()
+ {
+ System.setProperty( "test", "anil" );
+ System.setProperty( "person", "marcus" );
+ }
@Test
public void testSystemProperty() throws Exception
{
- System.setProperty( "test", "anil" );
- String str = "${test}";
- assertEquals( "anil", StringUtil.getSystemPropertyAsString( str ) );
+ 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" ) );
}
-
}
\ No newline at end of file
Show replies by date