Return-Path: X-Original-To: apmail-felix-commits-archive@www.apache.org Delivered-To: apmail-felix-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 022AA4398 for ; Fri, 10 Jun 2011 08:16:52 +0000 (UTC) Received: (qmail 38753 invoked by uid 500); 10 Jun 2011 08:16:51 -0000 Delivered-To: apmail-felix-commits-archive@felix.apache.org Received: (qmail 38693 invoked by uid 500); 10 Jun 2011 08:16:51 -0000 Mailing-List: contact commits-help@felix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@felix.apache.org Delivered-To: mailing list commits@felix.apache.org Received: (qmail 38686 invoked by uid 99); 10 Jun 2011 08:16:51 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 Jun 2011 08:16:51 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 Jun 2011 08:16:49 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 90A47238890D; Fri, 10 Jun 2011 08:16:29 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1134229 - /felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/InterpolationHelper.java Date: Fri, 10 Jun 2011 08:16:29 -0000 To: commits@felix.apache.org From: gnodet@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110610081629.90A47238890D@eris.apache.org> Author: gnodet Date: Fri Jun 10 08:16:29 2011 New Revision: 1134229 URL: http://svn.apache.org/viewvc?rev=1134229&view=rev Log: [FELIX-2988] Allow the substitution methods to receive a callback to obtain custom values Modified: felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/InterpolationHelper.java 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=1134229&r1=1134228&r2=1134229&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 Fri Jun 10 08:16:29 2011 @@ -16,22 +16,10 @@ */ package org.apache.felix.utils.properties; -import org.osgi.framework.BundleContext; +import java.util.HashMap; +import java.util.Map; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.FilterWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.LineNumberReader; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.Reader; -import java.io.Writer; -import java.net.URL; -import java.util.*; +import org.osgi.framework.BundleContext; /** *

@@ -50,6 +38,16 @@ public class InterpolationHelper { private static final String DELIM_START = "${"; private static final String DELIM_STOP = "}"; + + /** + * Callback for substitution + */ + public interface SubstitutionCallback { + + public String getValue(String key); + + } + /** * Perform substitution on a property set * @@ -57,7 +55,7 @@ public class InterpolationHelper { */ public static void performSubstitution(Map properties) { - performSubstitution(properties, null); + performSubstitution(properties, (BundleContext) null); } /** @@ -67,10 +65,20 @@ public class InterpolationHelper { */ public static void performSubstitution(Map properties, BundleContext context) { + performSubstitution(properties, new BundleContextSubstitutionCallback(context)); + } + + /** + * Perform substitution on a property set + * + * @param properties the property set to perform substitution on + */ + public static void performSubstitution(Map properties, SubstitutionCallback callback) + { for (String name : properties.keySet()) { String value = properties.get(name); - properties.put(name, substVars(value, name, null, properties, context)); + properties.put(name, substVars(value, name, null, properties, callback)); } } @@ -86,6 +94,7 @@ public class InterpolationHelper { * are substituted from inner most to outer most. Configuration * properties override system properties. *

+ * * @param val The string on which to perform property substitution. * @param currentKey The key of the property being evaluated used to * detect cycles. @@ -101,7 +110,40 @@ public class InterpolationHelper { Map cycleMap, Map configProps, BundleContext context) - throws IllegalArgumentException + throws IllegalArgumentException + { + return substVars(val, currentKey, cycleMap, configProps, new BundleContextSubstitutionCallback(context)); + } + + /** + *

+ * This method performs property variable substitution on the + * specified value. If the specified value contains the syntax + * ${<prop-name>}, where <prop-name> + * refers to either a configuration property or a system property, + * then the corresponding property value is substituted for the variable + * placeholder. Multiple variable placeholders may exist in the + * specified value as well as nested variable placeholders, which + * are substituted from inner most to outer most. Configuration + * properties override system properties. + *

+ * + * @param val The string on which to perform property substitution. + * @param currentKey The key of the property being evaluated used to + * detect cycles. + * @param cycleMap Map of variable references used to detect nested cycles. + * @param configProps Set of configuration properties. + * @param callback the callback to obtain substitution values + * @return The value of the specified string after system property substitution. + * @throws IllegalArgumentException If there was a syntax error in the + * property placeholder syntax or a recursive variable reference. + **/ + public static String substVars(String val, + String currentKey, + Map cycleMap, + Map configProps, + SubstitutionCallback callback) + throws IllegalArgumentException { if (cycleMap == null) { @@ -168,18 +210,17 @@ public class InterpolationHelper { { substValue = ""; } - else if (context != null) + else { - substValue = context.getProperty(variable); + if (callback != null) + { + substValue = callback.getValue(variable); + } if (substValue == null) { - substValue = ""; + substValue = System.getProperty(variable, ""); } } - else - { - substValue = System.getProperty(variable, ""); - } } // Remove the found variable from the cycle map, since @@ -194,7 +235,7 @@ public class InterpolationHelper { // Now perform substitution again, since there could still // be substitutions to make. - val = substVars(val, currentKey, cycleMap, configProps, context); + val = substVars(val, currentKey, cycleMap, configProps, callback); // Remove escape characters preceding {, } and \ val = unescape(val); @@ -203,7 +244,8 @@ public class InterpolationHelper { return val; } - private static String unescape(String val) { + private static String unescape(String val) + { int escape = val.indexOf(ESCAPE_CHAR); while (escape >= 0 && escape < val.length() - 1) { @@ -217,4 +259,28 @@ public class InterpolationHelper { return val; } + private static class BundleContextSubstitutionCallback implements SubstitutionCallback + { + private final BundleContext context; + + private BundleContextSubstitutionCallback(BundleContext context) + { + this.context = context; + } + + public String getValue(String key) + { + String value = null; + if (context != null) + { + value = context.getProperty(key); + } + if (value == null) + { + value = System.getProperty(key, ""); + } + return value; + } + } + } \ No newline at end of file