xml配置文件单例本地缓存设计

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></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></PASSPORT>
				<SendDir>/cam_report/</SendDir>
				<SendSuccDir></SendSuccDir>
				<SendErrDir></SendErrDir>
			</Send>
			<Receive>
				<isUse>true</isUse>
				<URL>113.140.22.242</URL>
				<USER>efreight</USER>
				<PASSPORT></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 不太好使,,,,
摸鱼博客。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小伙子渴望力量么

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值