|
@@ -0,0 +1,248 @@
|
|
|
|
|
+package com.leanwo.management;
|
|
|
|
|
+
|
|
|
|
|
+import java.awt.BorderLayout;
|
|
|
|
|
+import java.awt.GridBagConstraints;
|
|
|
|
|
+import java.awt.GridBagLayout;
|
|
|
|
|
+import java.awt.Insets;
|
|
|
|
|
+import java.awt.Toolkit;
|
|
|
|
|
+import java.awt.event.ActionEvent;
|
|
|
|
|
+import java.awt.event.ActionListener;
|
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+import javax.swing.BorderFactory;
|
|
|
|
|
+import javax.swing.JButton;
|
|
|
|
|
+import javax.swing.JFrame;
|
|
|
|
|
+import javax.swing.JLabel;
|
|
|
|
|
+import javax.swing.JPanel;
|
|
|
|
|
+import javax.swing.Timer;
|
|
|
|
|
+
|
|
|
|
|
+import org.apache.commons.exec.CommandLine;
|
|
|
|
|
+import org.apache.commons.exec.DefaultExecuteResultHandler;
|
|
|
|
|
+import org.apache.commons.exec.DefaultExecutor;
|
|
|
|
|
+import org.apache.commons.exec.ExecuteWatchdog;
|
|
|
|
|
+import org.apache.commons.exec.PumpStreamHandler;
|
|
|
|
|
+import org.apache.commons.logging.Log;
|
|
|
|
|
+import org.apache.commons.logging.LogFactory;
|
|
|
|
|
+import org.leanwo.management.util.SpringUtil;
|
|
|
|
|
+
|
|
|
|
|
+import com.leanwo.management.model.ApplicationSetting;
|
|
|
|
|
+import com.leanwo.management.model.ApplicationSettingCache;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 服务器管理界面
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author YangZhiJie
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+public class ServerManagmentJFrame extends JFrame implements ActionListener {
|
|
|
|
|
+ private static Log logger = LogFactory.getLog(ServerManagmentJFrame.class);
|
|
|
|
|
+
|
|
|
|
|
+ // 得到显示器屏幕的宽高
|
|
|
|
|
+ private int width = Toolkit.getDefaultToolkit().getScreenSize().width;
|
|
|
|
|
+ private int height = Toolkit.getDefaultToolkit().getScreenSize().height;
|
|
|
|
|
+
|
|
|
|
|
+ // 定义窗体的宽高
|
|
|
|
|
+ private int windowsWidth = 800;
|
|
|
|
|
+ private int windowsHeight = 400;
|
|
|
|
|
+
|
|
|
|
|
+ private JPanel jPanel = null;
|
|
|
|
|
+ private Timer timer;
|
|
|
|
|
+
|
|
|
|
|
+ private Map<ApplicationSetting, JLabel> statusLabels = null;
|
|
|
|
|
+
|
|
|
|
|
+ public ServerManagmentJFrame() {
|
|
|
|
|
+ this.setLayout(new BorderLayout());
|
|
|
|
|
+
|
|
|
|
|
+ jPanel = new JPanel(new GridBagLayout());
|
|
|
|
|
+ this.add(jPanel, BorderLayout.NORTH);
|
|
|
|
|
+
|
|
|
|
|
+ this.setTitle("Prodog程序管理器");
|
|
|
|
|
+ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设定按关闭时的操作,这里是关闭窗口,如果不设定,就什么也不会发生
|
|
|
|
|
+ // 设置窗体位置和大小
|
|
|
|
|
+ this.setBounds((width - windowsWidth) / 2, (height - windowsHeight) / 2, windowsWidth, windowsHeight);
|
|
|
|
|
+
|
|
|
|
|
+ statusLabels = new HashMap<ApplicationSetting, JLabel>();
|
|
|
|
|
+ generateViewFromJson();
|
|
|
|
|
+
|
|
|
|
|
+ this.setVisible(true); // 显示,如果不设置就什么都看不到
|
|
|
|
|
+
|
|
|
|
|
+ timer = new Timer(1000,this);
|
|
|
|
|
+ timer.start();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void generateViewFromJson() {
|
|
|
|
|
+ GridBagConstraints constrains = null;
|
|
|
|
|
+
|
|
|
|
|
+ JLabel label = new JLabel("程序");
|
|
|
|
|
+ label.setBorder(BorderFactory.createEtchedBorder());
|
|
|
|
|
+ constrains = new GridBagConstraints();
|
|
|
|
|
+ constrains.insets = new Insets(0, 0, 0, 0);
|
|
|
|
|
+ constrains.fill = GridBagConstraints.BOTH;
|
|
|
|
|
+ constrains.gridx = 0;
|
|
|
|
|
+ constrains.gridy = 0;
|
|
|
|
|
+ constrains.weightx = 25;
|
|
|
|
|
+ constrains.anchor = GridBagConstraints.NORTH;
|
|
|
|
|
+ jPanel.add(label, constrains);
|
|
|
|
|
+
|
|
|
|
|
+ label = new JLabel("状态");
|
|
|
|
|
+ label.setBorder(BorderFactory.createEtchedBorder());
|
|
|
|
|
+ constrains = new GridBagConstraints();
|
|
|
|
|
+ constrains.insets = new Insets(0, 0, 0, 0);
|
|
|
|
|
+ constrains.fill = GridBagConstraints.BOTH;
|
|
|
|
|
+ constrains.gridx = 1;
|
|
|
|
|
+ constrains.gridy = 0;
|
|
|
|
|
+ constrains.weightx = 25;
|
|
|
|
|
+ constrains.anchor = GridBagConstraints.NORTH;
|
|
|
|
|
+ jPanel.add(label, constrains);
|
|
|
|
|
+
|
|
|
|
|
+ label = new JLabel("启动");
|
|
|
|
|
+ label.setBorder(BorderFactory.createEtchedBorder());
|
|
|
|
|
+ constrains = new GridBagConstraints();
|
|
|
|
|
+ constrains.insets = new Insets(0, 0, 0, 0);
|
|
|
|
|
+ constrains.fill = GridBagConstraints.BOTH;
|
|
|
|
|
+ constrains.gridx = 2;
|
|
|
|
|
+ constrains.gridy = 0;
|
|
|
|
|
+ constrains.weightx = 10;
|
|
|
|
|
+ constrains.anchor = GridBagConstraints.NORTH;
|
|
|
|
|
+ jPanel.add(label, constrains);
|
|
|
|
|
+
|
|
|
|
|
+ label = new JLabel("关闭");
|
|
|
|
|
+ label.setBorder(BorderFactory.createEtchedBorder());
|
|
|
|
|
+ constrains = new GridBagConstraints();
|
|
|
|
|
+ constrains.insets = new Insets(0, 0, 0, 0);
|
|
|
|
|
+ constrains.fill = GridBagConstraints.BOTH;
|
|
|
|
|
+ constrains.gridx = 3;
|
|
|
|
|
+ constrains.gridy = 0;
|
|
|
|
|
+ constrains.weightx = 10;
|
|
|
|
|
+ constrains.anchor = GridBagConstraints.NORTH;
|
|
|
|
|
+ jPanel.add(label, constrains);
|
|
|
|
|
+
|
|
|
|
|
+ ApplicationSettingCache applicationSettingCache = (ApplicationSettingCache) SpringUtil.getSingleBean(ApplicationSettingCache.class);
|
|
|
|
|
+ List<ApplicationSetting> settings = applicationSettingCache.getSettings();
|
|
|
|
|
+ if (settings != null && settings.size() > 0) {
|
|
|
|
|
+ for(int i = 0; i < settings.size(); i ++) {
|
|
|
|
|
+ int rowIndex = (i + 1);
|
|
|
|
|
+ final ApplicationSetting setting = settings.get(i);
|
|
|
|
|
+
|
|
|
|
|
+ label = new JLabel(setting.getName());
|
|
|
|
|
+ constrains = new GridBagConstraints();
|
|
|
|
|
+ constrains.insets = new Insets(5, 5, 5, 5);
|
|
|
|
|
+ constrains.fill = GridBagConstraints.BOTH;
|
|
|
|
|
+ constrains.gridx = 0;
|
|
|
|
|
+ constrains.gridy = rowIndex;
|
|
|
|
|
+ constrains.weightx = 25;
|
|
|
|
|
+ jPanel.add(label, constrains);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ label = new JLabel(setting.getStatus());
|
|
|
|
|
+ constrains = new GridBagConstraints();
|
|
|
|
|
+ constrains.insets = new Insets(5, 5, 5, 5);
|
|
|
|
|
+ constrains.fill = GridBagConstraints.BOTH;
|
|
|
|
|
+ constrains.gridx = 1;
|
|
|
|
|
+ constrains.gridy = rowIndex;
|
|
|
|
|
+ constrains.weightx = 25;
|
|
|
|
|
+ jPanel.add(label, constrains);
|
|
|
|
|
+
|
|
|
|
|
+ statusLabels.put(setting, label);
|
|
|
|
|
+
|
|
|
|
|
+ JButton startButton = new JButton("启动");
|
|
|
|
|
+ constrains = new GridBagConstraints();
|
|
|
|
|
+ constrains.insets = new Insets(5, 5, 5, 5);
|
|
|
|
|
+ constrains.fill = GridBagConstraints.BOTH;
|
|
|
|
|
+ constrains.gridx = 2;
|
|
|
|
|
+ constrains.gridy = rowIndex;
|
|
|
|
|
+ constrains.weightx = 10;
|
|
|
|
|
+ jPanel.add(startButton, constrains);
|
|
|
|
|
+ startButton.addActionListener(new ActionListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void actionPerformed(ActionEvent e) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ExecuteWatchdog executeWatchdog = runProcess(setting.getStartBatFile());
|
|
|
|
|
+ setting.setExecuteWatchdog(executeWatchdog);
|
|
|
|
|
+ } catch (IOException e1) {
|
|
|
|
|
+ e1.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ JButton stopButton = new JButton("停止");
|
|
|
|
|
+ constrains = new GridBagConstraints();
|
|
|
|
|
+ constrains.insets = new Insets(5, 5, 5, 5);
|
|
|
|
|
+ constrains.fill = GridBagConstraints.BOTH;
|
|
|
|
|
+ constrains.gridx = 3;
|
|
|
|
|
+ constrains.gridy = rowIndex;
|
|
|
|
|
+ constrains.weightx = 10;
|
|
|
|
|
+ jPanel.add(stopButton, constrains);
|
|
|
|
|
+ stopButton.addActionListener(new ActionListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void actionPerformed(ActionEvent e) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ExecuteWatchdog executeWatchdog = runProcess(setting.getStopBatFile());
|
|
|
|
|
+ setting.setExecuteWatchdog(null);
|
|
|
|
|
+ } catch (IOException e1) {
|
|
|
|
|
+ e1.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 运行流程
|
|
|
|
|
+ * @param command "cmd.exe /c start c://example.exe"
|
|
|
|
|
+ * @return
|
|
|
|
|
+ * @throws IOException
|
|
|
|
|
+ */
|
|
|
|
|
+ private ExecuteWatchdog runProcess(String command) throws IOException {
|
|
|
|
|
+ final CommandLine cmdLine = CommandLine.parse(command);
|
|
|
|
|
+
|
|
|
|
|
+ final ExecuteWatchdog watchDog = new ExecuteWatchdog(Long.MAX_VALUE);
|
|
|
|
|
+
|
|
|
|
|
+ final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
|
|
|
|
|
+ DefaultExecutor executor = new DefaultExecutor();
|
|
|
|
|
+ executor.setWatchdog(watchDog);
|
|
|
|
|
+
|
|
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
|
+ executor.setStreamHandler(new PumpStreamHandler(baos, baos));
|
|
|
|
|
+
|
|
|
|
|
+ executor.execute(cmdLine, resultHandler);
|
|
|
|
|
+
|
|
|
|
|
+ //等待5秒。
|
|
|
|
|
+ try {
|
|
|
|
|
+ resultHandler.waitFor(5000);
|
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //这里开始的代码会被立即执行下去,因为上面的语句不会被阻塞。
|
|
|
|
|
+ final String result = baos.toString().trim();
|
|
|
|
|
+ logger.debug(result);//这个result就是ping输出的结果。如果是JAVA程序,抛出了异常,也被它获取。
|
|
|
|
|
+ return watchDog;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void actionPerformed(ActionEvent e) {
|
|
|
|
|
+ ApplicationSettingCache applicationSettingCache = (ApplicationSettingCache) SpringUtil.getSingleBean(ApplicationSettingCache.class);
|
|
|
|
|
+
|
|
|
|
|
+ List<ApplicationSetting> settings = applicationSettingCache.getSettings();
|
|
|
|
|
+ if (settings != null && settings.size() > 0) {
|
|
|
|
|
+ for(int i = 0; i < settings.size(); i ++) {
|
|
|
|
|
+ final ApplicationSetting setting = settings.get(i);
|
|
|
|
|
+ JLabel statusLabel = statusLabels.get(setting);
|
|
|
|
|
+ if(statusLabel != null) {
|
|
|
|
|
+ ExecuteWatchdog executeWatchdog = setting.getExecuteWatchdog();
|
|
|
|
|
+ if(executeWatchdog == null) {
|
|
|
|
|
+ statusLabel.setText("未运行");
|
|
|
|
|
+ }else{
|
|
|
|
|
+ statusLabel.setText("运行中");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|