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