001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 005import static org.openstreetmap.josm.tools.I18n.tr; 006import static org.openstreetmap.josm.tools.Utils.getSystemProperty; 007 008import java.awt.event.ActionEvent; 009import java.awt.event.KeyEvent; 010import java.io.File; 011import java.io.IOException; 012import java.lang.management.ManagementFactory; 013import java.util.ArrayList; 014import java.util.Arrays; 015import java.util.Collection; 016import java.util.List; 017 018import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec; 019import org.openstreetmap.josm.gui.MainApplication; 020import org.openstreetmap.josm.gui.io.SaveLayersDialog; 021import org.openstreetmap.josm.spi.preferences.Config; 022import org.openstreetmap.josm.tools.ImageProvider; 023import org.openstreetmap.josm.tools.ImageProvider.ImageSizes; 024import org.openstreetmap.josm.tools.Logging; 025import org.openstreetmap.josm.tools.PlatformManager; 026import org.openstreetmap.josm.tools.Shortcut; 027 028/** 029 * Restarts JOSM as it was launched. Comes from "restart" plugin, originally written by Upliner. 030 * <br><br> 031 * Mechanisms have been improved based on #8561 discussions and 032 * <a href="http://lewisleo.blogspot.jp/2012/08/programmatically-restart-java.html">this article</a>. 033 * @since 5857 034 */ 035public class RestartAction extends JosmAction { 036 037 // AppleScript to restart OS X package 038 private static final String RESTART_APPLE_SCRIPT = 039 "tell application \"System Events\"\n" 040 + "repeat until not (exists process \"JOSM\")\n" 041 + "delay 0.2\n" 042 + "end repeat\n" 043 + "end tell\n" 044 + "tell application \"JOSM\" to activate"; 045 046 /** 047 * Constructs a new {@code RestartAction}. 048 */ 049 public RestartAction() { 050 super(tr("Restart"), "restart", tr("Restart the application."), 051 Shortcut.registerShortcut("file:restart", tr("File: {0}", tr("Restart")), KeyEvent.VK_J, Shortcut.ALT_CTRL_SHIFT), false); 052 setHelpId(ht("/Action/Restart")); 053 putValue("toolbar", "action/restart"); 054 if (MainApplication.getToolbar() != null) { 055 MainApplication.getToolbar().register(this); 056 } 057 setEnabled(isRestartSupported()); 058 } 059 060 @Override 061 public void actionPerformed(ActionEvent e) { 062 try { 063 restartJOSM(); 064 } catch (IOException ex) { 065 Logging.error(ex); 066 } 067 } 068 069 /** 070 * Determines if restarting the application should be possible on this platform. 071 * @return {@code true} if the mandatory system property {@code sun.java.command} is defined, {@code false} otherwise. 072 * @since 5951 073 */ 074 public static boolean isRestartSupported() { 075 return getSystemProperty("sun.java.command") != null; 076 } 077 078 /** 079 * Restarts the current Java application. 080 * @throws IOException in case of any I/O error 081 */ 082 public static void restartJOSM() throws IOException { 083 // If JOSM has been started with property 'josm.restart=true' this means 084 // it is executed by a start script that can handle restart. 085 // Request for restart is indicated by exit code 9. 086 String scriptRestart = getSystemProperty("josm.restart"); 087 if ("true".equals(scriptRestart)) { 088 MainApplication.exitJosm(true, 9, SaveLayersDialog.Reason.RESTART); 089 } 090 091 if (isRestartSupported() && !MainApplication.exitJosm(false, 0, SaveLayersDialog.Reason.RESTART)) return; 092 final List<String> cmd; 093 // special handling for OSX .app package 094 if (PlatformManager.isPlatformOsx() && getSystemProperty("java.library.path").contains("/JOSM.app/Contents/MacOS")) { 095 cmd = getAppleCommands(); 096 } else { 097 cmd = getCommands(); 098 } 099 Logging.info("Restart "+cmd); 100 if (Logging.isDebugEnabled() && Config.getPref().getBoolean("restart.debug.simulation")) { 101 Logging.debug("Restart cancelled to get debug info"); 102 return; 103 } 104 // execute the command in a shutdown hook, to be sure that all the 105 // resources have been disposed before restarting the application 106 Runtime.getRuntime().addShutdownHook(new Thread("josm-restarter") { 107 @Override 108 public void run() { 109 try { 110 Runtime.getRuntime().exec(cmd.toArray(new String[0])); 111 } catch (IOException e) { 112 Logging.error(e); 113 } 114 } 115 }); 116 // exit 117 System.exit(0); 118 } 119 120 private static List<String> getAppleCommands() { 121 final List<String> cmd = new ArrayList<>(); 122 cmd.add("/usr/bin/osascript"); 123 for (String line : RESTART_APPLE_SCRIPT.split("\n")) { 124 cmd.add("-e"); 125 cmd.add(line); 126 } 127 return cmd; 128 } 129 130 private static List<String> getCommands() throws IOException { 131 final List<String> cmd = new ArrayList<>(); 132 // java binary 133 cmd.add(getJavaRuntime()); 134 // vm arguments 135 addVMArguments(cmd); 136 // Determine webstart JNLP file. Use jnlpx.origFilenameArg instead of jnlp.application.href, 137 // because only this one is present when run from j2plauncher.exe (see #10795) 138 final String jnlp = getSystemProperty("jnlpx.origFilenameArg"); 139 // program main and program arguments (be careful a sun property. might not be supported by all JVM) 140 final String javaCommand = getSystemProperty("sun.java.command"); 141 if (javaCommand == null) { 142 throw new IOException("Unable to retrieve sun.java.command property"); 143 } 144 String[] mainCommand = javaCommand.split(" "); 145 if (javaCommand.endsWith(".jnlp") && jnlp == null) { 146 // see #11751 - jnlp on Linux 147 Logging.debug("Detected jnlp without jnlpx.origFilenameArg property set"); 148 cmd.addAll(Arrays.asList(mainCommand)); 149 } else { 150 // look for a .jar in all chunks to support paths with spaces (fix #9077) 151 StringBuilder sb = new StringBuilder(mainCommand[0]); 152 for (int i = 1; i < mainCommand.length && !mainCommand[i-1].endsWith(".jar"); i++) { 153 sb.append(' ').append(mainCommand[i]); 154 } 155 String jarPath = sb.toString(); 156 // program main is a jar 157 if (jarPath.endsWith(".jar")) { 158 // if it's a jar, add -jar mainJar 159 cmd.add("-jar"); 160 cmd.add(new File(jarPath).getPath()); 161 } else { 162 // else it's a .class, add the classpath and mainClass 163 cmd.add("-cp"); 164 cmd.add('"' + getSystemProperty("java.class.path") + '"'); 165 cmd.add(mainCommand[0].replace("jdk.plugin/", "")); // Main class appears to be invalid on Java WebStart 9 166 } 167 // add JNLP file. 168 if (jnlp != null) { 169 cmd.add(jnlp); 170 } 171 } 172 // finally add program arguments 173 cmd.addAll(MainApplication.getCommandLineArgs()); 174 return cmd; 175 } 176 177 private static String getJavaRuntime() throws IOException { 178 final String java = getSystemProperty("java.home") + File.separator + "bin" + File.separator + 179 (PlatformManager.isPlatformWindows() ? "java.exe" : "java"); 180 if (!new File(java).isFile()) { 181 throw new IOException("Unable to find suitable java runtime at "+java); 182 } 183 return java; 184 } 185 186 private static void addVMArguments(Collection<String> cmd) { 187 List<String> arguments = ManagementFactory.getRuntimeMXBean().getInputArguments(); 188 Logging.debug("VM arguments: {0}", arguments); 189 for (String arg : arguments) { 190 // When run from jp2launcher.exe, jnlpx.remove is true, while it is not when run from javaws 191 // Always set it to false to avoid error caused by a missing jnlp file on the second restart 192 arg = arg.replace("-Djnlpx.remove=true", "-Djnlpx.remove=false"); 193 // if it's the agent argument : we ignore it otherwise the 194 // address of the old application and the new one will be in conflict 195 if (!arg.contains("-agentlib")) { 196 cmd.add(arg); 197 } 198 } 199 } 200 201 /** 202 * Returns a new {@code ButtonSpec} instance that performs this action. 203 * @return A new {@code ButtonSpec} instance that performs this action. 204 */ 205 public static ButtonSpec getRestartButtonSpec() { 206 return new ButtonSpec( 207 tr("Restart"), 208 ImageProvider.get("restart", ImageSizes.LARGEICON), 209 tr("Restart the application."), 210 ht("/Action/Restart"), 211 isRestartSupported() 212 ); 213 } 214 215 /** 216 * Returns a new {@code ButtonSpec} instance that do not perform this action. 217 * @return A new {@code ButtonSpec} instance that do not perform this action. 218 */ 219 public static ButtonSpec getCancelButtonSpec() { 220 return new ButtonSpec( 221 tr("Cancel"), 222 new ImageProvider("cancel"), 223 tr("Click to restart later."), 224 null /* no specific help context */ 225 ); 226 } 227 228 /** 229 * Returns default {@code ButtonSpec} instances for this action (Restart/Cancel). 230 * @return Default {@code ButtonSpec} instances for this action. 231 * @see #getRestartButtonSpec 232 * @see #getCancelButtonSpec 233 */ 234 public static ButtonSpec[] getButtonSpecs() { 235 return new ButtonSpec[] { 236 getRestartButtonSpec(), 237 getCancelButtonSpec() 238 }; 239 } 240}