You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2018/04/20 14:55:38 UTC
[lang] [LANG-1393] Add API SystemUtils.String
getEnvironmentVariable(final String name, final String defaultValue).
Repository: commons-lang
Updated Branches:
refs/heads/master 8e3ec1722 -> efba54d35
[LANG-1393] Add API SystemUtils.String getEnvironmentVariable(final
String name, final String defaultValue).
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/efba54d3
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/efba54d3
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/efba54d3
Branch: refs/heads/master
Commit: efba54d35fa094de5e580b200a8607bfc7bd5a7a
Parents: 8e3ec17
Author: Gary Gregory <ga...@gmail.com>
Authored: Fri Apr 20 08:55:32 2018 -0600
Committer: Gary Gregory <ga...@gmail.com>
Committed: Fri Apr 20 08:55:32 2018 -0600
----------------------------------------------------------------------
src/changes/changes.xml | 1 +
.../org/apache/commons/lang3/SystemUtils.java | 27 ++++++++++++++++++++
.../apache/commons/lang3/SystemUtilsTest.java | 18 +++++++++++++
3 files changed, 46 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/efba54d3/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a4cbbf3..4ec1984 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -59,6 +59,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1372" type="add" dev="pschumacher" due-to="Sérgio Ozaki">Add ToStringSummary annotation</action>
<action issue="LANG-1356" type="add" dev="pschumacher" due-to="Yathos UG">Add bypass option for classes to recursive and reflective EqualsBuilder</action>
<action issue="LANG-1391" type="add" dev="ggregory" due-to="Sauro Matulli, Oleg Chubaryov">Improve Javadoc for StringUtils.isAnyEmpty(null)</action>
+ <action issue="LANG-1393" type="add" dev="ggregory" due-to="Gary Gregory">Add API SystemUtils.String getEnvironmentVariable(final String name, final String defaultValue)</action>
</release>
<release version="3.7" date="2017-11-04" description="New features and bug fixes. Requires Java 7, supports Java 8, 9, 10.">
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/efba54d3/src/main/java/org/apache/commons/lang3/SystemUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/SystemUtils.java b/src/main/java/org/apache/commons/lang3/SystemUtils.java
index f91628a..2289d5d 100644
--- a/src/main/java/org/apache/commons/lang3/SystemUtils.java
+++ b/src/main/java/org/apache/commons/lang3/SystemUtils.java
@@ -1607,6 +1607,33 @@ public class SystemUtils {
/**
* <p>
+ * Gets an environment variable, defaulting to {@code defaultValue} if the variable cannot be read.
+ * </p>
+ * <p>
+ * If a {@code SecurityException} is caught, the return value is {@code defaultValue} and a message is written to
+ * {@code System.err}.
+ * </p>
+ *
+ * @param name
+ * the environment variable name
+ * @param defaultValue
+ * the default value
+ * @return the environment variable value or {@code defaultValue} if a security problem occurs
+ * @since 3.7
+ */
+ public static String getEnvironmentVariable(final String name, final String defaultValue) {
+ try {
+ final String value = System.getenv(name);
+ return value == null ? defaultValue : value;
+ } catch (final SecurityException ex) {
+ // we are not allowed to look at this property
+ System.err.println("Caught a SecurityException reading the environment variable '" + name + "'.");
+ return defaultValue;
+ }
+ }
+
+ /**
+ * <p>
* Gets the user directory as a {@code File}.
* </p>
*
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/efba54d3/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java b/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
index 77e6078..e89e20c 100644
--- a/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
@@ -40,6 +40,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Locale;
+import org.junit.Assert;
import org.junit.Test;
/**
@@ -60,6 +61,23 @@ public class SystemUtilsTest {
}
@Test
+ public void testGetEnvironmentVariableAbsent() {
+ final String name = "THIS_ENV_VAR_SHOULD_NOT_EXIST_FOR_THIS_TEST_TO_PASS";
+ final String expected = System.getenv(name);
+ Assert.assertNull(expected);
+ final String value = SystemUtils.getEnvironmentVariable(name, "DEFAULT");
+ assertEquals("DEFAULT", value);
+ }
+
+ @Test
+ public void testGetEnvironmentVariablePresent() {
+ final String name = "PATH";
+ final String expected = System.getenv(name);
+ final String value = SystemUtils.getEnvironmentVariable(name, null);
+ assertEquals(expected, value);
+ }
+
+ @Test
public void testGetHostName() {
final String hostName = SystemUtils.getHostName();
final String expected = SystemUtils.IS_OS_WINDOWS ? System.getenv("COMPUTERNAME") : System.getenv("HOSTNAME");
Re: [lang] [LANG-1393] Add API SystemUtils.String getEnvironmentVariable(final
String name, final String defaultValue).
Posted by Gary Gregory <ga...@gmail.com>.
Thank you for the review.
Done: https://issues.apache.org/jira/browse/LANG-1394
Gary
On Sun, Apr 22, 2018 at 7:06 AM, sebb <se...@gmail.com> wrote:
> On 22 April 2018 at 13:44, Gary Gregory <ga...@gmail.com> wrote:
> > On Sun, Apr 22, 2018, 03:55 sebb <se...@gmail.com> wrote:
> >
> >> On 20 April 2018 at 15:55, <gg...@apache.org> wrote:
> >> > Repository: commons-lang
> >> > Updated Branches:
> >> > refs/heads/master 8e3ec1722 -> efba54d35
> >> >
> >> >
> >> > [LANG-1393] Add API SystemUtils.String getEnvironmentVariable(final
> >> > String name, final String defaultValue).
> >> >
> >> > Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
> >> > Commit:
> >> http://git-wip-us.apache.org/repos/asf/commons-lang/commit/efba54d3
> >> > Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/
> efba54d3
> >> > Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/
> efba54d3
> >> >
> >> > Branch: refs/heads/master
> >> > Commit: efba54d35fa094de5e580b200a8607bfc7bd5a7a
> >> > Parents: 8e3ec17
> >> > Author: Gary Gregory <ga...@gmail.com>
> >> > Authored: Fri Apr 20 08:55:32 2018 -0600
> >> > Committer: Gary Gregory <ga...@gmail.com>
> >> > Committed: Fri Apr 20 08:55:32 2018 -0600
> >> >
> >> > ------------------------------------------------------------
> ----------
> >> > src/changes/changes.xml | 1 +
> >> > .../org/apache/commons/lang3/SystemUtils.java | 27
> >> ++++++++++++++++++++
> >> > .../apache/commons/lang3/SystemUtilsTest.java | 18 +++++++++++++
> >> > 3 files changed, 46 insertions(+)
> >> > ------------------------------------------------------------
> ----------
> >> >
> >> >
> >> >
> >> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/
> efba54d3/src/changes/changes.xml
> >> > ------------------------------------------------------------
> ----------
> >> > diff --git a/src/changes/changes.xml b/src/changes/changes.xml
> >> > index a4cbbf3..4ec1984 100644
> >> > --- a/src/changes/changes.xml
> >> > +++ b/src/changes/changes.xml
> >> > @@ -59,6 +59,7 @@ The <action> type attribute can be
> >> add,update,fix,remove.
> >> > <action issue="LANG-1372" type="add" dev="pschumacher"
> >> due-to="Sérgio Ozaki">Add ToStringSummary annotation</action>
> >> > <action issue="LANG-1356" type="add" dev="pschumacher"
> >> due-to="Yathos UG">Add bypass option for classes to recursive and
> >> reflective EqualsBuilder</action>
> >> > <action issue="LANG-1391" type="add" dev="ggregory" due-to="Sauro
> >> Matulli, Oleg Chubaryov">Improve Javadoc for
> >> StringUtils.isAnyEmpty(null)</action>
> >> > + <action issue="LANG-1393" type="add" dev="ggregory" due-to="Gary
> >> Gregory">Add API SystemUtils.String getEnvironmentVariable(final String
> >> name, final String defaultValue)</action>
> >> > </release>
> >> >
> >> > <release version="3.7" date="2017-11-04" description="New features
> >> and bug fixes. Requires Java 7, supports Java 8, 9, 10.">
> >> >
> >> >
> >> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/
> efba54d3/src/main/java/org/apache/commons/lang3/SystemUtils.java
> >> > ------------------------------------------------------------
> ----------
> >> > diff --git a/src/main/java/org/apache/commons/lang3/SystemUtils.java
> >> b/src/main/java/org/apache/commons/lang3/SystemUtils.java
> >> > index f91628a..2289d5d 100644
> >> > --- a/src/main/java/org/apache/commons/lang3/SystemUtils.java
> >> > +++ b/src/main/java/org/apache/commons/lang3/SystemUtils.java
> >> > @@ -1607,6 +1607,33 @@ public class SystemUtils {
> >> >
> >> > /**
> >> > * <p>
> >> > + * Gets an environment variable, defaulting to {@code
> defaultValue}
> >> if the variable cannot be read.
> >> > + * </p>
> >> > + * <p>
> >> > + * If a {@code SecurityException} is caught, the return value is
> >> {@code defaultValue} and a message is written to
> >> > + * {@code System.err}.
> >> > + * </p>
> >> > + *
> >> > + * @param name
> >> > + * the environment variable name
> >> > + * @param defaultValue
> >> > + * the default value
> >> > + * @return the environment variable value or {@code defaultValue}
> >> if a security problem occurs
> >> > + * @since 3.7
> >> > + */
> >> > + public static String getEnvironmentVariable(final String name,
> >> final String defaultValue) {
> >> > + try {
> >> > + final String value = System.getenv(name);
> >> > + return value == null ? defaultValue : value;
> >> > + } catch (final SecurityException ex) {
> >> > + // we are not allowed to look at this property
> >> > + System.err.println("Caught a SecurityException reading
> the
> >> environment variable '" + name + "'.");
> >>
> >> -1
> >>
> >> Library code should not write to stderr or stdout
> >>
> >
> > We already do, in the same class.
>
> Then that also needs to be fixed...
>
> > Gary
> >
> >>
> >> > + return defaultValue;
> >> > + }
> >> > + }
> >> > +
> >> > + /**
> >> > + * <p>
> >> > * Gets the user directory as a {@code File}.
> >> > * </p>
> >> > *
> >> >
> >> >
> >> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/
> efba54d3/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
> >> > ------------------------------------------------------------
> ----------
> >> > diff --git a/src/test/java/org/apache/commons/lang3/SystemUtilsTest.
> java
> >> b/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
> >> > index 77e6078..e89e20c 100644
> >> > --- a/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
> >> > +++ b/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
> >> > @@ -40,6 +40,7 @@ import java.lang.reflect.Constructor;
> >> > import java.lang.reflect.Modifier;
> >> > import java.util.Locale;
> >> >
> >> > +import org.junit.Assert;
> >> > import org.junit.Test;
> >> >
> >> > /**
> >> > @@ -60,6 +61,23 @@ public class SystemUtilsTest {
> >> > }
> >> >
> >> > @Test
> >> > + public void testGetEnvironmentVariableAbsent() {
> >> > + final String name =
> >> "THIS_ENV_VAR_SHOULD_NOT_EXIST_FOR_THIS_TEST_TO_PASS";
> >> > + final String expected = System.getenv(name);
> >> > + Assert.assertNull(expected);
> >> > + final String value = SystemUtils.
> getEnvironmentVariable(name,
> >> "DEFAULT");
> >> > + assertEquals("DEFAULT", value);
> >> > + }
> >> > +
> >> > + @Test
> >> > + public void testGetEnvironmentVariablePresent() {
> >> > + final String name = "PATH";
> >> > + final String expected = System.getenv(name);
> >> > + final String value = SystemUtils.
> getEnvironmentVariable(name,
> >> null);
> >> > + assertEquals(expected, value);
> >> > + }
> >> > +
> >> > + @Test
> >> > public void testGetHostName() {
> >> > final String hostName = SystemUtils.getHostName();
> >> > final String expected = SystemUtils.IS_OS_WINDOWS ?
> >> System.getenv("COMPUTERNAME") : System.getenv("HOSTNAME");
> >> >
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> >> For additional commands, e-mail: dev-help@commons.apache.org
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>
Re: [lang] [LANG-1393] Add API SystemUtils.String getEnvironmentVariable(final
String name, final String defaultValue).
Posted by sebb <se...@gmail.com>.
On 22 April 2018 at 13:44, Gary Gregory <ga...@gmail.com> wrote:
> On Sun, Apr 22, 2018, 03:55 sebb <se...@gmail.com> wrote:
>
>> On 20 April 2018 at 15:55, <gg...@apache.org> wrote:
>> > Repository: commons-lang
>> > Updated Branches:
>> > refs/heads/master 8e3ec1722 -> efba54d35
>> >
>> >
>> > [LANG-1393] Add API SystemUtils.String getEnvironmentVariable(final
>> > String name, final String defaultValue).
>> >
>> > Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
>> > Commit:
>> http://git-wip-us.apache.org/repos/asf/commons-lang/commit/efba54d3
>> > Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/efba54d3
>> > Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/efba54d3
>> >
>> > Branch: refs/heads/master
>> > Commit: efba54d35fa094de5e580b200a8607bfc7bd5a7a
>> > Parents: 8e3ec17
>> > Author: Gary Gregory <ga...@gmail.com>
>> > Authored: Fri Apr 20 08:55:32 2018 -0600
>> > Committer: Gary Gregory <ga...@gmail.com>
>> > Committed: Fri Apr 20 08:55:32 2018 -0600
>> >
>> > ----------------------------------------------------------------------
>> > src/changes/changes.xml | 1 +
>> > .../org/apache/commons/lang3/SystemUtils.java | 27
>> ++++++++++++++++++++
>> > .../apache/commons/lang3/SystemUtilsTest.java | 18 +++++++++++++
>> > 3 files changed, 46 insertions(+)
>> > ----------------------------------------------------------------------
>> >
>> >
>> >
>> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/efba54d3/src/changes/changes.xml
>> > ----------------------------------------------------------------------
>> > diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>> > index a4cbbf3..4ec1984 100644
>> > --- a/src/changes/changes.xml
>> > +++ b/src/changes/changes.xml
>> > @@ -59,6 +59,7 @@ The <action> type attribute can be
>> add,update,fix,remove.
>> > <action issue="LANG-1372" type="add" dev="pschumacher"
>> due-to="Sérgio Ozaki">Add ToStringSummary annotation</action>
>> > <action issue="LANG-1356" type="add" dev="pschumacher"
>> due-to="Yathos UG">Add bypass option for classes to recursive and
>> reflective EqualsBuilder</action>
>> > <action issue="LANG-1391" type="add" dev="ggregory" due-to="Sauro
>> Matulli, Oleg Chubaryov">Improve Javadoc for
>> StringUtils.isAnyEmpty(null)</action>
>> > + <action issue="LANG-1393" type="add" dev="ggregory" due-to="Gary
>> Gregory">Add API SystemUtils.String getEnvironmentVariable(final String
>> name, final String defaultValue)</action>
>> > </release>
>> >
>> > <release version="3.7" date="2017-11-04" description="New features
>> and bug fixes. Requires Java 7, supports Java 8, 9, 10.">
>> >
>> >
>> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/efba54d3/src/main/java/org/apache/commons/lang3/SystemUtils.java
>> > ----------------------------------------------------------------------
>> > diff --git a/src/main/java/org/apache/commons/lang3/SystemUtils.java
>> b/src/main/java/org/apache/commons/lang3/SystemUtils.java
>> > index f91628a..2289d5d 100644
>> > --- a/src/main/java/org/apache/commons/lang3/SystemUtils.java
>> > +++ b/src/main/java/org/apache/commons/lang3/SystemUtils.java
>> > @@ -1607,6 +1607,33 @@ public class SystemUtils {
>> >
>> > /**
>> > * <p>
>> > + * Gets an environment variable, defaulting to {@code defaultValue}
>> if the variable cannot be read.
>> > + * </p>
>> > + * <p>
>> > + * If a {@code SecurityException} is caught, the return value is
>> {@code defaultValue} and a message is written to
>> > + * {@code System.err}.
>> > + * </p>
>> > + *
>> > + * @param name
>> > + * the environment variable name
>> > + * @param defaultValue
>> > + * the default value
>> > + * @return the environment variable value or {@code defaultValue}
>> if a security problem occurs
>> > + * @since 3.7
>> > + */
>> > + public static String getEnvironmentVariable(final String name,
>> final String defaultValue) {
>> > + try {
>> > + final String value = System.getenv(name);
>> > + return value == null ? defaultValue : value;
>> > + } catch (final SecurityException ex) {
>> > + // we are not allowed to look at this property
>> > + System.err.println("Caught a SecurityException reading the
>> environment variable '" + name + "'.");
>>
>> -1
>>
>> Library code should not write to stderr or stdout
>>
>
> We already do, in the same class.
Then that also needs to be fixed...
> Gary
>
>>
>> > + return defaultValue;
>> > + }
>> > + }
>> > +
>> > + /**
>> > + * <p>
>> > * Gets the user directory as a {@code File}.
>> > * </p>
>> > *
>> >
>> >
>> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/efba54d3/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
>> > ----------------------------------------------------------------------
>> > diff --git a/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
>> b/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
>> > index 77e6078..e89e20c 100644
>> > --- a/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
>> > +++ b/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
>> > @@ -40,6 +40,7 @@ import java.lang.reflect.Constructor;
>> > import java.lang.reflect.Modifier;
>> > import java.util.Locale;
>> >
>> > +import org.junit.Assert;
>> > import org.junit.Test;
>> >
>> > /**
>> > @@ -60,6 +61,23 @@ public class SystemUtilsTest {
>> > }
>> >
>> > @Test
>> > + public void testGetEnvironmentVariableAbsent() {
>> > + final String name =
>> "THIS_ENV_VAR_SHOULD_NOT_EXIST_FOR_THIS_TEST_TO_PASS";
>> > + final String expected = System.getenv(name);
>> > + Assert.assertNull(expected);
>> > + final String value = SystemUtils.getEnvironmentVariable(name,
>> "DEFAULT");
>> > + assertEquals("DEFAULT", value);
>> > + }
>> > +
>> > + @Test
>> > + public void testGetEnvironmentVariablePresent() {
>> > + final String name = "PATH";
>> > + final String expected = System.getenv(name);
>> > + final String value = SystemUtils.getEnvironmentVariable(name,
>> null);
>> > + assertEquals(expected, value);
>> > + }
>> > +
>> > + @Test
>> > public void testGetHostName() {
>> > final String hostName = SystemUtils.getHostName();
>> > final String expected = SystemUtils.IS_OS_WINDOWS ?
>> System.getenv("COMPUTERNAME") : System.getenv("HOSTNAME");
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> For additional commands, e-mail: dev-help@commons.apache.org
>>
>>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: [lang] [LANG-1393] Add API SystemUtils.String getEnvironmentVariable(final
String name, final String defaultValue).
Posted by Gary Gregory <ga...@gmail.com>.
On Sun, Apr 22, 2018, 03:55 sebb <se...@gmail.com> wrote:
> On 20 April 2018 at 15:55, <gg...@apache.org> wrote:
> > Repository: commons-lang
> > Updated Branches:
> > refs/heads/master 8e3ec1722 -> efba54d35
> >
> >
> > [LANG-1393] Add API SystemUtils.String getEnvironmentVariable(final
> > String name, final String defaultValue).
> >
> > Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
> > Commit:
> http://git-wip-us.apache.org/repos/asf/commons-lang/commit/efba54d3
> > Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/efba54d3
> > Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/efba54d3
> >
> > Branch: refs/heads/master
> > Commit: efba54d35fa094de5e580b200a8607bfc7bd5a7a
> > Parents: 8e3ec17
> > Author: Gary Gregory <ga...@gmail.com>
> > Authored: Fri Apr 20 08:55:32 2018 -0600
> > Committer: Gary Gregory <ga...@gmail.com>
> > Committed: Fri Apr 20 08:55:32 2018 -0600
> >
> > ----------------------------------------------------------------------
> > src/changes/changes.xml | 1 +
> > .../org/apache/commons/lang3/SystemUtils.java | 27
> ++++++++++++++++++++
> > .../apache/commons/lang3/SystemUtilsTest.java | 18 +++++++++++++
> > 3 files changed, 46 insertions(+)
> > ----------------------------------------------------------------------
> >
> >
> >
> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/efba54d3/src/changes/changes.xml
> > ----------------------------------------------------------------------
> > diff --git a/src/changes/changes.xml b/src/changes/changes.xml
> > index a4cbbf3..4ec1984 100644
> > --- a/src/changes/changes.xml
> > +++ b/src/changes/changes.xml
> > @@ -59,6 +59,7 @@ The <action> type attribute can be
> add,update,fix,remove.
> > <action issue="LANG-1372" type="add" dev="pschumacher"
> due-to="Sérgio Ozaki">Add ToStringSummary annotation</action>
> > <action issue="LANG-1356" type="add" dev="pschumacher"
> due-to="Yathos UG">Add bypass option for classes to recursive and
> reflective EqualsBuilder</action>
> > <action issue="LANG-1391" type="add" dev="ggregory" due-to="Sauro
> Matulli, Oleg Chubaryov">Improve Javadoc for
> StringUtils.isAnyEmpty(null)</action>
> > + <action issue="LANG-1393" type="add" dev="ggregory" due-to="Gary
> Gregory">Add API SystemUtils.String getEnvironmentVariable(final String
> name, final String defaultValue)</action>
> > </release>
> >
> > <release version="3.7" date="2017-11-04" description="New features
> and bug fixes. Requires Java 7, supports Java 8, 9, 10.">
> >
> >
> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/efba54d3/src/main/java/org/apache/commons/lang3/SystemUtils.java
> > ----------------------------------------------------------------------
> > diff --git a/src/main/java/org/apache/commons/lang3/SystemUtils.java
> b/src/main/java/org/apache/commons/lang3/SystemUtils.java
> > index f91628a..2289d5d 100644
> > --- a/src/main/java/org/apache/commons/lang3/SystemUtils.java
> > +++ b/src/main/java/org/apache/commons/lang3/SystemUtils.java
> > @@ -1607,6 +1607,33 @@ public class SystemUtils {
> >
> > /**
> > * <p>
> > + * Gets an environment variable, defaulting to {@code defaultValue}
> if the variable cannot be read.
> > + * </p>
> > + * <p>
> > + * If a {@code SecurityException} is caught, the return value is
> {@code defaultValue} and a message is written to
> > + * {@code System.err}.
> > + * </p>
> > + *
> > + * @param name
> > + * the environment variable name
> > + * @param defaultValue
> > + * the default value
> > + * @return the environment variable value or {@code defaultValue}
> if a security problem occurs
> > + * @since 3.7
> > + */
> > + public static String getEnvironmentVariable(final String name,
> final String defaultValue) {
> > + try {
> > + final String value = System.getenv(name);
> > + return value == null ? defaultValue : value;
> > + } catch (final SecurityException ex) {
> > + // we are not allowed to look at this property
> > + System.err.println("Caught a SecurityException reading the
> environment variable '" + name + "'.");
>
> -1
>
> Library code should not write to stderr or stdout
>
We already do, in the same class.
Gary
>
> > + return defaultValue;
> > + }
> > + }
> > +
> > + /**
> > + * <p>
> > * Gets the user directory as a {@code File}.
> > * </p>
> > *
> >
> >
> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/efba54d3/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
> > ----------------------------------------------------------------------
> > diff --git a/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
> b/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
> > index 77e6078..e89e20c 100644
> > --- a/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
> > +++ b/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
> > @@ -40,6 +40,7 @@ import java.lang.reflect.Constructor;
> > import java.lang.reflect.Modifier;
> > import java.util.Locale;
> >
> > +import org.junit.Assert;
> > import org.junit.Test;
> >
> > /**
> > @@ -60,6 +61,23 @@ public class SystemUtilsTest {
> > }
> >
> > @Test
> > + public void testGetEnvironmentVariableAbsent() {
> > + final String name =
> "THIS_ENV_VAR_SHOULD_NOT_EXIST_FOR_THIS_TEST_TO_PASS";
> > + final String expected = System.getenv(name);
> > + Assert.assertNull(expected);
> > + final String value = SystemUtils.getEnvironmentVariable(name,
> "DEFAULT");
> > + assertEquals("DEFAULT", value);
> > + }
> > +
> > + @Test
> > + public void testGetEnvironmentVariablePresent() {
> > + final String name = "PATH";
> > + final String expected = System.getenv(name);
> > + final String value = SystemUtils.getEnvironmentVariable(name,
> null);
> > + assertEquals(expected, value);
> > + }
> > +
> > + @Test
> > public void testGetHostName() {
> > final String hostName = SystemUtils.getHostName();
> > final String expected = SystemUtils.IS_OS_WINDOWS ?
> System.getenv("COMPUTERNAME") : System.getenv("HOSTNAME");
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>
Re: [lang] [LANG-1393] Add API SystemUtils.String getEnvironmentVariable(final
String name, final String defaultValue).
Posted by sebb <se...@gmail.com>.
On 20 April 2018 at 15:55, <gg...@apache.org> wrote:
> Repository: commons-lang
> Updated Branches:
> refs/heads/master 8e3ec1722 -> efba54d35
>
>
> [LANG-1393] Add API SystemUtils.String getEnvironmentVariable(final
> String name, final String defaultValue).
>
> Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
> Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/efba54d3
> Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/efba54d3
> Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/efba54d3
>
> Branch: refs/heads/master
> Commit: efba54d35fa094de5e580b200a8607bfc7bd5a7a
> Parents: 8e3ec17
> Author: Gary Gregory <ga...@gmail.com>
> Authored: Fri Apr 20 08:55:32 2018 -0600
> Committer: Gary Gregory <ga...@gmail.com>
> Committed: Fri Apr 20 08:55:32 2018 -0600
>
> ----------------------------------------------------------------------
> src/changes/changes.xml | 1 +
> .../org/apache/commons/lang3/SystemUtils.java | 27 ++++++++++++++++++++
> .../apache/commons/lang3/SystemUtilsTest.java | 18 +++++++++++++
> 3 files changed, 46 insertions(+)
> ----------------------------------------------------------------------
>
>
> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/efba54d3/src/changes/changes.xml
> ----------------------------------------------------------------------
> diff --git a/src/changes/changes.xml b/src/changes/changes.xml
> index a4cbbf3..4ec1984 100644
> --- a/src/changes/changes.xml
> +++ b/src/changes/changes.xml
> @@ -59,6 +59,7 @@ The <action> type attribute can be add,update,fix,remove.
> <action issue="LANG-1372" type="add" dev="pschumacher" due-to="Sérgio Ozaki">Add ToStringSummary annotation</action>
> <action issue="LANG-1356" type="add" dev="pschumacher" due-to="Yathos UG">Add bypass option for classes to recursive and reflective EqualsBuilder</action>
> <action issue="LANG-1391" type="add" dev="ggregory" due-to="Sauro Matulli, Oleg Chubaryov">Improve Javadoc for StringUtils.isAnyEmpty(null)</action>
> + <action issue="LANG-1393" type="add" dev="ggregory" due-to="Gary Gregory">Add API SystemUtils.String getEnvironmentVariable(final String name, final String defaultValue)</action>
> </release>
>
> <release version="3.7" date="2017-11-04" description="New features and bug fixes. Requires Java 7, supports Java 8, 9, 10.">
>
> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/efba54d3/src/main/java/org/apache/commons/lang3/SystemUtils.java
> ----------------------------------------------------------------------
> diff --git a/src/main/java/org/apache/commons/lang3/SystemUtils.java b/src/main/java/org/apache/commons/lang3/SystemUtils.java
> index f91628a..2289d5d 100644
> --- a/src/main/java/org/apache/commons/lang3/SystemUtils.java
> +++ b/src/main/java/org/apache/commons/lang3/SystemUtils.java
> @@ -1607,6 +1607,33 @@ public class SystemUtils {
>
> /**
> * <p>
> + * Gets an environment variable, defaulting to {@code defaultValue} if the variable cannot be read.
> + * </p>
> + * <p>
> + * If a {@code SecurityException} is caught, the return value is {@code defaultValue} and a message is written to
> + * {@code System.err}.
> + * </p>
> + *
> + * @param name
> + * the environment variable name
> + * @param defaultValue
> + * the default value
> + * @return the environment variable value or {@code defaultValue} if a security problem occurs
> + * @since 3.7
> + */
> + public static String getEnvironmentVariable(final String name, final String defaultValue) {
> + try {
> + final String value = System.getenv(name);
> + return value == null ? defaultValue : value;
> + } catch (final SecurityException ex) {
> + // we are not allowed to look at this property
> + System.err.println("Caught a SecurityException reading the environment variable '" + name + "'.");
-1
Library code should not write to stderr or stdout
> + return defaultValue;
> + }
> + }
> +
> + /**
> + * <p>
> * Gets the user directory as a {@code File}.
> * </p>
> *
>
> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/efba54d3/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
> ----------------------------------------------------------------------
> diff --git a/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java b/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
> index 77e6078..e89e20c 100644
> --- a/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
> +++ b/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
> @@ -40,6 +40,7 @@ import java.lang.reflect.Constructor;
> import java.lang.reflect.Modifier;
> import java.util.Locale;
>
> +import org.junit.Assert;
> import org.junit.Test;
>
> /**
> @@ -60,6 +61,23 @@ public class SystemUtilsTest {
> }
>
> @Test
> + public void testGetEnvironmentVariableAbsent() {
> + final String name = "THIS_ENV_VAR_SHOULD_NOT_EXIST_FOR_THIS_TEST_TO_PASS";
> + final String expected = System.getenv(name);
> + Assert.assertNull(expected);
> + final String value = SystemUtils.getEnvironmentVariable(name, "DEFAULT");
> + assertEquals("DEFAULT", value);
> + }
> +
> + @Test
> + public void testGetEnvironmentVariablePresent() {
> + final String name = "PATH";
> + final String expected = System.getenv(name);
> + final String value = SystemUtils.getEnvironmentVariable(name, null);
> + assertEquals(expected, value);
> + }
> +
> + @Test
> public void testGetHostName() {
> final String hostName = SystemUtils.getHostName();
> final String expected = SystemUtils.IS_OS_WINDOWS ? System.getenv("COMPUTERNAME") : System.getenv("HOSTNAME");
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org