Reputation: 103
What is the best way to read out system environment in an Java application and class visibility function?
I need to e.g. os.name and have designed a class like
private String osName;
private void readSystemSettings() {
osName = System.getProperty("os.name");
}
public void printSystemSettings() {
System.out.println(this.osName);
...
}
public SystemEnvironment() {
readSystemSettings();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
What are the best practice for getting those information? Call this function always on start-up or only from time to time?
I want to read out those information as soon as the class get instantiated. Therefor the constructor is calling the readSystemSettings()
function.
As this is information will always be the same during runtime, I actually need only no stance. Means all variables + functions shall be final. Or am I wrong?
Upvotes: 3
Views: 2517
Reputation: 4413
You can have a class with all the variables marked as final and then initialize in a static block.
public class SystemProperties{
public static final String OS_NAME;
// other properties
static{
OS_NAME = System.getProperty("os.name");
// initialize other properties
}
}
Else, if you're in a managed environment like Spring or EJB, you can mark SystemProperties
as singleton
and initialize the variable in a method annotated with @PostContruct
.
public class SystemProperties{
public static String OS_NAME;
// other properties
@PostConstruct
private void init(){
OS_NAME = System.getProperty("os.name");
// initialize other properties
}
}
Upvotes: 3
Reputation: 2743
You can use the OWNER library.
The OWNER API is a Java library with the goal of minimizing the code required to handle application configuration via Java properties files.
So to load properties you should create class like this
public interface MyConfig extends Config {
@Key("os.name")
String osName();
}
Then you can load config whenever you need:
ServerConfig cfg = ConfigFactory.create(MyConfig.class,
System.getProperties(),
System.getenv()
);
System.out.println("Os name:" + cfg.osName());
For more information you can see the documentation http://owner.aeonbits.org/docs/usage/
Upvotes: 0
Reputation: 533492
Since the value is set globally you could do
enum SystemEnvironment {
;
public static final String OS_NAME = System.getProperty("os.name");
or you could look ti up each time as you shouldn't be call it that often.
enum SystemEnvironment {
;
public static getOsName() {
return System.getProperty("os.name");
}
if you are using it often I suggest creating some tests for it.
enum SystemEnvironment {
;
public static final boolean IS_WINDOWS = getOsName().startsWith("Window");
public static getOsName() {
return System.getProperty("os.name");
}
Upvotes: 0
Reputation: 137084
You could use a utility class:
public final class Utilities {
public static final String OS_NAME = System.getProperty("os.name");
private Utilities() { } // utility classes can't be instantiated
}
This way, this property will only be initialized once, during your application startup. You can then access this property from everywhere in your code with Utilities.OS_NAME
.
Upvotes: 0