summaryrefslogtreecommitdiff
path: root/qpid/java/common/src
diff options
context:
space:
mode:
authorRafael H. Schloming <rhs@apache.org>2009-06-18 21:03:12 +0000
committerRafael H. Schloming <rhs@apache.org>2009-06-18 21:03:12 +0000
commit0ac0ba5e1d146a133abbd1ea0ddcabe6d25ab987 (patch)
tree0d65318b1d4b1ff9eef825c31e6113d334146871 /qpid/java/common/src
parentc2e8e0f59377b04019decbe2eb6c6f3db634b988 (diff)
downloadqpid-python-0ac0ba5e1d146a133abbd1ea0ddcabe6d25ab987.tar.gz
made test ports configurable; this required adding variable expansion to PropertiesFileInitialContextFactory
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@786269 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java/common/src')
-rw-r--r--qpid/java/common/src/main/java/org/apache/qpid/util/Strings.java144
1 files changed, 144 insertions, 0 deletions
diff --git a/qpid/java/common/src/main/java/org/apache/qpid/util/Strings.java b/qpid/java/common/src/main/java/org/apache/qpid/util/Strings.java
index a0bbbb22de..04bf174ff0 100644
--- a/qpid/java/common/src/main/java/org/apache/qpid/util/Strings.java
+++ b/qpid/java/common/src/main/java/org/apache/qpid/util/Strings.java
@@ -22,6 +22,12 @@ package org.apache.qpid.util;
import java.io.UnsupportedEncodingException;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Stack;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
/**
* Strings
@@ -91,4 +97,142 @@ public final class Strings
}
}
+ private static final Pattern VAR = Pattern.compile("(?:\\$\\{([^\\}]*)\\})|(?:\\$(\\$))");
+
+ public static interface Resolver
+ {
+ String resolve(String variable);
+ }
+
+ public static class MapResolver implements Resolver
+ {
+
+ private final Map<String,String> map;
+
+ public MapResolver(Map<String,String> map)
+ {
+ this.map = map;
+ }
+
+ public String resolve(String variable)
+ {
+ return map.get(variable);
+ }
+ }
+
+ public static class PropertiesResolver implements Resolver
+ {
+
+ private final Properties properties;
+
+ public PropertiesResolver(Properties properties)
+ {
+ this.properties = properties;
+ }
+
+ public String resolve(String variable)
+ {
+ return properties.getProperty(variable);
+ }
+ }
+
+ public static class ChainedResolver implements Resolver
+ {
+ private final Resolver primary;
+ private final Resolver secondary;
+
+ public ChainedResolver(Resolver primary, Resolver secondary)
+ {
+ this.primary = primary;
+ this.secondary = secondary;
+ }
+
+ public String resolve(String variable)
+ {
+ String result = primary.resolve(variable);
+ if (result == null)
+ {
+ result = secondary.resolve(variable);
+ }
+ return result;
+ }
+ }
+
+ public static final Resolver SYSTEM_RESOLVER = new Resolver()
+ {
+ public String resolve(String variable)
+ {
+ String result = System.getProperty(variable);
+ if (result == null)
+ {
+ result = System.getenv(variable);
+ }
+ return result;
+ }
+ };
+
+ public static final String expand(String input)
+ {
+ return expand(input, SYSTEM_RESOLVER);
+ }
+
+ public static final String expand(String input, Resolver resolver)
+ {
+ return expand(input, resolver, new Stack());
+ }
+
+ private static final String expand(String input, Resolver resolver, Stack<String> stack)
+ {
+ Matcher m = VAR.matcher(input);
+ StringBuffer result = new StringBuffer();
+ while (m.find())
+ {
+ String var = m.group(1);
+ if (var == null)
+ {
+ String esc = m.group(2);
+ if ("$".equals(esc))
+ {
+ m.appendReplacement(result, Matcher.quoteReplacement("$"));
+ }
+ else
+ {
+ throw new IllegalArgumentException(esc);
+ }
+ }
+ else
+ {
+ m.appendReplacement(result, Matcher.quoteReplacement(resolve(var, resolver, stack)));
+ }
+ }
+ m.appendTail(result);
+ return result.toString();
+ }
+
+ private static final String resolve(String var, Resolver resolver, Stack<String> stack)
+ {
+ if (stack.contains(var))
+ {
+ throw new IllegalArgumentException
+ (String.format("recursively defined variable: %s stack=%s", var,
+ stack));
+ }
+
+ String result = resolver.resolve(var);
+ if (result == null)
+ {
+ throw new IllegalArgumentException("no such variable: " + var);
+ }
+
+ stack.push(var);
+ try
+ {
+ return expand(result, resolver, stack);
+ }
+ finally
+ {
+ stack.pop();
+ }
+ }
+
}