diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0841741
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.idea
+/out
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0841741
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.idea
+/out
diff --git a/CasicTimeGuard.iml b/CasicTimeGuard.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/CasicTimeGuard.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0841741
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.idea
+/out
diff --git a/CasicTimeGuard.iml b/CasicTimeGuard.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/CasicTimeGuard.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..e139a98
--- /dev/null
+++ b/META-INF/MANIFEST.MF
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Main-Class: com.casic.swing.ui.TimeGuardNtp
+
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0841741
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.idea
+/out
diff --git a/CasicTimeGuard.iml b/CasicTimeGuard.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/CasicTimeGuard.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..e139a98
--- /dev/null
+++ b/META-INF/MANIFEST.MF
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Main-Class: com.casic.swing.ui.TimeGuardNtp
+
diff --git a/image/dot_gray.png b/image/dot_gray.png
new file mode 100644
index 0000000..57254db
--- /dev/null
+++ b/image/dot_gray.png
Binary files differ
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0841741
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.idea
+/out
diff --git a/CasicTimeGuard.iml b/CasicTimeGuard.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/CasicTimeGuard.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..e139a98
--- /dev/null
+++ b/META-INF/MANIFEST.MF
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Main-Class: com.casic.swing.ui.TimeGuardNtp
+
diff --git a/image/dot_gray.png b/image/dot_gray.png
new file mode 100644
index 0000000..57254db
--- /dev/null
+++ b/image/dot_gray.png
Binary files differ
diff --git a/image/dot_green.png b/image/dot_green.png
new file mode 100644
index 0000000..05f439d
--- /dev/null
+++ b/image/dot_green.png
Binary files differ
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0841741
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.idea
+/out
diff --git a/CasicTimeGuard.iml b/CasicTimeGuard.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/CasicTimeGuard.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..e139a98
--- /dev/null
+++ b/META-INF/MANIFEST.MF
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Main-Class: com.casic.swing.ui.TimeGuardNtp
+
diff --git a/image/dot_gray.png b/image/dot_gray.png
new file mode 100644
index 0000000..57254db
--- /dev/null
+++ b/image/dot_gray.png
Binary files differ
diff --git a/image/dot_green.png b/image/dot_green.png
new file mode 100644
index 0000000..05f439d
--- /dev/null
+++ b/image/dot_green.png
Binary files differ
diff --git a/image/dot_red.png b/image/dot_red.png
new file mode 100644
index 0000000..bc64f7c
--- /dev/null
+++ b/image/dot_red.png
Binary files differ
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0841741
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.idea
+/out
diff --git a/CasicTimeGuard.iml b/CasicTimeGuard.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/CasicTimeGuard.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..e139a98
--- /dev/null
+++ b/META-INF/MANIFEST.MF
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Main-Class: com.casic.swing.ui.TimeGuardNtp
+
diff --git a/image/dot_gray.png b/image/dot_gray.png
new file mode 100644
index 0000000..57254db
--- /dev/null
+++ b/image/dot_gray.png
Binary files differ
diff --git a/image/dot_green.png b/image/dot_green.png
new file mode 100644
index 0000000..05f439d
--- /dev/null
+++ b/image/dot_green.png
Binary files differ
diff --git a/image/dot_red.png b/image/dot_red.png
new file mode 100644
index 0000000..bc64f7c
--- /dev/null
+++ b/image/dot_red.png
Binary files differ
diff --git a/src/com/casic/swing/ui/TimeGuardNtp.form b/src/com/casic/swing/ui/TimeGuardNtp.form
new file mode 100644
index 0000000..461c41d
--- /dev/null
+++ b/src/com/casic/swing/ui/TimeGuardNtp.form
@@ -0,0 +1,124 @@
+
+
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0841741
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.idea
+/out
diff --git a/CasicTimeGuard.iml b/CasicTimeGuard.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/CasicTimeGuard.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..e139a98
--- /dev/null
+++ b/META-INF/MANIFEST.MF
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Main-Class: com.casic.swing.ui.TimeGuardNtp
+
diff --git a/image/dot_gray.png b/image/dot_gray.png
new file mode 100644
index 0000000..57254db
--- /dev/null
+++ b/image/dot_gray.png
Binary files differ
diff --git a/image/dot_green.png b/image/dot_green.png
new file mode 100644
index 0000000..05f439d
--- /dev/null
+++ b/image/dot_green.png
Binary files differ
diff --git a/image/dot_red.png b/image/dot_red.png
new file mode 100644
index 0000000..bc64f7c
--- /dev/null
+++ b/image/dot_red.png
Binary files differ
diff --git a/src/com/casic/swing/ui/TimeGuardNtp.form b/src/com/casic/swing/ui/TimeGuardNtp.form
new file mode 100644
index 0000000..461c41d
--- /dev/null
+++ b/src/com/casic/swing/ui/TimeGuardNtp.form
@@ -0,0 +1,124 @@
+
+
diff --git a/src/com/casic/swing/ui/TimeGuardNtp.java b/src/com/casic/swing/ui/TimeGuardNtp.java
new file mode 100644
index 0000000..847f933
--- /dev/null
+++ b/src/com/casic/swing/ui/TimeGuardNtp.java
@@ -0,0 +1,159 @@
+package com.casic.swing.ui;
+
+import com.casic.swing.utils.CommandUtil;
+import com.casic.swing.utils.TimeOrDateUtil;
+
+import javax.swing.*;
+import javax.swing.border.EmptyBorder;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
+
+/**
+ * @author a203
+ */
+public class TimeGuardNtp {
+ private JPanel ntpPanel;
+ private JComboBox hostComboBox;
+ private JLabel timeValueLabel;
+ private JButton updateTimeButton;
+ private JLabel dotView;
+ private JLabel currentTimeLabel;
+ private JCheckBox autoCheckBox;
+ private JComboBox periodComboBox;
+
+ private static final String[] HOST_NAME = {"NTP服务器(上海)", "中国国家授时中心", "清华大学", "北京大学", "自定义"};
+ private static final String[] HOST_IP = {"ntp.api.bz", "210.72.145.44", "s1b.time.edu.cn", "s1c.time.edu.cn"};
+ /**
+ * 同步周期,单位:小时
+ */
+ private static final Integer[] PERIOD = {1, 6, 12, 24};
+ private boolean hasNtp = false;
+ private String host = HOST_IP[0];
+
+ public static void main(String[] args) {
+ TimeGuardNtp timeGuard = new TimeGuardNtp();
+ JFrame frame = new JFrame("TimeGuardNtp");
+ frame.setContentPane(timeGuard.ntpPanel);
+ frame.setResizable(false);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.pack();
+ frame.setSize(375, 240);
+ //居中
+ frame.setLocationRelativeTo(null);
+ frame.setVisible(true);
+ //内边距
+ timeGuard.ntpPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
+ timeGuard.dotView.setText("未同步");
+ timeGuard.dotView.setIcon(new ImageIcon("image/dot_gray.png"));
+
+ /**
+ * 时间间隔,单位为毫秒
+ * */
+ timeGuard.currentTimeLabel.setForeground(Color.BLUE);
+ new Timer(1000, new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ String systemTime = TimeOrDateUtil.timestampToTime(System.currentTimeMillis());
+ timeGuard.currentTimeLabel.setText(systemTime);
+ }
+ }).start();
+
+ //初始化JComboBox
+ initComBox(timeGuard);
+ timeGuard.hostComboBox.addItemListener(new ItemListener() {
+ @Override
+ public void itemStateChanged(ItemEvent e) {
+ String item = (String) e.getItem();
+ if (e.getStateChange() == ItemEvent.SELECTED) {
+ if ("自定义".equals(item)) {
+ String inputContent = JOptionPane.showInputDialog(timeGuard.ntpPanel, "请输入授时中心域名或者IP地址", "", JOptionPane.INFORMATION_MESSAGE);
+ if (inputContent.isEmpty()) {
+ JOptionPane.showMessageDialog(timeGuard.ntpPanel, "输入错误,请检查", "Runtime Error", JOptionPane.ERROR_MESSAGE);
+ } else {
+ timeGuard.host = inputContent;
+ }
+ } else {
+ for (int i = 0; i < HOST_NAME.length; i++) {
+ if (item.equals(HOST_NAME[i])) {
+ timeGuard.host = HOST_IP[i];
+ }
+ }
+
+ }
+ }
+ }
+ });
+
+ //检查环境
+ new SwingWorker() {
+
+ @Override
+ protected Boolean doInBackground() {
+ timeGuard.hasNtp = CommandUtil.checkEnv();
+ return timeGuard.hasNtp;
+ }
+
+ @Override
+ protected void done() {
+ if (!timeGuard.hasNtp) {
+ JOptionPane.showMessageDialog(timeGuard.ntpPanel, "未当前设备发现可用的NTP配置", "Runtime Error", JOptionPane.ERROR_MESSAGE);
+ //环境不对,关闭窗体
+ frame.dispose();
+ }
+ super.done();
+ }
+ }.execute();
+
+ timeGuard.autoCheckBox.addChangeListener(new ChangeListener() {
+ @Override
+ public void stateChanged(ChangeEvent e) {
+ JCheckBox checkBox = (JCheckBox) e.getSource();
+ timeGuard.updateTimeButton.setEnabled(!checkBox.isSelected());
+ timeGuard.periodComboBox.setEnabled(checkBox.isSelected());
+ }
+ });
+
+ timeGuard.periodComboBox.addItemListener(new ItemListener() {
+ @Override
+ public void itemStateChanged(ItemEvent e) {
+ int period = (Integer) e.getItem();
+ if (e.getStateChange() == ItemEvent.SELECTED) {
+ System.out.println(period + "h");
+ }
+ }
+ });
+
+ //按钮点击事件
+ timeGuard.updateTimeButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ String result = CommandUtil.ntpDate(timeGuard.host);
+ System.out.println("命令执行结果 ===> " + result);
+ boolean isSuccess = result.contains("step time server");
+ if (isSuccess) {
+ timeGuard.dotView.setIcon(new ImageIcon("image/dot_green.png"));
+ timeGuard.dotView.setText("同步成功");
+ timeGuard.timeValueLabel.setText(result);
+ } else {
+ timeGuard.dotView.setIcon(new ImageIcon("image/dot_red.png"));
+ timeGuard.dotView.setText("同步失败");
+ timeGuard.timeValueLabel.setText("");
+ }
+ }
+ });
+ }
+
+ private static void initComBox(TimeGuardNtp timeGuard) {
+ for (String s : HOST_NAME) {
+ timeGuard.hostComboBox.addItem(s);
+ }
+ for (Integer integer : PERIOD) {
+ timeGuard.periodComboBox.addItem(integer);
+ }
+ }
+}
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0841741
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.idea
+/out
diff --git a/CasicTimeGuard.iml b/CasicTimeGuard.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/CasicTimeGuard.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..e139a98
--- /dev/null
+++ b/META-INF/MANIFEST.MF
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Main-Class: com.casic.swing.ui.TimeGuardNtp
+
diff --git a/image/dot_gray.png b/image/dot_gray.png
new file mode 100644
index 0000000..57254db
--- /dev/null
+++ b/image/dot_gray.png
Binary files differ
diff --git a/image/dot_green.png b/image/dot_green.png
new file mode 100644
index 0000000..05f439d
--- /dev/null
+++ b/image/dot_green.png
Binary files differ
diff --git a/image/dot_red.png b/image/dot_red.png
new file mode 100644
index 0000000..bc64f7c
--- /dev/null
+++ b/image/dot_red.png
Binary files differ
diff --git a/src/com/casic/swing/ui/TimeGuardNtp.form b/src/com/casic/swing/ui/TimeGuardNtp.form
new file mode 100644
index 0000000..461c41d
--- /dev/null
+++ b/src/com/casic/swing/ui/TimeGuardNtp.form
@@ -0,0 +1,124 @@
+
+
diff --git a/src/com/casic/swing/ui/TimeGuardNtp.java b/src/com/casic/swing/ui/TimeGuardNtp.java
new file mode 100644
index 0000000..847f933
--- /dev/null
+++ b/src/com/casic/swing/ui/TimeGuardNtp.java
@@ -0,0 +1,159 @@
+package com.casic.swing.ui;
+
+import com.casic.swing.utils.CommandUtil;
+import com.casic.swing.utils.TimeOrDateUtil;
+
+import javax.swing.*;
+import javax.swing.border.EmptyBorder;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
+
+/**
+ * @author a203
+ */
+public class TimeGuardNtp {
+ private JPanel ntpPanel;
+ private JComboBox hostComboBox;
+ private JLabel timeValueLabel;
+ private JButton updateTimeButton;
+ private JLabel dotView;
+ private JLabel currentTimeLabel;
+ private JCheckBox autoCheckBox;
+ private JComboBox periodComboBox;
+
+ private static final String[] HOST_NAME = {"NTP服务器(上海)", "中国国家授时中心", "清华大学", "北京大学", "自定义"};
+ private static final String[] HOST_IP = {"ntp.api.bz", "210.72.145.44", "s1b.time.edu.cn", "s1c.time.edu.cn"};
+ /**
+ * 同步周期,单位:小时
+ */
+ private static final Integer[] PERIOD = {1, 6, 12, 24};
+ private boolean hasNtp = false;
+ private String host = HOST_IP[0];
+
+ public static void main(String[] args) {
+ TimeGuardNtp timeGuard = new TimeGuardNtp();
+ JFrame frame = new JFrame("TimeGuardNtp");
+ frame.setContentPane(timeGuard.ntpPanel);
+ frame.setResizable(false);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.pack();
+ frame.setSize(375, 240);
+ //居中
+ frame.setLocationRelativeTo(null);
+ frame.setVisible(true);
+ //内边距
+ timeGuard.ntpPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
+ timeGuard.dotView.setText("未同步");
+ timeGuard.dotView.setIcon(new ImageIcon("image/dot_gray.png"));
+
+ /**
+ * 时间间隔,单位为毫秒
+ * */
+ timeGuard.currentTimeLabel.setForeground(Color.BLUE);
+ new Timer(1000, new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ String systemTime = TimeOrDateUtil.timestampToTime(System.currentTimeMillis());
+ timeGuard.currentTimeLabel.setText(systemTime);
+ }
+ }).start();
+
+ //初始化JComboBox
+ initComBox(timeGuard);
+ timeGuard.hostComboBox.addItemListener(new ItemListener() {
+ @Override
+ public void itemStateChanged(ItemEvent e) {
+ String item = (String) e.getItem();
+ if (e.getStateChange() == ItemEvent.SELECTED) {
+ if ("自定义".equals(item)) {
+ String inputContent = JOptionPane.showInputDialog(timeGuard.ntpPanel, "请输入授时中心域名或者IP地址", "", JOptionPane.INFORMATION_MESSAGE);
+ if (inputContent.isEmpty()) {
+ JOptionPane.showMessageDialog(timeGuard.ntpPanel, "输入错误,请检查", "Runtime Error", JOptionPane.ERROR_MESSAGE);
+ } else {
+ timeGuard.host = inputContent;
+ }
+ } else {
+ for (int i = 0; i < HOST_NAME.length; i++) {
+ if (item.equals(HOST_NAME[i])) {
+ timeGuard.host = HOST_IP[i];
+ }
+ }
+
+ }
+ }
+ }
+ });
+
+ //检查环境
+ new SwingWorker() {
+
+ @Override
+ protected Boolean doInBackground() {
+ timeGuard.hasNtp = CommandUtil.checkEnv();
+ return timeGuard.hasNtp;
+ }
+
+ @Override
+ protected void done() {
+ if (!timeGuard.hasNtp) {
+ JOptionPane.showMessageDialog(timeGuard.ntpPanel, "未当前设备发现可用的NTP配置", "Runtime Error", JOptionPane.ERROR_MESSAGE);
+ //环境不对,关闭窗体
+ frame.dispose();
+ }
+ super.done();
+ }
+ }.execute();
+
+ timeGuard.autoCheckBox.addChangeListener(new ChangeListener() {
+ @Override
+ public void stateChanged(ChangeEvent e) {
+ JCheckBox checkBox = (JCheckBox) e.getSource();
+ timeGuard.updateTimeButton.setEnabled(!checkBox.isSelected());
+ timeGuard.periodComboBox.setEnabled(checkBox.isSelected());
+ }
+ });
+
+ timeGuard.periodComboBox.addItemListener(new ItemListener() {
+ @Override
+ public void itemStateChanged(ItemEvent e) {
+ int period = (Integer) e.getItem();
+ if (e.getStateChange() == ItemEvent.SELECTED) {
+ System.out.println(period + "h");
+ }
+ }
+ });
+
+ //按钮点击事件
+ timeGuard.updateTimeButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ String result = CommandUtil.ntpDate(timeGuard.host);
+ System.out.println("命令执行结果 ===> " + result);
+ boolean isSuccess = result.contains("step time server");
+ if (isSuccess) {
+ timeGuard.dotView.setIcon(new ImageIcon("image/dot_green.png"));
+ timeGuard.dotView.setText("同步成功");
+ timeGuard.timeValueLabel.setText(result);
+ } else {
+ timeGuard.dotView.setIcon(new ImageIcon("image/dot_red.png"));
+ timeGuard.dotView.setText("同步失败");
+ timeGuard.timeValueLabel.setText("");
+ }
+ }
+ });
+ }
+
+ private static void initComBox(TimeGuardNtp timeGuard) {
+ for (String s : HOST_NAME) {
+ timeGuard.hostComboBox.addItem(s);
+ }
+ for (Integer integer : PERIOD) {
+ timeGuard.periodComboBox.addItem(integer);
+ }
+ }
+}
diff --git a/src/com/casic/swing/utils/CommandUtil.java b/src/com/casic/swing/utils/CommandUtil.java
new file mode 100644
index 0000000..4217863
--- /dev/null
+++ b/src/com/casic/swing/utils/CommandUtil.java
@@ -0,0 +1,75 @@
+package com.casic.swing.utils;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.Charset;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author a203
+ */
+public class CommandUtil {
+ /**
+ * 检查当前系统是否已安装NTP
+ */
+ public static boolean checkEnv() {
+ try {
+ Process exec = Runtime.getRuntime().exec("ls");
+// Process exec = Runtime.getRuntime().exec("rpm -q ntp");
+ try {
+ //等待命令执行完成
+ exec.waitFor(5, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ String result = parseResult(exec.getInputStream());
+ System.out.println("命令执行结果 ===> " + result);
+ return !result.isEmpty();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ /**
+ * 安装NTP
+ * yum -y install ntp
+ * */
+
+ /**
+ * 授时同步
+ * ntpdate -u ntp.api.bz
+ */
+ public static String ntpDate(String host) {
+ try {
+ Process exec = Runtime.getRuntime().exec("ntpdate -u " + host);
+ try {
+ //等待命令执行完成
+ exec.waitFor(5, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ return parseResult(exec.getInputStream());
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return "";
+ }
+
+ /**
+ * 解析命令结果
+ */
+ private static String parseResult(InputStream inputStream) throws IOException {
+ // 读取输出流内容
+ InputStreamReader streamReader = new InputStreamReader(inputStream, Charset.defaultCharset());
+ BufferedReader reader = new BufferedReader(streamReader);
+ StringBuilder builder = new StringBuilder();
+ String line;
+ while ((line = reader.readLine()) != null) {
+ builder.append(line).append("\n");
+ }
+ return builder.toString();
+ }
+}
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0841741
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.idea
+/out
diff --git a/CasicTimeGuard.iml b/CasicTimeGuard.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/CasicTimeGuard.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..e139a98
--- /dev/null
+++ b/META-INF/MANIFEST.MF
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Main-Class: com.casic.swing.ui.TimeGuardNtp
+
diff --git a/image/dot_gray.png b/image/dot_gray.png
new file mode 100644
index 0000000..57254db
--- /dev/null
+++ b/image/dot_gray.png
Binary files differ
diff --git a/image/dot_green.png b/image/dot_green.png
new file mode 100644
index 0000000..05f439d
--- /dev/null
+++ b/image/dot_green.png
Binary files differ
diff --git a/image/dot_red.png b/image/dot_red.png
new file mode 100644
index 0000000..bc64f7c
--- /dev/null
+++ b/image/dot_red.png
Binary files differ
diff --git a/src/com/casic/swing/ui/TimeGuardNtp.form b/src/com/casic/swing/ui/TimeGuardNtp.form
new file mode 100644
index 0000000..461c41d
--- /dev/null
+++ b/src/com/casic/swing/ui/TimeGuardNtp.form
@@ -0,0 +1,124 @@
+
+
diff --git a/src/com/casic/swing/ui/TimeGuardNtp.java b/src/com/casic/swing/ui/TimeGuardNtp.java
new file mode 100644
index 0000000..847f933
--- /dev/null
+++ b/src/com/casic/swing/ui/TimeGuardNtp.java
@@ -0,0 +1,159 @@
+package com.casic.swing.ui;
+
+import com.casic.swing.utils.CommandUtil;
+import com.casic.swing.utils.TimeOrDateUtil;
+
+import javax.swing.*;
+import javax.swing.border.EmptyBorder;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
+
+/**
+ * @author a203
+ */
+public class TimeGuardNtp {
+ private JPanel ntpPanel;
+ private JComboBox hostComboBox;
+ private JLabel timeValueLabel;
+ private JButton updateTimeButton;
+ private JLabel dotView;
+ private JLabel currentTimeLabel;
+ private JCheckBox autoCheckBox;
+ private JComboBox periodComboBox;
+
+ private static final String[] HOST_NAME = {"NTP服务器(上海)", "中国国家授时中心", "清华大学", "北京大学", "自定义"};
+ private static final String[] HOST_IP = {"ntp.api.bz", "210.72.145.44", "s1b.time.edu.cn", "s1c.time.edu.cn"};
+ /**
+ * 同步周期,单位:小时
+ */
+ private static final Integer[] PERIOD = {1, 6, 12, 24};
+ private boolean hasNtp = false;
+ private String host = HOST_IP[0];
+
+ public static void main(String[] args) {
+ TimeGuardNtp timeGuard = new TimeGuardNtp();
+ JFrame frame = new JFrame("TimeGuardNtp");
+ frame.setContentPane(timeGuard.ntpPanel);
+ frame.setResizable(false);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.pack();
+ frame.setSize(375, 240);
+ //居中
+ frame.setLocationRelativeTo(null);
+ frame.setVisible(true);
+ //内边距
+ timeGuard.ntpPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
+ timeGuard.dotView.setText("未同步");
+ timeGuard.dotView.setIcon(new ImageIcon("image/dot_gray.png"));
+
+ /**
+ * 时间间隔,单位为毫秒
+ * */
+ timeGuard.currentTimeLabel.setForeground(Color.BLUE);
+ new Timer(1000, new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ String systemTime = TimeOrDateUtil.timestampToTime(System.currentTimeMillis());
+ timeGuard.currentTimeLabel.setText(systemTime);
+ }
+ }).start();
+
+ //初始化JComboBox
+ initComBox(timeGuard);
+ timeGuard.hostComboBox.addItemListener(new ItemListener() {
+ @Override
+ public void itemStateChanged(ItemEvent e) {
+ String item = (String) e.getItem();
+ if (e.getStateChange() == ItemEvent.SELECTED) {
+ if ("自定义".equals(item)) {
+ String inputContent = JOptionPane.showInputDialog(timeGuard.ntpPanel, "请输入授时中心域名或者IP地址", "", JOptionPane.INFORMATION_MESSAGE);
+ if (inputContent.isEmpty()) {
+ JOptionPane.showMessageDialog(timeGuard.ntpPanel, "输入错误,请检查", "Runtime Error", JOptionPane.ERROR_MESSAGE);
+ } else {
+ timeGuard.host = inputContent;
+ }
+ } else {
+ for (int i = 0; i < HOST_NAME.length; i++) {
+ if (item.equals(HOST_NAME[i])) {
+ timeGuard.host = HOST_IP[i];
+ }
+ }
+
+ }
+ }
+ }
+ });
+
+ //检查环境
+ new SwingWorker() {
+
+ @Override
+ protected Boolean doInBackground() {
+ timeGuard.hasNtp = CommandUtil.checkEnv();
+ return timeGuard.hasNtp;
+ }
+
+ @Override
+ protected void done() {
+ if (!timeGuard.hasNtp) {
+ JOptionPane.showMessageDialog(timeGuard.ntpPanel, "未当前设备发现可用的NTP配置", "Runtime Error", JOptionPane.ERROR_MESSAGE);
+ //环境不对,关闭窗体
+ frame.dispose();
+ }
+ super.done();
+ }
+ }.execute();
+
+ timeGuard.autoCheckBox.addChangeListener(new ChangeListener() {
+ @Override
+ public void stateChanged(ChangeEvent e) {
+ JCheckBox checkBox = (JCheckBox) e.getSource();
+ timeGuard.updateTimeButton.setEnabled(!checkBox.isSelected());
+ timeGuard.periodComboBox.setEnabled(checkBox.isSelected());
+ }
+ });
+
+ timeGuard.periodComboBox.addItemListener(new ItemListener() {
+ @Override
+ public void itemStateChanged(ItemEvent e) {
+ int period = (Integer) e.getItem();
+ if (e.getStateChange() == ItemEvent.SELECTED) {
+ System.out.println(period + "h");
+ }
+ }
+ });
+
+ //按钮点击事件
+ timeGuard.updateTimeButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ String result = CommandUtil.ntpDate(timeGuard.host);
+ System.out.println("命令执行结果 ===> " + result);
+ boolean isSuccess = result.contains("step time server");
+ if (isSuccess) {
+ timeGuard.dotView.setIcon(new ImageIcon("image/dot_green.png"));
+ timeGuard.dotView.setText("同步成功");
+ timeGuard.timeValueLabel.setText(result);
+ } else {
+ timeGuard.dotView.setIcon(new ImageIcon("image/dot_red.png"));
+ timeGuard.dotView.setText("同步失败");
+ timeGuard.timeValueLabel.setText("");
+ }
+ }
+ });
+ }
+
+ private static void initComBox(TimeGuardNtp timeGuard) {
+ for (String s : HOST_NAME) {
+ timeGuard.hostComboBox.addItem(s);
+ }
+ for (Integer integer : PERIOD) {
+ timeGuard.periodComboBox.addItem(integer);
+ }
+ }
+}
diff --git a/src/com/casic/swing/utils/CommandUtil.java b/src/com/casic/swing/utils/CommandUtil.java
new file mode 100644
index 0000000..4217863
--- /dev/null
+++ b/src/com/casic/swing/utils/CommandUtil.java
@@ -0,0 +1,75 @@
+package com.casic.swing.utils;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.Charset;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author a203
+ */
+public class CommandUtil {
+ /**
+ * 检查当前系统是否已安装NTP
+ */
+ public static boolean checkEnv() {
+ try {
+ Process exec = Runtime.getRuntime().exec("ls");
+// Process exec = Runtime.getRuntime().exec("rpm -q ntp");
+ try {
+ //等待命令执行完成
+ exec.waitFor(5, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ String result = parseResult(exec.getInputStream());
+ System.out.println("命令执行结果 ===> " + result);
+ return !result.isEmpty();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ /**
+ * 安装NTP
+ * yum -y install ntp
+ * */
+
+ /**
+ * 授时同步
+ * ntpdate -u ntp.api.bz
+ */
+ public static String ntpDate(String host) {
+ try {
+ Process exec = Runtime.getRuntime().exec("ntpdate -u " + host);
+ try {
+ //等待命令执行完成
+ exec.waitFor(5, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ return parseResult(exec.getInputStream());
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return "";
+ }
+
+ /**
+ * 解析命令结果
+ */
+ private static String parseResult(InputStream inputStream) throws IOException {
+ // 读取输出流内容
+ InputStreamReader streamReader = new InputStreamReader(inputStream, Charset.defaultCharset());
+ BufferedReader reader = new BufferedReader(streamReader);
+ StringBuilder builder = new StringBuilder();
+ String line;
+ while ((line = reader.readLine()) != null) {
+ builder.append(line).append("\n");
+ }
+ return builder.toString();
+ }
+}
diff --git a/src/com/casic/swing/utils/TimeOrDateUtil.java b/src/com/casic/swing/utils/TimeOrDateUtil.java
new file mode 100644
index 0000000..7adc5a6
--- /dev/null
+++ b/src/com/casic/swing/utils/TimeOrDateUtil.java
@@ -0,0 +1,18 @@
+package com.casic.swing.utils;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * @author a203
+ */
+public class TimeOrDateUtil {
+
+ /**
+ * 时间戳转时间
+ */
+ public static String timestampToTime(long millSeconds) {
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ return format.format(new Date(millSeconds));
+ }
+}