diff --git a/pom.xml b/pom.xml index f4fdbab..18255c8 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ istlab.KisoJikkenNWP KisoJikkenNWP jar - 0.67 + 0.68 KisoJikkenNWP http://maven.apache.org diff --git a/src/main/java/istlab/KisoJikken/App.java b/src/main/java/istlab/KisoJikken/App.java index 1ce5d44..1b18461 100644 --- a/src/main/java/istlab/KisoJikken/App.java +++ b/src/main/java/istlab/KisoJikken/App.java @@ -1,9 +1,22 @@ package istlab.KisoJikken; +import java.io.BufferedReader; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; +import java.nio.channels.ReadableByteChannel; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; +import java.security.CodeSource; +import java.security.ProtectionDomain; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; @@ -15,10 +28,35 @@ */ public class App { public static String userhome; - public static String nwpsrc = File.separator+"NWP"+File.separator+"src"; + public static String nwpsrc = File.separator + "NWP" + File.separator + "src"; public static String gitrepos = "https://git.istlab.info/git/miura250/NWP.git"; + public static String latestVersion; + public static Path execPath; + public static String downloadurl = "https://cit.istlab.info/KisoJikkenNWP/target/"; + + public App() { + + } public static void main(String[] args) { + try { + App.execPath = getApplicationPath(new App().getClass()); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + + // 最新版かどうかチェック + if (isLatest()) { + System.out.println("Different! " + latestVersion + " from " + execPath.getFileName().toString()); + String pathfile = download(); + if (pathfile != null) { + int res = JOptionPane.showConfirmDialog(null, "アプリの再起動をしますか?"); + if (res == JOptionPane.YES_OPTION) { + reboot(pathfile); + } + } + } + // ファイルがあるかチェック userhome = System.getProperty("user.home"); getNWPifnotexist(false); @@ -34,11 +72,82 @@ } - public static void getNWPifnotexist(boolean showMessageIfExist){ + public static Path getApplicationPath(Class cls) throws URISyntaxException { + ProtectionDomain pd = cls.getProtectionDomain(); + CodeSource cs = pd.getCodeSource(); + URL location = cs.getLocation(); + URI uri = location.toURI(); + Path path = Paths.get(uri); + return path; + } + + public static boolean isLatest() { + // 最初に、ダウンロードサイトから最新版の情報を得る + // 更新があれば、ダウンロードし、ファイルを表示する。 + StringBuilder sb = new StringBuilder(); + latestVersion = null; + try { + URL url = new URL(downloadurl); + InputStream is = url.openConnection().getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + String line = null; + while ((line = reader.readLine()) != null) { + if (latestVersion == null) { + latestVersion = line.trim(); + System.out.println(line); + } + sb.append(line + "\n"); + } + reader.close(); + } catch (Exception ex) { + } + // System.out.println(sb.toString()); + // 同じかどうかチェック + // System.out.println("[" + latestVersion + "]"); + // System.out.println("=" + execPath.getFileName() + "="); + if (execPath.getFileName().toString().equals("classes")) + return true; + return latestVersion.equals(execPath.getFileName().toString()); + } + + // ダウンロードして実行パーミッションをつける + public static String download() { + try { + URL url = new URL(downloadurl + "/" + latestVersion); + ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream()); + FileOutputStream fileOutputStream = new FileOutputStream(latestVersion); + FileChannel fileChannel = fileOutputStream.getChannel(); + fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE); + fileChannel.close(); + fileOutputStream.close(); + + File execjar = new File(execPath.getParent().toString() + File.separator + latestVersion); + execjar.setExecutable(true); + return execjar.getAbsolutePath(); + } catch (Exception ex) { + } finally { + } + return null; + } + + public static void reboot(String pathfile) { + String com = "sleep 2 ; java -jar " + pathfile; + System.out.println(com); + ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", com); + try { + processBuilder.start(); + System.exit(0); + }catch(Exception ex){ + + } + } + + public static void getNWPifnotexist(boolean showMessageIfExist) { if (Files.notExists(Paths.get(userhome + nwpsrc))) { - int res = JOptionPane.showConfirmDialog(Launcher.theapp, "NWP実験用のファイルが " + userhome + nwpsrc + " にありません。ダウンロードしますか?"); + int res = JOptionPane.showConfirmDialog(Launcher.theapp, + "NWP実験用のファイルが " + userhome + nwpsrc + " にありません。ダウンロードしますか?"); if (res == JOptionPane.YES_OPTION) { - System.out.println("cd ; git clone "+gitrepos+" を実行。"); + System.out.println("cd ; git clone " + gitrepos + " を実行。"); ProcessBuilder processBuilder = new ProcessBuilder("git", "clone", gitrepos); processBuilder.directory(new File(userhome)); processBuilder.inheritIO(); @@ -50,11 +159,13 @@ e1.printStackTrace(); } } else { - System.out.println("cd ; git clone "+gitrepos+" を実行してください。"); + System.out.println("cd ; git clone " + gitrepos + " を実行してください。"); System.exit(0); } } else { - if (showMessageIfExist) JOptionPane.showMessageDialog(Launcher.theapp, "NWP実験用のファイルが " + userhome + nwpsrc + " にすでにあるため、ダウンロードをキャンセルしました。"); + if (showMessageIfExist) + JOptionPane.showMessageDialog(Launcher.theapp, + "NWP実験用のファイルが " + userhome + nwpsrc + " にすでにあるため、ダウンロードをキャンセルしました。"); } } } diff --git a/src/main/java/istlab/KisoJikken/Launcher.java b/src/main/java/istlab/KisoJikken/Launcher.java index 91ffccc..5971cfb 100644 --- a/src/main/java/istlab/KisoJikken/Launcher.java +++ b/src/main/java/istlab/KisoJikken/Launcher.java @@ -5,22 +5,10 @@ import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; -import java.io.BufferedReader; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.net.URI; import java.net.URISyntaxException; -import java.net.URL; -import java.nio.channels.Channels; -import java.nio.channels.FileChannel; -import java.nio.channels.ReadableByteChannel; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.security.CodeSource; -import java.security.ProtectionDomain; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; @@ -41,9 +29,7 @@ public class Launcher extends JFrame implements MouseInputListener, KeyListener { public static Launcher theapp; - public static String version = "0.67"; - public static String downloadurl = "https://cit.istlab.info/KisoJikkenNWP/target/"; - public static Path execPath; + public static String version = "0.68"; // JPanel mainP; File root; JTree tree; @@ -59,11 +45,6 @@ // mainP = new JPanel(); file2editor = new Hashtable(); theapp = this; - try { - execPath = getApplicationPath(this.getClass()); - } catch (URISyntaxException e) { - e.printStackTrace(); - } if (args.length > 0) root = new MyFile(args[0]); @@ -98,12 +79,12 @@ mi.addActionListener(ae -> JOptionPane.showMessageDialog(this, "Kiso NWP Launcher v" + version)); menu.add(mi); - mi = new JMenuItem("download latest"); + mi = new JMenuItem("最新版をチェック"); mi.addActionListener(ae -> downloadLatest()); menu.add(mi); mi = new JMenuItem("open download site"); - mi.addActionListener(ae -> openURL(downloadurl)); + mi.addActionListener(ae -> openURL(App.downloadurl)); menu.add(mi); mi = new JMenuItem("open JAR folder"); @@ -114,7 +95,7 @@ mi.addActionListener(ae -> System.exit(0)); menu.add(mi); - menu = new JMenu("File"); /**************************************************** */ + menu = new JMenu("File"); /**************************************************** */ menuBar.add(menu); jcbmi = new JCheckBoxMenuItem("exclude class files from filetree"); @@ -135,8 +116,7 @@ mi.addActionListener(ae -> App.getNWPifnotexist(true)); menu.add(mi); - - menu = new JMenu("Process"); /**************************************************** */ + menu = new JMenu("Process"); /**************************************************** */ menuBar.add(menu); mi = new JMenuItem("stop alive"); @@ -147,11 +127,11 @@ mi.addActionListener(ae -> stopProcess(true)); menu.add(mi); - mi = new JMenuItem("jps"); + mi = new JMenuItem("jps (Javaのプロセス確認)"); mi.addActionListener(ae -> runCommand(ae.getActionCommand(), null)); menu.add(mi); - mi = new JMenuItem("killall java"); + mi = new JMenuItem("killall java (Javaプロセスをすべて強制終了)"); mi.addActionListener(ae -> runCommand(ae.getActionCommand(), null)); menu.add(mi); @@ -161,17 +141,18 @@ setSize(310, 600); } - public void renameOrDeleteNWPFolder(){ + public void renameOrDeleteNWPFolder() { int res = JOptionPane.showConfirmDialog(this, "NWPフォルダを削除するなら「いいえ」、『NWP_日付_時刻』にフォルダ名を変更するなら「はい」を押してください。"); - if (res==JOptionPane.YES_OPTION){ + if (res == JOptionPane.YES_OPTION) { SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd_HHmm"); - runCommand("mv NWP NWP_"+sdf.format(new Date()), App.userhome); + runCommand("mv NWP NWP_" + sdf.format(new Date()), App.userhome); reloadFiles(); - } else if (res==JOptionPane.NO_OPTION){ + } else if (res == JOptionPane.NO_OPTION) { runCommand("rm -fr ~/NWP", App.userhome); reloadFiles(); } } + public void runCommand(String cmd, String chdir) { System.out.println(cmd); new CommandRunner(cmd, chdir).startstop(); @@ -204,55 +185,24 @@ } public void downloadLatest() { - // 最初に、ダウンロードサイトから最新版の情報を得る - // 更新があれば、ダウンロードし、ファイルを表示する。 - StringBuilder sb = new StringBuilder(); - String latestVersion = null; - try { - URL url = new URL(downloadurl); - InputStream is = url.openConnection().getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(is)); - String line = null; - while ((line = reader.readLine()) != null) { - if (latestVersion == null) - latestVersion = line.trim(); - System.out.println(line); - sb.append(line + "\n"); - } - reader.close(); - } catch (Exception ex) { - } - // System.out.println(sb.toString()); - // 同じかどうかチェック - System.out.println("[" + latestVersion + "]"); - System.out.println("=" + execPath.getFileName() + "="); + boolean isLatest = App.isLatest(); - if (latestVersion.equals(execPath.getFileName().toString())) { - JOptionPane.showMessageDialog(this, "This is latest version : v" + version+"\n(download folder: "+execPath.getParent().toString()+")"); + // if () { + if (isLatest) { + JOptionPane.showMessageDialog(this, + "すでに最新版です : v" + version + "\n(download folder: " + App.execPath.getParent().toString() + ")"); } else { - int res = JOptionPane.showConfirmDialog(this, "Really download new version \n(" + latestVersion + ") ?\n(download folder: "+execPath.getParent().toString()+")"); + int res = JOptionPane.showConfirmDialog(this, "最新版 \n(" + App.latestVersion + ") をダウンロードする?\n(download folder: " + + App.execPath.getParent().toString() + ")"); if (res == JOptionPane.YES_OPTION) { - try { - URL url = new URL(downloadurl + "/" + latestVersion); - ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream()); - FileOutputStream fileOutputStream = new FileOutputStream(latestVersion); - FileChannel fileChannel = fileOutputStream.getChannel(); - fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE); - fileChannel.close(); - fileOutputStream.close(); - - File execjar = new File(execPath.getParent().toString() + File.separator + latestVersion); - execjar.setExecutable(true); - } catch (Exception ex) { - } finally { - } + App.download(); openExecPath(); } } } public void openExecPath() { - File execFolder = execPath.getParent().toFile(); + File execFolder = App.execPath.getParent().toFile(); openFolder(execFolder); } @@ -274,15 +224,6 @@ } } - public static Path getApplicationPath(Class cls) throws URISyntaxException { - ProtectionDomain pd = cls.getProtectionDomain(); - CodeSource cs = pd.getCodeSource(); - URL location = cs.getLocation(); - URI uri = location.toURI(); - Path path = Paths.get(uri); - return path; - } - @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { @@ -373,4 +314,3 @@ } } -