Attached is the patch, adding three new functions,
expandEnvironmentalVariables( ExtendedProperties ),
expandEnvironmentalVariables(Vector ), expandEnvironmentalVariables(String).
The expansion should really take place in ExtendedProperties, but this
might work in the interim .
Is their someone to codereview this ?
Thanks!
Charlei
Index:
/home/csanders/workspace/Velocity/src/java/org/apache/velocity/runtime/RuntimeInstance.java
===================================================================
---
/home/csanders/workspace/Velocity/src/java/org/apache/velocity/runtime/RuntimeInstance.java
(revision 653790)
+++
/home/csanders/workspace/Velocity/src/java/org/apache/velocity/runtime/RuntimeInstance.java
(working copy)
@@ -29,8 +29,10 @@
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
+import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
+import java.util.Vector;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.Template;
@@ -395,7 +397,12 @@
overridingProperties = new ExtendedProperties();
}
- overridingProperties.setProperty(key, value);
+ Object newValue = value;
+
+ if ( value instanceof String ) newValue =
expandEnvironmentalVariable((String)value);
+ else if ( value instanceof Vector ) newValue =
this.expandEnvironmentalVariables((Vector)value);
+
+ overridingProperties.setProperty(key, newValue);
}
/**
@@ -410,6 +417,9 @@
*/
public void setConfiguration( ExtendedProperties configuration)
{
+
+ expandEnvironmentalVariables(configuration);
+
if (overridingProperties == null)
{
overridingProperties = configuration;
@@ -450,7 +460,12 @@
overridingProperties = new ExtendedProperties();
}
- overridingProperties.addProperty(key, value);
+ Object newValue = value;
+
+ if ( value instanceof String ) newValue =
expandEnvironmentalVariable((String)value);
+ else if ( value instanceof Vector ) newValue =
this.expandEnvironmentalVariables((Vector)value);
+
+ overridingProperties.addProperty(key, newValue);
}
/**
@@ -521,6 +536,7 @@
if (configuration.isInitialized() == false)
{
setDefaultProperties();
+ expandEnvironmentalVariables(configuration);
}
if( overridingProperties != null)
@@ -525,6 +541,7 @@
if( overridingProperties != null)
{
+ expandEnvironmentalVariables(overridingProperties);
configuration.combine(overridingProperties);
}
}
@@ -529,13 +546,85 @@
}
}
+
/**
- * Initialize the Velocity Runtime with a Properties
- * object.
- *
- * @param p
- * @throws Exception When an error occurs during initialization.
- */
+ * Replace all environmental variables in the form of ${VELOCITY_HOME}
with their expansions
+ *
+ * @param properties
+ */
+
+ private Vector expandEnvironmentalVariables(Vector stringVector) {
+ Vector newVector = new Vector();
+ Iterator vectorIterator = stringVector.iterator();
+
+ while (vectorIterator.hasNext()) {
+ String newValue = expandEnvironmentalVariable((String)
vectorIterator.next());
+ newVector.add(newValue);
+ }
+
+ return newVector;
+ }
+
+ private void expandEnvironmentalVariables(ExtendedProperties
properties) {
+
+ Iterator propertiesIterator = properties.getKeys();
+ Map convertedValues = new HashMap();
+
+ while (propertiesIterator.hasNext()) {
+ String key = (String) propertiesIterator.next();
+ Object value = properties.get(key);
+
+ if (value instanceof String) {
+ String newValue =
expandEnvironmentalVariable((String) value);
+ convertedValues.put(key, newValue);
+ }
+ else if (value instanceof Vector) {
+ Vector newVector =
expandEnvironmentalVariables((Vector) value);
+ convertedValues.put(key, newVector);
+ }
+ }
+ properties.putAll(convertedValues);
+ }
+
+ /**
+ * Replace all occurrences of ${} in a string with its corresponding
expanded environmental var's
+ * enviornment
+ * @param string
+ * @return
+ */
+
+ private String expandEnvironmentalVariable(String string) {
+
+ int start = string.indexOf("${");
+
+ if (start != -1) {
+
+ int end = string.indexOf('}');
+
+ if (end != -1) {
+ String env = string.substring(start + 2, end);
+ String regex = "\\$\\{" + env + "\\}";
+ String replacement = System.getenv(env);
+
+ if (replacement != null) { return
string.replaceAll(regex, replacement);
+
+ }
+
+ }
+
+ }
+ return string;
+
+ }
+
+
+ /**
+ * Initialize the Velocity Runtime with a Properties object.
+ *
+ * @param p
+ * @throws Exception
+ * When an error occurs during initialization.
+ */
public void init(Properties p) throws Exception
{
overridingProperties = ExtendedProperties.convertProperties(p);
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]