< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/file/Locations.java

Print this page
*** 128,12 ***
  
      static final Path javaHome = FileSystems.getDefault().getPath(System.getProperty("java.home"));
      static final Path thisSystemModules = javaHome.resolve("lib").resolve("modules");
  
      Map<Path, FileSystem> fileSystems = new LinkedHashMap<>();
!     List<Closeable> closeables = new ArrayList<>();
      private String releaseVersion = null;
  
      Locations() {
          initHandlers();
      }
  
--- 128,14 ---
  
      static final Path javaHome = FileSystems.getDefault().getPath(System.getProperty("java.home"));
      static final Path thisSystemModules = javaHome.resolve("lib").resolve("modules");
  
      Map<Path, FileSystem> fileSystems = new LinkedHashMap<>();
!     // List of resources to be closed (self-sychronized).
+     private final List<Closeable> closeables = new ArrayList<>();
      private String releaseVersion = null;
+     private boolean previewMode = false;
  
      Locations() {
          initHandlers();
      }
  

*** 143,19 ***
          } catch (InvalidPathException ipe) {
              throw new IllegalArgumentException(ipe);
          }
      }
  
      public void close() throws IOException {
          ListBuffer<IOException> list = new ListBuffer<>();
!         closeables.forEach(closeable -> {
              try {
                  closeable.close();
              } catch (IOException ex) {
                  list.add(ex);
              }
!         });
          if (list.nonEmpty()) {
              IOException ex = new IOException();
              for (IOException e: list)
                  ex.addSuppressed(e);
              throw ex;
--- 145,30 ---
          } catch (InvalidPathException ipe) {
              throw new IllegalArgumentException(ipe);
          }
      }
  
+     private void addCloseable(Closeable c) {
+         synchronized (closeables) {
+             closeables.add(c);
+         }
+     }
+ 
      public void close() throws IOException {
          ListBuffer<IOException> list = new ListBuffer<>();
!         Closeable[] arr;
+         synchronized (closeables) {
+             arr = closeables.toArray(Closeable[]::new);
+             closeables.clear();
+         }
+         for (Closeable closeable : arr) {
              try {
                  closeable.close();
              } catch (IOException ex) {
                  list.add(ex);
              }
!         }
          if (list.nonEmpty()) {
              IOException ex = new IOException();
              for (IOException e: list)
                  ex.addSuppressed(e);
              throw ex;

*** 224,10 ***
--- 237,15 ---
      public void setMultiReleaseValue(String multiReleaseValue) {
          // Null is implicitly allowed and unsets the value.
          this.releaseVersion = multiReleaseValue;
      }
  
+     public void setPreviewMode(boolean previewMode) {
+         // Null is implicitly allowed and unsets the value.
+         this.previewMode = previewMode;
+     }
+ 
      private boolean contains(Collection<Path> searchPath, Path file) throws IOException {
  
          if (searchPath == null) {
              return false;
          }

*** 1457,11 ***
                              try {
                                  Path moduleInfoClass = fs.getPath("classes/module-info.class");
                                  String moduleName = readModuleName(moduleInfoClass);
                                  Path modulePath = fs.getPath("classes");
                                  fileSystems.put(p, fs);
!                                 closeables.add(fs);
                                  fs = null; // prevent fs being closed in the finally clause
                                  return new Pair<>(moduleName, modulePath);
                              } finally {
                                  if (fs != null)
                                      fs.close();
--- 1475,11 ---
                              try {
                                  Path moduleInfoClass = fs.getPath("classes/module-info.class");
                                  String moduleName = readModuleName(moduleInfoClass);
                                  Path modulePath = fs.getPath("classes");
                                  fileSystems.put(p, fs);
!                                 addCloseable(fs);
                                  fs = null; // prevent fs being closed in the finally clause
                                  return new Pair<>(moduleName, modulePath);
                              } finally {
                                  if (fs != null)
                                      fs.close();

*** 1948,28 ***
                  try {
                      URI jrtURI = URI.create("jrt:/");
                      FileSystem jrtfs;
  
                      if (isCurrentPlatform(systemJavaHome)) {
!                         jrtfs = FileSystems.getFileSystem(jrtURI);
                      } else {
                          try {
                              Map<String, String> attrMap =
!                                     Collections.singletonMap("java.home", systemJavaHome.toString());
                              jrtfs = FileSystems.newFileSystem(jrtURI, attrMap);
                          } catch (ProviderNotFoundException ex) {
                              URL jfsJar = resolveInJavaHomeLib(systemJavaHome, "jrt-fs.jar").toUri().toURL();
                              ClassLoader currentLoader = Locations.class.getClassLoader();
                              URLClassLoader fsLoader =
                                      new URLClassLoader(new URL[] {jfsJar}, currentLoader);
  
                              jrtfs = FileSystems.newFileSystem(jrtURI, Collections.emptyMap(), fsLoader);
  
!                             closeables.add(fsLoader);
                          }
  
!                         closeables.add(jrtfs);
                      }
  
                      modules = jrtfs.getPath("/modules");
                  } catch (FileSystemNotFoundException | ProviderNotFoundException e) {
                      modules = resolveInJavaHomeLib(systemJavaHome, "modules");
--- 1966,31 ---
                  try {
                      URI jrtURI = URI.create("jrt:/");
                      FileSystem jrtfs;
  
                      if (isCurrentPlatform(systemJavaHome)) {
!                         JRTIndex jrtIndex = JRTIndex.instance(previewMode);
+                         addCloseable(jrtIndex);
+                         jrtfs = jrtIndex.getFileSystem();
                      } else {
                          try {
                              Map<String, String> attrMap =
!                                     Map.of("java.home", systemJavaHome.toString(),
+                                             "previewMode", String.valueOf(previewMode));
                              jrtfs = FileSystems.newFileSystem(jrtURI, attrMap);
                          } catch (ProviderNotFoundException ex) {
                              URL jfsJar = resolveInJavaHomeLib(systemJavaHome, "jrt-fs.jar").toUri().toURL();
                              ClassLoader currentLoader = Locations.class.getClassLoader();
                              URLClassLoader fsLoader =
                                      new URLClassLoader(new URL[] {jfsJar}, currentLoader);
  
                              jrtfs = FileSystems.newFileSystem(jrtURI, Collections.emptyMap(), fsLoader);
  
!                             addCloseable(fsLoader);
                          }
  
!                         addCloseable(jrtfs);
                      }
  
                      modules = jrtfs.getPath("/modules");
                  } catch (FileSystemNotFoundException | ProviderNotFoundException e) {
                      modules = resolveInJavaHomeLib(systemJavaHome, "modules");
< prev index next >