I wrote the following code to read the config/property files. I will be exposing my api as jar to multiple application teams.
Functionality:
- Reads the property files and returns the resource bundle object. I am returning the resource bundle object as, there can be multiple config files in the application.
Getting the properties or the configs as variables from the resource bundle and use it across the application.
public class FileConfigLoader { private static final Logger LOGGER = Logger.getLogger(FileConfigLoader.class); private static String EMPTY = ""; public static ResourceBundle getBundle(String basePath, String baseName) throws MalformedURLException { File file = new File(basePath); URL[] uris = { file.toURI().toURL() }; ClassLoader loader = new URLClassLoader(uris); ResourceBundle rb = ResourceBundle.getBundle(basePath, Locale.getDefault(), loader); return rb; } public static String getValue(ResourceBundle rb, String key) { String value = EMPTY; try { value = rb.getString(key); } catch (MissingResourceException mREx) { LOGGER.error("Missing Resource : " + key); } return value; } }
I need code reviews on the following lines
- Does my code looks reusable?
- Do you think i am using the variable names and class names as per the java sandards?
- If my code does not seems to be reusable, Please provide suggestions.
The below code is the demonstration of how my code used by applications in the realtime.
The config is the name of my config file.The file is located in the classpath.
import java.net.MalformedURLException;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
public class MainApp {
private static final Logger LOGGER = Logger.getLogger(MainApp.class);
public static void main(String[] args) {
ResourceBundle resource = null;
try {
resource = FileConfigLoader.getBundle("config", "");
} catch (MalformedURLException e) {
e.printStackTrace();
}
String hive_db = FileConfigLoader.getValue(resource, "hive_db");
String EDGE_HIVE_CONN = FileConfigLoader.getValue(resource, "EDGE_HIVE_CONN");
String target_dir = FileConfigLoader.getValue(resource, "target_dir");
String somekey = FileConfigLoader.getValue(resource, "somekey");
LOGGER.info(hive_db);
LOGGER.info(EDGE_HIVE_CONN);
LOGGER.info(target_dir);
LOGGER.info(somekey);
}
}