You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigurationInfo.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (c) 2013, 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. package com.github.dcevm.installer;
  25. import java.io.BufferedReader;
  26. import java.io.IOException;
  27. import java.io.InputStream;
  28. import java.io.InputStreamReader;
  29. import java.nio.file.Files;
  30. import java.nio.file.Path;
  31. import java.util.regex.Matcher;
  32. import java.util.regex.Pattern;
  33. /**
  34. * @author Kerstin Breiteneder
  35. * @author Christoph Wimberger
  36. * @author Ivan Dubrov
  37. * @author Jiri Bubnik
  38. * @author Przemysław Rumik
  39. * @author Denis Zygann
  40. */
  41. public enum ConfigurationInfo {
  42. // Note: 32-bit is not supported on Mac OS X
  43. MAC_OS(null, "bsd_amd64_compiler2",
  44. "lib/client", "lib/server", "lib/dcevm", "lib/server", "lib/dcevm",
  45. "bin/java", "libjvm.dylib") {
  46. @Override
  47. public String[] paths() {
  48. return new String[] { "/Library/Java/JavaVirtualMachines/" };
  49. }
  50. },
  51. LINUX("linux_i486_compiler2", "linux_amd64_compiler2",
  52. "lib/i386/client", "lib/i386/server", "lib/i386/dcevm", "lib/amd64/server", "lib/amd64/dcevm",
  53. "bin/java", "libjvm.so") {
  54. @Override
  55. public String[] paths() {
  56. return new String[]{"/usr/java", "/usr/lib/jvm"};
  57. }
  58. },
  59. WINDOWS("windows_i486_compiler2", "windows_amd64_compiler2",
  60. "bin/client", "bin/server", "bin/dcevm", "bin/server", "bin/dcevm",
  61. "bin/java.exe", "jvm.dll") {
  62. @Override
  63. public String[] paths() {
  64. return new String[]{
  65. System.getenv("JAVA_HOME") + "/..",
  66. System.getenv("PROGRAMW6432") + "/JAVA",
  67. System.getenv("PROGRAMFILES") + "/JAVA",
  68. System.getenv("PROGRAMFILES(X86)") + "/JAVA",
  69. System.getenv("SYSTEMDRIVE") + "/JAVA"};
  70. }
  71. };
  72. private final String resourcePath32;
  73. private final String resourcePath64;
  74. private final String clientPath;
  75. private final String server32Path;
  76. private final String dcevm32Path;
  77. private final String server64Path;
  78. private final String dcevm64Path;
  79. private final String javaExecutable;
  80. private final String libraryName;
  81. ConfigurationInfo(String resourcePath32, String resourcePath64,
  82. String clientPath,
  83. String server32Path, String dcevm32Path,
  84. String server64Path, String dcevm64Path,
  85. String javaExecutable, String libraryName) {
  86. this.resourcePath32 = resourcePath32;
  87. this.resourcePath64 = resourcePath64;
  88. this.clientPath = clientPath;
  89. this.server32Path = server32Path;
  90. this.dcevm32Path = dcevm32Path;
  91. this.server64Path = server64Path;
  92. this.dcevm64Path = dcevm64Path;
  93. this.javaExecutable = javaExecutable;
  94. this.libraryName = libraryName;
  95. }
  96. public String getResourcePath(boolean bit64) {
  97. return bit64 ? resourcePath64 : resourcePath32;
  98. }
  99. public String getResourcePath32() {
  100. return resourcePath32;
  101. }
  102. public String getResourcePath64() {
  103. return resourcePath64;
  104. }
  105. public String getClientPath() {
  106. return clientPath;
  107. }
  108. public String getServerPath(boolean bit64) {
  109. return bit64 ? server64Path : server32Path;
  110. }
  111. public String getServer32Path() {
  112. return server32Path;
  113. }
  114. public String getServer64Path() {
  115. return server64Path;
  116. }
  117. public String getDcevm32Path() {
  118. return dcevm32Path;
  119. }
  120. public String getDcevm64Path() {
  121. return dcevm64Path;
  122. }
  123. public String getJavaExecutable() {
  124. return javaExecutable;
  125. }
  126. public String getLibraryName() {
  127. return libraryName;
  128. }
  129. public String getBackupLibraryName() {
  130. return libraryName + ".backup";
  131. }
  132. public String[] paths() {
  133. return new String[0];
  134. }
  135. public static ConfigurationInfo current() {
  136. String os = System.getProperty("os.name").toLowerCase();
  137. if (os.contains("windows")) {
  138. return ConfigurationInfo.WINDOWS;
  139. } else if (os.contains("mac") || os.contains("darwin")) {
  140. return ConfigurationInfo.MAC_OS;
  141. } else if (os.contains("unix") || os.contains("linux")) {
  142. return ConfigurationInfo.LINUX;
  143. }
  144. throw new IllegalStateException("OS is unsupported: " + os);
  145. }
  146. // Utility methods to query installation directories
  147. public boolean isJRE(Path directory) {
  148. if (Files.isDirectory(directory)) {
  149. if (!Files.exists(directory.resolve(getJavaExecutable()))) {
  150. return false;
  151. }
  152. Path client = directory.resolve(getClientPath());
  153. if (Files.exists(client)) {
  154. if (!Files.exists(client.resolve(getLibraryName()))) {
  155. return Files.exists(client.resolve(getBackupLibraryName()));
  156. }
  157. }
  158. Path server = directory.resolve(getServer64Path());
  159. if (Files.exists(server)) {
  160. if (!Files.exists(server.resolve(getLibraryName()))) {
  161. return Files.exists(server.resolve(getBackupLibraryName()));
  162. }
  163. }
  164. return true;
  165. }
  166. return false;
  167. }
  168. public boolean isJDK(Path directory) {
  169. if (Files.isDirectory(directory)) {
  170. Path jreDir = directory.resolve(getJREDirectory());
  171. return isJRE(jreDir);
  172. }
  173. return false;
  174. }
  175. public String executeJava(Path jreDir, String... params) throws IOException {
  176. Path executable = jreDir.resolve(getJavaExecutable());
  177. String[] commands = new String[params.length + 1];
  178. System.arraycopy(params, 0, commands, 1, params.length);
  179. commands[0] = executable.toAbsolutePath().toString();
  180. Process p = Runtime.getRuntime().exec(commands);
  181. StringBuilder result = new StringBuilder();
  182. try (InputStream in = p.getErrorStream();
  183. BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
  184. String line;
  185. while ((line = reader.readLine()) != null) {
  186. result.append(line);
  187. result.append('\n');
  188. }
  189. }
  190. return result.toString();
  191. }
  192. public boolean isDCEInstalled(Path dir, boolean altjvm) {
  193. Path jreDir;
  194. if (isJDK(dir)) {
  195. jreDir = dir.resolve("jre");
  196. } else {
  197. jreDir = dir;
  198. }
  199. if (altjvm) {
  200. Path altvm32Path = jreDir.resolve(getDcevm32Path());
  201. Path altvm64Path = jreDir.resolve(getDcevm64Path());
  202. return Files.exists(altvm32Path) || Files.exists(altvm64Path);
  203. } else {
  204. Path clientPath = jreDir.resolve(getClientPath());
  205. Path clientBackup = clientPath.resolve(getBackupLibraryName());
  206. Path serverPath = jreDir.resolve(getServer32Path());
  207. if (!Files.exists(serverPath)) {
  208. serverPath = jreDir.resolve(getServer64Path());
  209. }
  210. Path serverBackup = serverPath.resolve(getBackupLibraryName());
  211. if (Files.exists(clientPath) && Files.exists(serverPath)) {
  212. if (Files.exists(clientBackup) != Files.exists(serverBackup)) {
  213. throw new IllegalStateException(jreDir.toAbsolutePath() + " has invalid state.");
  214. }
  215. }
  216. return Files.exists(clientBackup) || Files.exists(serverBackup);
  217. }
  218. }
  219. public String getVersionString(Path jreDir, boolean altjvm) {
  220. try {
  221. if (altjvm) {
  222. return executeJava(jreDir, "-XXaltjvm=dcevm", "-version");
  223. } else {
  224. return executeJava(jreDir, "-version");
  225. }
  226. } catch (Throwable e) {
  227. return e.getMessage();
  228. }
  229. }
  230. public boolean is64Bit(Path jreDir) {
  231. String versionString = getVersionString(jreDir, false);
  232. return versionString.contains("64-Bit") || versionString.contains("amd64");
  233. }
  234. public String getJavaVersion(Path jreDir) throws IOException {
  235. return getVersionHelper(jreDir, ".*java version.*\"(.*)\".*", true, false);
  236. }
  237. final public String getDCEVersion(Path jreDir, boolean altjvm) throws IOException {
  238. return getVersionHelper(jreDir, ".*Dynamic Code Evolution.*build ([^,]+),.*", false, altjvm);
  239. }
  240. private String getVersionHelper(Path jreDir, String regex, boolean javaVersion, boolean altjvm) {
  241. String version = getVersionString(jreDir, altjvm);
  242. version = version.replaceAll("\n", "");
  243. Matcher matcher = Pattern.compile(regex).matcher(version);
  244. if (!matcher.matches()) {
  245. return "Could not get " + (javaVersion ? "java" : "dce") +
  246. "version of " + jreDir.toAbsolutePath() + ".";
  247. }
  248. version = matcher.replaceFirst("$1");
  249. return version;
  250. }
  251. public String getJREDirectory() {
  252. return "jre";
  253. }
  254. }