felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
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 GMT
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;
 
 /**
  * <p>
@@ -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<String,String> properties)
     {
-        performSubstitution(properties, null);
+        performSubstitution(properties, (BundleContext) null);
     }
 
     /**
@@ -67,10 +65,20 @@ public class InterpolationHelper {
      */
     public static void performSubstitution(Map<String,String> 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<String,String> 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.
      * </p>
+     *
      * @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<String,String> cycleMap,
                                    Map<String,String> configProps,
                                    BundleContext context)
-        throws IllegalArgumentException
+            throws IllegalArgumentException
+    {
+        return substVars(val, currentKey, cycleMap, configProps, new BundleContextSubstitutionCallback(context));
+    }
+
+    /**
+     * <p>
+     * This method performs property variable substitution on the
+     * specified value. If the specified value contains the syntax
+     * <tt>${&lt;prop-name&gt;}</tt>, where <tt>&lt;prop-name&gt;</tt>
+     * 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.
+     * </p>
+     *
+     * @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<String,String> cycleMap,
+                                   Map<String,String> 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



Mime
View raw message