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;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.Arrays;
|
---|
14 | import java.util.Collections;
|
---|
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.PlatformHookWindows;
|
---|
21 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Restarts JOSM as it was launched. Comes from "restart" plugin, originally written by Upliner.
|
---|
25 | * <br/><br/>
|
---|
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);
|
---|
40 | setEnabled(isRestartSupported());
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public void actionPerformed(ActionEvent e) {
|
---|
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 | }
|
---|
52 |
|
---|
53 | try {
|
---|
54 | restartJOSM();
|
---|
55 | } catch (IOException ex) {
|
---|
56 | ex.printStackTrace();
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Determines if restarting the application should be possible on this platform.
|
---|
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 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Restarts the current Java application
|
---|
71 | * @throws IOException
|
---|
72 | */
|
---|
73 | public static void restartJOSM() throws IOException {
|
---|
74 | if (isRestartSupported() && !Main.exitJosm(false, 0)) return;
|
---|
75 | try {
|
---|
76 | // java binary
|
---|
77 | final String java = System.getProperty("java.home") + File.separator + "bin" + File.separator +
|
---|
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 | }
|
---|
82 | final List<String> cmd = new ArrayList<String>(Collections.singleton(java));
|
---|
83 | // vm arguments
|
---|
84 | for (String arg : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
|
---|
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")) {
|
---|
88 | cmd.add(arg);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | // program main and program arguments (be careful a sun property. might not be supported by all JVM)
|
---|
92 | String[] mainCommand = System.getProperty("sun.java.command").split(" ");
|
---|
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 | }
|
---|
98 | // program main is a jar
|
---|
99 | if (jarPath.endsWith(".jar")) {
|
---|
100 | // if it's a jar, add -jar mainJar
|
---|
101 | cmd.add("-jar");
|
---|
102 | cmd.add(new File(jarPath).getPath());
|
---|
103 | } else {
|
---|
104 | // else it's a .class, add the classpath and mainClass
|
---|
105 | cmd.add("-cp");
|
---|
106 | cmd.add("\"" + System.getProperty("java.class.path") + "\"");
|
---|
107 | cmd.add(mainCommand[0]);
|
---|
108 | }
|
---|
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 | }
|
---|
114 | // finally add program arguments
|
---|
115 | cmd.addAll(Arrays.asList(Main.commandLineArgs));
|
---|
116 | Main.info("Restart "+cmd);
|
---|
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 {
|
---|
123 | Runtime.getRuntime().exec(cmd.toArray(new String[cmd.size()]));
|
---|
124 | } catch (IOException e) {
|
---|
125 | e.printStackTrace();
|
---|
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 | }
|
---|
136 |
|
---|
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."),
|
---|
146 | ht("/Action/Restart"),
|
---|
147 | isRestartSupported()
|
---|
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 | }
|
---|
163 |
|
---|
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 | }
|
---|