Changeset 12219 in josm


Ignore:
Timestamp:
2017-05-20T19:06:29+02:00 (5 months ago)
Author:
Don-vip
Message:

see #14652 - ask Windows/macOS users to update their version of Java when it expires (i.e when the built-in JRE expiration date is passed, about 4 months after release, 1 month after Java should have asked to update by itself). It currently proposes to update all versions of Java 8 up to update 121, released on January 17, 2017, as its expiration date is May 18, 2017.

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/PlatformHook.java

    r12217 r12219  
    22package org.openstreetmap.josm.tools;
    33
     4import static org.openstreetmap.josm.tools.I18n.tr;
     5
     6import java.awt.Dimension;
    47import java.awt.GraphicsEnvironment;
    58import java.io.BufferedReader;
     
    1316import java.security.cert.CertificateException;
    1417import java.security.cert.X509Certificate;
     18import java.text.DateFormat;
     19import java.util.Date;
    1520import java.util.List;
    1621
     22import javax.swing.JOptionPane;
     23
     24import org.openstreetmap.josm.Main;
     25import org.openstreetmap.josm.gui.ExtendedDialog;
    1726import org.openstreetmap.josm.io.CertificateAmendment.CertAmend;
     27import org.openstreetmap.josm.tools.date.DateUtils;
    1828
    1929/**
     
    224234     */
    225235    List<File> getDefaultProj4NadshiftDirectories();
     236
     237    /**
     238     * Determines if the JVM is OpenJDK-based.
     239     * @return {@code true} if {@code java.home} contains "openjdk", {@code false} otherwise
     240     * @since 12219
     241     */
     242    default boolean isOpenJDK() {
     243        String javaHome = System.getProperty("java.home");
     244        return javaHome != null && javaHome.contains("openjdk");
     245    }
     246
     247    /**
     248     * Asks user to update its version of Java.
     249     * @param updVersion target update version
     250     * @param url download URL
     251     * @param major true for a migration towards a major version of Java (8->9), false otherwise
     252     * @param eolDate the EOL/expiration date
     253     * @since 12219
     254     */
     255    default void askUpdateJava(String updVersion, String url, String eolDate, boolean major) {
     256        ExtendedDialog ed = new ExtendedDialog(
     257                Main.parent,
     258                tr("Outdated Java version"),
     259                new String[]{tr("OK"), tr("Update Java"), tr("Cancel")});
     260        // Check if the dialog has not already been permanently hidden by user
     261        if (!ed.toggleEnable("askUpdateJava"+updVersion).toggleCheckState()) {
     262            ed.setButtonIcons(new String[]{"ok", "java", "cancel"}).setCancelButton(3);
     263            ed.setMinimumSize(new Dimension(480, 300));
     264            ed.setIcon(JOptionPane.WARNING_MESSAGE);
     265            StringBuilder content = new StringBuilder(tr("You are running version {0} of Java.",
     266                    "<b>"+System.getProperty("java.version")+"</b>")).append("<br><br>");
     267            if ("Sun Microsystems Inc.".equals(System.getProperty("java.vendor")) && !isOpenJDK()) {
     268                content.append("<b>").append(tr("This version is no longer supported by {0} since {1} and is not recommended for use.",
     269                        "Oracle", eolDate)).append("</b><br><br>");
     270            }
     271            content.append("<b>")
     272                   .append(major ?
     273                        tr("JOSM will soon stop working with this version; we highly recommend you to update to Java {0}.", updVersion) :
     274                        tr("You may face critical Java bugs; we highly recommend you to update to Java {0}.", updVersion))
     275                   .append("</b><br><br>")
     276                   .append(tr("Would you like to update now ?"));
     277            ed.setContent(content.toString());
     278
     279            if (ed.showDialog().getValue() == 2) {
     280                try {
     281                    openUrl(url);
     282                } catch (IOException e) {
     283                    Main.warn(e);
     284                }
     285            }
     286        }
     287    }
     288
     289    /**
     290     * Checks if the running version of Java has expired, proposes to user to update it if needed.
     291     * @since 12219
     292     */
     293    default void checkExpiredJava() {
     294        Date expiration = Utils.getJavaExpirationDate();
     295        if (expiration != null && expiration.before(new Date())) {
     296            String version = Utils.getJavaLatestVersion();
     297            askUpdateJava(version != null ? version : "latest",
     298                    Main.pref.get("java.update.url", "https://www.java.com/download"),
     299                    DateUtils.getDateFormat(DateFormat.MEDIUM).format(expiration), false);
     300        }
     301    }
    226302}
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookOsx.java

    r12217 r12219  
    7979            Main.warn("Failed to register with OSX: " + ex);
    8080        }
     81        checkExpiredJava();
    8182    }
    8283
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookUnixoid.java

    r12217 r12219  
    55
    66import java.awt.Desktop;
    7 import java.awt.Dimension;
    87import java.awt.event.KeyEvent;
    98import java.io.BufferedReader;
     
    2019import java.util.Locale;
    2120
    22 import javax.swing.JOptionPane;
    23 
    2421import org.openstreetmap.josm.Main;
    25 import org.openstreetmap.josm.gui.ExtendedDialog;
    26 import org.openstreetmap.josm.gui.util.GuiHelper;
    2722
    2823/**
    29  * {@code PlatformHook} base implementation.
    30  *
    31  * Don't write (Main.platform instanceof PlatformHookUnixoid) because other platform
    32  * hooks are subclasses of this class.
     24 * {@code PlatformHook} implementation for Unix systems.
     25 * @since 1023
    3326 */
    3427public class PlatformHookUnixoid implements PlatformHook {
     
    9790            return false;
    9891        }
    99     }
    100 
    101     /**
    102      * Determines if the JVM is OpenJDK-based.
    103      * @return {@code true} if {@code java.home} contains "openjdk", {@code false} otherwise
    104      * @since 6951
    105      */
    106     public static boolean isOpenJDK() {
    107         String javaHome = System.getProperty("java.home");
    108         return javaHome != null && javaHome.contains("openjdk");
    10992    }
    11093
     
    200183    }
    201184
    202     protected String buildOSDescription() {
     185    private String buildOSDescription() {
    203186        String osName = System.getProperty("os.name");
    204187        if ("Linux".equalsIgnoreCase(osName)) {
     
    246229    }
    247230
    248     protected static class LinuxReleaseInfo {
     231    private static class LinuxReleaseInfo {
    249232        private final String path;
    250233        private final String descriptionField;
     
    254237        private final String prefix;
    255238
    256         public LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField) {
     239        LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField) {
    257240            this(path, descriptionField, idField, releaseField, false, null);
    258241        }
    259242
    260         public LinuxReleaseInfo(String path) {
     243        LinuxReleaseInfo(String path) {
    261244            this(path, null, null, null, true, null);
    262245        }
    263246
    264         public LinuxReleaseInfo(String path, String prefix) {
     247        LinuxReleaseInfo(String path, String prefix) {
    265248            this(path, null, null, null, true, prefix);
    266249        }
     
    275258        }
    276259
    277         @Override public String toString() {
     260        @Override
     261        public String toString() {
    278262            return "ReleaseInfo [path=" + path + ", descriptionField=" + descriptionField +
    279263                    ", idField=" + idField + ", releaseField=" + releaseField + ']';
     
    331315    }
    332316
    333     // Method unused, but kept for translation already done. To reuse during Java 9 migration
    334     protected void askUpdateJava(final String version, final String url) {
    335         GuiHelper.runInEDTAndWait(() -> {
    336             ExtendedDialog ed = new ExtendedDialog(
    337                     Main.parent,
    338                     tr("Outdated Java version"),
    339                     new String[]{tr("OK"), tr("Update Java"), tr("Cancel")});
    340             // Check if the dialog has not already been permanently hidden by user
    341             if (!ed.toggleEnable("askUpdateJava9").toggleCheckState()) {
    342                 ed.setButtonIcons(new String[]{"ok", "java", "cancel"}).setCancelButton(3);
    343                 ed.setMinimumSize(new Dimension(480, 300));
    344                 ed.setIcon(JOptionPane.WARNING_MESSAGE);
    345                 StringBuilder content = new StringBuilder(tr("You are running version {0} of Java.", "<b>"+version+"</b>"))
    346                         .append("<br><br>");
    347                 if ("Sun Microsystems Inc.".equals(System.getProperty("java.vendor")) && !isOpenJDK()) {
    348                     content.append("<b>").append(tr("This version is no longer supported by {0} since {1} and is not recommended for use.",
    349                             "Oracle", tr("April 2015"))).append("</b><br><br>"); // TODO: change date once Java 8 EOL is announced
    350                 }
    351                 content.append("<b>")
    352                        .append(tr("JOSM will soon stop working with this version; we highly recommend you to update to Java {0}.", "8"))
    353                        .append("</b><br><br>")
    354                        .append(tr("Would you like to update now ?"));
    355                 ed.setContent(content.toString());
    356 
    357                 if (ed.showDialog().getValue() == 2) {
    358                     try {
    359                         openUrl(url);
    360                     } catch (IOException e) {
    361                         Main.warn(e);
    362                     }
    363                 }
    364             }
    365         });
    366     }
    367 
    368317    /**
    369318     * Get the dot directory <code>~/.josm</code>.
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookWindows.java

    r12218 r12219  
    180180            }
    181181        }
     182    }
     183
     184    @Override
     185    public void startupHook() {
     186        checkExpiredJava();
    182187    }
    183188
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r12217 r12219  
    3232import java.security.PrivilegedAction;
    3333import java.text.Bidi;
     34import java.text.DateFormat;
    3435import java.text.MessageFormat;
     36import java.text.ParseException;
    3537import java.util.AbstractCollection;
    3638import java.util.AbstractList;
     
    3941import java.util.Collection;
    4042import java.util.Collections;
     43import java.util.Date;
    4144import java.util.Iterator;
    4245import java.util.List;
     
    16461649        return Integer.parseInt(version.substring(bPos > -1 ? bPos + 1 : pPos + 1, version.length()));
    16471650    }
     1651
     1652    /**
     1653     * Returns the JRE expiration date.
     1654     * @return the JRE expiration date, or null
     1655     * @since 12219
     1656     */
     1657    public static Date getJavaExpirationDate() {
     1658        try {
     1659            Object value = Class.forName("com.sun.deploy.config.BuiltInProperties").getDeclaredField("JRE_EXPIRATION_DATE").get(null);
     1660            if (value instanceof String) {
     1661                return DateFormat.getDateInstance(3, Locale.US).parse((String) value);
     1662            }
     1663        } catch (IllegalArgumentException | ReflectiveOperationException | SecurityException | ParseException e) {
     1664            Main.debug(e);
     1665        }
     1666        return null;
     1667    }
     1668
     1669    /**
     1670     * Returns the latest version of Java, from Oracle website.
     1671     * @return the latest version of Java, from Oracle website
     1672     * @since 12219
     1673     */
     1674    public static String getJavaLatestVersion() {
     1675        try {
     1676            return HttpClient.create(
     1677                    new URL(Main.pref.get("java.baseline.version.url", "http://javadl-esd-secure.oracle.com/update/baseline.version")))
     1678                    .connect().fetchContent().split("\n")[0];
     1679        } catch (IOException e) {
     1680            Main.error(e);
     1681        }
     1682        return null;
     1683    }
    16481684}
Note: See TracChangeset for help on using the changeset viewer.