/**
*
*/
package com.yulong.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Properties;
import org.apache.log4j.Logger;
/**
*
*/
public class PropUtil
{
private final Logger logger = Logger.getLogger(this.getClass());
private static final String DEFAULT_PROPERTY = "application_config.properties";
private String propFile;
private File configFile;
private long fileLastModified;
private Properties props;
public PropUtil()
{
this(DEFAULT_PROPERTY);
}
public PropUtil(String propFile)
{
this.propFile = propFile;
init();
}
private void init()
{
URL url = PropUtil.class.getResource(propFile);
if (url == null)
url = PropUtil.class.getClassLoader().getResource(propFile);
try {
url = new URL(URLDecoder.decode(url.toString(),"UTF-8"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (url == null)
url = ClassLoader.getSystemResource(propFile);
//if (url == null)
// throw new IllegalArgumentException("coundn't find resource [" + propFile + "]");
if (url != null)
configFile = new File(url.getFile());
else
configFile = new File(propFile);
if (configFile == null || !configFile.exists())
throw new IllegalArgumentException("coundn't find resource [" + propFile + "]");
fileLastModified = configFile.lastModified();
if (logger.isDebugEnabled())
logger.debug("properties file is: " + propFile +", last modified time is: "+ fileLastModified);
props = new Properties();
loadProperty();
}
private void loadProperty()
{
try
{
props.load(new FileInputStream(configFile));
fileLastModified = configFile.lastModified();
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public String getProperty(String key)
{
return (getProperty(key, ""));
}
public String getProperty(String key, String defValue)
{
if (configFile.lastModified() > fileLastModified)
{
loadProperty();
if (logger.isDebugEnabled())
logger.debug("properties file is reload at " + fileLastModified);
}
String prop = props.getProperty(key);
if (prop == null || "".equals(prop))
prop = defValue;
if (logger.isDebugEnabled())
logger.debug("property is: " + prop);
return (prop);
}
public synchronized void setProperty(String key, String value)
{
props.setProperty(key, value);
}
}
*
*/
package com.yulong.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Properties;
import org.apache.log4j.Logger;
/**
*
*/
public class PropUtil
{
private final Logger logger = Logger.getLogger(this.getClass());
private static final String DEFAULT_PROPERTY = "application_config.properties";
private String propFile;
private File configFile;
private long fileLastModified;
private Properties props;
public PropUtil()
{
this(DEFAULT_PROPERTY);
}
public PropUtil(String propFile)
{
this.propFile = propFile;
init();
}
private void init()
{
URL url = PropUtil.class.getResource(propFile);
if (url == null)
url = PropUtil.class.getClassLoader().getResource(propFile);
try {
url = new URL(URLDecoder.decode(url.toString(),"UTF-8"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (url == null)
url = ClassLoader.getSystemResource(propFile);
//if (url == null)
// throw new IllegalArgumentException("coundn't find resource [" + propFile + "]");
if (url != null)
configFile = new File(url.getFile());
else
configFile = new File(propFile);
if (configFile == null || !configFile.exists())
throw new IllegalArgumentException("coundn't find resource [" + propFile + "]");
fileLastModified = configFile.lastModified();
if (logger.isDebugEnabled())
logger.debug("properties file is: " + propFile +", last modified time is: "+ fileLastModified);
props = new Properties();
loadProperty();
}
private void loadProperty()
{
try
{
props.load(new FileInputStream(configFile));
fileLastModified = configFile.lastModified();
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public String getProperty(String key)
{
return (getProperty(key, ""));
}
public String getProperty(String key, String defValue)
{
if (configFile.lastModified() > fileLastModified)
{
loadProperty();
if (logger.isDebugEnabled())
logger.debug("properties file is reload at " + fileLastModified);
}
String prop = props.getProperty(key);
if (prop == null || "".equals(prop))
prop = defValue;
if (logger.isDebugEnabled())
logger.debug("property is: " + prop);
return (prop);
}
public synchronized void setProperty(String key, String value)
{
props.setProperty(key, value);
}
}