You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by gn...@apache.org on 2015/01/08 09:50:37 UTC
svn commit: r1650235 - in /felix/trunk: fileinstall/pom.xml
utils/src/main/java/org/apache/felix/utils/properties/InterpolationHelper.java
utils/src/test/java/org/apache/felix/utils/properties/InterpolationHelperTest.java
Author: gnodet
Date: Thu Jan 8 08:50:36 2015
New Revision: 1650235
URL: http://svn.apache.org/r1650235
Log:
[FELIX-4759] Support env:XXX subtitution for environment variables
[FELIX-4760] Support default/alternate values for variable substitution
Modified:
felix/trunk/fileinstall/pom.xml
felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/InterpolationHelper.java
felix/trunk/utils/src/test/java/org/apache/felix/utils/properties/InterpolationHelperTest.java
Modified: felix/trunk/fileinstall/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/fileinstall/pom.xml?rev=1650235&r1=1650234&r2=1650235&view=diff
==============================================================================
--- felix/trunk/fileinstall/pom.xml (original)
+++ felix/trunk/fileinstall/pom.xml Thu Jan 8 08:50:36 2015
@@ -57,7 +57,7 @@
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.utils</artifactId>
- <version>1.6.0</version>
+ <version>1.6.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
Modified: felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/InterpolationHelper.java
URL: http://svn.apache.org/viewvc/felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/InterpolationHelper.java?rev=1650235&r1=1650234&r2=1650235&view=diff
==============================================================================
--- felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/InterpolationHelper.java (original)
+++ felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/InterpolationHelper.java Thu Jan 8 08:50:36 2015
@@ -38,6 +38,7 @@ public class InterpolationHelper {
private static final String DELIM_START = "${";
private static final String DELIM_STOP = "}";
private static final String MARKER = "$__";
+ private static final String ENV_PREFIX = "env:";
/**
@@ -299,6 +300,18 @@ public class InterpolationHelper {
// Using the start and stop delimiter indices, extract
// the first, deepest nested variable placeholder.
String variable = val.substring(startDelim + DELIM_START.length(), stopDelim);
+ String org = variable;
+
+ // Strip expansion modifiers
+ int idx1 = variable.lastIndexOf(":-");
+ int idx2 = variable.lastIndexOf(":+");
+ int idx = idx1 >= 0 && idx2 >= 0 ? Math.min(idx1, idx2) : idx1 >= 0 ? idx1 : idx2;
+ String op = null;
+ if (idx >= 0 && idx < variable.length())
+ {
+ op = variable.substring(idx);
+ variable = variable.substring(0, idx);
+ }
// Verify that this is not a recursive variable reference.
if (cycleMap.get(variable) != null)
@@ -315,11 +328,7 @@ public class InterpolationHelper {
}
if (substValue == null)
{
- if (variable.length() <= 0)
- {
- substValue = "";
- }
- else
+ if (variable.length() > 0)
{
if (callback != null)
{
@@ -329,20 +338,43 @@ public class InterpolationHelper {
{
substValue = System.getProperty(variable);
}
- if (substValue == null)
+ }
+ }
+
+ if (op != null)
+ {
+ if (op.startsWith(":-"))
+ {
+ if (substValue == null || substValue.isEmpty())
+ {
+ substValue = op.substring(":-".length());
+ }
+ }
+ else if (op.startsWith(":+"))
+ {
+ if (substValue != null && !substValue.isEmpty())
{
- if (defaultsToEmptyString)
- {
- substValue = "";
- }
- else
- {
- // alters the original token to avoid infinite recursion
- // altered tokens are reverted in substVarsPreserveUnresolved()
- substValue = MARKER + "{" + variable + "}";
- }
+ substValue = op.substring(":+".length());
}
}
+ else
+ {
+ throw new IllegalArgumentException("Bad substitution: ${" + org + "}");
+ }
+ }
+
+ if (substValue == null)
+ {
+ if (defaultsToEmptyString)
+ {
+ substValue = "";
+ }
+ else
+ {
+ // alters the original token to avoid infinite recursion
+ // altered tokens are reverted in substVarsPreserveUnresolved()
+ substValue = MARKER + "{" + variable + "}";
+ }
}
// Remove the found variable from the cycle map, since
@@ -391,13 +423,20 @@ public class InterpolationHelper {
public String getValue(String key)
{
String value = null;
- if (context != null)
+ if (key.startsWith(ENV_PREFIX))
{
- value = context.getProperty(key);
+ value = System.getenv(key.substring(ENV_PREFIX.length()));
}
- if (value == null)
+ else
{
- value = System.getProperty(key, "");
+ if (context != null)
+ {
+ value = context.getProperty(key);
+ }
+ if (value == null)
+ {
+ value = System.getProperty(key);
+ }
}
return value;
}
Modified: felix/trunk/utils/src/test/java/org/apache/felix/utils/properties/InterpolationHelperTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/utils/src/test/java/org/apache/felix/utils/properties/InterpolationHelperTest.java?rev=1650235&r1=1650234&r2=1650235&view=diff
==============================================================================
--- felix/trunk/utils/src/test/java/org/apache/felix/utils/properties/InterpolationHelperTest.java (original)
+++ felix/trunk/utils/src/test/java/org/apache/felix/utils/properties/InterpolationHelperTest.java Thu Jan 8 08:50:36 2015
@@ -162,7 +162,6 @@ public class InterpolationHelperTest ext
assertEquals("$\\{var}bc", map1.get("abc"));
}
- @Test
public void testPreserveUnresolved() {
Map<String, String> props = new HashMap<String, String>();
props.put("a", "${b}");
@@ -176,4 +175,28 @@ public class InterpolationHelperTest ext
props.put("c", "${d}${d}");
assertEquals("${d}${d}", InterpolationHelper.substVars("${d}${d}", "c", null, props, null, false, false, false));
}
+
+ public void testExpansion() {
+ Map<String, String> props = new LinkedHashMap<String, String>();
+ props.put("a", "foo");
+ props.put("b", "");
+
+ props.put("a_cm", "${a:-bar}");
+ props.put("b_cm", "${b:-bar}");
+ props.put("c_cm", "${c:-bar}");
+
+ props.put("a_cp", "${a:+bar}");
+ props.put("b_cp", "${b:+bar}");
+ props.put("c_cp", "${c:+bar}");
+
+ InterpolationHelper.performSubstitution(props);
+
+ assertEquals("foo", props.get("a_cm"));
+ assertEquals("bar", props.get("b_cm"));
+ assertEquals("bar", props.get("c_cm"));
+
+ assertEquals("bar", props.get("a_cp"));
+ assertEquals("", props.get("b_cp"));
+ assertEquals("", props.get("c_cp"));
+ }
}