YangZhiJie 8 yıl önce
ebeveyn
işleme
2d7715858a

+ 1 - 1
autoStart.bat

@@ -1,2 +1,2 @@
-CD D:\LeanwoProgram_2017\ProdogV6\ServerManagement
+CD D:\LeanwoProgram_2017\ProdogV7-ServerManagement\ServerManagement
 D:

+ 2 - 2
config/applicationContext.xml

@@ -57,14 +57,14 @@
 	
 	<bean id="applicationSetting2" class="com.leanwo.management.model.ApplicationSetting">
 		<property name="name" value="报表服务程序"></property>
-		<property name="installPath" value="C:\Users\YangZhiJie\Desktop\Servers\DingTalk_Server_20171218"></property>
+		<property name="installPath" value="C:\Users\TianGuangHui\Service\Report_Server_20171218"></property>
 		<property name="startBatFile" value="start.bat"></property>
 		<property name="stopBatFile" value="stop.bat"></property>
 		<property name="monitorUrl" value="http://127.0.0.1:81/monitor"></property>
 		<property name="token" value="123456"></property>
 		<property name="autoUpdateFileNames">
 			<list>
-				<value>DingDingServer-0.0.1-SNAPSHOT</value>
+				<value>report-server-0.0.1-SNAPSHOT.jar</value>
 			</list>
 		</property>
 		<property name="xmlConfigPath" >

+ 116 - 1
src/main/java/org/leanwo/management/util/AutoUpdateService.java

@@ -1,5 +1,17 @@
 package org.leanwo.management.util;
 
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.List;
+
 import com.leanwo.management.model.ApplicationSetting;
 
 /**
@@ -37,14 +49,117 @@ public class AutoUpdateService {
 	 * @return
 	 */
 	public boolean canUpdate(ApplicationSetting setting) {
+		List<String> autoUpdateFileNames = setting.getAutoUpdateFileNames();
 		
+		if (autoUpdateFileNames == null || autoUpdateFileNames.size() < 1) {
+			return false;
+		}
 		return true;
 	}
-
+	/**
+	 * 从输入流中获取字节数组 
+	 * @param inputStream
+	 * @return
+	 * @throws IOException
+	 */
+	 public static  byte[] readInputStream(InputStream inputStream) {    
+	        byte[] buffer = new byte[1024];    
+	        int len = 0;    
+	        ByteArrayOutputStream bos = new ByteArrayOutputStream();    
+	        try {
+				while((len = inputStream.read(buffer)) != -1) {    
+				    bos.write(buffer, 0, len);    
+				}
+			} catch (IOException e) {
+				e.printStackTrace();
+			} finally {
+				if(bos != null){
+					try {
+						bos.close();
+					} catch (IOException e) {
+						e.printStackTrace();
+					}    
+				}
+			}   
+	        return bos.toByteArray();    
+	    }    
+	
+	 /**
+	  * 判断文件是否存在
+	  * @param httpPath
+	  * @return
+	  */
+	 private static Boolean existHttpPath(String httpPath){
+		 URL httpurl = null;
+		 try {
+		 httpurl = new URL(httpPath);
+		 URLConnection rulConnection = httpurl.openConnection();
+		 rulConnection.getInputStream();
+		 return true;
+		 } catch (Exception e) {
+		 return false;
+		 }
+		 }
+	
 	/**
 	 * 确定执行更新.
 	 */
 	public void autoUpdate(ApplicationSetting setting) {
+		List<String> autoUpdateFileNames = setting.getAutoUpdateFileNames();
+		String savePath = setting.getInstallPath();
+		String fileName = savePath.substring(savePath.lastIndexOf("\\")+1);
+		URL url;
+		InputStream inputStream = null;
+		FileOutputStream fos = null;
+		try {
+			for (String s : autoUpdateFileNames) {
+				String requestPath =  updateSite + "/" + fileName + "/" + s;
+				if (!existHttpPath(requestPath)) {
+					throw new RuntimeException("需要更新的文件"+ requestPath + "不存在");
+				}
+				url = new URL(requestPath);
+				HttpURLConnection conn = (HttpURLConnection)url.openConnection();    
+				//设置超时间为5秒  
+				conn.setConnectTimeout(5*1000);  
+				//防止屏蔽程序抓取而返回403错误  
+				conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");  
+					
+				//得到输入流  
+				inputStream = conn.getInputStream();    
+				//获取自己数组  
+				byte[] getData = readInputStream(inputStream);      
+					
+				//文件保存位置  
+				File saveDir = new File(savePath);  
+				if(!saveDir.exists()){  
+					saveDir.mkdir();  
+				}  
+				File file = new File(saveDir + "/"  + s);      
+				fos = new FileOutputStream(file);  
+				fos.write(getData); 
+				}
+			} catch (MalformedURLException e) {
+				e.printStackTrace();
+			} catch (FileNotFoundException e) {
+				e.printStackTrace();
+			} catch (IOException e) {
+				e.printStackTrace();
+			} finally {
+				if(fos!=null){  
+					try {
+						fos.close();
+					} catch (IOException e) {
+						e.printStackTrace();
+					}    
+				}  
+				if(inputStream!=null){  
+					try {
+						inputStream.close();
+					} catch (IOException e) {
+						e.printStackTrace();
+					}  
+				} 
+			}   
 		
 	}
 	

+ 164 - 8
src/main/java/org/leanwo/management/util/XmlConfigPathService.java

@@ -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+",不存在");
+				}
+		
 		
 	}
 }