I have int, float, boolean and string from Properties file. Everything has loaded in Properties. Currently, I am parsing values as I know expected value for particular key.
Boolean.parseBoolean("false"); Integer.parseInt("3")
What is better way of setting these constants values, If I don't know what could be primitive value datatype for a key.
public class Messages { Properties appProperties = null; FileInputStream file = null; public void initialization() throws Exception { appProperties = new Properties(); try { loadPropertiesFile(); } catch (Exception e) { throw new Exception(e.getMessage(), e); } } public void loadPropertiesFile() throws IOException { String path = "./cfg/message.properties"; file = new FileInputStream(path); appProperties.load(file); file.close(); } }
Properties File. messassge.properties
SSO_URL = https://example.com/connect/token SSO_API_USERNAME = test SSO_API_PASSWORD = Uo88YmMpKUp SSO_API_SCOPE = intraday_api SSO_IS_PROXY_ENABLED = false SSO_MAX_RETRY_COUNT = 3 SSO_FLOAT_VALUE = 3.0
Constant.java
public class Constants { public static String SSO_URL = null; public static String SSO_API_USERNAME = null; public static String SSO_API_PASSWORD = null; public static String SSO_API_SCOPE = null; public static boolean SSO_IS_PROXY_ENABLED = false; public static int SSO_MAX_RETRY_COUNT = 0; public static float SSO_FLOAT_VALUE = 0; }
Scalar is a simple data type. In YAML, scalar means a simple value for a key. The value of the scalar can be integer, float, Boolean, and string.
When the Properties of your file are loaded you can use the Boolean -Class to get the Properties: Boolean. getBoolean("your. property");
If you have a class of configuration values, like your Constants
class, and you want to load all values from a configuration (properties) file, you can create a little helper class and use reflection:
public class ConfigLoader { public static void load(Class<?> configClass, String file) { try { Properties props = new Properties(); try (FileInputStream propStream = new FileInputStream(file)) { props.load(propStream); } for (Field field : configClass.getDeclaredFields()) if (Modifier.isStatic(field.getModifiers())) field.set(null, getValue(props, field.getName(), field.getType())); } catch (Exception e) { throw new RuntimeException("Error loading configuration: " + e, e); } } private static Object getValue(Properties props, String name, Class<?> type) { String value = props.getProperty(name); if (value == null) throw new IllegalArgumentException("Missing configuration value: " + name); if (type == String.class) return value; if (type == boolean.class) return Boolean.parseBoolean(value); if (type == int.class) return Integer.parseInt(value); if (type == float.class) return Float.parseFloat(value); throw new IllegalArgumentException("Unknown configuration value type: " + type.getName()); } }
Then you call it like this:
ConfigLoader.load(Constants.class, "/path/to/constants.properties");
You can extend the code to handle more types. You can also change it to ignore missing properties, instead of failing like it does now, such that assignments in the field declaration will remain unchanged, i.e. be the default.
If you know the type of constant, you can use Apache Commons Collections.
For example, you can use some utilities method based on type of your constant.
booelan SSO_IS_PROXY_ENABLED = MapUtils.getBooleanValue(appProperties, "SSO_IS_PROXY_ENABLED", false); String SSO_URL = MapUtils.getString(appProperties, "SSO_URL", "https://example.com/connect/token");
You can even use default values to avoid errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With