On Jan 9, 2008 10:03 PM, <[EMAIL PROTECTED]> wrote: > Author: maartenc > Date: Wed Jan 9 13:02:58 2008 > New Revision: 610562 > > URL: http://svn.apache.org/viewvc?rev=610562&view=rev > Log: > NEW: Add support for importing environment variables (IVY-608)
[...] > Modified: > ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvyVariableContainerImpl.java > URL: > http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvyVariableContainerImpl.java?rev=610562&r1=610561&r2=610562&view=diff > > ============================================================================== > --- > ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvyVariableContainerImpl.java > (original) > +++ > ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvyVariableContainerImpl.java > Wed Jan 9 13:02:58 2008 > @@ -25,8 +25,17 @@ > > public class IvyVariableContainerImpl implements IvyVariableContainer { > > - private HashMap variables = new HashMap(); > + private Map variables; > + private String envPrefix; > > + public IvyVariableContainerImpl() { > + this.variables = new HashMap(); > + } > + > + public IvyVariableContainerImpl(Map variables) { > + this.variables = variables; > + } > + > /* > * (non-Javadoc) > * > @@ -40,21 +49,27 @@ > } else { > Message.debug("'" + varName + "' already set: discarding '" + > value + "'"); > } > - > + } > + > + public void setEnvironmentPrefix(String prefix) { > + if ((prefix != null) && !prefix.endsWith(".")) { > + this.envPrefix = prefix + "."; > + } else { > + this.envPrefix = prefix; > + } > } > > private String substitute(String value) { > - return IvyPatternHelper.substituteVariables(value, > getVariables()); > + return IvyPatternHelper.substituteVariables(value, this); > } > > - /* > - * (non-Javadoc) > - * > - * @see > org.apache.ivy.core.settings.IvyVariableContainer#getVariables() > - */ > - public Map getVariables() { > + protected Map getVariables() { > return variables; > } > + > + protected String getEnvironmentPrefix() { > + return envPrefix; > + } > > /* > * (non-Javadoc) > @@ -62,8 +77,14 @@ > * @see org.apache.ivy.core.settings.IvyVariableContainer#getVariable( > java.lang.String) > */ > public String getVariable(String name) { > - String val = (String) variables.get(name); > - return val == null ? val : substitute(val); > + String val = null; > + if ((envPrefix != null) && name.startsWith(envPrefix)) { > + val = System.getenv(name.substring(envPrefix.length())); I think System.getenv behavior is very jre dependent. Here is the implementation in sun JDK 1.4.2_13: public static String getenv(String name) { throw new Error("getenv no longer supported, use properties and -D instead: " + name); } This won't really help the user :-) So maybe we could surround the call with a try catch (catching Error or even Throwable, too bad they didn't use a more specific throwable, we may catch OutOfMemoryError here :-( ) and reporting the problem to the user in a more friendly way like "Impossible to use Ivy environment prefix: System.getenv(String) doesn't seem to be supported on your platform" and chaining the original Throwable. Then in the documentation we could report that environmentPrefix is supported only in Ant or with JRE supporting System.getenv(String) (i.e. >= 1.5.0 from what I know). Xavier -- Xavier Hanin - Independent Java Consultant http://xhab.blogspot.com/ http://ant.apache.org/ivy/ http://www.xoocode.org/