Issue Details (XML | Word | Printable)

Key: CONFIGURATION-284
Type: New Feature New Feature
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Oliver Heger
Reporter: Aksel Schmidt
Votes: 1
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
Commons Configuration

ability to read OS environment variables

Created: 06/Jul/07 06:45 AM   Updated: 06/Oct/09 04:56 AM
Return to search
Component/s: Interpolation
Affects Version/s: None
Fix Version/s: 1.5

Time Tracking:
Not Specified

File Attachments:
  Size
Text File Licensed for inclusion in ASF works CONFIGURATION-284.patch 2007-09-21 07:18 AM nicolas de loof 5 kB
Java Source File Licensed for inclusion in ASF works EnvironmentConfiguration.java 2007-07-12 05:21 AM Vasily Ivanov 2 kB

Resolution Date: 23/Oct/07 08:11 PM


 Description  « Hide
suggestion:
interpolation with system environment, i.e.
${sys:user.name} reads from systemproperties (as of today)
${sysenv:TEMP} reads from OS environment

these can be parsed from a supported set of OS's - windows read from a set in a "cmd /c", unix/linux from an "env" etc. (just once on demand)



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Joerg Schaible added a comment - 06/Jul/07 07:07 AM - edited
Interpolation for system properties is already available, simply set up your configuration properly:
final CompositeConfiguration configuration = new CompositeConfiguration();

// system properties override every setting
configuration.addConfiguration(ConfigurationConverter.getConfiguration(System.getProperties()));

// add other configuration instances here

Oliver Heger added a comment - 07/Jul/07 02:55 PM
Joerg, I think this is a misunderstanding: This feature request is about environment variables, while Configuration so far only supports Java's system properties.

I had a vague memory that the topic "access to environment variables" was discussed before on the commons mailing list, and found this reference:

http://thread.gmane.org/gmane.comp.jakarta.commons.devel/33239/focus=33325

Obviously this thread did not come to a final solution, AFAIK this feature was not added to one of the commons libraries. If it was (for instance in commons-lang), it would be really easy to incorporate it into commons-configuration. But having to implement it ourselves, situation looks different. The problem I see here is that it will really be difficult to implement a robust and portable solution, and besides this feature is probably out of scope for commons-configuration.

From Java 1.5 on, access to environment properties will be possible using methods of the System class. So when we switch to this version (no idea when this will happen though) it will be no problem to satisfy this request. But for the moment I am reluctant.


Vasily Ivanov added a comment - 12/Jul/07 05:21 AM
I have create simple Configuration to read environment variables. Please see file attached.

Also, I have tried to use PropertiesConfiguration.PropertiesReader#parseProperty(String line) magic, but it returns corrupted Windows paths by unescapeing variables values when it is not necessary. I didn't find a way to prevent it doing that. However, here is the code for you convenience if you want to play around:

private static Properties readEnvVariables(final InputStream in) throws IOException
{
Properties props = new Properties();
PropertiesConfiguration.PropertiesReader reader =
new PropertiesConfiguration.PropertiesReader(new InputStreamReader(in));
while (reader.nextProperty()) { props.setProperty(reader.getPropertyName(), reader.getPropertyValue()); }

return props;
}

Note: I haven't tested it in UNIX, but it should work just fine.


Oliver Heger added a comment - 21/Jul/07 02:37 PM
Vasily, many thanks for your contribution.

For the reasons outlined above I am still reluctant to add this feature to the code base. If you don't mind I would bring this topic again to the commons developer list and ask whether the opinion to support environment variables in one of the core libraries has changed in the mean time.

What we could do in any case is adding your code to the WIKI in our examples section:
http://wiki.apache.org/jakarta-commons/Configuration


Vasily Ivanov added a comment - 22/Jul/07 01:20 AM
Thanks Oliver, feel free to add that code anywhere you want.

nicolas de loof added a comment - 21/Sep/07 07:18 AM
As discussed on mailing list
(http://mail-archives.apache.org/mod_mbox/commons-dev/200709.mbox/%3c46F17EC2.2080600@oliver-heger.de%3e)
This patch introduces a new EnvironmentConfiguration implementation of Configuration that uses apache Ant on java < 5 to read the platform environment. On java5 runtime it uses System.getEnv() to avoid unecessary dependency on ant.

the ant dependency has been set optionnal in the POM, as it is required only for this feature and only on java < 5


Oliver Heger added a comment - 22/Sep/07 08:04 PM
Many thanks for this contribution. Some questions:
  • Is it possible to add a unit test?
  • Do you know whether ant is compatible with JDK 1.3? This is still our target JDK. If it is, I can directly apply this patch. Otherwise we probably have to wait for the next version (I expect configuration 1.5 to be last version that is compatible to JDK 1.3; the next version - 1.6 or 2.0, however it will be called - will require JDK 1.4).

Thanks again.


Oliver Heger added a comment - 23/Oct/07 08:11 PM
The patch was applied with some modifications:
  • The code in the constructor for obtaining the environment properties was refactored into new methods. This makes testing much easier.
  • For the check of the Java version SystemUtils from commons-lang is used; we depend on lang anyway.
  • The used getenv() method is available since 1.5 only. To make the class compile on earlier versions, the method has to be invoked per reflection.
  • Some additional methods have been overridden to ensure that the configuration is read-only.
  • A unit test was added.

I did some testing on Java 1.5 and on earlier versions as well. Everything seems to work.


Chandra added a comment - 06/Oct/09 04:56 AM
Hello there,
The "env" interpolator prefix still does not seem to work. On investigation, I noticed that we have a new java class to support reading environment variables from different OSes, but there is no interpolator class (extending StrLookup) that supports the "env" prefix.
Could someone look into this? For the moment, I have put in a class myself locally. Will add to the repository once I find some time..

Regards.