-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
52 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 50 additions & 9 deletions
59
guice-inject-client/src/main/java/com/guicedee/client/Environment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,60 @@ | ||
package com.guicedee.client; | ||
|
||
public class Environment | ||
{ | ||
public static String getProperty(String key, String defaultValue) | ||
{ | ||
if (System.getProperty(key) == null) | ||
{ | ||
if (System.getenv(key) == null) | ||
{ | ||
import lombok.extern.java.Log; | ||
|
||
import java.util.logging.Level; | ||
|
||
@Log | ||
public class Environment { | ||
public static String getProperty(String key, String defaultValue) { | ||
if (System.getProperty(key) == null) { | ||
if (System.getenv(key) == null) { | ||
System.setProperty(key, defaultValue); | ||
}else { | ||
} else { | ||
System.setProperty(key, System.getenv(key)); | ||
} | ||
} | ||
return System.getProperty(key); | ||
} | ||
|
||
/** | ||
* Returns an environment or system defined property with a default value | ||
* | ||
* System Defined Properties (-Dxxx=xxx) override environment variables | ||
* | ||
* @param name The name of the variable | ||
* @param defaultValue The default value to always return | ||
* @return The required value from the environment | ||
*/ | ||
public static String getSystemPropertyOrEnvironment(String name, String defaultValue) { | ||
if (System.getProperty(name) != null) { | ||
return System.getProperty(name); | ||
} | ||
if (System.getenv(name) != null) { | ||
try { | ||
System.setProperty(name, System.getenv(name)); | ||
return System.getProperty(name); | ||
} catch (Exception T) { | ||
log.log(Level.CONFIG, | ||
"Couldn't set system property value from environment [" + name + "] - [" + defaultValue + "]", | ||
T); | ||
return System.getenv(name); | ||
} | ||
} else { | ||
if (defaultValue == null) { | ||
return ""; | ||
} | ||
log.log(Level.CONFIG, "Return default value for default property [" + name + "] - [" + defaultValue + "]"); | ||
try { | ||
System.setProperty(name, defaultValue); | ||
return System.getProperty(name); | ||
} catch (Exception T) { | ||
log.log(Level.CONFIG, | ||
"Couldn't set system property value to the default specified [" + name + "] - [" + defaultValue + "]", | ||
T); | ||
return defaultValue; | ||
} | ||
} | ||
} | ||
|
||
} |