|
| 1 | +package io.github.fvarrui.javapackager.utils; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileNotFoundException; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Properties; |
| 10 | + |
| 11 | +import io.github.fvarrui.javapackager.model.Platform; |
| 12 | + |
| 13 | +public class JDKUtils { |
| 14 | + |
| 15 | + /** |
| 16 | + * Converts "release" file from specified JDK to map |
| 17 | + * @param jdkPath JDK directory path |
| 18 | + * @return Map with all properties |
| 19 | + * @throws FileNotFoundException release file not found |
| 20 | + * @throws IOException release file could not be read |
| 21 | + */ |
| 22 | + public static Map<String, String> getRelease(File jdkPath) throws FileNotFoundException, IOException { |
| 23 | + Map<String, String> propertiesMap = new HashMap<>(); |
| 24 | + |
| 25 | + File releaseFile = new File(jdkPath, "release"); |
| 26 | + |
| 27 | + if (!releaseFile.exists()) { |
| 28 | + releaseFile = new File(jdkPath, "Contents/Home/release"); |
| 29 | + } |
| 30 | + |
| 31 | + if (!releaseFile.exists()) { |
| 32 | + throw new FileNotFoundException("release file not found"); |
| 33 | + } |
| 34 | + |
| 35 | + Properties properties = new Properties(); |
| 36 | + properties.load(new FileInputStream(releaseFile)); |
| 37 | + properties.forEach((key, value) -> propertiesMap.put(key.toString(), value.toString().replaceAll("^\"|\"$", ""))); |
| 38 | + |
| 39 | + return propertiesMap; |
| 40 | + } |
| 41 | + |
| 42 | + public static boolean isValidJdk(Platform platform, File jdkPath) throws Exception { |
| 43 | + |
| 44 | + try { |
| 45 | + Map<String, String> releaseMap = getRelease(jdkPath); |
| 46 | + String osName = releaseMap.get("OS_NAME"); |
| 47 | + switch (platform) { |
| 48 | + case linux: return "Linux".equalsIgnoreCase(osName); |
| 49 | + case mac: return "Darwin".equalsIgnoreCase(osName); |
| 50 | + case windows: return "Windows".equalsIgnoreCase(osName); |
| 51 | + default: |
| 52 | + } |
| 53 | + } catch (FileNotFoundException e) { |
| 54 | + throw new Exception(e.getMessage(), e); |
| 55 | + } catch (IOException e) { |
| 56 | + throw new Exception(e.getMessage(), e); |
| 57 | + } |
| 58 | + |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments