1 /*
   2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.debug;
  26 
  27 import java.io.File;
  28 import java.io.IOException;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.util.Collections;
  32 import java.util.HashMap;
  33 import java.util.Map;
  34 
  35 import jdk.vm.ci.services.Services;
  36 
  37 /** Avoid using directly. Only public for the needs of unit testing. */
  38 public final class Versions {
  39     static final Versions VERSIONS;
  40     static {
  41         String home = Services.getSavedProperties().get("java.home");
  42         VERSIONS = new Versions(home == null ? null : new File(home).toPath());
  43     }
  44 
  45     private final Map<Object, Object> versions;
  46 
  47     public Versions(Path home) {
  48         Map<Object, Object> map = new HashMap<>();
  49         ASSIGN: try {
  50             Path info = findReleaseInfo(home);
  51             if (info == null) {
  52                 break ASSIGN;
  53             }
  54             for (String line : Files.readAllLines(info)) {
  55                 final String prefix = "SOURCE=";
  56                 if (line.startsWith(prefix)) {
  57                     for (String versionInfo : line.substring(prefix.length()).replace('"', ' ').split(" ")) {
  58                         String[] idVersion = versionInfo.split(":");
  59                         if (idVersion != null && idVersion.length == 2) {
  60                             map.put("version." + idVersion[0], idVersion[1]);
  61                         }
  62                     }
  63                     break ASSIGN;
  64                 }
  65             }
  66         } catch (IOException ex) {
  67             // no versions file found
  68         }
  69         versions = Collections.unmodifiableMap(map);
  70     }
  71 
  72     public Map<Object, Object> withVersions(Map<Object, Object> properties) {
  73         if (properties == null) {
  74             return versions;
  75         } else {
  76             properties.putAll(versions);
  77             return properties;
  78         }
  79     }
  80 
  81     private static Path findReleaseInfo(Path jreDir) {
  82         if (jreDir == null) {
  83             return null;
  84         }
  85         Path releaseInJre = jreDir.resolve("release");
  86         if (Files.exists(releaseInJre)) {
  87             return releaseInJre;
  88         }
  89         Path jdkDir = jreDir.getParent();
  90         if (jdkDir == null) {
  91             return null;
  92         }
  93         Path releaseInJdk = jdkDir.resolve("release");
  94         return Files.exists(releaseInJdk) ? releaseInJdk : null;
  95     }
  96 
  97 }