source: josm/trunk/src/org/openstreetmap/josm/actions/RestartAction.java@ 12288

Last change on this file since 12288 was 12273, checked in by Don-vip, 7 years ago

fix #14850 - NPE on launch: make sure RestartAction (and all the GUI stuff behind) is not constructed before GUI is fully initialized

  • Property svn:eol-style set to native
File size: 9.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.io.File;
10import java.io.IOException;
11import java.lang.management.ManagementFactory;
12import java.util.ArrayList;
13import java.util.Arrays;
14import java.util.Collection;
15import java.util.List;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
19import org.openstreetmap.josm.gui.MainApplication;
20import org.openstreetmap.josm.gui.io.SaveLayersDialog;
21import org.openstreetmap.josm.tools.ImageProvider;
22import org.openstreetmap.josm.tools.Shortcut;
23
24/**
25 * Restarts JOSM as it was launched. Comes from "restart" plugin, originally written by Upliner.
26 * <br><br>
27 * Mechanisms have been improved based on #8561 discussions and
28 * <a href="http://lewisleo.blogspot.jp/2012/08/programmatically-restart-java.html">this article</a>.
29 * @since 5857
30 */
31public class RestartAction extends JosmAction {
32
33 // AppleScript to restart OS X package
34 private static final String RESTART_APPLE_SCRIPT =
35 "tell application \"System Events\"\n"
36 + "repeat until not (exists process \"JOSM\")\n"
37 + "delay 0.2\n"
38 + "end repeat\n"
39 + "end tell\n"
40 + "tell application \"JOSM\" to activate";
41
42 /**
43 * Constructs a new {@code RestartAction}.
44 */
45 public RestartAction() {
46 super(tr("Restart"), "restart", tr("Restart the application."),
47 Shortcut.registerShortcut("file:restart", tr("File: {0}", tr("Restart")), KeyEvent.VK_J, Shortcut.ALT_CTRL_SHIFT), false);
48 putValue("help", ht("/Action/Restart"));
49 putValue("toolbar", "action/restart");
50 if (Main.toolbar != null) {
51 Main.toolbar.register(this);
52 }
53 setEnabled(isRestartSupported());
54 }
55
56 @Override
57 public void actionPerformed(ActionEvent e) {
58 try {
59 restartJOSM();
60 } catch (IOException ex) {
61 Main.error(ex);
62 }
63 }
64
65 /**
66 * Determines if restarting the application should be possible on this platform.
67 * @return {@code true} if the mandatory system property {@code sun.java.command} is defined, {@code false} otherwise.
68 * @since 5951
69 */
70 public static boolean isRestartSupported() {
71 return System.getProperty("sun.java.command") != null;
72 }
73
74 /**
75 * Restarts the current Java application.
76 * @throws IOException in case of any I/O error
77 */
78 public static void restartJOSM() throws IOException {
79 // If JOSM has been started with property 'josm.restart=true' this means
80 // it is executed by a start script that can handle restart.
81 // Request for restart is indicated by exit code 9.
82 String scriptRestart = System.getProperty("josm.restart");
83 if ("true".equals(scriptRestart)) {
84 Main.exitJosm(true, 9, SaveLayersDialog.Reason.RESTART);
85 }
86
87 if (isRestartSupported() && !Main.exitJosm(false, 0, SaveLayersDialog.Reason.RESTART)) return;
88 final List<String> cmd;
89 // special handling for OSX .app package
90 if (Main.isPlatformOsx() && System.getProperty("java.library.path").contains("/JOSM.app/Contents/MacOS")) {
91 cmd = getAppleCommands();
92 } else {
93 cmd = getCommands();
94 }
95 Main.info("Restart "+cmd);
96 if (Main.isDebugEnabled() && Main.pref.getBoolean("restart.debug.simulation")) {
97 Main.debug("Restart cancelled to get debug info");
98 return;
99 }
100 // execute the command in a shutdown hook, to be sure that all the
101 // resources have been disposed before restarting the application
102 Runtime.getRuntime().addShutdownHook(new Thread("josm-restarter") {
103 @Override
104 public void run() {
105 try {
106 Runtime.getRuntime().exec(cmd.toArray(new String[cmd.size()]));
107 } catch (IOException e) {
108 Main.error(e);
109 }
110 }
111 });
112 // exit
113 System.exit(0);
114 }
115
116 private static List<String> getAppleCommands() {
117 final List<String> cmd = new ArrayList<>();
118 cmd.add("/usr/bin/osascript");
119 for (String line : RESTART_APPLE_SCRIPT.split("\n")) {
120 cmd.add("-e");
121 cmd.add(line);
122 }
123 return cmd;
124 }
125
126 private static List<String> getCommands() throws IOException {
127 final List<String> cmd = new ArrayList<>();
128 // java binary
129 cmd.add(getJavaRuntime());
130 // vm arguments
131 addVMArguments(cmd);
132 // Determine webstart JNLP file. Use jnlpx.origFilenameArg instead of jnlp.application.href,
133 // because only this one is present when run from j2plauncher.exe (see #10795)
134 final String jnlp = System.getProperty("jnlpx.origFilenameArg");
135 // program main and program arguments (be careful a sun property. might not be supported by all JVM)
136 final String javaCommand = System.getProperty("sun.java.command");
137 String[] mainCommand = javaCommand.split(" ");
138 if (javaCommand.endsWith(".jnlp") && jnlp == null) {
139 // see #11751 - jnlp on Linux
140 if (Main.isDebugEnabled()) {
141 Main.debug("Detected jnlp without jnlpx.origFilenameArg property set");
142 }
143 cmd.addAll(Arrays.asList(mainCommand));
144 } else {
145 // look for a .jar in all chunks to support paths with spaces (fix #9077)
146 StringBuilder sb = new StringBuilder(mainCommand[0]);
147 for (int i = 1; i < mainCommand.length && !mainCommand[i-1].endsWith(".jar"); i++) {
148 sb.append(' ').append(mainCommand[i]);
149 }
150 String jarPath = sb.toString();
151 // program main is a jar
152 if (jarPath.endsWith(".jar")) {
153 // if it's a jar, add -jar mainJar
154 cmd.add("-jar");
155 cmd.add(new File(jarPath).getPath());
156 } else {
157 // else it's a .class, add the classpath and mainClass
158 cmd.add("-cp");
159 cmd.add('"' + System.getProperty("java.class.path") + '"');
160 cmd.add(mainCommand[0]);
161 }
162 // add JNLP file.
163 if (jnlp != null) {
164 cmd.add(jnlp);
165 }
166 }
167 // finally add program arguments
168 cmd.addAll(MainApplication.getCommandLineArgs());
169 return cmd;
170 }
171
172 private static String getJavaRuntime() throws IOException {
173 final String java = System.getProperty("java.home") + File.separator + "bin" + File.separator +
174 (Main.isPlatformWindows() ? "java.exe" : "java");
175 if (!new File(java).isFile()) {
176 throw new IOException("Unable to find suitable java runtime at "+java);
177 }
178 return java;
179 }
180
181 private static void addVMArguments(Collection<String> cmd) {
182 List<String> arguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
183 if (Main.isDebugEnabled()) {
184 Main.debug("VM arguments: "+arguments);
185 }
186 for (String arg : arguments) {
187 // When run from jp2launcher.exe, jnlpx.remove is true, while it is not when run from javaws
188 // Always set it to false to avoid error caused by a missing jnlp file on the second restart
189 arg = arg.replace("-Djnlpx.remove=true", "-Djnlpx.remove=false");
190 // if it's the agent argument : we ignore it otherwise the
191 // address of the old application and the new one will be in conflict
192 if (!arg.contains("-agentlib")) {
193 cmd.add(arg);
194 }
195 }
196 }
197
198 /**
199 * Returns a new {@code ButtonSpec} instance that performs this action.
200 * @return A new {@code ButtonSpec} instance that performs this action.
201 */
202 public static ButtonSpec getRestartButtonSpec() {
203 return new ButtonSpec(
204 tr("Restart"),
205 ImageProvider.get("restart"),
206 tr("Restart the application."),
207 ht("/Action/Restart"),
208 isRestartSupported()
209 );
210 }
211
212 /**
213 * Returns a new {@code ButtonSpec} instance that do not perform this action.
214 * @return A new {@code ButtonSpec} instance that do not perform this action.
215 */
216 public static ButtonSpec getCancelButtonSpec() {
217 return new ButtonSpec(
218 tr("Cancel"),
219 ImageProvider.get("cancel"),
220 tr("Click to restart later."),
221 null /* no specific help context */
222 );
223 }
224
225 /**
226 * Returns default {@code ButtonSpec} instances for this action (Restart/Cancel).
227 * @return Default {@code ButtonSpec} instances for this action.
228 * @see #getRestartButtonSpec
229 * @see #getCancelButtonSpec
230 */
231 public static ButtonSpec[] getButtonSpecs() {
232 return new ButtonSpec[] {
233 getRestartButtonSpec(),
234 getCancelButtonSpec()
235 };
236 }
237}
Note: See TracBrowser for help on using the repository browser.