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.7KB

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