This is something which everyone of us like to do as it saves some of our time.
We need to thank our collegue Adrian Young who is the author of the code.
We can re-use below functions from any front-end controller which can be invoked from jsp.
Hope it will be useful for any one...
Code which can be re-used
public class ReloadSystemProperties {
private static HashMap<String, Properties> propfiles = new HashMap<String, Properties>();
public static void main(String[] args) {
loadPropertiesFile("test.properties",false);
String value=getValue("test.properties","test");
System.out.println("Printing Value:" + value);
reloadProperties();
value=getValue("test.properties","test");
System.out.println("Printing Value:" + value);
}
/**
* reloadProperties - Reload all of the property files
*/
public static void reloadProperties() {
//Get a list of files loaded
ArrayList<String> files = getFileList();
//Cycle through list and reload all files
for (int icnt =0;icnt < files.size();icnt++) {
loadPropertiesFile(files.get(icnt),true);
}
}
public static boolean loadPropertiesFile(String filename, boolean reload) {
//If property file is already loaded and reload is required then remove it from the loaded file list.
if (propfiles.containsKey(filename)) {
if (!reload) return true;
propfiles.remove(filename);
}
try {
//Find the property file on the class path and load it.
System.out.println("Loading properties file: "+filename);
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream is = loader.getResourceAsStream(filename);
Properties props = new Properties();
props.load(is);
//Add it to the hashtable
propfiles.put(filename,props);
return true;
}
catch (Exception e) {
System.out.println("Failed to load properties file: "+e.getMessage());
return false;
}
}
/**
* getFileList - returns a sorted list of property file names that have been loaded
*/
public static ArrayList<String> getFileList() {
//Get empty list
ArrayList<String> files = new ArrayList<String>();
//Cycle through property files hash table and add names to the return list
Set<String> keys = propfiles.keySet();
Iterator it = keys.iterator();
while (it.hasNext()) {
files.add((String) it.next());
}
//Sort list
Collections.sort(files);
return files;
}
public static String getValue(String propertiesFile,String key) {
// If property file is not load it then load it now
if (!propfiles.containsKey(propertiesFile)) {
if (!loadPropertiesFile(propertiesFile,true)) {
throw new RuntimeException("Failed to load properties file: "+propertiesFile);
}
}
//Get the property file from the hashmap
Properties props = propfiles.get(propertiesFile);
//Return the property value
try {
System.out.println("Getting properties: "+key);
return props.getProperty(key);
}
catch (Exception e) {
System.out.println("Failed to find property in file");
throw new RuntimeException("Failed to find property");
}
}
}
4 comments:
fub..its b*lls**t
total crap
I have no words for this great post such a awe-some information i got gathered. Thanks to Author.
WTF
Post a Comment