package foundation.send.sms;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.Map;
|
|
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.Logger;
|
import org.dom4j.Document;
|
import org.dom4j.Element;
|
import org.dom4j.io.SAXReader;
|
|
import foundation.server.config.Configer;
|
|
|
public class SmsConfigerLoader {
|
|
protected static Logger logger;
|
private static SmsConfigerLoader instance;
|
private static Map<String, SmsConfig> smsConfigMap;
|
|
static {
|
logger = LogManager.getLogger(SmsConfigerLoader.class);
|
smsConfigMap = new HashMap<String, SmsConfig>();
|
}
|
|
public static SmsConfigerLoader getInstance() {
|
if (instance == null) {
|
synchronized (SmsConfigerLoader.class) {
|
if (instance == null) {
|
instance = new SmsConfigerLoader();
|
}
|
}
|
}
|
return instance;
|
}
|
|
public static void staticLoad() throws Exception {
|
getInstance();
|
instance.load();
|
}
|
|
private void load() throws Exception {
|
String path = Configer.getPath_SmsConfig();
|
File file = new File(path);
|
loadOneFile(file);
|
}
|
|
private void loadOneFile(File file) throws Exception {
|
try {
|
logger.debug("load config file:" + file);
|
InputStream inputStream = new FileInputStream(file);
|
|
try {
|
SAXReader reader = new SAXReader();
|
reader.setValidation(false);
|
|
Document doc = reader.read(inputStream);
|
Element root = doc.getRootElement();
|
|
loadParams(root);
|
}
|
finally {
|
try {
|
inputStream.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
} catch (Exception e) {
|
logger.error("can not load dispatch file: " + file);
|
logger.error(e);
|
throw e;
|
}
|
}
|
|
private void loadParams(Element root) throws Exception {
|
Iterator<?> iterator = root.elementIterator("message");
|
|
Element elementBranch = null;
|
|
SmsConfig smsConfig = null;
|
|
while (iterator.hasNext()) {
|
Element elementInner = (Element) iterator.next();
|
|
Iterator<?> mailIterator = elementInner.elementIterator("sms");
|
|
while (mailIterator.hasNext()) {
|
elementBranch = (Element) mailIterator.next();
|
|
smsConfig = new SmsConfig();
|
smsConfig.setName(elementBranch.attributeValue("name"));
|
smsConfig.setEnable(elementBranch.attributeValue("enable"));
|
|
smsConfig.setIdkey(elementBranch.elementText("idkey"));
|
smsConfig.setSecretkey(elementBranch.elementText("secretkey"));
|
smsConfig.setRegion(elementBranch.elementText("region"));
|
smsConfig.setEndpoint(elementBranch.elementText("endpoint"));
|
smsConfigMap.put(smsConfig.getName(), smsConfig);
|
}
|
}
|
}
|
|
public static void addSms(SmsConfig smsConfig) {
|
smsConfigMap.put(smsConfig.getName(), smsConfig);
|
}
|
|
public static void clear() {
|
smsConfigMap.clear();
|
}
|
|
public static Map<String, SmsConfig> getSmsConfigMap() {
|
return smsConfigMap;
|
}
|
|
public static void setSmsConfigMap(Map<String, SmsConfig> smsConfigMap) {
|
SmsConfigerLoader.smsConfigMap = smsConfigMap;
|
}
|
|
}
|