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

Last change on this file since 9944 was 9238, checked in by stoecker, 8 years ago

see #8562 - restart JOSM in case we switched from IPv6 to IPv4 only, so we have no broken connection

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