import java.io.File;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.log4j.Logger;
public class ConfigUtil {
private static Logger log = Logger.getLogger(ConfigUtil.class);
private static PropertiesConfiguration configuration = null;
private static final String PROPERTY_FILENAME = "config.properties";
private static String RELOADBLE_PROPERTY_FILENAME = null;
private static PropertiesConfiguration reloadConfiguration = null;
static
{
try {
configuration = new PropertiesConfiguration(PROPERTY_FILENAME);
log.info("Loading property file from classpath :");
RELOADBLE_PROPERTY_FILENAME = ConfigUtil.getProperty("RELOAD_PROERTY_FILENAME");
if(null != RELOADBLE_PROPERTY_FILENAME){
File f = new File(RELOADBLE_PROPERTY_FILENAME);
if(f.exists()){
reloadConfiguration = new PropertiesConfiguration(RELOADBLE_PROPERTY_FILENAME);
reloadConfiguration.setReloadingStrategy(new FileChangedReloadingStrategy());
log.info("Loading reloadble property file from path : " + RELOADBLE_PROPERTY_FILENAME);
}else{
log.info("Error: reloadble property file not Found : " + RELOADBLE_PROPERTY_FILENAME);
}
} // end of if
} catch (ConfigurationException e) {
// e.printStackTrace();
log.debug(e.getStackTrace().toString());
}
}
public static synchronized String getProperty(final String key)
{
if(reloadConfiguration != null){
if(getReloadableProperty(key) != null){
return getReloadableProperty(key);
}
else
return configuration.getString(key);
}
else
return configuration.getString(key);
}
private static synchronized String getReloadableProperty(final String key)
{
return reloadConfiguration.getString(key);
}
public static void main(String[] args) {
System.out.println(RELOADBLE_PROPERTY_FILENAME);
}
}
=========================================
RELOAD_PROERTY_FILENAME=C:/config.properties
#DB Properties
db-url=jdbc:db2://127.0.0.1:3306/test
db-username=jini
db-password=123
=========================================
import com.jini.util.ConfigUtil;
public class zzZZTestReloading {
public static void main(String[] args)
{
while(true)
{
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(ConfigUtil.getProperty("DATASOURCE_ENABLE=TRUE"));
}
}
}
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.log4j.Logger;
public class ConfigUtil {
private static Logger log = Logger.getLogger(ConfigUtil.class);
private static PropertiesConfiguration configuration = null;
private static final String PROPERTY_FILENAME = "config.properties";
private static String RELOADBLE_PROPERTY_FILENAME = null;
private static PropertiesConfiguration reloadConfiguration = null;
static
{
try {
configuration = new PropertiesConfiguration(PROPERTY_FILENAME);
log.info("Loading property file from classpath :");
RELOADBLE_PROPERTY_FILENAME = ConfigUtil.getProperty("RELOAD_PROERTY_FILENAME");
if(null != RELOADBLE_PROPERTY_FILENAME){
File f = new File(RELOADBLE_PROPERTY_FILENAME);
if(f.exists()){
reloadConfiguration = new PropertiesConfiguration(RELOADBLE_PROPERTY_FILENAME);
reloadConfiguration.setReloadingStrategy(new FileChangedReloadingStrategy());
log.info("Loading reloadble property file from path : " + RELOADBLE_PROPERTY_FILENAME);
}else{
log.info("Error: reloadble property file not Found : " + RELOADBLE_PROPERTY_FILENAME);
}
} // end of if
} catch (ConfigurationException e) {
// e.printStackTrace();
log.debug(e.getStackTrace().toString());
}
}
public static synchronized String getProperty(final String key)
{
if(reloadConfiguration != null){
if(getReloadableProperty(key) != null){
return getReloadableProperty(key);
}
else
return configuration.getString(key);
}
else
return configuration.getString(key);
}
private static synchronized String getReloadableProperty(final String key)
{
return reloadConfiguration.getString(key);
}
public static void main(String[] args) {
System.out.println(RELOADBLE_PROPERTY_FILENAME);
}
}
=========================================
RELOAD_PROERTY_FILENAME=C:/config.properties
#DB Properties
db-url=jdbc:db2://127.0.0.1:3306/test
db-username=jini
db-password=123
=========================================
import com.jini.util.ConfigUtil;
public class zzZZTestReloading {
public static void main(String[] args)
{
while(true)
{
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(ConfigUtil.getProperty("DATASOURCE_ENABLE=TRUE"));
}
}
}
No comments:
Post a Comment