|
|
@@ -1,10 +1,26 @@
|
|
|
package org.leanwo.management.util;
|
|
|
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.util.Collection;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Properties;
|
|
|
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
+import org.dom4j.Attribute;
|
|
|
+import org.dom4j.Document;
|
|
|
+import org.dom4j.DocumentException;
|
|
|
+import org.dom4j.Element;
|
|
|
+import org.dom4j.io.SAXReader;
|
|
|
import com.leanwo.management.model.ApplicationSetting;
|
|
|
import com.leanwo.management.model.XmlConfigPath;
|
|
|
|
|
|
@@ -17,18 +33,105 @@ public class XmlConfigPathService {
|
|
|
* @param setting
|
|
|
*/
|
|
|
public void load(ApplicationSetting setting) {
|
|
|
+ Map<String, String> contentMap = new HashMap<String, String>();
|
|
|
XmlConfigPath xmlConfigPath = setting.getXmlConfigPath();
|
|
|
+ String xmlFilePath = setting.getInstallPath() + "/" + xmlConfigPath.getXmlFileName();
|
|
|
+ File file = new File(xmlFilePath);
|
|
|
+ if (file.exists()) {
|
|
|
+ try {
|
|
|
+
|
|
|
+ SAXReader reader = new SAXReader();
|
|
|
+ Document document = reader.read(file);
|
|
|
+ Element root = document.getRootElement();
|
|
|
+ List<String> nodeNames = xmlConfigPath.getNodeNames();
|
|
|
+ for (String s : nodeNames) {
|
|
|
+ Element dataSource = parse(root , "id" , s);
|
|
|
+ if (dataSource == null) {
|
|
|
+ throw new RuntimeException("在文件:"+xmlFilePath+",中未找到id="+s+"的标签");
|
|
|
+ }
|
|
|
+ String asXML = dataSource.asXML();
|
|
|
+ String result = asXML.substring(asXML.indexOf(">"), asXML.lastIndexOf("<"));
|
|
|
+ /*StringBuilder sb = new StringBuilder();
|
|
|
+ List<Element> propertys = dataSource.elements();
|
|
|
+ for (Element e : propertys) {
|
|
|
+ int i = 1;
|
|
|
+ List<Attribute> attributes = e.attributes();
|
|
|
+ for (Attribute a : attributes) {
|
|
|
+ if(i == 1) {
|
|
|
+ sb.append(a.getStringValue());
|
|
|
+ }else{
|
|
|
+ sb.append("=").append(a.getStringValue()).append(",");
|
|
|
+ }
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ contentMap.put(s, sb.toString());*/
|
|
|
+ contentMap.put(s, result);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (DocumentException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ throw new RuntimeException("文件:"+xmlFilePath+",不存在");
|
|
|
+ }
|
|
|
+ //读取propertis信息
|
|
|
+ String propertiesPath = setting.getInstallPath()+"/config/config.properties";
|
|
|
+ Properties properties = new Properties();
|
|
|
+ File file2 = new File(propertiesPath);
|
|
|
+ Integer port = null;
|
|
|
+ String token = "";
|
|
|
+ InputStream in = null;
|
|
|
+ if (file2.exists()) {
|
|
|
+ try {
|
|
|
+ in = new BufferedInputStream(new FileInputStream(file2));
|
|
|
+ properties.load(in);
|
|
|
+ port = Integer.parseInt(properties.getProperty("port"));
|
|
|
+ token = properties.getProperty("token");
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally {
|
|
|
+ if(in != null){
|
|
|
+ try {
|
|
|
+ in.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ throw new RuntimeException("文件:"+propertiesPath+",不存在");
|
|
|
+ }
|
|
|
if(xmlConfigPath != null) {
|
|
|
- xmlConfigPath.setPortNo(100);
|
|
|
- xmlConfigPath.setToken("123456");
|
|
|
-
|
|
|
- Map<String, String> contentMap = new HashMap<String, String>();
|
|
|
- contentMap.put("dataSource1", "1");
|
|
|
- contentMap.put("dataSource2", "2");
|
|
|
+ xmlConfigPath.setPortNo(port);
|
|
|
+ xmlConfigPath.setToken(token);
|
|
|
xmlConfigPath.setContents(contentMap);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得X属性结果是X值的整个标签
|
|
|
+ */
|
|
|
+ public static Element parse(Element node , String type , String val) {
|
|
|
+ for (Iterator iter = node.elementIterator(); iter.hasNext();) {
|
|
|
+ Element element = (Element) iter.next();
|
|
|
+ Attribute name = element.attribute(type);
|
|
|
+ if (name != null) {
|
|
|
+ String value = name.getValue();
|
|
|
+ if (value != null && val.equals(value))
|
|
|
+ return element;
|
|
|
+ else
|
|
|
+ parse(element , type , val);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 将ApplicationSetting的属性portNo,token保存到config/config.properteis文件中
|
|
|
* 从xml配中获取node 的数据
|
|
|
@@ -36,6 +139,59 @@ public class XmlConfigPathService {
|
|
|
* @param setting
|
|
|
*/
|
|
|
public void save(ApplicationSetting setting) {
|
|
|
+ XmlConfigPath xmlConfigPath = setting.getXmlConfigPath();
|
|
|
+ Map<String, String> contents = xmlConfigPath.getContents();
|
|
|
+ //Collection<String> values = contents.values();
|
|
|
+ String xmlFilePath = setting.getInstallPath() + "/" + xmlConfigPath.getXmlFileName();
|
|
|
+ File file = new File(xmlFilePath);
|
|
|
+ if (file.exists()) {
|
|
|
+ try {
|
|
|
+ SAXReader reader = new SAXReader();
|
|
|
+ Document document = reader.read(file);
|
|
|
+ Element root = document.getRootElement();
|
|
|
+ List<String> nodeNames = xmlConfigPath.getNodeNames();
|
|
|
+ for (String s : nodeNames) {
|
|
|
+ Element dataSource = parse(root , "id" , s);
|
|
|
+ if (dataSource == null) {
|
|
|
+ throw new RuntimeException("在文件:"+xmlFilePath+",中未找到id="+s+"的标签");
|
|
|
+ }
|
|
|
+ dataSource.setText(contents.get(s));
|
|
|
+ }
|
|
|
+ } catch (DocumentException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //设置propertis信息
|
|
|
+ String propertiesPath = setting.getInstallPath()+"/config/config.properties";
|
|
|
+ Properties properties = new Properties();
|
|
|
+ File file2 = new File(propertiesPath);
|
|
|
+ Integer port = xmlConfigPath.getPortNo();
|
|
|
+ String token = xmlConfigPath.getToken();
|
|
|
+ OutputStream out = null;
|
|
|
+ if (file2.exists()) {
|
|
|
+ try {
|
|
|
+ out = new BufferedOutputStream(new FileOutputStream(file2,true));
|
|
|
+ properties.setProperty("token", token);
|
|
|
+ properties.setProperty("port", port.toString());
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally {
|
|
|
+ if(out != null){
|
|
|
+ try {
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ throw new RuntimeException("文件:"+propertiesPath+",不存在");
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|
|
|
}
|