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