Ignore:
Timestamp:
2014-07-18T22:27:52+02:00 (10 years ago)
Author:
Don-vip
Message:

see #10272 - refactor Linux package info retrieval to work on more distributions than Debian/Ubuntu. Only relies on presence of dpkg-query or rpm instead of distribution name

File:
1 edited

Legend:

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

    r7206 r7314  
    1616import java.net.URISyntaxException;
    1717import java.nio.charset.StandardCharsets;
     18import java.nio.file.Files;
     19import java.nio.file.Paths;
    1820import java.security.KeyStore;
    1921import java.security.KeyStoreException;
     
    113115
    114116    /**
    115      * Determines if the distribution is Debian or Ubuntu, or a derivative.
    116      * @return {@code true} if the distribution is Debian, Ubuntu or Mint, {@code false} otherwise
    117      */
    118     public static boolean isDebianOrUbuntu() {
    119         try {
    120             String dist = Utils.execOutput(Arrays.asList("lsb_release", "-i", "-s"));
    121             return "Debian".equalsIgnoreCase(dist) || "Ubuntu".equalsIgnoreCase(dist) || "Mint".equalsIgnoreCase(dist);
    122         } catch (IOException e) {
    123             Main.warn(e);
    124             return false;
    125         }
    126     }
    127 
    128     /**
    129117     * Determines if the JVM is OpenJDK-based.
    130118     * @return {@code true} if {@code java.home} contains "openjdk", {@code false} otherwise
     
    138126    /**
    139127     * Get the package name including detailed version.
    140      * @param packageName The package name
     128     * @param packageNames The possible package names (when a package can have different names on different distributions)
    141129     * @return The package name and package version if it can be identified, null otherwise
    142      */
    143     public static String getPackageDetails(String packageName) {
     130     * @since 7314
     131     */
     132    public static String getPackageDetails(String ... packageNames) {
    144133        try {
    145             String version = Utils.execOutput(Arrays.asList(
    146                     "dpkg-query", "--show", "--showformat", "${Architecture}-${Version}", packageName));
    147             if (version != null) {
    148                 return packageName + ":" + version;
     134            boolean dpkg = Files.exists(Paths.get("/usr/bin/dpkg-query"));
     135            boolean rpm  = Files.exists(Paths.get("/usr/bin/rpm"));
     136            if (dpkg || rpm) {
     137                for (String packageName : packageNames) {
     138                    String[] args = null;
     139                    if (dpkg) {
     140                        args = new String[] {"dpkg-query", "--show", "--showformat", "${Architecture}-${Version}", packageName};
     141                    } else {
     142                        args = new String[] {"rpm", "-q", "--qf", "%{arch}-%{version}", packageName};
     143                    }
     144                    String version = Utils.execOutput(Arrays.asList(args));
     145                    if (version != null) {
     146                        return packageName + ":" + version;
     147                    }
     148                }
    149149            }
    150150        } catch (IOException e) {
     
    160160     * to the Java version, we also need the exact package version.
    161161     *
    162      * This was originally written for #8921, so only Debian based distributions
    163      * are covered at the moment. This can be extended to other distributions
    164      * if needed.
    165      *
    166      * @return The package name and package version if it can be identified, null
    167      * otherwise
     162     * @return The package name and package version if it can be identified, null otherwise
    168163     */
    169164    public String getJavaPackageDetails() {
    170         if (isDebianOrUbuntu()) {
    171             switch(System.getProperty("java.home")) {
    172             case "/usr/lib/jvm/java-7-openjdk-amd64/jre":
    173             case "/usr/lib/jvm/java-7-openjdk-i386/jre":
    174                 return getPackageDetails("openjdk-7-jre");
    175             }
     165        switch(System.getProperty("java.home")) {
     166        case "/usr/lib/jvm/java-7-openjdk-amd64/jre":
     167        case "/usr/lib/jvm/java-7-openjdk-i386/jre":
     168        case "/usr/lib64/jvm/java-1.7.0-openjdk-1.7.0/jre":
     169        case "/usr/lib/jvm/java-1.7.0-openjdk-1.7.0/jre":
     170            return getPackageDetails("openjdk-7-jre", "java-1_7_0-openjdk");
    176171        }
    177172        return null;
     
    181176     * Get the Web Start package name including detailed version.
    182177     *
    183      * Debian and Ubuntu OpenJDK packages are shipped with icedtea-web package,
    184      * but its version does not match main java package version.
    185      *
    186      * Only Debian based distributions are covered at the moment.
    187      * This can be extended to other distributions if needed.
     178     * OpenJDK packages are shipped with icedtea-web package,
     179     * but its version generally does not match main java package version.
    188180     *
    189181     * Simply return {@code null} if there's no separate package for Java WebStart.
     
    192184     */
    193185    public String getWebStartPackageDetails() {
    194         if (isDebianOrUbuntu() && isOpenJDK()) {
    195             return getPackageDetails("icedtea-netx");
     186        if (isOpenJDK()) {
     187            return getPackageDetails("icedtea-netx", "icedtea-web");
    196188        }
    197189        return null;
     
    224216                        new LinuxReleaseInfo("/etc/fedora-release"),
    225217                        new LinuxReleaseInfo("/etc/gentoo-release"),
    226                         new LinuxReleaseInfo("/etc/redhat-release")
     218                        new LinuxReleaseInfo("/etc/redhat-release"),
     219                        new LinuxReleaseInfo("/etc/SuSE-release")
    227220                }) {
    228221                    String description = info.extractDescription();
Note: See TracChangeset for help on using the changeset viewer.