Changeset 12217 in josm


Ignore:
Timestamp:
2017-05-20T17:11:37+02:00 (7 years ago)
Author:
Don-vip
Message:

see #14821 - workaround for JDK-8180379/JDK-8179014 : prevent JVM crash when opening a file chooser on Windows 10 Creators Update with Windows look & feel + add information about OS build number for Windows & macOS + add utilities to get Java update/build version numbers

Location:
trunk
Files:
1 added
7 edited

Legend:

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

    r11746 r12217  
    8484        String runtimeVersion = System.getProperty("java.runtime.version");
    8585        text.append(Version.getInstance().getReleaseAttributes())
    86             .append("\nIdentification: ").append(Version.getInstance().getAgentString())
    87             .append("\nMemory Usage: ")
     86            .append("\nIdentification: ").append(Version.getInstance().getAgentString());
     87        String buildNumber = Main.platform.getOSBuildNumber();
     88        if (!buildNumber.isEmpty()) {
     89            text.append("\nOS Build number: ").append(buildNumber);
     90        }
     91        text.append("\nMemory Usage: ")
    8892            .append(Runtime.getRuntime().totalMemory()/1024/1024)
    8993            .append(" MB / ")
  • trunk/src/org/openstreetmap/josm/tools/PlatformHook.java

    r11944 r12217  
    33
    44import java.awt.GraphicsEnvironment;
     5import java.io.BufferedReader;
    56import java.io.File;
    67import java.io.IOException;
     8import java.io.InputStreamReader;
     9import java.nio.charset.StandardCharsets;
    710import java.security.KeyStore;
    811import java.security.KeyStoreException;
     
    140143
    141144    /**
     145     * Returns OS build number.
     146     * @return OS build number.
     147     * @since 12217
     148     */
     149    default String getOSBuildNumber() {
     150        return "";
     151    }
     152
     153    /**
    142154     * Setup system keystore to add JOSM HTTPS certificate (for remote control).
    143155     * @param entryAlias The entry alias to use
     
    172184
    173185    /**
     186     * Executes a native command and returns the first line of standard output.
     187     * @param command array containing the command to call and its arguments.
     188     * @return first stripped line of standard output
     189     * @throws IOException if an I/O error occurs
     190     * @since 12217
     191     */
     192    default String exec(String... command) throws IOException {
     193        Process p = Runtime.getRuntime().exec(command);
     194        try (BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8))) {
     195            return Utils.strip(input.readLine());
     196        }
     197    }
     198
     199    /**
    174200     * Returns the platform-dependent default cache directory.
    175201     * @return the platform-dependent default cache directory
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookOsx.java

    r12123 r12217  
    3232
    3333    private static final PlatformHookOsx ivhandler = new PlatformHookOsx();
     34
     35    private String oSBuildNumber;
    3436
    3537    @Override
     
    386388    }
    387389
     390    private String buildOSBuildNumber() {
     391        StringBuilder sb = new StringBuilder();
     392        try {
     393            sb.append(exec("sw_vers", "-productName"))
     394              .append(' ')
     395              .append(exec("sw_vers", "-productVersion"))
     396              .append(" (")
     397              .append(exec("sw_vers", "-buildVersion"))
     398              .append(')');
     399        } catch (IOException e) {
     400            Main.error(e);
     401        }
     402        return sb.toString();
     403    }
     404
     405    @Override
     406    public String getOSBuildNumber() {
     407        if (oSBuildNumber == null) {
     408            oSBuildNumber = buildOSBuildNumber();
     409        }
     410        return oSBuildNumber;
     411    }
     412
    388413    @Override
    389414    public File getDefaultCacheDirectory() {
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookUnixoid.java

    r11747 r12217  
    1010import java.io.File;
    1111import java.io.IOException;
    12 import java.io.InputStreamReader;
    1312import java.net.URI;
    1413import java.net.URISyntaxException;
     
    207206                // Try lsb_release (only available on LSB-compliant Linux systems,
    208207                // see https://www.linuxbase.org/lsb-cert/productdir.php?by_prod )
    209                 Process p = Runtime.getRuntime().exec("lsb_release -ds");
    210                 try (BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8))) {
    211                     String line = Utils.strip(input.readLine());
    212                     if (line != null && !line.isEmpty()) {
    213                         line = line.replaceAll("\"+", "");
    214                         line = line.replaceAll("NAME=", ""); // strange code for some Gentoo's
    215                         if (line.startsWith("Linux ")) // e.g. Linux Mint
    216                             return line;
    217                         else if (!line.isEmpty())
    218                             return "Linux " + line;
    219                     }
     208                String line = exec("lsb_release -ds");
     209                if (line != null && !line.isEmpty()) {
     210                    line = line.replaceAll("\"+", "");
     211                    line = line.replaceAll("NAME=", ""); // strange code for some Gentoo's
     212                    if (line.startsWith("Linux ")) // e.g. Linux Mint
     213                        return line;
     214                    else if (!line.isEmpty())
     215                        return "Linux " + line;
    220216                }
    221217            } catch (IOException e) {
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookWindows.java

    r12146 r12217  
    2727import static java.awt.event.KeyEvent.VK_Z;
    2828import static org.openstreetmap.josm.tools.I18n.tr;
     29import static org.openstreetmap.josm.tools.WinRegistry.HKEY_LOCAL_MACHINE;
    2930
    3031import java.awt.GraphicsEnvironment;
     
    3637import java.io.OutputStreamWriter;
    3738import java.io.Writer;
     39import java.lang.reflect.InvocationTargetException;
    3840import java.nio.charset.StandardCharsets;
    3941import java.nio.file.DirectoryStream;
     
    6466
    6567import javax.swing.JOptionPane;
     68import javax.swing.UIManager;
    6669
    6770import org.openstreetmap.josm.Main;
    6871import org.openstreetmap.josm.data.Preferences;
     72import org.openstreetmap.josm.gui.preferences.display.LafPreference;
    6973import org.openstreetmap.josm.io.CertificateAmendment.CertAmend;
    7074
     
    150154    private static final String WINDOWS_ROOT = "Windows-ROOT";
    151155
     156    private static final String CURRENT_VERSION = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";
     157
     158    private String oSBuildNumber;
     159
    152160    @Override
    153161    public void afterPrefStartupHook() {
    154162        extendFontconfig("fontconfig.properties.src");
     163        // Workaround for JDK-8180379: crash on Windows 10 1703 with Windows L&F and java < 8u152 / 9+171
     164        // To remove during Java 9 migration
     165        if (System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows 10") &&
     166                getDefaultStyle().equals(LafPreference.LAF.get())) {
     167            try {
     168                final int currentBuild = Integer.parseInt(getCurrentBuild());
     169                final int javaVersion = Utils.getJavaVersion();
     170                final int javaUpdate = Utils.getJavaUpdate();
     171                final int javaBuild = Utils.getJavaBuild();
     172                // See https://technet.microsoft.com/en-us/windows/release-info.aspx
     173                if (currentBuild >= 15_063 && ((javaVersion == 8 && javaUpdate < 152)
     174                        || (javaVersion == 9 && javaUpdate == 0 && javaBuild < 171))) {
     175                    // Workaround from https://bugs.openjdk.java.net/browse/JDK-8179014
     176                    UIManager.put("FileChooser.useSystemExtensionHiding", false);
     177                }
     178            } catch (NumberFormatException | ReflectiveOperationException e) {
     179                Main.error(e);
     180            }
     181        }
    155182    }
    156183
     
    250277    }
    251278
     279    private static String getProductName() throws IllegalAccessException, InvocationTargetException {
     280        return WinRegistry.readString(HKEY_LOCAL_MACHINE, CURRENT_VERSION, "ProductName");
     281    }
     282
     283    private static String getReleaseId() throws IllegalAccessException, InvocationTargetException {
     284        return WinRegistry.readString(HKEY_LOCAL_MACHINE, CURRENT_VERSION, "ReleaseId");
     285    }
     286
     287    private static String getCurrentBuild() throws IllegalAccessException, InvocationTargetException {
     288        return WinRegistry.readString(HKEY_LOCAL_MACHINE, CURRENT_VERSION, "CurrentBuild");
     289    }
     290
     291    private static String buildOSBuildNumber() {
     292        StringBuilder sb = new StringBuilder();
     293        try {
     294            sb.append(getProductName()).append(' ')
     295              .append(getReleaseId()).append(" (")
     296              .append(getCurrentBuild()).append(')');
     297        } catch (IllegalAccessException | InvocationTargetException e) {
     298            Main.error(e);
     299        }
     300        return sb.toString();
     301    }
     302
     303    @Override
     304    public String getOSBuildNumber() {
     305        if (oSBuildNumber == null) {
     306            oSBuildNumber = buildOSBuildNumber();
     307        }
     308        return oSBuildNumber;
     309    }
     310
    252311    /**
    253312     * Loads Windows-ROOT keystore.
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r12130 r12217  
    16071607                dotPos > -1 ? dotPos : dashPos > -1 ? dashPos : 1));
    16081608    }
     1609
     1610    /**
     1611     * Returns the Java update as an int value.
     1612     * @return the Java update as an int value (121, 131, etc.)
     1613     * @since 12217
     1614     */
     1615    public static int getJavaUpdate() {
     1616        String version = System.getProperty("java.version");
     1617        if (version.startsWith("1.")) {
     1618            version = version.substring(2);
     1619        }
     1620        // Allow these formats:
     1621        // 1.8.0_72-ea
     1622        // 9-ea
     1623        // 9
     1624        // 9.0.1
     1625        int undePos = version.indexOf('_');
     1626        int dashPos = version.indexOf('-');
     1627        if (undePos > -1) {
     1628            return Integer.parseInt(version.substring(undePos + 1,
     1629                    dashPos > -1 ? dashPos : version.length()));
     1630        }
     1631        int firstDotPos = version.indexOf('.');
     1632        int lastDotPos = version.lastIndexOf('.');
     1633        return firstDotPos > - 1 ? Integer.parseInt(version.substring(firstDotPos + 1,
     1634                lastDotPos > -1 ? lastDotPos : version.length())) : 0;
     1635    }
     1636
     1637    /**
     1638     * Returns the Java build number as an int value.
     1639     * @return the Java build number as an int value (0, 1, etc.)
     1640     * @since 12217
     1641     */
     1642    public static int getJavaBuild() {
     1643        String version = System.getProperty("java.runtime.version");
     1644        int bPos = version.indexOf('b');
     1645        int pPos = version.indexOf('+');
     1646        return Integer.parseInt(version.substring(bPos > -1 ? bPos + 1 : pPos + 1, version.length()));
     1647    }
    16091648}
  • trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    r12130 r12217  
    219219
    220220    /**
     221     * Test {@link Utils#getJavaUpdate}
     222     */
     223    @Test
     224    public void testGetJavaUpdate() {
     225        String javaVersion = System.getProperty("java.version");
     226        try {
     227            System.setProperty("java.version", "1.8.0_131");
     228            assertEquals(131, Utils.getJavaUpdate());
     229
     230            System.setProperty("java.version", "1.8.0_152-ea");
     231            assertEquals(152, Utils.getJavaUpdate());
     232
     233            System.setProperty("java.version", "9-ea");
     234            assertEquals(0, Utils.getJavaUpdate());
     235
     236            System.setProperty("java.version", "9");
     237            assertEquals(0, Utils.getJavaUpdate());
     238
     239            System.setProperty("java.version", "9.1.2");
     240            assertEquals(1, Utils.getJavaUpdate());
     241        } finally {
     242            System.setProperty("java.version", javaVersion);
     243        }
     244    }
     245
     246    /**
     247     * Test {@link Utils#getJavaBuild}
     248     */
     249    @Test
     250    public void testGetJavaBuild() {
     251        String javaVersion = System.getProperty("java.runtime.version");
     252        try {
     253            System.setProperty("java.runtime.version", "1.8.0_131-b11");
     254            assertEquals(11, Utils.getJavaBuild());
     255
     256            System.setProperty("java.runtime.version", "1.8.0_152-ea-b04");
     257            assertEquals(4, Utils.getJavaBuild());
     258
     259            System.setProperty("java.runtime.version", "9-ea+170");
     260            assertEquals(170, Utils.getJavaBuild());
     261
     262            System.setProperty("java.runtime.version", "9+200");
     263            assertEquals(200, Utils.getJavaBuild());
     264
     265            System.setProperty("java.runtime.version", "9.1.2+62");
     266            assertEquals(62, Utils.getJavaBuild());
     267        } finally {
     268            System.setProperty("java.runtime.version", javaVersion);
     269        }
     270    }
     271
     272    /**
    221273     * Tests if readBytesFromStream handles null streams (might happen when there is no data on error stream)
    222274     * @throws IOException in case of I/O error
Note: See TracChangeset for help on using the changeset viewer.