package info.istlab.IoTP;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
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.util.ArrayList;
/**
* Hello world!
*
*/
public class App {
static String userhome;
static ArrayList<File> iotDirs;
static String workingDir;
public static String latestVersion;
public static Path execPath;
public static String downloadurl = "https://cit.istlab.info/IoTP/target/";
public static boolean isWindows;
public static String serialName;
static {
userhome = System.getProperty("user.home");
String os = System.getProperty("os.name").toLowerCase();
isWindows = os.contains("windows");
}
public static void main(String[] args) {
try {
App.execPath = getApplicationPath(new App().getClass());
} catch (URISyntaxException e) {
e.printStackTrace();
}
// IoTPフォルダを探す
iotDirs = new ArrayList<File>();
findDotGitFileRecursive(new File(userhome), iotDirs, 0); // 指定フォルダ以下の.gitを探索する
FolderSelector<File> fs = new FolderSelector<File>(iotDirs, "フォルダが複数みつかりました。フォルダを選択してください。",
"フォルダが1つもみつかりませんでした");
File ret = fs.getSelectedItem();
if (ret == null)
System.exit(0);
workingDir = ret.getAbsolutePath();
System.out.println(ret);
new Launcher(ret).setVisible(true);
}
public static void findDotGitFileRecursive(File dir, ArrayList<File> iotDirs, int level) {
if (level > 3)
return;
// System.out.println("checking " + dir.getAbsolutePath());
File[] list = dir.listFiles(new FileFilter() {
public boolean accept(File path) {
return (path.isDirectory());
}
});
if (list == null)
return;
for (File tempdir : list) {
// if (tempdir.getName().equals(".git")) {
if (tempdir.getName().equals("SampleSrc")) {
iotDirs.add(tempdir);
} else {
findDotGitFileRecursive(tempdir, iotDirs, level + 1);
}
}
}
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 + "]"); // [IoTP-0.18-launcher.jar]
// System.out.println("=" + execPath.getFileName() + "=");
if (execPath.getFileName().toString().equals("classes"))
return true; // exec:javaで起動しているので、へんな場所にダウンロードしないように、ダウンロード抑制する。
// if (latestVersion.indexOf("0-") > -1) { //デバッグ用:バージョン番号が 0.180 のように、0で終わる時は、常に更新するためfalseを返す
// return false;
// }
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;
ProcessBuilder processBuilder;
if (App.isWindows){
pathfile = pathfile.replaceAll("\\\\","\\\\\\\\");
com = "java -jar " + new File(pathfile).getName();
processBuilder = new ProcessBuilder("cmd", "/c", com);
} else {
com = "sleep 1 ; java -jar " + pathfile;
processBuilder = new ProcessBuilder("bash", "-c", com);
}
System.out.println(com);
try {
processBuilder.start();
} catch (Exception ex) {
}
Thread t = new Thread(Launcher.theapp);
t.start();
}
}