📄 propertymanager.java
字号:
package com.beiktech.hib.dao;
import java.util.*;
import java.io.*;
/**
* <p>Title: 用于属性配置文件的读写</p>
* <p>Description: 这个类用来对属性文件的读写,
* 有些模块专门需要一个配置文件来读取文件
* </p>
* <p>Copyright: 版权所有(C)2004</p>
* <p>Company:bigonline chenbingc</p>
* @author Layout
* @version 1.0
*/
public class PropertyManager {
/**
* The Major version number
*/
private static final int MAJOR_VERSION = 1;
/**
* The Minor version number. i.e. x.1.x.
*/
private static final int MINOR_VERSION = 2;
/**
* The revision version number . i.e. x.x.1.
*/
private static final int REVISION_VERSION = 4;
private static PropertyManager manager = null; //初始化本身
private static Object managerLock = new Object(); //管理同步锁
private static String propsFName = "sjzd.properties"; //配置文件名称
private static String propsPName = ""; //配置文件路径名
private Properties G_properties = null; //资源装载属性
private Object propertiesLock = new Object(); //属性锁
private String resourceURI; //资源定位
/**
* PropertyManager构造函数
*@param resourceURI 资源定位,属性配置文件
*/
private PropertyManager(String resourceURI) {
this.resourceURI = resourceURI;
}
/**
*初始化本身
* @return boolean 判断是否初始化本身
*/
public static boolean initSelf() {
return true;
}
/**
*公用获取配置属性中的属性值
*@param name 属性名称
*@return String 属性值
*/
public static String getProperty(String name) {
if (manager == null) {
synchronized (managerLock) {
if (manager == null) {
manager = new PropertyManager(propsPName + propsFName);
}
}
}
return toChinese(manager.getProp(name));
}
/**
*公用设置属性值:设置属性值,该属性已经存在
*@param name 属性名称
*@param value 属性值
*/
public static void setProperty(String name, String value) {
if (manager == null) {
synchronized (managerLock) { //管理同步锁
if (manager == null) {
manager = new PropertyManager(propsPName + propsFName);
}
}
}
manager.setProp(name, value);
}
/**
*公用删除属性
*@param name 属性名称
*/
public static void deleteProperty(String name) {
if (manager == null) {
synchronized (managerLock) { //同步
if (manager == null) {
manager = new PropertyManager(propsPName + propsFName);
}
}
}
manager.deleteProp(name);
}
/**
*公用保存属性值
*
*/
public static void saveProperty() {
if (manager == null) {
synchronized (managerLock) {
if (manager == null) {
manager = new PropertyManager(propsPName + propsFName);
}
}
}
manager.saveProps();
}
/**
*新建一个属性,该属性原本不存在
*@param m_name 属性的名称
*@param m_value 属性值
*/
public static void putProperty(String m_name, String m_value) {
if (manager == null) {
synchronized (managerLock) {
if (manager == null) {
manager = new PropertyManager(propsPName + propsFName);
}
}
}
manager.putProp(m_name, m_value);
}
/**
*获取所有的属性,并且以枚举的形式返回
*@return Enumeration 属性的枚举
*/
public static Enumeration propertyNames() {
if (manager == null) {
synchronized (managerLock) {
if (manager == null) {
manager = new PropertyManager(propsPName + propsFName);
}
}
}
return manager.propNames();
}
/**
* 确定属性配置文件可以读取
* @return boolean 属性配置文件是否可读
*/
public static boolean propertyFileIsReadable() {
if (manager == null) {
synchronized (managerLock) {
if (manager == null) {
manager = new PropertyManager(propsPName + propsFName);
}
}
}
return manager.propFileIsReadable();
}
/**
*确定属性文件是否可写(修改或者新建的时候会用到)
*@return boolean 属性配置文件是否可写
*/
public static boolean propertyFileIsWritable() {
if (manager == null) {
synchronized (managerLock) {
if (manager == null) {
manager = new PropertyManager(propsPName + propsFName);
}
}
}
return manager.propFileIsWritable();
}
/**
*确定属性配置文件是否存在
*@return boolean 文件是否存在
*/
public static boolean propertyFileExists() {
if (manager == null) {
synchronized (managerLock) {
if (manager == null) {
manager = new PropertyManager(propsPName + propsFName);
}
}
}
return manager.propFileExists();
}
/**
* 返回本类的版本号
*@return String 版本号
*/
public static String getVersion() {
return MAJOR_VERSION + "." + MINOR_VERSION + "." + REVISION_VERSION;
}
/**
* 获取主版本
*@return int 主版本号
*/
public static int getVersionMajor() {
return MAJOR_VERSION;
}
/**
* 获取最小版本号
@return int 最小版本号
*/
public static int getVersionMinor() {
return MINOR_VERSION;
}
/**
*获取修正版本号
*@return int 修正版本号
*/
public static int getVersionRevision() {
return REVISION_VERSION;
}
/**
*重新装载属性,如果属性值有所修改的话,会自动修改其值
*
*/
public static void reloadProps() {
if (manager == null) {
synchronized (managerLock) {
if (manager == null) {
manager = new PropertyManager(propsPName + propsFName);
}
}
}
manager.loadProps();
}
/**
* 私有方法:获取属性值
* @param name 想要获取得属性名称
* @return 想要的属性值
*/
protected String getProp(String name) {
//如果属性还没有装载,需要同步装载
//以保证线程安全
if (G_properties == null) {
synchronized (propertiesLock) {
//需要额外的检查一下子
if (G_properties == null) {
if (loadProps() == false) {
return null;
}
}
}
}
String property = G_properties.getProperty(name);
if (property == null) {
return null;
}
else {
return property.trim();
}
}
/**
* 私有方法:设定一个属性值
* 每次设置属性都需要保存文件,所以这里会有些慢
* @param name 属性名称
* @param value 属性的值
*/
protected void setProp(String name, String value) {
//系统同时只能让一个线程写文件
synchronized (propertiesLock) { //同步写
if (G_properties == null) {
loadProps();
}
G_properties.setProperty(name, value);
saveProps(); //保存属性
}
}
/**
* 私有方法:新建一个属性
* @param name 属性名称
* @param value 属性值
*/
protected void putProp(String name, String value) {
//系统只能同时让一个线程写文件
synchronized (propertiesLock) {
if (G_properties == null) {
loadProps();
}
G_properties.put(name, value);
saveProps();
}
}
/**
* 保护方法:删掉一个属性
* @param name 属性的名称
*/
protected void deleteProp(String name) {
//系统只能同时让一个线程写文件
synchronized (propertiesLock) {
if (G_properties == null) {
loadProps();
}
G_properties.remove(name);
saveProps();
}
}
/**
* 保护方法:返回属性枚举
* @return Enumeration 返回属性枚举
*/
protected Enumeration propNames() {
//如果属性还没有装载,需要同步装载
//以保证线程安全
if (G_properties == null) {
synchronized (propertiesLock) {
//额外检查
if (G_properties == null) {
loadProps();
}
}
}
return G_properties.propertyNames();
}
/**
* 装载属性---conf.properties
* @return boolean 装载是否成功
*/
private boolean loadProps() {
boolean m_success = true;
// 2001-08-25 reload()
if (G_properties != null) {
G_properties.clear();
}
else {
G_properties = new Properties();
}
InputStream in = null;
try {
// String inFile = System.getProperty("user.dir");
// inFile = inFile + File.separator + resourceURI;
in = getClass().getResourceAsStream(resourceURI);
G_properties.load(in);
// G_properties.getProperty("path")
}
catch (Exception e) {
System.err.println(
"Error reading conf.properties in PropertyManager.loadProps() " + e);
e.printStackTrace();
m_success = false;
}
finally {
try {
in.close();
in = null;
}
catch (Exception e) {}
}
return m_success;
}
/**
* 保存属性到硬盘(文件)
*/
private void saveProps() {
String path = G_properties.getProperty("path").trim();
OutputStream out = null;
try {
out = new FileOutputStream(path);
G_properties.store(out, propsFName + " -- " + (new java.util.Date()));
}
catch (Exception ioe) {
System.err.println("There was an error writing jive.properties to " +
path + ". " +
"Ensure that the path exists and that the Jive process has permission " +
"to write to it -- " + ioe);
ioe.printStackTrace();
}
finally {
try {
out.close();
}
catch (Exception e) {}
}
}
/**
* 确定属性配置文件可以读取
* @return boolean 属性配置文件是否可读
*/
public boolean propFileIsReadable() {
try {
InputStream in = getClass().getResourceAsStream(resourceURI);
return true;
}
catch (Exception e) {
return false;
}
}
/**
*确定属性配置文件是否存在
*@return boolean 文件是否存在
*/
public boolean propFileExists() {
String path = getProp("path");
if (path == null) {
return false;
}
File file = new File(path);
if (file.isFile()) {
return true;
}
else {
return false;
}
}
/**
*确定属性文件是否可写(修改或者新建的时候会用到)
*@return boolean 属性配置文件是否可写
*/
public boolean propFileIsWritable() {
String path = getProp("path");
File file = new File(path);
if (file.isFile()) {
//See if we can write to the file
if (file.canWrite()) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
//把字符串转化成为中文字符串
public static String toChinese(String strvalue) {
try {
if (strvalue == null || strvalue.trim().length() == 0) {
return null;
}
else {
String temp = strvalue.trim();
byte[] temp_t = temp.getBytes("ISO8859_1");
String tempvalue = new String(temp_t);
return tempvalue;
}
}
catch (Exception e) {
return null;
}
}
//
//测试
//
public static void main(String argv[]) {
String test_path = PropertyManager.getProperty("factory_name");
System.out.println(test_path);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -