|
|
@@ -8,10 +8,17 @@ import java.awt.Image;
|
|
|
import java.awt.Toolkit;
|
|
|
import java.awt.event.ActionEvent;
|
|
|
import java.awt.event.ActionListener;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.FileWriter;
|
|
|
import java.io.IOException;
|
|
|
import java.net.URL;
|
|
|
+import java.rmi.ServerException;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Vector;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
@@ -24,13 +31,30 @@ import javax.swing.JToolBar;
|
|
|
import javax.swing.ListSelectionModel;
|
|
|
import javax.swing.table.DefaultTableModel;
|
|
|
import javax.swing.table.TableColumnModel;
|
|
|
+import javax.xml.parsers.DocumentBuilder;
|
|
|
+import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
+import javax.xml.parsers.ParserConfigurationException;
|
|
|
+import javax.xml.transform.Transformer;
|
|
|
+import javax.xml.transform.TransformerConfigurationException;
|
|
|
+import javax.xml.transform.TransformerException;
|
|
|
+import javax.xml.transform.TransformerFactory;
|
|
|
+import javax.xml.transform.dom.DOMSource;
|
|
|
+import javax.xml.transform.stream.StreamResult;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.w3c.dom.Document;
|
|
|
+import org.w3c.dom.Element;
|
|
|
+import org.w3c.dom.NamedNodeMap;
|
|
|
+import org.w3c.dom.Node;
|
|
|
+import org.w3c.dom.NodeList;
|
|
|
+import org.xml.sax.SAXException;
|
|
|
+import org.yaml.snakeyaml.Yaml;
|
|
|
|
|
|
import com.leanwo.management.config.AppConfig;
|
|
|
import com.leanwo.management.config.DataSourceConfig;
|
|
|
import com.leanwo.management.config.DataSourceConfigList;
|
|
|
+import com.leanwo.management.model.ApplicationSetting;
|
|
|
import com.leanwo.management.util.SpringUtil;
|
|
|
import com.leanwo.management.widget.SelectEventListener;
|
|
|
import com.leanwo.management.widget.SelectObject;
|
|
|
@@ -147,6 +171,7 @@ public class DataSourceSettingFrame extends JFrame {
|
|
|
*/
|
|
|
private void save() {
|
|
|
Vector vector = tableModel.getDataVector();
|
|
|
+ Map<String, DataSourceConfig> map = new HashMap<>();
|
|
|
List<DataSourceConfig> dataSourceConfigs = new ArrayList<DataSourceConfig>();
|
|
|
for(int i = 0; i < vector.size(); i ++) {
|
|
|
DataSourceConfig dataSourceConfig = new DataSourceConfig();
|
|
|
@@ -156,17 +181,18 @@ public class DataSourceSettingFrame extends JFrame {
|
|
|
dataSourceConfig.setUsername((String)rowData.elementAt(2));
|
|
|
dataSourceConfig.setPassword((String)rowData.elementAt(3));
|
|
|
dataSourceConfigs.add(dataSourceConfig);
|
|
|
+ map.put(dataSourceConfig.getBeanId(), dataSourceConfig);
|
|
|
}
|
|
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
stringBuilder.append("您修改了程序管理器的配置,");
|
|
|
- // 根据dataSourceConfigs修改yml
|
|
|
-
|
|
|
+ //修ServerManagement程序管理器application.yml的参数
|
|
|
+ updateYml( dataSourceConfigs);
|
|
|
+ //
|
|
|
+ updateApplicationXml(map);
|
|
|
// 根据dataSourceConfigs修改各个服务器中applicationContext.xml里面的数据
|
|
|
stringBuilder.append("xxx的配置,");
|
|
|
-
|
|
|
// 可以参考XmlConfigPathService中的方法
|
|
|
-
|
|
|
stringBuilder.append("请重新启动本程序。");
|
|
|
// 如果您修改了配置,请重新启动本程序
|
|
|
JOptionPane.showMessageDialog(this, stringBuilder.toString(), "提示:重新启动", JOptionPane.INFORMATION_MESSAGE);
|
|
|
@@ -174,4 +200,171 @@ public class DataSourceSettingFrame extends JFrame {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 只用于正式服务器
|
|
|
+ * 修改其他服务器所对应的application.xml参数
|
|
|
+ * url、password、userName
|
|
|
+ * @param map
|
|
|
+ * @author GuoZhiBo 20200522
|
|
|
+ */
|
|
|
+ private void updateApplicationXml(Map<String, DataSourceConfig> map) {
|
|
|
+ File directory = new File("");
|
|
|
+ String path = directory.getAbsolutePath();
|
|
|
+ File parentPath = new File(path).getParentFile();
|
|
|
+ // 取 文件/文件夹
|
|
|
+ File files[] = parentPath.listFiles();
|
|
|
+ // 对象为空 直接返回
|
|
|
+ if(files != null){
|
|
|
+ // 存在文件 遍历 判断
|
|
|
+ for (File f : files) {
|
|
|
+ // 判断是否为 文件夹
|
|
|
+ if(f.isDirectory()){
|
|
|
+ String filePath = f.getAbsolutePath();
|
|
|
+ if(path.equals(filePath)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ File applicationContextXml = new File(filePath + "\\resources\\applicationContext.xml");
|
|
|
+ if(!applicationContextXml.exists()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ updateXml(applicationContextXml, map);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateXml(File file, Map<String, DataSourceConfig> map) {
|
|
|
+ // 1、创建 DocumentBuilderFactory 对象,用来创建 DocumentBuilder 对象
|
|
|
+ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
|
|
+ try {
|
|
|
+ // 2、创建 DocumentBuilder 对象,用来将 XML 文件 转化为 Document 对象
|
|
|
+ DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
|
|
+ // 3、创建 Document 对象,解析 XML 文件
|
|
|
+ Document document = documentBuilder.parse(file);
|
|
|
+ NodeList beanList = document.getElementsByTagName("bean");
|
|
|
+ if(beanList != null && beanList.getLength() > 0) {
|
|
|
+ for(int i = 0 ; i < beanList.getLength() ; i ++) {
|
|
|
+ // 获取第一个对象
|
|
|
+ Node bean = beanList.item(i);
|
|
|
+ Element beanElement = (Element) bean;
|
|
|
+ //获取所有属性
|
|
|
+ NamedNodeMap namedNodeMap = bean.getAttributes();
|
|
|
+ //获取id
|
|
|
+ Node idNode = namedNodeMap.getNamedItem("id");
|
|
|
+ if(idNode == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String id = idNode.getTextContent();
|
|
|
+ if(id != null && id.length() > 0) {
|
|
|
+ //找到bean的Id是否在数据源中
|
|
|
+ if(map.containsKey(id)) {
|
|
|
+ DataSourceConfig dataSourceConfig = map.get(id);
|
|
|
+ NodeList propertyList = beanElement.getElementsByTagName("property");
|
|
|
+ if(propertyList != null && propertyList.getLength() > 0) {
|
|
|
+ for(int j = 0 ; j < propertyList.getLength() ; j ++) {
|
|
|
+ //获取property第一个对象
|
|
|
+ Node property = propertyList.item(j);
|
|
|
+ //获取property所有属性
|
|
|
+ NamedNodeMap propertyMap = property.getAttributes();
|
|
|
+ //获取name
|
|
|
+ Node propertyNameNode = propertyMap.getNamedItem("name");
|
|
|
+ String propertyName = propertyNameNode.getTextContent();
|
|
|
+ if(propertyName.equals("url")) {
|
|
|
+ Node valueNode = propertyMap.getNamedItem("value");
|
|
|
+ valueNode.setTextContent(dataSourceConfig.getUrl());
|
|
|
+ }
|
|
|
+ if(propertyName.equals("username")) {
|
|
|
+ Node valueNode = propertyMap.getNamedItem("value");
|
|
|
+ valueNode.setTextContent(dataSourceConfig.getUsername());
|
|
|
+ }
|
|
|
+ if(propertyName.equals("password")) {
|
|
|
+ Node valueNode = propertyMap.getNamedItem("value");
|
|
|
+ valueNode.setTextContent(dataSourceConfig.getPassword());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }else {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 创建 TransformerFactory 对象
|
|
|
+ TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
|
|
+ // 创建 Transformer 对象
|
|
|
+ Transformer transformer = transformerFactory.newTransformer();
|
|
|
+ // 创建 DOMSource 对象
|
|
|
+ DOMSource domSource = new DOMSource(document);
|
|
|
+ // 创建 StreamResult 对象
|
|
|
+ StreamResult reStreamResult = new StreamResult(file);
|
|
|
+ transformer.transform(domSource, reStreamResult);
|
|
|
+
|
|
|
+ } catch (ParserConfigurationException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (SAXException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (TransformerConfigurationException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (TransformerException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 本地使用:src/main/resources/Application.yml
|
|
|
+ * 服务器使用:resources/Application.yml
|
|
|
+ * 修ServerManagement程序管理器application.yml的参数
|
|
|
+ * @param dataSourceConfigs
|
|
|
+ * @author GuoZhiBo 20200522
|
|
|
+ */
|
|
|
+ private void updateYml(List<DataSourceConfig> dataSourceConfigs) {
|
|
|
+ // 根据dataSourceConfigs修改yml
|
|
|
+ Yaml yaml = new Yaml();
|
|
|
+ File file = new File("resources/Application.yml");
|
|
|
+ String path = file.getAbsolutePath();
|
|
|
+ if(file.exists()) {
|
|
|
+ try {
|
|
|
+ Map m1 = (Map) yaml.load(new FileInputStream(file));
|
|
|
+ Map m2 = (Map) m1.get("data-source");
|
|
|
+ List m3 = (List)m2.get("items");
|
|
|
+ for(int i = 0; i < dataSourceConfigs.size(); i ++) {
|
|
|
+ Map mapi = (Map)m3.get(i);
|
|
|
+ mapi.put("beanId", dataSourceConfigs.get(i).getBeanId());
|
|
|
+ mapi.put("url", dataSourceConfigs.get(i).getUrl());
|
|
|
+ mapi.put("username", dataSourceConfigs.get(i).getUsername());
|
|
|
+ mapi.put("password", dataSourceConfigs.get(i).getPassword());
|
|
|
+ }
|
|
|
+ FileWriter fileWriter = new FileWriter(file);
|
|
|
+ fileWriter.write(yaml.dump(m1));
|
|
|
+ fileWriter.flush();
|
|
|
+ fileWriter.close();
|
|
|
+
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ try {
|
|
|
+ throw new ServerException("地址错误,请确认是服务器还是本地再修改。");
|
|
|
+ } catch (ServerException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|