Bladeren bron

Merge branch 'master' of http://192.168.0.3:3000/ShangHaiLeanwo/ServerManagement

# Conflicts:
#	src/main/resources/Application.yml
郭志波 6 jaren geleden
bovenliggende
commit
83bc73691e

+ 7 - 1
pom.xml

@@ -48,13 +48,19 @@
 			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
 		</dependency>
 
+		<!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
+		<dependency>
+		    <groupId>org.dom4j</groupId>
+		    <artifactId>dom4j</artifactId>
+		    <version>2.1.3</version>
+		</dependency>
+
 
 		<dependency>
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-test</artifactId>
 			<scope>test</scope>
 		</dependency>
-
 		<dependency>
 			<groupId>junit</groupId>
 			<artifactId>junit</artifactId>

+ 167 - 0
src/main/java/com/leanwo/management/DataSourceSettingFrame.java

@@ -0,0 +1,167 @@
+package com.leanwo.management;
+
+import java.awt.AWTEvent;
+import java.awt.BorderLayout;
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Image;
+import java.awt.Toolkit;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Vector;
+
+import javax.imageio.ImageIO;
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JScrollPane;
+import javax.swing.JTable;
+import javax.swing.JToolBar;
+import javax.swing.ListSelectionModel;
+import javax.swing.table.DefaultTableModel;
+import javax.swing.table.TableColumnModel;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.leanwo.management.config.AppConfig;
+import com.leanwo.management.config.DataSourceConfig;
+import com.leanwo.management.config.DataSourceConfigList;
+import com.leanwo.management.util.SpringUtil;
+import com.leanwo.management.widget.SelectEventListener;
+import com.leanwo.management.widget.SelectObject;
+
+/**
+ * 数据源设置
+ * 
+ * @author YangZhiJie
+ *
+ */
+public class DataSourceSettingFrame extends JFrame {
+
+	private static Logger logger = LoggerFactory.getLogger(DataSourceSettingFrame.class);
+	
+	private JButton saveButton;
+
+	// 得到显示器屏幕的宽高
+	private int width = Toolkit.getDefaultToolkit().getScreenSize().width;
+	private int height = Toolkit.getDefaultToolkit().getScreenSize().height;
+
+	// 定义窗体的宽高
+	private int windowsWidth = 800;
+	private int windowsHeight = 400;
+	
+	private DefaultTableModel tableModel;   //表格模型对象
+	
+	public DataSourceSettingFrame() {
+		super();
+		this.initView();
+		setTitle("数据源配置");
+	}
+	
+
+	private void initView() {
+		this.setLayout(new BorderLayout(5,5));
+
+        JToolBar toolbar = new JToolBar();
+        toolbar.setFloatable(false);
+        toolbar.setBorderPainted(false);
+        
+        add(toolbar, BorderLayout.NORTH);
+                
+        saveButton = new JButton("保存");
+        toolbar.add(saveButton);
+
+
+		JScrollPane scrollPane = new JScrollPane(); // 支持滚动
+		getContentPane().add(scrollPane, BorderLayout.CENTER);
+		String[] columnNames = { "数据源beanId", "链接字符串", "用户名", "密码" };
+
+		DataSourceConfigList dataSourceConfigList = (DataSourceConfigList)SpringUtil.getSingleBean(DataSourceConfigList.class);
+		String [][] tableValues= null;
+		if(dataSourceConfigList != null) {
+			List<DataSourceConfig> items = dataSourceConfigList.getItems();
+			tableValues= new String[items.size()][4];
+			if(items != null) {
+				for (int row = 0; row < items.size(); row++) { // 获得数据
+					DataSourceConfig item = items.get(row);
+					String[] rowValue = { item.getBeanId(), item.getUrl(), item.getUsername(), item.getPassword() };
+					tableValues[row] = rowValue;
+				}
+			}
+		}
+		
+		
+		tableModel = new DefaultTableModel(tableValues,columnNames);
+		
+		JTable table = new JTable(tableModel); // 自定义的表格
+		table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); // 关闭表格列的自动调整功能。
+		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 单选
+//		table.setSelectionBackground(Color.YELLOW);
+//		table.setSelectionForeground(Color.RED);
+		table.setRowHeight(30);
+		
+		TableColumnModel columnModel = table.getColumnModel();
+		columnModel.getColumn(0).setPreferredWidth(100);
+		columnModel.getColumn(1).setPreferredWidth(400);
+		columnModel.getColumn(2).setPreferredWidth(100);		
+		columnModel.getColumn(3).setPreferredWidth(100);		
+		table.setColumnModel(columnModel);
+		
+		scrollPane.setViewportView(table); // 支持滚动
+		
+        add(scrollPane, BorderLayout.CENTER);
+        
+		// 设置图标
+		URL path = DataSourceSettingFrame.class.getResource("/prodog_16.png");
+		Image image;
+		try {
+			image = ImageIO.read(path);
+			this.setIconImage(image);
+		} catch (IOException e) {
+			logger.error("读取图片异常。", e);
+		}   
+		
+        // 激活窗口事件
+        this.enableEvents(AWTEvent.WINDOW_EVENT_MASK);
+        
+		// 设置窗体位置和大小
+		this.setBounds((width - windowsWidth) / 2, (height - windowsHeight) / 2, windowsWidth, windowsHeight);
+		
+		// 保存按钮
+		this.saveButton.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				save();
+			}
+		});
+	}
+	
+	/**
+	 * 保存数据
+	 * 首先更新yml里面的内容,然后更新各个服务中applicationContext.xml里面的数据
+	 */
+	private void save() {
+		Vector vector = tableModel.getDataVector();
+		List<DataSourceConfig> dataSourceConfigs = new ArrayList<DataSourceConfig>();
+		for(int i = 0; i < vector.size(); i ++) {
+			DataSourceConfig dataSourceConfig = new DataSourceConfig();
+			Vector rowData = (Vector)vector.elementAt(i);
+			dataSourceConfig.setBeanId((String)rowData.elementAt(0));
+			dataSourceConfig.setUrl((String)rowData.elementAt(1));
+			dataSourceConfig.setUsername((String)rowData.elementAt(2));
+			dataSourceConfig.setPassword((String)rowData.elementAt(3));
+			dataSourceConfigs.add(dataSourceConfig);
+		}
+		
+		// 根据dataSourceConfigs修改yml
+		
+		// 根据dataSourceConfigs修改各个服务器中applicationContext.xml里面的数据
+		
+		// 可以参考XmlConfigPathService中的方法
+	}
+	
+}

+ 22 - 1
src/main/java/com/leanwo/management/MainFrame.java

@@ -30,11 +30,12 @@ import javax.swing.JToolBar;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Value;
+
+import com.leanwo.management.config.AppConfig;
 import com.leanwo.management.model.ApplicationSetting;
 import com.leanwo.management.model.ApplicationSettingCache;
 import com.leanwo.management.service.ProcessService;
 import com.leanwo.management.service.ProgramRunResult;
-import com.leanwo.management.util.AppConfig;
 import com.leanwo.management.util.FileUtil;
 import com.leanwo.management.util.SpringUtil;
 import com.leanwo.management.widget.SelectEventListener;
@@ -47,6 +48,9 @@ public class MainFrame extends JFrame{
 	private JButton stopMenu = null;
 	private JButton startAllMenu = null;
 	private JButton stopAllMenu = null;
+
+	private JButton dbSettingMenu = null;
+	
 	
 	private ServerFrame serverFrame = null;
 	private ServerLogFrame serverLogFrame =  null;
@@ -116,6 +120,10 @@ public class MainFrame extends JFrame{
         stopAllMenu = new JButton("全部关闭");
         toolbar.add(stopAllMenu);
         
+        dbSettingMenu = new JButton("数据源设置");
+        toolbar.add(dbSettingMenu);
+        
+        
 		serverFrame = new ServerFrame(settings);
 		serverFrame.setPreferredSize(new Dimension(300, 0));
         add(serverFrame, BorderLayout.WEST);
@@ -263,6 +271,19 @@ public class MainFrame extends JFrame{
 				}
 			}
 		});
+		
+
+		// 数据源设置
+		dbSettingMenu.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				DataSourceSettingFrame dataSourceSetting = new DataSourceSettingFrame();
+				dataSourceSetting.show(true);
+			}
+		});
+		
+		
+		
 	}
 	
 	/**

+ 5 - 3
src/main/java/com/leanwo/management/util/AppConfig.java → src/main/java/com/leanwo/management/config/AppConfig.java

@@ -1,6 +1,10 @@
-package com.leanwo.management.util;
+package com.leanwo.management.config;
+
+import java.util.ArrayList;
+import java.util.List;
 
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.context.annotation.Configuration;
 
 /**
@@ -32,6 +36,4 @@ public class AppConfig {
 	public void setAutoConfig(Boolean autoConfig) {
 		this.autoConfig = autoConfig;
 	}
-
-
 }

+ 94 - 0
src/main/java/com/leanwo/management/config/DataSourceConfig.java

@@ -0,0 +1,94 @@
+package com.leanwo.management.config;
+
+/**
+ * 数据源配置.
+ *
+ * @author YangZhiJie
+ */
+public class DataSourceConfig {
+
+	/**  bean Id. */
+	private String beanId;
+	
+	/**  数据库访问地址. */
+	private String url;
+
+	/**  数据库访问用户名. */
+	private String username;
+
+	/**  数据库访问密码. */
+	private String password;
+
+	/**
+	 * Gets the bean Id.
+	 *
+	 * @return the bean Id
+	 */
+	public String getBeanId() {
+		return beanId;
+	}
+
+	/**
+	 * Sets the bean Id.
+	 *
+	 * @param beanId the new bean Id
+	 */
+	public void setBeanId(String beanId) {
+		this.beanId = beanId;
+	}
+
+	/**
+	 * Gets the 数据库访问地址.
+	 *
+	 * @return the 数据库访问地址
+	 */
+	public String getUrl() {
+		return url;
+	}
+
+	/**
+	 * Sets the 数据库访问地址.
+	 *
+	 * @param url the new 数据库访问地址
+	 */
+	public void setUrl(String url) {
+		this.url = url;
+	}
+
+	/**
+	 * Gets the 数据库访问用户名.
+	 *
+	 * @return the 数据库访问用户名
+	 */
+	public String getUsername() {
+		return username;
+	}
+
+	/**
+	 * Sets the 数据库访问用户名.
+	 *
+	 * @param username the new 数据库访问用户名
+	 */
+	public void setUsername(String username) {
+		this.username = username;
+	}
+
+	/**
+	 * Gets the 数据库访问密码.
+	 *
+	 * @return the 数据库访问密码
+	 */
+	public String getPassword() {
+		return password;
+	}
+
+	/**
+	 * Sets the 数据库访问密码.
+	 *
+	 * @param password the new 数据库访问密码
+	 */
+	public void setPassword(String password) {
+		this.password = password;
+	}
+	
+}

+ 41 - 0
src/main/java/com/leanwo/management/config/DataSourceConfigList.java

@@ -0,0 +1,41 @@
+package com.leanwo.management.config;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+// TODO: Auto-generated Javadoc
+/**
+ * AppConfig配置.
+ *
+ * @author GuoZhiBo
+ */
+@Configuration
+@ConfigurationProperties("data-source")
+public class DataSourceConfigList {
+
+	/** 数据源配置集合. */
+	private List<DataSourceConfig> items = new ArrayList<DataSourceConfig>();
+
+	/**
+	 * Gets the items.
+	 *
+	 * @return the items
+	 */
+	public List<DataSourceConfig> getItems() {
+		return items;
+	}
+
+	/**
+	 * Sets the items.
+	 *
+	 * @param items the new items
+	 */
+	public void setItems(List<DataSourceConfig> items) {
+		this.items = items;
+	}
+
+}

+ 172 - 0
src/main/java/com/leanwo/management/util/XmlConfigPathService.java

@@ -0,0 +1,172 @@
+package com.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.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import org.dom4j.Attribute;
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
+import org.dom4j.io.OutputFormat;
+import org.dom4j.io.SAXReader;
+import org.dom4j.io.XMLWriter;
+
+import com.leanwo.management.exception.ConfigFileException;
+import com.leanwo.management.model.ApplicationSetting;
+
+public class XmlConfigPathService {
+	
+	
+	
+
+	/**
+	 * 从文件中加载ApplicationSetting中的
+	 * 从config/config.properteis文件中加载portNo,token属性
+	 * 从xml配中获取node 的数据
+	 * ,contents等属性
+	 * @param setting
+	 * @throws ConfigFileException 
+	 */
+	public void load(String xmlFilePath, List<String> nodeNames) throws ConfigFileException {
+		Map<String, String> contentMap = new HashMap<String, String>();
+		File file = new File(xmlFilePath);
+		if (file.exists()) {
+			try {
+				
+				SAXReader reader = new SAXReader();  
+				Document document = reader.read(file);
+				Element root = document.getRootElement();
+				for (String s : nodeNames) {
+					Element dataSource = parse(root , "id" , s);
+					if (dataSource == null) {
+						throw new ConfigFileException("在文件:"+xmlFilePath+",中未找到id="+s+"的标签");
+					}
+					String asXML = dataSource.asXML();
+					String result = asXML.substring(asXML.indexOf(">") + 1 , 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 ConfigFileException("文件:"+xmlFilePath+",不存在");
+		}
+		
+	}
+	
+	
+	/**
+	 * 获得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 的数据
+	 * ,contents保存到文件
+	 * @param setting
+	 * @throws ConfigFileException 
+	 */
+	public void save(String xmlFilePath, List<String> nodeNames) throws ConfigFileException {
+		//目标文件对象
+		File file = new File(xmlFilePath);
+		XMLWriter writer = null;// 声明写XML的对象  
+		if (file.exists()) {
+			try {
+		        SAXReader reader = new SAXReader();  
+		        OutputFormat format = OutputFormat.createPrettyPrint();  
+		        format.setEncoding("utf-8");// 设置XML文件的编码格式  
+				Document document = reader.read(file);
+				Element root = document.getRootElement();
+				for (String s : nodeNames) {
+					Element dataSource = parse(root , "id" , s);
+					if (dataSource == null) {
+						throw new ConfigFileException("在文件:"+xmlFilePath+",中未找到id="+s+"的标签");
+					}
+					//清空bean节点
+					dataSource.clearContent();
+					// 这里存在BUG。
+					//String xmlText = contents.get(s).replaceAll("\n|\t", "");
+					String xmlText = null;
+					//将xml文本转换为document对象
+					Document parseText = DocumentHelper.parseText("<myroot>" + xmlText.trim() + "</myroot>");
+					Element rootElement = parseText.getRootElement();
+					//将文本节点写入目标xml的节点下
+					dataSource.setContent(rootElement.content());
+					writer = new XMLWriter(new FileWriter(file), format);  
+					writer.write(document);  
+					/*List<Element> dataSources = dataSource.elements();
+					for (Element e : dataSources) {
+						dataSource.remove(e);
+					}*/
+					/*List<Element> elements = rootElement.elements();
+					for (Element e : elements) {
+						dataSource.addElement("property");
+						
+					}*/
+					//dataSource.addCDATA(contents.get(s));
+					//dataSource.addText(contents.get(s));
+					//dataSource.setText("<![CDATA["+contents.get(s)+"]]>");
+					 
+				}
+               
+				} catch (DocumentException e) {
+					e.printStackTrace();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}finally {
+					if (writer != null){
+						try {
+							writer.close();
+						} catch (IOException e) {
+							e.printStackTrace();
+						}
+					}
+				}
+			}
+	}
+}