package com.haxademic.core.system;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;

import processing.core.PApplet;

import com.haxademic.core.app.P;

public class AppRestart {

	/** 
	 * Sun property pointing the main class and its arguments. 
	 * Might not be defined on non Hotspot VM implementations.
	 */
	public static final String SUN_JAVA_COMMAND = "sun.java.command";

	public static void restart( PApplet p ) {
		try {
			P.println("Attempting to restart...");
			restartApplication( p, null);
		} catch (IOException e) {
			P.println("Error: couldn't restart. crap.");
		}
	}

	/**
	 * Restart the current Java application - currently this only seems to work once and fails after that...
	 * @param runBeforeRestart some custom code to be run before restarting
	 * @throws IOException
	 */
	public static void restartApplication( PApplet p, Runnable runBeforeRestart) throws IOException {
		try {
			// java binary
			String java = System.getProperty("java.home") + "/bin/java";
			// vm arguments
			//			List<String> vmArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
			StringBuffer vmArgsOneLine = new StringBuffer();
			for (String arg : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
				// if it's the agent argument : we ignore it otherwise the
				// address of the old application and the new one will be in conflict
				if (!arg.contains("-agentlib")) {
					vmArgsOneLine.append(arg);
					vmArgsOneLine.append(" ");
				}
			}
			// init the command to execute, add the vm args
			final StringBuffer cmd = new StringBuffer("" + java + " " + vmArgsOneLine);

			// program main and program arguments
			String[] mainCommand = System.getProperty(SUN_JAVA_COMMAND).split(" ");
			// program main is a jar
			if (mainCommand[0].endsWith(".jar")) {
				// if it's a jar, add -jar mainJar
				cmd.append("-jar " + new File(mainCommand[0]).getPath());
			} else {
				// else it's a .class, add the classpath and mainClass
				cmd.append("-cp " + System.getProperty("java.class.path") + " " + mainCommand[0]);
			}
			// finally add program arguments
			for (int i = 1; i < mainCommand.length; i++) {
				cmd.append(" ");
				cmd.append(mainCommand[i]);
			}
			

			// execute the command in a shutdown hook, to be sure that all the
			// resources have been disposed before restarting the application
			Runtime.getRuntime().addShutdownHook(new Thread() {
				@Override
				public void run() {
					try {
						// FileUtil.writeTextToFile( FileUtil.getHaxademicOutputPath() + "" + SystemUtil.getTimestamp(P.p)+"-restart-file.txt", cmd.toString());
						Runtime.getRuntime().exec(cmd.toString());
					} catch (IOException e) {
						// FileUtil.writeTextToFile( FileUtil.getHaxademicOutputPath() + "" + SystemUtil.getTimestamp(P.p)+"-error-file.txt", e.toString());
						e.printStackTrace();
					}
				}
			});
			// execute some custom code before restarting
			if (runBeforeRestart!= null) {
				runBeforeRestart.run();
			}
			// exit
			System.exit(0);
		} catch (Exception e) {
			// something went wrong
			// FileUtil.writeTextToFile( FileUtil.getHaxademicOutputPath() + "" + SystemUtil.getTimestamp(P.p)+"-error-file.txt", e.toString());
			throw new IOException("Error while trying to restart the application", e);
		}
	}

}

@@ -2510,363 +2510,81 @@
 %09%09%09%09
-// FileUtil.writeTextToFile( FileUtil.getHaxademicOutputPath() + %22%22 + SystemUtil.getTimestamp(P.p)+%22-restart-file.txt%22, cmd.toString());%0A%09%09%09%09%09%09Runtime.getRuntime().exec(cmd.toString());%0A%09%09%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09%09%09// FileUtil.writeTextToFile( FileUtil.getHaxademicOutputPath() + %22%22 + SystemUtil.getTimestamp(P.p)+%22-error-file.txt%22, e.toString());
+Runtime.getRuntime().exec(cmd.toString());%0A%09%09%09%09%09%7D catch (IOException e) %7B
 %0A%09%09%09
@@ -2830,144 +2830,8 @@
 ong%0A
-%09%09%09// FileUtil.writeTextToFile( FileUtil.getHaxademicOutputPath() + %22%22 + SystemUtil.getTimestamp(P.p)+%22-error-file.txt%22, e.toString());%0A
 %09%09%09t

Remove comments