All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.becauseQA.window.utils.RestartUtils Maven / Gradle / Ivy

package com.github.becauseQA.window.utils;

/*-
 * false
 * commons-window
 * sectionDelimiter
 * Copyright (C) 2016 Alter Hu
 * sectionDelimiter
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;

public class RestartUtils {
	

	 	private static final String JAVA_HOME = "java.home";
	 	private static final String JAVA_CLASS_PATH = "java.class.path";

	 	private static final String KSE_INSTALL_DIR = "kse.install.dir";

	 	private static final String KSE_EXE = "kse.exe";
	 	private static final String KSE_APP = "kse.app";
	 	private static final String KSE_JAR = "kse.jar";


	 	/**
	 	 * Restart KeyStore Explorer in the same manner in which it was started.
	 	 */
	 	public static void restart() {
	 		if (System.getProperty(KSE_EXE) != null) {
	 			restartAsKseExe();
	 		} else if (System.getProperty(KSE_APP) != null) {
	 			restartAsKseApp();
	 		} else if (System.getProperty(JAVA_CLASS_PATH).equals(KSE_JAR)) {
	 			restartAsKseJar();
	 		} else {
	 			restartAsKseClass();
	 		}
	 	}

	 	private static void restartAsKseExe() {
	 		File kseInstallDir = new File(System.getProperty(KSE_INSTALL_DIR));

	 		File kseExe = new File(kseInstallDir, KSE_EXE);

	 		String toExec[] = new String[] { kseExe.getPath() };

	 		try {
	 			Runtime.getRuntime().exec(toExec);
	 		} catch (IOException ex) {
	 			ex.printStackTrace(); // Ignore
	 		}
	 	}

	 	private static void restartAsKseJar() {
	 		File javaBin = new File(new File(System.getProperty(JAVA_HOME), "bin"), "java");

	 		File kseInstallDir = new File(System.getProperty(KSE_INSTALL_DIR));

	 		File kseJar = new File(kseInstallDir, KSE_JAR);

	 		String toExec[] = new String[] { javaBin.getPath(), "-jar", kseJar.getPath() };

	 		try {
	 			Runtime.getRuntime().exec(toExec);
	 		} catch (IOException ex) {
	 			ex.printStackTrace(); // Ignore
	 		}
	 	}

	 	private static void restartAsKseApp() {
	 		File kseInstallDir = new File(System.getProperty(KSE_INSTALL_DIR));

	 		String kseApp = MessageFormat.format("{0} {1}.app", "", "teee");

	 		File javaAppStub = new File(new File(new File(new File(kseInstallDir, kseApp), "Contents"), "MacOS"),
	 				"JavaApplicationStub");

	 		String toExec[] = new String[] { javaAppStub.getPath() };

	 		try {
	 			Runtime.getRuntime().exec(toExec);
	 		} catch (IOException ex) {
	 			ex.printStackTrace(); // Ignore
	 		}
	 	}

	 	private static void restartAsKseClass() {
	 		File javaBin = new File(new File(System.getProperty(JAVA_HOME), "bin"), "java");

	 		String kseClasspath = System.getProperty(JAVA_CLASS_PATH);

	 		String toExec[] = new String[] { javaBin.getPath(), "-classpath", kseClasspath, "" };

	 		try {
	 			Runtime.getRuntime().exec(toExec);
	 		} catch (IOException ex) {
	 			ex.printStackTrace(); // Ignore
	 		}
	 	}
	 }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy