xml配置文件單例本地緩存設計
很久不寫博客了,今天公司需要便寫了一個簡單的xml文件緩存工具類,可能以後還會經常用到,在此做個記錄吧。
忘了怎麼博客上傳文件了,廢話少說,直接上代碼,簡單明瞭
緩存工具類
package TraCustomMessage;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ClientConfig {
private static HashMap<String, HashMap<String, String>> portConfig;
private static ChannelConfigModel channelConfigModel;
private static ArrayList<String> portConfigChannelIdList;
private static ArrayList<String> channelConfigChannelIdList;
private volatile static ClientConfig config = null;
private ClientConfig() {
}
public static ClientConfig getInstance() {
if (config == null) {
synchronized (ClientConfig.class) {
if (config == null) {
config = new ClientConfig();
config.getPortConfig();
config.getChannelConfig();
if(!judgeConfigValid()){
try {
throw new Exception("port_config.xml與channel.xmml 配置的 Channel_ID不一致!!!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
return config;
}
/**
* 判斷配置文件是否同步(port_config.xml 與 channel.xml channelId 是否同步一致 )
*
* @return
*/
private static boolean judgeConfigValid() {
HashMap<String, String> temporaryHm = new HashMap<String, String>();
for (int i = 0; i < portConfigChannelIdList.size(); i++) {
temporaryHm.put(portConfigChannelIdList.get(i), "");
}
for (int i = 0; i < channelConfigChannelIdList.size(); i++) {
if (temporaryHm.get(channelConfigChannelIdList.get(i)) == null) {
return false;
}
}
return true;
}
/**
* 根據口岸獲得對應口岸的配置
*
* @param port
* @return
*/
public HashMap<String, String> getPortConfigByPort(String port) {
return ClientConfig.portConfig.get(port);
}
/**
* 根據channelId獲得發送對應的channel配置
*
* @param channelId
* @return
*/
public HashMap<String, String> getChannelSendConfigByChannelId(String channelId) {
return ClientConfig.channelConfigModel.channelSend.get(channelId);
}
/**
* 根據channelId獲得接收對應的channel配置
*
* @param channelId
* @return
*/
public HashMap<String, String> getChannelReceiveConfigByChannelId(String channelId) {
return ClientConfig.channelConfigModel.channelReceive.get(channelId);
}
/**
* 根據channelId獲得channel其它相關配置
*
* @param channelId
* @return
*/
public HashMap<String, String> getChannelConfigByChannelId(String channelId) {
return ClientConfig.channelConfigModel.channel.get(channelId);
}
/**
* 通過port獲取對應的ChannelId
*
* @param port
* @return
*/
public String getChannelIdByPort(String port) {
HashMap<String, String> portConfig = ClientConfig.portConfig.get(port);
if (portConfig != null && portConfig.get("Channel_ID") != null) {
return portConfig.get("Channel_ID");
} else {
return "";
}
}
/**
* 通過channelId獲得ChannelType
*
* @param channelId
* @return
*/
public String getChannelTypeByChannelId(String channelId) {
HashMap<String, String> channelConfig = ClientConfig.channelConfigModel.channel.get(channelId);
if (channelConfig != null && channelConfig.get("Channel_Type") != null) {
return channelConfig.get("Channel_Type");
} else {
return "";
}
}
/**
* 讀取port_config.xml 配置文件 存於緩存之中
*/
private void getPortConfig() {
portConfig = new HashMap<String, HashMap<String, String>>();
portConfigChannelIdList = new ArrayList<String>();
try {
File f = new File(config.getClass().getResource("/port_config.xml").toURI().getPath());
if (!f.exists()) {
System.out.println("Error : Config file doesn't exist:"
+ config.getClass().getResource("/port_config.xml").getPath());
System.exit(1);
}
SAXReader reader = new SAXReader();
Document doc;
doc = reader.read(f);
Element root = doc.getRootElement();
for (Iterator<?> jr = root.elementIterator(); jr.hasNext();) {
HashMap<String, String> hm = new HashMap<String, String>();
Element context = (Element) jr.next();
String portCode = "";
for (Iterator<?> lr = context.elementIterator(); lr.hasNext();) {
Element conquerysub = (Element) lr.next();
if ("PortCode".equalsIgnoreCase(conquerysub.getName()) && judgeValid(conquerysub.getTextTrim())) {
portCode = conquerysub.getTextTrim();
} else if (judgeValid(portCode)) {
hm.put(conquerysub.getName(), conquerysub.getTextTrim());
if ("Channel_ID".equalsIgnoreCase(conquerysub.getName())
&& judgeValid(conquerysub.getTextTrim())) {
portConfigChannelIdList.add(conquerysub.getTextTrim());
}
}
}
if (judgeValid(portCode)) {
ClientConfig.portConfig.put(portCode, hm);
}else{
throw new Exception("port_config.xml 配置文件中 PortCode無效");
}
}
} catch (Exception ex) {
System.out.println("Error : " + ex.toString());
}
// System.out.println(ClientConfig.portConfig);
}
/**
* 讀取Channel.xml 配置文件 存於緩存之中
*/
private void getChannelConfig() {
channelConfigModel = new ChannelConfigModel();
channelConfigChannelIdList = new ArrayList<String>();
boolean filePathJudgeSwitch = true; // 文件路徑檢查開關,默認爲打開的狀態
try {
File f = new File(config.getClass().getResource("/Channel.xml").toURI().getPath());
if (!f.exists()) {
System.out.println(
"Error : Config file doesn't exist:" + config.getClass().getResource("/Channel.xml").getPath());
System.exit(1);
}
SAXReader reader = new SAXReader();
Document doc;
doc = reader.read(f);
Element root = doc.getRootElement();
for (Iterator<?> jr = root.elementIterator(); jr.hasNext();) {
String channel_ID = "";
String Channel_Type = "";
HashMap<String, String> hm = new HashMap<String, String>();
Element context = (Element) jr.next();
for (Iterator<?> lr = context.elementIterator(); lr.hasNext();) {
Element conquerysub = (Element) lr.next();
if ("Channel_Type".equalsIgnoreCase(conquerysub.getName())
&& judgeValid(conquerysub.getTextTrim())) { // 提前記錄一次,下面判斷文件路徑要用
Channel_Type = conquerysub.getTextTrim();
}
if ("Channel_ID".equalsIgnoreCase(conquerysub.getName()) && judgeValid(conquerysub.getTextTrim())) {
channel_ID = conquerysub.getTextTrim();
channelConfigChannelIdList.add(channel_ID);
} else if ("Send".equalsIgnoreCase(conquerysub.getName())) {
HashMap<String, String> hm_send = new HashMap<String, String>();
for (Iterator<?> cr = conquerysub.elementIterator(); cr.hasNext();) {
Element conquerysub_send = (Element) cr.next();
// System.out.println(conquerysub_send.getName() + "
// " + conquerysub_send.getTextTrim());
if(!judgeValid(conquerysub_send.getTextTrim())){
throw new Exception("channel_ID爲"+channel_ID+"的 "+conquerysub_send.getName()+" value值無效");
}
hm_send.put(conquerysub_send.getName(), conquerysub_send.getTextTrim());
/**
* 判斷路徑是否存在
*/
if (filePathJudgeSwitch) {
if ("canhttp".equalsIgnoreCase(Channel_Type)) {
if ("SendSuccDir".equalsIgnoreCase(conquerysub_send.getName())
&& judgeValid(conquerysub_send.getTextTrim())) {
judgeDirExists(conquerysub_send.getTextTrim());
}
if ("SendErrDir".equalsIgnoreCase(conquerysub_send.getName())
&& judgeValid(conquerysub_send.getTextTrim())) {
judgeDirExists(conquerysub_send.getTextTrim());
}
} else if ("ftp".equalsIgnoreCase(Channel_Type)) {
if ("SendSuccDir".equalsIgnoreCase(conquerysub_send.getName())
&& judgeValid(conquerysub_send.getTextTrim())) {
judgeDirExists(conquerysub_send.getTextTrim());
}
if ("SendErrDir".equalsIgnoreCase(conquerysub_send.getName())
&& judgeValid(conquerysub_send.getTextTrim())) {
judgeDirExists(conquerysub_send.getTextTrim());
}
} else if ("dir".equalsIgnoreCase(Channel_Type)) {
if ("SendSuccDir".equalsIgnoreCase(conquerysub_send.getName())
&& judgeValid(conquerysub_send.getTextTrim())) {
judgeDirExists(conquerysub_send.getTextTrim());
}
if ("SendErrDir".equalsIgnoreCase(conquerysub_send.getName())
&& judgeValid(conquerysub_send.getTextTrim())) {
judgeDirExists(conquerysub_send.getTextTrim());
}
}
}
}
if (judgeValid(channel_ID)) {
ClientConfig.channelConfigModel.channelSend.put(channel_ID, hm_send);
}else{
new Exception("channel_ID爲"+channel_ID+"的 "+"ClientConfig.channelConfigModel.channelSend.put 過程中 channel_ID 無效");
}
} else if ("Receive".equalsIgnoreCase(conquerysub.getName())) {
HashMap<String, String> hm_receive = new HashMap<String, String>();
for (Iterator<?> cr = conquerysub.elementIterator(); cr.hasNext();) {
Element conquerysub_receive = (Element) cr.next();
// System.out.println(conquerysub_send.getName() + "
// " + conquerysub_send.getTextTrim());
hm_receive.put(conquerysub_receive.getName(), conquerysub_receive.getTextTrim());
if(!judgeValid(conquerysub_receive.getTextTrim())){
throw new Exception("channel_ID爲"+channel_ID+"的 "+conquerysub_receive.getName()+" value值無效");
}
/**
* 判斷路徑是否存在
*/
if (filePathJudgeSwitch) {
if ("canhttp".equalsIgnoreCase(Channel_Type)) {
} else if ("ftp".equalsIgnoreCase(Channel_Type)) {
if ("DownLoadPath".equalsIgnoreCase(conquerysub_receive.getName())
&& judgeValid(conquerysub_receive.getTextTrim())) {
judgeDirExists(conquerysub_receive.getTextTrim());
}
if ("ReceiptSuccDirBak".equalsIgnoreCase(conquerysub_receive.getName())
&& judgeValid(conquerysub_receive.getTextTrim())) {
judgeDirExists(conquerysub_receive.getTextTrim());
}
if ("ReceiptErrDirBak".equalsIgnoreCase(conquerysub_receive.getName())
&& judgeValid(conquerysub_receive.getTextTrim())) {
judgeDirExists(conquerysub_receive.getTextTrim());
}
} else if ("dir".equalsIgnoreCase(Channel_Type)) {
if ("ReceiptSuccDirBak".equalsIgnoreCase(conquerysub_receive.getName())
&& judgeValid(conquerysub_receive.getTextTrim())) {
judgeDirExists(conquerysub_receive.getTextTrim());
}
if ("ReceiptErrDirBak".equalsIgnoreCase(conquerysub_receive.getName())
&& judgeValid(conquerysub_receive.getTextTrim())) {
judgeDirExists(conquerysub_receive.getTextTrim());
}
}
}
}
if(judgeValid(channel_ID)){
ClientConfig.channelConfigModel.channelReceive.put(channel_ID, hm_receive);
}else{
new Exception("channel_ID爲"+channel_ID+"的 "+"ClientConfig.channelConfigModel.channelReceive.put 過程中 channel_ID 無效");
}
} else if (judgeValid(channel_ID) && judgeValid(conquerysub.getTextTrim())) {
hm.put(conquerysub.getName(), conquerysub.getTextTrim());
}
// System.out.println(conquerysub.getName() + " " +
// conquerysub.getTextTrim());
}
if (judgeValid(channel_ID)) {
ClientConfig.channelConfigModel.channel.put(channel_ID, hm);
}else{
new Exception("channel_ID爲"+channel_ID+"的 "+"ClientConfig.channelConfigModel.channel.put 過程中 channel_ID 無效");
}
}
} catch (Exception ex) {
System.out.println("Error : " + ex.toString());
}
/*
* System.out.println(ClientConfig.channelConfigModel.channel);
* System.out.println(ClientConfig.channelConfigModel.channelReceive);
* System.out.println(ClientConfig.channelConfigModel.channelSend);
*/
}
/**
* 判斷路徑是否存在,不存在則創建
*
* @param filePath
*/
private static void judgeDirExists(String filePath) {
File file = new File(filePath);
if (file.exists()) {
if (file.isDirectory()) {
System.out.println(filePath + " dir exists");
}
} else {
System.out.println(filePath + " dir not exists, create it ...");
file.mkdirs();
}
}
/**
* 判斷字符串的有效性
*
* @param text
* @return
*/
private boolean judgeValid(String text) {
if (text == null || "".equals(text) || text.length() <= 0) {
return false;
} else {
return true;
}
}
public static void main(String[] args) {
ClientConfig client = ClientConfig.getInstance();
System.out.println(portConfigChannelIdList);
System.out.println(channelConfigChannelIdList);
System.out.println(judgeConfigValid());
}
}
class ChannelConfigModel {
public HashMap<String, HashMap<String, String>> channelSend = new HashMap<String, HashMap<String, String>>(); // 存send下的信息
public HashMap<String, HashMap<String, String>> channelReceive = new HashMap<String, HashMap<String, String>>(); // 存receive下的信息
public HashMap<String, HashMap<String, String>> channel = new HashMap<String, HashMap<String, String>>(); // 存channel信息(send與receive父節點兄弟級別信息)
}
涉及到的xml文件channel.xml
<ChannelConfig>
<Channel>
<Channel_ID>CAN_single_http</Channel_ID>
<Channel_Type>canhttp</Channel_Type>
<Send>
<URL>https://service.singlewindow.gz.cn/airdeclService/restful/airCraftDeclare/import/</URL>
<USERNAME>061315188</USERNAME>
<TOKEN>7FFD4E6C61B20FE6DA5CB9B573D2DC3D</TOKEN>
<PK12URL>F:\\temp\\ssl\\product\\client.p12</PK12URL>
<PK12_PASSPORT>123456</PK12_PASSPORT>
<CERURL>F:\\temp\\ssl\\product\\ca.crt</CERURL>
<SendSuccDir></SendSuccDir>
<SendErrDir></SendErrDir>
</Send>
<Receive>
<isUse>false</isUse>
</Receive>
</Channel>
<Channel>
<Channel_ID>XIY_single_ftp</Channel_ID>
<Channel_Type>ftp</Channel_Type>
<Send>
<URL>113.140.22.242</URL>
<USER>efreight</USER>
<PASSPORT>efreight.xcd</PASSPORT>
<SendDir>/cam_report/</SendDir>
<SendSuccDir></SendSuccDir>
<SendErrDir></SendErrDir>
</Send>
<Receive>
<isUse>true</isUse>
<URL>113.140.22.242</URL>
<USER>efreight</USER>
<PASSPORT>efreight.xcd</PASSPORT>
<ReceiptDir>/cam_result/</ReceiptDir>
<DownLoadPath></DownLoadPath>
<ReceiptSuccDirBak></ReceiptSuccDirBak>
<ReceiptErrDirBak></ReceiptErrDirBak>
</Receive>
</Channel>
<Channel>
<Channel_ID>TSN_single_mq</Channel_ID>
<Channel_Type>dir</Channel_Type>
<Send>
<SendDir>/cam_report/</SendDir>
<SendSuccDir></SendSuccDir>
<SendErrDir></SendErrDir>
</Send>
<Receive>
<isUse>true</isUse>
<ReceiptDir>/cam_result/</ReceiptDir>
<ReceiptSuccDirBak></ReceiptSuccDirBak>
<ReceiptErrDirBak></ReceiptErrDirBak>
</Receive>
</Channel>
涉及到的xml文件port_config.xml
<Config>
<Cam_Port>
<PortCode>CAN</PortCode>
<Channel_ID>CAN_single_http</Channel_ID>
<Operator>xiyadmin</Operator>
<OpTime>2019-06-25 19:00:01</OpTime>
</Cam_Port>
<Cam_Port>
<PortCode>XIY</PortCode>
<Channel_ID>XIY_single_ftp</Channel_ID>
<Operator>xiyadmin</Operator>
<OpTime>2019-06-25 19:00:01</OpTime>
</Cam_Port>
<Cam_Port>
<PortCode>TSN</PortCode>
<Channel_ID>TSN_single_mq</Channel_ID>
<Operator>xiyadmin</Operator>
<OpTime>2019-06-25 19:00:01</OpTime>
</Cam_Port>
</Config>
markdown 不太好使,,,,
摸魚博客。。。