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

Last change on this file since 10775 was 10212, checked in by Don-vip, 8 years ago

sonar - squid:S2221 - "Exception" should not be caught when not required by called methods

  • Property svn:eol-style set to native
File size: 8.9 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 I/O error
83 */
84 public static void restartJOSM() throws IOException {
85 if (isRestartSupported() && !Main.exitJosm(false, 0)) return;
86 final List<String> cmd;
87 // special handling for OSX .app package
88 if (Main.isPlatformOsx() && System.getProperty("java.library.path").contains("/JOSM.app/Contents/MacOS")) {
89 cmd = getAppleCommands();
90 } else {
91 cmd = getCommands();
92 }
93 Main.info("Restart "+cmd);
94 if (Main.isDebugEnabled() && Main.pref.getBoolean("restart.debug.simulation")) {
95 Main.debug("Restart cancelled to get debug info");
96 return;
97 }
98 // execute the command in a shutdown hook, to be sure that all the
99 // resources have been disposed before restarting the application
100 Runtime.getRuntime().addShutdownHook(new Thread("josm-restarter") {
101 @Override
102 public void run() {
103 try {
104 Runtime.getRuntime().exec(cmd.toArray(new String[cmd.size()]));
105 } catch (IOException e) {
106 Main.error(e);
107 }
108 }
109 });
110 // exit
111 System.exit(0);
112 }
113
114 private static List<String> getAppleCommands() {
115 final List<String> cmd = new ArrayList<>();
116 cmd.add("/usr/bin/osascript");
117 for (String line : RESTART_APPLE_SCRIPT.split("\n")) {
118 cmd.add("-e");
119 cmd.add(line);
120 }
121 return cmd;
122 }
123
124 private static List<String> getCommands() throws IOException {
125 final List<String> cmd = new ArrayList<>();
126 // java binary
127 cmd.add(getJavaRuntime());
128 // vm arguments
129 addVMArguments(cmd);
130 // Determine webstart JNLP file. Use jnlpx.origFilenameArg instead of jnlp.application.href,
131 // because only this one is present when run from j2plauncher.exe (see #10795)
132 final String jnlp = System.getProperty("jnlpx.origFilenameArg");
133 // program main and program arguments (be careful a sun property. might not be supported by all JVM)
134 final String javaCommand = System.getProperty("sun.java.command");
135 String[] mainCommand = javaCommand.split(" ");
136 if (javaCommand.endsWith(".jnlp") && jnlp == null) {
137 // see #11751 - jnlp on Linux
138 if (Main.isDebugEnabled()) {
139 Main.debug("Detected jnlp without jnlpx.origFilenameArg property set");
140 }
141 cmd.addAll(Arrays.asList(mainCommand));
142 } else {
143 // look for a .jar in all chunks to support paths with spaces (fix #9077)
144 StringBuilder sb = new StringBuilder(mainCommand[0]);
145 for (int i = 1; i < mainCommand.length && !mainCommand[i-1].endsWith(".jar"); i++) {
146 sb.append(' ').append(mainCommand[i]);
147 }
148 String jarPath = sb.toString();
149 // program main is a jar
150 if (jarPath.endsWith(".jar")) {
151 // if it's a jar, add -jar mainJar
152 cmd.add("-jar");
153 cmd.add(new File(jarPath).getPath());
154 } else {
155 // else it's a .class, add the classpath and mainClass
156 cmd.add("-cp");
157 cmd.add('"' + System.getProperty("java.class.path") + '"');
158 cmd.add(mainCommand[0]);
159 }
160 // add JNLP file.
161 if (jnlp != null) {
162 cmd.add(jnlp);
163 }
164 }
165 // finally add program arguments
166 cmd.addAll(Main.getCommandLineArgs());
167 return cmd;
168 }
169
170 private static String getJavaRuntime() throws IOException {
171 final String java = System.getProperty("java.home") + File.separator + "bin" + File.separator +
172 (Main.isPlatformWindows() ? "java.exe" : "java");
173 if (!new File(java).isFile()) {
174 throw new IOException("Unable to find suitable java runtime at "+java);
175 }
176 return java;
177 }
178
179 private static void addVMArguments(Collection<String> cmd) {
180 List<String> arguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
181 if (Main.isDebugEnabled()) {
182 Main.debug("VM arguments: "+arguments);
183 }
184 for (String arg : arguments) {
185 // When run from jp2launcher.exe, jnlpx.remove is true, while it is not when run from javaws
186 // Always set it to false to avoid error caused by a missing jnlp file on the second restart
187 arg = arg.replace("-Djnlpx.remove=true", "-Djnlpx.remove=false");
188 // if it's the agent argument : we ignore it otherwise the
189 // address of the old application and the new one will be in conflict
190 if (!arg.contains("-agentlib")) {
191 cmd.add(arg);
192 }
193 }
194 }
195
196 /**
197 * Returns a new {@code ButtonSpec} instance that performs this action.
198 * @return A new {@code ButtonSpec} instance that performs this action.
199 */
200 public static ButtonSpec getRestartButtonSpec() {
201 return new ButtonSpec(
202 tr("Restart"),
203 ImageProvider.get("restart"),
204 tr("Restart the application."),
205 ht("/Action/Restart"),
206 isRestartSupported()
207 );
208 }
209
210 /**
211 * Returns a new {@code ButtonSpec} instance that do not perform this action.
212 * @return A new {@code ButtonSpec} instance that do not perform this action.
213 */
214 public static ButtonSpec getCancelButtonSpec() {
215 return new ButtonSpec(
216 tr("Cancel"),
217 ImageProvider.get("cancel"),
218 tr("Click to restart later."),
219 null /* no specific help context */
220 );
221 }
222
223 /**
224 * Returns default {@code ButtonSpec} instances for this action (Restart/Cancel).
225 * @return Default {@code ButtonSpec} instances for this action.
226 * @see #getRestartButtonSpec
227 * @see #getCancelButtonSpec
228 */
229 public static ButtonSpec[] getButtonSpecs() {
230 return new ButtonSpec[] {
231 getRestartButtonSpec(),
232 getCancelButtonSpec()
233 };
234 }
235}
Note: See TracBrowser for help on using the repository browser.