package foundation.server.config;
|
|
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.dom4j.Document;
|
import org.dom4j.Element;
|
import org.dom4j.io.SAXReader;
|
|
public class SecretMap {
|
|
private static SecretMap instance;
|
private Map<String, String> secrets;
|
|
public static SecretMap getInstance() {
|
if (instance == null) {
|
instance = new SecretMap();
|
}
|
|
return instance;
|
}
|
|
|
public SecretMap() {
|
this.secrets = new HashMap<String, String>();
|
}
|
|
|
public void loadOneFile(File file) throws Exception {
|
try {
|
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) {
|
}
|
}
|
} catch (Exception e) {
|
throw e;
|
}
|
}
|
|
private void loadParams(Element root) throws Exception {
|
Iterator<?> iterator = root.elementIterator("secret");
|
|
while (iterator.hasNext()) {
|
Element element = (Element) iterator.next();
|
|
String name = element.attributeValue("systemId");
|
String value = element.attributeValue("secret");
|
|
secrets.put(name, value);
|
}
|
}
|
|
public static String getSecret(String systemId) {
|
return instance.secrets.get(systemId);
|
}
|
}
|