import java.io.*;
import java.util.*;
/**
* 读取程序配置信息
* 文件格式
* 注释行: 以*开始的行
* 配置行:item#value
*/
public class ConfigFile {
private BufferedReader reader;
private Hashtable m_HashTable;
private String m_configfile;
public ConfigFile(String inifilename) {
m_configfile = inifilename;
m_HashTable = new Hashtable(15);
readConfig();
}
public String get(String item){
return (String)m_HashTable.get(item);
}
public int getInt(String item,int defval){
try{
return Integer.parseInt((String)m_HashTable.get(item));
}catch(Exception e){
return defval;
}
}
/**
* 读取配置文件
* @return boolean
*/
private boolean readConfig(String configfile) {
try {
String str = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
str = str.substring(0, str.lastIndexOf("/"));
//读取配置文件
str = str + "/" + configfile;
FileInputStream fi = new FileInputStream(str);
BufferedReader reader = new BufferedReader(new InputStreamReader(fi));
String line;
String name = null, value = null;
while (true) {
line = reader.readLine();
if (line == null) {
break;
}
if (line.equals("")) {
continue;
}
if (line.charAt(0) == '*') {
continue;
}
line = line.trim();
StringTokenizer sz = new StringTokenizer(line, "#");
if (sz.hasMoreTokens()) {
name = sz.nextToken().toUpperCase().trim();
}
if (sz.hasMoreTokens()) {
value = sz.nextToken().trim();
}
if (name.equals("SERVER_OCSP_IP")) {
m_host = value;
}
else if (name.equals("SERVER_OCSP_PORT")) {
m_port = Integer.parseInt(value);
}
else if (name.equals("SERVER_OCSP_CERT")) {
m_ocsp_cert_file = value;
}
}
reader.close();
fi.close();
return true;
}
catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}