[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;
|
---|
| 6 |
|
---|
| 7 | import java.awt.event.ActionEvent;
|
---|
| 8 | import java.awt.event.KeyEvent;
|
---|
| 9 | import java.io.File;
|
---|
| 10 | import java.io.IOException;
|
---|
| 11 | import java.lang.management.ManagementFactory;
|
---|
[5904] | 12 | import java.util.ArrayList;
|
---|
| 13 | import java.util.Arrays;
|
---|
| 14 | import java.util.Collections;
|
---|
[5857] | 15 | import java.util.List;
|
---|
| 16 |
|
---|
| 17 | import org.openstreetmap.josm.Main;
|
---|
| 18 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
| 19 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[5967] | 20 | import org.openstreetmap.josm.tools.PlatformHookWindows;
|
---|
[5857] | 21 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * Restarts JOSM as it was launched. Comes from "restart" plugin, originally written by Upliner.
|
---|
[6830] | 25 | * <br><br>
|
---|
[5857] | 26 | * Mechanisms have been improved based on #8561 discussions and <a href="http://lewisleo.blogspot.jp/2012/08/programmatically-restart-java.html">this article</a>.
|
---|
| 27 | * @since 5857
|
---|
| 28 | */
|
---|
| 29 | public class RestartAction extends JosmAction {
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
| 32 | * Constructs a new {@code RestartAction}.
|
---|
| 33 | */
|
---|
| 34 | public RestartAction() {
|
---|
| 35 | super(tr("Restart"), "restart", tr("Restart the application."),
|
---|
| 36 | Shortcut.registerShortcut("file:restart", tr("File: {0}", tr("Restart")), KeyEvent.VK_J, Shortcut.ALT_CTRL_SHIFT), false);
|
---|
| 37 | putValue("help", ht("/Action/Restart"));
|
---|
| 38 | putValue("toolbar", "action/restart");
|
---|
| 39 | Main.toolbar.register(this);
|
---|
[5951] | 40 | setEnabled(isRestartSupported());
|
---|
[5857] | 41 | }
|
---|
| 42 |
|
---|
[6084] | 43 | @Override
|
---|
[5857] | 44 | public void actionPerformed(ActionEvent e) {
|
---|
[6125] | 45 | // If JOSM has been started with property 'josm.restart=true' this means
|
---|
| 46 | // it is executed by a start script that can handle restart.
|
---|
| 47 | // Request for restart is indicated by exit code 9.
|
---|
| 48 | String scriptRestart = System.getProperty("josm.restart");
|
---|
| 49 | if ("true".equals(scriptRestart)) {
|
---|
| 50 | Main.exitJosm(true, 9);
|
---|
| 51 | }
|
---|
[6830] | 52 |
|
---|
[5857] | 53 | try {
|
---|
| 54 | restartJOSM();
|
---|
| 55 | } catch (IOException ex) {
|
---|
[6643] | 56 | Main.error(ex);
|
---|
[5857] | 57 | }
|
---|
| 58 | }
|
---|
[6069] | 59 |
|
---|
[5857] | 60 | /**
|
---|
[5967] | 61 | * Determines if restarting the application should be possible on this platform.
|
---|
[5951] | 62 | * @return {@code true} if the mandatory system property {@code sun.java.command} is defined, {@code false} otherwise.
|
---|
| 63 | * @since 5951
|
---|
| 64 | */
|
---|
| 65 | public static boolean isRestartSupported() {
|
---|
| 66 | return System.getProperty("sun.java.command") != null;
|
---|
| 67 | }
|
---|
[6069] | 68 |
|
---|
[5951] | 69 | /**
|
---|
[5857] | 70 | * Restarts the current Java application
|
---|
| 71 | * @throws IOException
|
---|
| 72 | */
|
---|
| 73 | public static void restartJOSM() throws IOException {
|
---|
[6125] | 74 | if (isRestartSupported() && !Main.exitJosm(false, 0)) return;
|
---|
[5857] | 75 | try {
|
---|
| 76 | // java binary
|
---|
[6069] | 77 | final String java = System.getProperty("java.home") + File.separator + "bin" + File.separator +
|
---|
[5967] | 78 | (Main.platform instanceof PlatformHookWindows ? "java.exe" : "java");
|
---|
| 79 | if (!new File(java).isFile()) {
|
---|
| 80 | throw new IOException("Unable to find suitable java runtime at "+java);
|
---|
| 81 | }
|
---|
[7005] | 82 | final List<String> cmd = new ArrayList<>(Collections.singleton(java));
|
---|
[5857] | 83 | // vm arguments
|
---|
[5904] | 84 | for (String arg : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
|
---|
[5857] | 85 | // if it's the agent argument : we ignore it otherwise the
|
---|
| 86 | // address of the old application and the new one will be in conflict
|
---|
| 87 | if (!arg.contains("-agentlib")) {
|
---|
[5904] | 88 | cmd.add(arg);
|
---|
[5857] | 89 | }
|
---|
| 90 | }
|
---|
[6069] | 91 | // program main and program arguments (be careful a sun property. might not be supported by all JVM)
|
---|
[5857] | 92 | String[] mainCommand = System.getProperty("sun.java.command").split(" ");
|
---|
[6234] | 93 | // look for a .jar in all chunks to support paths with spaces (fix #9077)
|
---|
| 94 | String jarPath = mainCommand[0];
|
---|
| 95 | for (int i = 1; i < mainCommand.length && !jarPath.endsWith(".jar"); i++) {
|
---|
| 96 | jarPath += " " + mainCommand[i];
|
---|
| 97 | }
|
---|
[5857] | 98 | // program main is a jar
|
---|
[6234] | 99 | if (jarPath.endsWith(".jar")) {
|
---|
[5857] | 100 | // if it's a jar, add -jar mainJar
|
---|
[5904] | 101 | cmd.add("-jar");
|
---|
[6234] | 102 | cmd.add(new File(jarPath).getPath());
|
---|
[5857] | 103 | } else {
|
---|
| 104 | // else it's a .class, add the classpath and mainClass
|
---|
[5904] | 105 | cmd.add("-cp");
|
---|
| 106 | cmd.add("\"" + System.getProperty("java.class.path") + "\"");
|
---|
| 107 | cmd.add(mainCommand[0]);
|
---|
[5857] | 108 | }
|
---|
[6111] | 109 | // if it's webstart add JNLP file
|
---|
| 110 | String jnlp = System.getProperty("jnlp.application.href");
|
---|
| 111 | if (jnlp != null) {
|
---|
| 112 | cmd.add(jnlp);
|
---|
| 113 | }
|
---|
[5857] | 114 | // finally add program arguments
|
---|
[5904] | 115 | cmd.addAll(Arrays.asList(Main.commandLineArgs));
|
---|
[5967] | 116 | Main.info("Restart "+cmd);
|
---|
[5857] | 117 | // execute the command in a shutdown hook, to be sure that all the
|
---|
| 118 | // resources have been disposed before restarting the application
|
---|
| 119 | Runtime.getRuntime().addShutdownHook(new Thread() {
|
---|
| 120 | @Override
|
---|
| 121 | public void run() {
|
---|
| 122 | try {
|
---|
[6083] | 123 | Runtime.getRuntime().exec(cmd.toArray(new String[cmd.size()]));
|
---|
[5857] | 124 | } catch (IOException e) {
|
---|
[6643] | 125 | Main.error(e);
|
---|
[5857] | 126 | }
|
---|
| 127 | }
|
---|
| 128 | });
|
---|
| 129 | // exit
|
---|
| 130 | System.exit(0);
|
---|
| 131 | } catch (Exception e) {
|
---|
| 132 | // something went wrong
|
---|
| 133 | throw new IOException("Error while trying to restart the application", e);
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
[6069] | 136 |
|
---|
[5857] | 137 | /**
|
---|
| 138 | * Returns a new {@code ButtonSpec} instance that performs this action.
|
---|
| 139 | * @return A new {@code ButtonSpec} instance that performs this action.
|
---|
| 140 | */
|
---|
| 141 | public static ButtonSpec getRestartButtonSpec() {
|
---|
| 142 | return new ButtonSpec(
|
---|
| 143 | tr("Restart"),
|
---|
| 144 | ImageProvider.get("restart"),
|
---|
| 145 | tr("Restart the application."),
|
---|
[5951] | 146 | ht("/Action/Restart"),
|
---|
| 147 | isRestartSupported()
|
---|
[5857] | 148 | );
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /**
|
---|
| 152 | * Returns a new {@code ButtonSpec} instance that do not perform this action.
|
---|
| 153 | * @return A new {@code ButtonSpec} instance that do not perform this action.
|
---|
| 154 | */
|
---|
| 155 | public static ButtonSpec getCancelButtonSpec() {
|
---|
| 156 | return new ButtonSpec(
|
---|
| 157 | tr("Cancel"),
|
---|
| 158 | ImageProvider.get("cancel"),
|
---|
| 159 | tr("Click to restart later."),
|
---|
| 160 | null /* no specific help context */
|
---|
| 161 | );
|
---|
| 162 | }
|
---|
[6069] | 163 |
|
---|
[5857] | 164 | /**
|
---|
| 165 | * Returns default {@code ButtonSpec} instances for this action (Restart/Cancel).
|
---|
| 166 | * @return Default {@code ButtonSpec} instances for this action.
|
---|
| 167 | * @see #getRestartButtonSpec
|
---|
| 168 | * @see #getCancelButtonSpec
|
---|
| 169 | */
|
---|
| 170 | public static ButtonSpec[] getButtonSpecs() {
|
---|
| 171 | return new ButtonSpec[] {
|
---|
| 172 | getRestartButtonSpec(),
|
---|
| 173 | getCancelButtonSpec()
|
---|
| 174 | };
|
---|
| 175 | }
|
---|
| 176 | }
|
---|