Changeset 17390 in josm for trunk/src


Ignore:
Timestamp:
2020-12-06T16:56:46+01:00 (4 years ago)
Author:
Don-vip
Message:

fix #13784 - restart robustness

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/RestartAction.java

    r16643 r17390  
    1414import java.util.Arrays;
    1515import java.util.Collection;
     16import java.util.Collections;
    1617import java.util.List;
    1718
     
    3536public class RestartAction extends JosmAction {
    3637
     38    private static final String APPLE_OSASCRIPT = "/usr/bin/osascript";
     39
    3740    // AppleScript to restart OS X package
    3841    private static final String RESTART_APPLE_SCRIPT =
     
    4447            + "tell application \"JOSM\" to activate";
    4548
     49    // Make sure we're able to retrieve restart commands before initiating shutdown (#13784)
     50    private static final List<String> cmd = determineRestartCommands();
     51
    4652    /**
    4753     * Constructs a new {@code RestartAction}.
     
    6066    @Override
    6167    public void actionPerformed(ActionEvent e) {
    62         try {
    63             restartJOSM();
    64         } catch (IOException ex) {
    65             Logging.error(ex);
    66         }
     68        restartJOSM();
    6769    }
    6870
     
    7375     */
    7476    public static boolean isRestartSupported() {
    75         return getSystemProperty("sun.java.command") != null;
     77        return !cmd.isEmpty();
    7678    }
    7779
    7880    /**
    7981     * Restarts the current Java application.
    80      * @throws IOException in case of any I/O error
    81      */
    82     public static void restartJOSM() throws IOException {
     82     */
     83    public static void restartJOSM() {
    8384        // If JOSM has been started with property 'josm.restart=true' this means
    8485        // it is executed by a start script that can handle restart.
     
    8990        }
    9091
    91         if (isRestartSupported() && !MainApplication.exitJosm(false, 0, SaveLayersDialog.Reason.RESTART)) return;
    92         final List<String> cmd;
    93         // special handling for OSX .app package
    94         if (PlatformManager.isPlatformOsx() && getSystemProperty("java.library.path").contains("/JOSM.app/Contents/MacOS")) {
    95             cmd = getAppleCommands();
    96         } else {
    97             cmd = getCommands();
     92        // Log every related environmentvariable for debug purpose
     93        if (isDebugSimulation()) {
     94            logEnvironment();
    9895        }
    9996        Logging.info("Restart "+cmd);
    100         if (Logging.isDebugEnabled() && Config.getPref().getBoolean("restart.debug.simulation")) {
     97        if (isDebugSimulation()) {
    10198            Logging.debug("Restart cancelled to get debug info");
    10299            return;
    103100        }
     101
     102        // Initiate shutdown
     103        if (isRestartSupported() && !MainApplication.exitJosm(false, 0, SaveLayersDialog.Reason.RESTART))
     104            return;
     105
    104106        // execute the command in a shutdown hook, to be sure that all the
    105107        // resources have been disposed before restarting the application
     
    118120    }
    119121
    120     private static List<String> getAppleCommands() {
     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        }
    121160        final List<String> cmd = new ArrayList<>();
    122         cmd.add("/usr/bin/osascript");
     161        cmd.add(APPLE_OSASCRIPT);
    123162        for (String line : RESTART_APPLE_SCRIPT.split("\n", -1)) {
    124163            cmd.add("-e");
     
    178217        final String java = getSystemProperty("java.home") + File.separator + "bin" + File.separator +
    179218                (PlatformManager.isPlatformWindows() ? "java.exe" : "java");
    180         if (!new File(java).isFile()) {
     219        if (!isExecutableFile(new File(java))) {
    181220            throw new IOException("Unable to find suitable java runtime at "+java);
    182221        }
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r17379 r17390  
    12321232                    Logging.info(tr("Detected no useable IPv6 network, preferring IPv4 over IPv6 after next restart."));
    12331233                    Config.getPref().putBoolean("validated.ipv6", hasv6); // be sure it is stored before the restart!
    1234                     try {
    1235                         RestartAction.restartJOSM();
    1236                     } catch (IOException e) {
    1237                         Logging.error(e);
    1238                     }
     1234                    RestartAction.restartJOSM();
    12391235                }
    12401236                Config.getPref().putBoolean("validated.ipv6", hasv6);
  • trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java

    r16975 r17390  
    617617                        Config.getPref().putList("plugins", new ArrayList<>(plugins));
    618618                        // restart
    619                         try {
    620                             RestartAction.restartJOSM();
    621                         } catch (IOException e) {
    622                             Logging.error(e);
    623                         }
     619                        RestartAction.restartJOSM();
    624620                    } else {
    625621                        Logging.warn("No plugin downloaded, restart canceled");
Note: See TracChangeset for help on using the changeset viewer.