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.

Installer.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2010, 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.IOException;
  26. import java.io.InputStream;
  27. import java.nio.file.*;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. /**
  31. * @author Kerstin Breiteneder
  32. * @author Christoph Wimberger
  33. * @author Ivan Dubrov
  34. * @author Jiri Bubnik
  35. * @author Przemysław Rumik
  36. */
  37. public class Installer {
  38. private ConfigurationInfo config;
  39. public Installer(ConfigurationInfo config) {
  40. this.config = config;
  41. }
  42. public void install(Path dir, boolean bit64, boolean altjvm) throws IOException {
  43. if (config.isJDK(dir)) {
  44. dir = dir.resolve(config.getJREDirectory());
  45. }
  46. if (!altjvm) {
  47. Path serverPath = dir.resolve(config.getServerPath(bit64));
  48. if (Files.exists(serverPath)) {
  49. installClientServer(serverPath, bit64);
  50. }
  51. Path clientPath = dir.resolve(config.getClientPath());
  52. if (Files.exists(clientPath) && !bit64) {
  53. installClientServer(clientPath, false);
  54. }
  55. } else {
  56. Path altjvmPath = dir.resolve(bit64 ? config.getDcevm64Path() : config.getDcevm32Path());
  57. if (!Files.exists(altjvmPath)) {
  58. Files.createDirectory(altjvmPath);
  59. }
  60. installClientServer(altjvmPath, bit64);
  61. }
  62. }
  63. /**
  64. * Try to uninstall DCEVM from all locations (skip if not exists).
  65. */
  66. public void uninstall(Path dir, boolean bit64) throws IOException {
  67. if (config.isJDK(dir)) {
  68. dir = dir.resolve(config.getJREDirectory());
  69. }
  70. Path serverPath = dir.resolve(config.getServerPath(bit64));
  71. if (Files.exists(serverPath)) {
  72. uninstallClientServer(serverPath);
  73. }
  74. Path clientPath = dir.resolve(config.getClientPath());
  75. if (Files.exists(clientPath) && !bit64) {
  76. uninstallClientServer(clientPath);
  77. }
  78. Path dcevm32Path = dir.resolve(config.getDcevm32Path());
  79. if (Files.exists(dcevm32Path)) {
  80. Files.deleteIfExists(dcevm32Path.resolve(config.getLibraryName()));
  81. Files.deleteIfExists(dcevm32Path.resolve(config.getBackupLibraryName()));
  82. Files.delete(dcevm32Path);
  83. }
  84. Path dcevm64Path = dir.resolve(config.getDcevm64Path());
  85. if (Files.exists(dcevm64Path)) {
  86. Files.deleteIfExists(dcevm64Path.resolve(config.getLibraryName()));
  87. Files.deleteIfExists(dcevm64Path.resolve(config.getBackupLibraryName()));
  88. Files.delete(dcevm64Path);
  89. }
  90. }
  91. public List<Installation> listInstallations() {
  92. return scanPaths(config.paths());
  93. }
  94. public ConfigurationInfo getConfiguration() {
  95. return config;
  96. }
  97. private void installClientServer(Path path, boolean bit64) throws IOException {
  98. String resource = config.getResourcePath(bit64) + "/product/" + config.getLibraryName();
  99. Path library = path.resolve(config.getLibraryName());
  100. Path backup = path.resolve(config.getBackupLibraryName());
  101. // backup any existing library (assume original JVM file)
  102. if (Files.exists(library)) {
  103. Files.move(library, backup);
  104. }
  105. try {
  106. // install actual DCEVM file
  107. try (InputStream in = getClass().getClassLoader().getResourceAsStream(resource)) {
  108. if (in == null) {
  109. throw new IOException("DCEVM not available for java at '" + path + "'. Missing resource " + resource);
  110. }
  111. Files.copy(in, library);
  112. }
  113. } catch (NullPointerException | IOException e) {
  114. // try to restore original file
  115. if (Files.exists(backup)) {
  116. Files.move(backup, library, StandardCopyOption.REPLACE_EXISTING);
  117. }
  118. throw e;
  119. }
  120. }
  121. private void uninstallClientServer(Path path) throws IOException {
  122. Path library = path.resolve(config.getLibraryName());
  123. Path backup = path.resolve(config.getBackupLibraryName());
  124. if (Files.exists(backup)) {
  125. Files.delete(library);
  126. Files.move(backup, library);
  127. }
  128. }
  129. private List<Installation> scanPaths(String... dirPaths) {
  130. List<Installation> installations = new ArrayList<>();
  131. for (String dirPath : dirPaths) {
  132. Path dir = Paths.get(dirPath);
  133. if (Files.isDirectory(dir)) {
  134. try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
  135. scanDirectory(stream, installations);
  136. } catch (Exception ex) {
  137. // Ignore, try different directory
  138. ex.printStackTrace();
  139. }
  140. }
  141. }
  142. return installations;
  143. }
  144. private void scanDirectory(DirectoryStream<Path> stream, List<Installation> installations) {
  145. for (Path path : stream) {
  146. if (Files.isDirectory(path)) {
  147. if (config.isJDK(path) || config.isJRE(path)) {
  148. try {
  149. Installation inst = new Installation(config, path);
  150. if (!installations.contains(inst)) {
  151. installations.add(inst);
  152. }
  153. } catch (Exception ex) {
  154. // FIXME: just ignore the installation for now..
  155. ex.printStackTrace();
  156. }
  157. } else {
  158. // in macOS/OSX we have more complicated strucuture of directories with JVM...
  159. // for example it may be /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home
  160. // so we will look deper in structure of passed folders hoping that we will find JVM ;-)
  161. try {
  162. scanDirectory(Files.newDirectoryStream(path),installations);
  163. } catch (IOException ignore) {
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }