1 /* 2 * Copyright (c) 2012, 2017, 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 /** Avoid using directly. Only public for the needs of unit testing. */ 36 public final class Versions { 37 static final Versions VERSIONS; 38 static { 39 String home = System.getProperty("java.home"); 40 VERSIONS = new Versions(home == null ? null : new File(home).toPath()); 41 } 42 43 private final Map<Object, Object> versions; 44 45 public Versions(Path home) { 46 Map<Object, Object> map = new HashMap<>(); 47 ASSIGN: try { 48 Path info = findReleaseInfo(home); 49 if (info == null) { 50 break ASSIGN; 51 } 52 for (String line : Files.readAllLines(info)) { 53 final String prefix = "SOURCE="; 54 if (line.startsWith(prefix)) { 55 for (String versionInfo : line.substring(prefix.length()).replace('"', ' ').split(" ")) { 56 String[] idVersion = versionInfo.split(":"); 57 if (idVersion != null && idVersion.length == 2) { 58 map.put("version." + idVersion[0], idVersion[1]); 59 } 60 } 61 break ASSIGN; 62 } 63 } 64 } catch (IOException ex) { 65 // no versions file found 66 } 67 versions = Collections.unmodifiableMap(map); 68 } 69 70 public Map<Object, Object> withVersions(Map<Object, Object> properties) { 71 if (properties == null) { 72 return versions; 73 } else { 74 properties.putAll(versions); 75 return properties; 76 } 77 } 78 79 private static Path findReleaseInfo(Path jreDir) { 80 if (jreDir == null) { 81 return null; 82 } 83 Path releaseInJre = jreDir.resolve("release"); 84 if (Files.exists(releaseInJre)) { 85 return releaseInJre; 86 } 87 Path jdkDir = jreDir.getParent(); 88 if (jdkDir == null) { 89 return null; 90 } 91 Path releaseInJdk = jdkDir.resolve("release"); 92 return Files.exists(releaseInJdk) ? releaseInJdk : null; 93 } 94 95 }