[5857] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
[13647] | 6 | import static org.openstreetmap.josm.tools.Utils.getSystemProperty;
|
---|
[5857] | 7 |
|
---|
| 8 | import java.awt.event.ActionEvent;
|
---|
| 9 | import java.awt.event.KeyEvent;
|
---|
| 10 | import java.io.File;
|
---|
| 11 | import java.io.IOException;
|
---|
| 12 | import java.lang.management.ManagementFactory;
|
---|
[5904] | 13 | import java.util.ArrayList;
|
---|
[8666] | 14 | import java.util.Arrays;
|
---|
| 15 | import java.util.Collection;
|
---|
[5857] | 16 | import java.util.List;
|
---|
| 17 |
|
---|
| 18 | import org.openstreetmap.josm.Main;
|
---|
| 19 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
[11650] | 20 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[11093] | 21 | import org.openstreetmap.josm.gui.io.SaveLayersDialog;
|
---|
[12846] | 22 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
[5857] | 23 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[12620] | 24 | import org.openstreetmap.josm.tools.Logging;
|
---|
[5857] | 25 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * Restarts JOSM as it was launched. Comes from "restart" plugin, originally written by Upliner.
|
---|
[6830] | 29 | * <br><br>
|
---|
[8471] | 30 | * Mechanisms have been improved based on #8561 discussions and
|
---|
| 31 | * <a href="http://lewisleo.blogspot.jp/2012/08/programmatically-restart-java.html">this article</a>.
|
---|
[5857] | 32 | * @since 5857
|
---|
| 33 | */
|
---|
| 34 | public class RestartAction extends JosmAction {
|
---|
| 35 |
|
---|
[7487] | 36 | // AppleScript to restart OS X package
|
---|
| 37 | private static final String RESTART_APPLE_SCRIPT =
|
---|
| 38 | "tell application \"System Events\"\n"
|
---|
| 39 | + "repeat until not (exists process \"JOSM\")\n"
|
---|
| 40 | + "delay 0.2\n"
|
---|
| 41 | + "end repeat\n"
|
---|
| 42 | + "end tell\n"
|
---|
| 43 | + "tell application \"JOSM\" to activate";
|
---|
| 44 |
|
---|
[5857] | 45 | /**
|
---|
| 46 | * Constructs a new {@code RestartAction}.
|
---|
| 47 | */
|
---|
| 48 | public RestartAction() {
|
---|
| 49 | super(tr("Restart"), "restart", tr("Restart the application."),
|
---|
| 50 | Shortcut.registerShortcut("file:restart", tr("File: {0}", tr("Restart")), KeyEvent.VK_J, Shortcut.ALT_CTRL_SHIFT), false);
|
---|
| 51 | putValue("help", ht("/Action/Restart"));
|
---|
| 52 | putValue("toolbar", "action/restart");
|
---|
[12637] | 53 | if (MainApplication.getToolbar() != null) {
|
---|
| 54 | MainApplication.getToolbar().register(this);
|
---|
[9238] | 55 | }
|
---|
[5951] | 56 | setEnabled(isRestartSupported());
|
---|
[5857] | 57 | }
|
---|
| 58 |
|
---|
[6084] | 59 | @Override
|
---|
[5857] | 60 | public void actionPerformed(ActionEvent e) {
|
---|
| 61 | try {
|
---|
| 62 | restartJOSM();
|
---|
| 63 | } catch (IOException ex) {
|
---|
[12620] | 64 | Logging.error(ex);
|
---|
[5857] | 65 | }
|
---|
| 66 | }
|
---|
[6069] | 67 |
|
---|
[5857] | 68 | /**
|
---|
[5967] | 69 | * Determines if restarting the application should be possible on this platform.
|
---|
[5951] | 70 | * @return {@code true} if the mandatory system property {@code sun.java.command} is defined, {@code false} otherwise.
|
---|
| 71 | * @since 5951
|
---|
| 72 | */
|
---|
| 73 | public static boolean isRestartSupported() {
|
---|
[13647] | 74 | return getSystemProperty("sun.java.command") != null;
|
---|
[5951] | 75 | }
|
---|
[6069] | 76 |
|
---|
[5951] | 77 | /**
|
---|
[10212] | 78 | * Restarts the current Java application.
|
---|
| 79 | * @throws IOException in case of any I/O error
|
---|
[5857] | 80 | */
|
---|
| 81 | public static void restartJOSM() throws IOException {
|
---|
[12273] | 82 | // If JOSM has been started with property 'josm.restart=true' this means
|
---|
| 83 | // it is executed by a start script that can handle restart.
|
---|
| 84 | // Request for restart is indicated by exit code 9.
|
---|
[13647] | 85 | String scriptRestart = getSystemProperty("josm.restart");
|
---|
[12273] | 86 | if ("true".equals(scriptRestart)) {
|
---|
[12636] | 87 | MainApplication.exitJosm(true, 9, SaveLayersDialog.Reason.RESTART);
|
---|
[12273] | 88 | }
|
---|
| 89 |
|
---|
[12636] | 90 | if (isRestartSupported() && !MainApplication.exitJosm(false, 0, SaveLayersDialog.Reason.RESTART)) return;
|
---|
[8666] | 91 | final List<String> cmd;
|
---|
[10212] | 92 | // special handling for OSX .app package
|
---|
[13647] | 93 | if (Main.isPlatformOsx() && getSystemProperty("java.library.path").contains("/JOSM.app/Contents/MacOS")) {
|
---|
[10212] | 94 | cmd = getAppleCommands();
|
---|
| 95 | } else {
|
---|
| 96 | cmd = getCommands();
|
---|
| 97 | }
|
---|
[12620] | 98 | Logging.info("Restart "+cmd);
|
---|
[12846] | 99 | if (Logging.isDebugEnabled() && Config.getPref().getBoolean("restart.debug.simulation")) {
|
---|
[12620] | 100 | Logging.debug("Restart cancelled to get debug info");
|
---|
[10212] | 101 | return;
|
---|
| 102 | }
|
---|
| 103 | // execute the command in a shutdown hook, to be sure that all the
|
---|
| 104 | // resources have been disposed before restarting the application
|
---|
| 105 | Runtime.getRuntime().addShutdownHook(new Thread("josm-restarter") {
|
---|
| 106 | @Override
|
---|
| 107 | public void run() {
|
---|
| 108 | try {
|
---|
[13206] | 109 | Runtime.getRuntime().exec(cmd.toArray(new String[0]));
|
---|
[10212] | 110 | } catch (IOException e) {
|
---|
[12620] | 111 | Logging.error(e);
|
---|
[10212] | 112 | }
|
---|
[5857] | 113 | }
|
---|
[10212] | 114 | });
|
---|
| 115 | // exit
|
---|
| 116 | System.exit(0);
|
---|
[5857] | 117 | }
|
---|
[6069] | 118 |
|
---|
[8666] | 119 | private static List<String> getAppleCommands() {
|
---|
| 120 | final List<String> cmd = new ArrayList<>();
|
---|
| 121 | cmd.add("/usr/bin/osascript");
|
---|
| 122 | for (String line : RESTART_APPLE_SCRIPT.split("\n")) {
|
---|
| 123 | cmd.add("-e");
|
---|
| 124 | cmd.add(line);
|
---|
| 125 | }
|
---|
| 126 | return cmd;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | private static List<String> getCommands() throws IOException {
|
---|
| 130 | final List<String> cmd = new ArrayList<>();
|
---|
| 131 | // java binary
|
---|
| 132 | cmd.add(getJavaRuntime());
|
---|
| 133 | // vm arguments
|
---|
| 134 | addVMArguments(cmd);
|
---|
| 135 | // Determine webstart JNLP file. Use jnlpx.origFilenameArg instead of jnlp.application.href,
|
---|
| 136 | // because only this one is present when run from j2plauncher.exe (see #10795)
|
---|
[13647] | 137 | final String jnlp = getSystemProperty("jnlpx.origFilenameArg");
|
---|
[8666] | 138 | // program main and program arguments (be careful a sun property. might not be supported by all JVM)
|
---|
[13647] | 139 | final String javaCommand = getSystemProperty("sun.java.command");
|
---|
[13393] | 140 | if (javaCommand == null) {
|
---|
| 141 | throw new IOException("Unable to retrieve sun.java.command property");
|
---|
| 142 | }
|
---|
[8666] | 143 | String[] mainCommand = javaCommand.split(" ");
|
---|
| 144 | if (javaCommand.endsWith(".jnlp") && jnlp == null) {
|
---|
| 145 | // see #11751 - jnlp on Linux
|
---|
[12620] | 146 | Logging.debug("Detected jnlp without jnlpx.origFilenameArg property set");
|
---|
[8666] | 147 | cmd.addAll(Arrays.asList(mainCommand));
|
---|
| 148 | } else {
|
---|
| 149 | // look for a .jar in all chunks to support paths with spaces (fix #9077)
|
---|
| 150 | StringBuilder sb = new StringBuilder(mainCommand[0]);
|
---|
| 151 | for (int i = 1; i < mainCommand.length && !mainCommand[i-1].endsWith(".jar"); i++) {
|
---|
| 152 | sb.append(' ').append(mainCommand[i]);
|
---|
| 153 | }
|
---|
| 154 | String jarPath = sb.toString();
|
---|
| 155 | // program main is a jar
|
---|
| 156 | if (jarPath.endsWith(".jar")) {
|
---|
| 157 | // if it's a jar, add -jar mainJar
|
---|
| 158 | cmd.add("-jar");
|
---|
| 159 | cmd.add(new File(jarPath).getPath());
|
---|
| 160 | } else {
|
---|
| 161 | // else it's a .class, add the classpath and mainClass
|
---|
| 162 | cmd.add("-cp");
|
---|
[13647] | 163 | cmd.add('"' + getSystemProperty("java.class.path") + '"');
|
---|
[13233] | 164 | cmd.add(mainCommand[0].replace("jdk.plugin/", "")); // Main class appears to be invalid on Java WebStart 9
|
---|
[8666] | 165 | }
|
---|
| 166 | // add JNLP file.
|
---|
| 167 | if (jnlp != null) {
|
---|
| 168 | cmd.add(jnlp);
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | // finally add program arguments
|
---|
[11650] | 172 | cmd.addAll(MainApplication.getCommandLineArgs());
|
---|
[8666] | 173 | return cmd;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | private static String getJavaRuntime() throws IOException {
|
---|
[13647] | 177 | final String java = getSystemProperty("java.home") + File.separator + "bin" + File.separator +
|
---|
[8666] | 178 | (Main.isPlatformWindows() ? "java.exe" : "java");
|
---|
| 179 | if (!new File(java).isFile()) {
|
---|
| 180 | throw new IOException("Unable to find suitable java runtime at "+java);
|
---|
| 181 | }
|
---|
| 182 | return java;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | private static void addVMArguments(Collection<String> cmd) {
|
---|
| 186 | List<String> arguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
|
---|
[12620] | 187 | Logging.debug("VM arguments: {0}", arguments);
|
---|
[8666] | 188 | for (String arg : arguments) {
|
---|
| 189 | // When run from jp2launcher.exe, jnlpx.remove is true, while it is not when run from javaws
|
---|
| 190 | // Always set it to false to avoid error caused by a missing jnlp file on the second restart
|
---|
| 191 | arg = arg.replace("-Djnlpx.remove=true", "-Djnlpx.remove=false");
|
---|
| 192 | // if it's the agent argument : we ignore it otherwise the
|
---|
| 193 | // address of the old application and the new one will be in conflict
|
---|
| 194 | if (!arg.contains("-agentlib")) {
|
---|
| 195 | cmd.add(arg);
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[5857] | 200 | /**
|
---|
| 201 | * Returns a new {@code ButtonSpec} instance that performs this action.
|
---|
| 202 | * @return A new {@code ButtonSpec} instance that performs this action.
|
---|
| 203 | */
|
---|
| 204 | public static ButtonSpec getRestartButtonSpec() {
|
---|
| 205 | return new ButtonSpec(
|
---|
| 206 | tr("Restart"),
|
---|
| 207 | ImageProvider.get("restart"),
|
---|
| 208 | tr("Restart the application."),
|
---|
[5951] | 209 | ht("/Action/Restart"),
|
---|
| 210 | isRestartSupported()
|
---|
[5857] | 211 | );
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | /**
|
---|
| 215 | * Returns a new {@code ButtonSpec} instance that do not perform this action.
|
---|
| 216 | * @return A new {@code ButtonSpec} instance that do not perform this action.
|
---|
| 217 | */
|
---|
| 218 | public static ButtonSpec getCancelButtonSpec() {
|
---|
| 219 | return new ButtonSpec(
|
---|
| 220 | tr("Cancel"),
|
---|
| 221 | ImageProvider.get("cancel"),
|
---|
| 222 | tr("Click to restart later."),
|
---|
| 223 | null /* no specific help context */
|
---|
| 224 | );
|
---|
| 225 | }
|
---|
[6069] | 226 |
|
---|
[5857] | 227 | /**
|
---|
| 228 | * Returns default {@code ButtonSpec} instances for this action (Restart/Cancel).
|
---|
| 229 | * @return Default {@code ButtonSpec} instances for this action.
|
---|
| 230 | * @see #getRestartButtonSpec
|
---|
| 231 | * @see #getCancelButtonSpec
|
---|
| 232 | */
|
---|
| 233 | public static ButtonSpec[] getButtonSpecs() {
|
---|
| 234 | return new ButtonSpec[] {
|
---|
| 235 | getRestartButtonSpec(),
|
---|
| 236 | getCancelButtonSpec()
|
---|
| 237 | };
|
---|
| 238 | }
|
---|
| 239 | }
|
---|