/**
* The Class SystemResource.
*/
public final class SystemResource {
/** The properties. */
private Properties properties = null;
/** The Constant FILENAME. */
public static final String FILENAME = "properties/sys-resource.properties";
/** The instance. */
private static SystemResource instance = null;
/**
* Instantiates a new system resource.
*/
private SystemResource() {
try {
readProperties(FILENAME);
} catch (AdsSystemException e) {
AdsLogger.error(e);
}
}
/**
* Gets the single instance of SystemResource.
*
* @return single instance of SystemResource
*/
public static SystemResource getInstance() {
if (instance == null) {
instance = new SystemResource();
}
return instance;
}
/**
* Get.
*
* @param key the key
* @return the string
*/
public String get(String key) {
if (properties.containsKey(key)) {
return properties.getProperty(key);
}
return null;
}
/**
* Read properties.
*
* @param file the file
* @throws AdsSystemException the ads system exception
*/
protected void readProperties(String file) throws AdsSystemException {
// 1. init?
if (properties != null) {
return;
}
// 3. first time
properties = new Properties();
// 4. read
InputStream in = this.getClass().getClassLoader().getResourceAsStream(file);
// 6. load file
try {
properties.load(in);
} catch (IOException e) {
throw new AdsSystemException(e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
throw new AdsSystemException(e);
}
}
}
}
}
* The Class SystemResource.
*/
public final class SystemResource {
/** The properties. */
private Properties properties = null;
/** The Constant FILENAME. */
public static final String FILENAME = "properties/sys-resource.properties";
/** The instance. */
private static SystemResource instance = null;
/**
* Instantiates a new system resource.
*/
private SystemResource() {
try {
readProperties(FILENAME);
} catch (AdsSystemException e) {
AdsLogger.error(e);
}
}
/**
* Gets the single instance of SystemResource.
*
* @return single instance of SystemResource
*/
public static SystemResource getInstance() {
if (instance == null) {
instance = new SystemResource();
}
return instance;
}
/**
* Get.
*
* @param key the key
* @return the string
*/
public String get(String key) {
if (properties.containsKey(key)) {
return properties.getProperty(key);
}
return null;
}
/**
* Read properties.
*
* @param file the file
* @throws AdsSystemException the ads system exception
*/
protected void readProperties(String file) throws AdsSystemException {
// 1. init?
if (properties != null) {
return;
}
// 3. first time
properties = new Properties();
// 4. read
InputStream in = this.getClass().getClassLoader().getResourceAsStream(file);
// 6. load file
try {
properties.load(in);
} catch (IOException e) {
throw new AdsSystemException(e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
throw new AdsSystemException(e);
}
}
}
}
}
本文介绍了一个用于管理系统资源的类,包括属性初始化、文件名常量、单例模式实例化和资源获取方法。
595

被折叠的 条评论
为什么被折叠?



