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