Ticket #11751: 11751.patch

File 11751.patch, 8.1 KB (added by simon04, 11 years ago)
  • src/org/openstreetmap/josm/actions/RestartAction.java

    diff --git a/src/org/openstreetmap/josm/actions/RestartAction.java b/src/org/openstreetmap/josm/actions/RestartAction.java
    index 7c8f3dc..d0ee483 100644
    a b  
    1010import java.io.IOException;
    1111import java.lang.management.ManagementFactory;
    1212import java.util.ArrayList;
     13import java.util.Arrays;
     14import java.util.Collection;
    1315import java.util.List;
    1416
    1517import org.openstreetmap.josm.Main;
    public static boolean isRestartSupported() {  
    7981     */
    8082    public static void restartJOSM() throws IOException {
    8183        if (isRestartSupported() && !Main.exitJosm(false, 0)) return;
     84        final List<String> cmd;
    8285        try {
    83             final List<String> cmd = new ArrayList<>();
    8486            // special handling for OSX .app package
    8587            if (Main.isPlatformOsx() && System.getProperty("java.library.path").contains("/JOSM.app/Contents/MacOS")) {
    86                 cmd.add("/usr/bin/osascript");
    87                 for (String line : RESTART_APPLE_SCRIPT.split("\n")) {
    88                     cmd.add("-e");
    89                     cmd.add(line);
    90                 }
     88                cmd = getAppleCommands();
    9189            } else {
    92                 // java binary
    93                 final String java = System.getProperty("java.home") + File.separator + "bin" + File.separator +
    94                         (Main.isPlatformWindows() ? "java.exe" : "java");
    95                 if (!new File(java).isFile()) {
    96                     throw new IOException("Unable to find suitable java runtime at "+java);
    97                 }
    98                 cmd.add(java);
    99                 // vm arguments
    100                 List<String> arguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
    101                 if (Main.isDebugEnabled()) {
    102                     Main.debug("VM arguments: "+arguments);
    103                 }
    104                 for (String arg : arguments) {
    105                     // When run from jp2launcher.exe, jnlpx.remove is true, while it is not when run from javaws
    106                     // Always set it to false to avoid error caused by a missing jnlp file on the second restart
    107                     arg = arg.replace("-Djnlpx.remove=true", "-Djnlpx.remove=false");
    108                     // if it's the agent argument : we ignore it otherwise the
    109                     // address of the old application and the new one will be in conflict
    110                     if (!arg.contains("-agentlib")) {
    111                         cmd.add(arg);
    112                     }
    113                 }
    114                 // program main and program arguments (be careful a sun property. might not be supported by all JVM)
    115                 String[] mainCommand = System.getProperty("sun.java.command").split(" ");
    116                 // look for a .jar in all chunks to support paths with spaces (fix #9077)
    117                 StringBuilder sb = new StringBuilder(mainCommand[0]);
    118                 for (int i = 1; i < mainCommand.length && !mainCommand[i-1].endsWith(".jar"); i++) {
    119                     sb.append(' ').append(mainCommand[i]);
    120                 }
    121                 String jarPath = sb.toString();
    122                 // program main is a jar
    123                 if (jarPath.endsWith(".jar")) {
    124                     // if it's a jar, add -jar mainJar
    125                     cmd.add("-jar");
    126                     cmd.add(new File(jarPath).getPath());
    127                 } else {
    128                     // else it's a .class, add the classpath and mainClass
    129                     cmd.add("-cp");
    130                     cmd.add("\"" + System.getProperty("java.class.path") + "\"");
    131                     cmd.add(mainCommand[0]);
    132                 }
    133                 // if it's webstart add JNLP file. Use jnlpx.origFilenameArg instead of jnlp.application.href,
    134                 // because only this one is present when run from j2plauncher.exe (see #10795)
    135                 String jnlp = System.getProperty("jnlpx.origFilenameArg");
    136                 if (jnlp != null) {
    137                     cmd.add(jnlp);
    138                 }
    139                 // finally add program arguments
    140                 cmd.addAll(Main.getCommandLineArgs());
     90                cmd = getCommands();
    14191            }
    14292            Main.info("Restart "+cmd);
    14393            if (Main.isDebugEnabled() && Main.pref.getBoolean("restart.debug.simulation")) {
    public void run() {  
    164114        }
    165115    }
    166116
     117    private static List<String> getAppleCommands() {
     118        final List<String> cmd = new ArrayList<>();
     119        cmd.add("/usr/bin/osascript");
     120        for (String line : RESTART_APPLE_SCRIPT.split("\n")) {
     121            cmd.add("-e");
     122            cmd.add(line);
     123        }
     124        return cmd;
     125    }
     126
     127    private static List<String> getCommands() throws IOException {
     128        final List<String> cmd = new ArrayList<>();
     129        // java binary
     130        cmd.add(getJavaRuntime());
     131        // vm arguments
     132        addVMArguments(cmd);
     133        // Determine webstart JNLP file. Use jnlpx.origFilenameArg instead of jnlp.application.href,
     134        // because only this one is present when run from j2plauncher.exe (see #10795)
     135        final String jnlp = System.getProperty("jnlpx.origFilenameArg");
     136        // program main and program arguments (be careful a sun property. might not be supported by all JVM)
     137        String[] mainCommand = System.getProperty("sun.java.command").split(" ");
     138        if (System.getProperty("sun.java.command").endsWith(".jnlp") && jnlp == null) {
     139            // see #11751
     140            Main.debug("Detected jnlp without jnlpx.origFilenameArg property set");
     141            cmd.addAll(Arrays.asList(mainCommand));
     142            cmd.addAll(Main.getCommandLineArgs());
     143            return cmd;
     144        }
     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        // finally add program arguments
     167        cmd.addAll(Main.getCommandLineArgs());
     168        return cmd;
     169    }
     170
     171    private static String getJavaRuntime() throws IOException {
     172        final String java = System.getProperty("java.home") + File.separator + "bin" + File.separator +
     173                (Main.isPlatformWindows() ? "java.exe" : "java");
     174        if (!new File(java).isFile()) {
     175            throw new IOException("Unable to find suitable java runtime at "+java);
     176        }
     177        return java;
     178    }
     179
     180    private static void addVMArguments(Collection<String> cmd) {
     181        List<String> arguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
     182        if (Main.isDebugEnabled()) {
     183            Main.debug("VM arguments: "+arguments);
     184        }
     185        for (String arg : arguments) {
     186            // When run from jp2launcher.exe, jnlpx.remove is true, while it is not when run from javaws
     187            // Always set it to false to avoid error caused by a missing jnlp file on the second restart
     188            arg = arg.replace("-Djnlpx.remove=true", "-Djnlpx.remove=false");
     189            // if it's the agent argument : we ignore it otherwise the
     190            // address of the old application and the new one will be in conflict
     191            if (!arg.contains("-agentlib")) {
     192                cmd.add(arg);
     193            }
     194        }
     195    }
     196
    167197    /**
    168198     * Returns a new {@code ButtonSpec} instance that performs this action.
    169199     * @return A new {@code ButtonSpec} instance that performs this action.