1 /*
   2  * Copyright (c) 2017, 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 
  25 
  26 package jdk.tools.jaotc.collect;
  27 
  28 import java.io.IOException;
  29 import java.net.MalformedURLException;
  30 import java.net.URI;
  31 import java.net.URISyntaxException;
  32 import java.net.URL;
  33 import java.net.URLClassLoader;
  34 import java.nio.file.DirectoryStream;
  35 import java.nio.file.FileSystem;
  36 import java.nio.file.FileSystems;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.util.HashMap;
  40 
  41 public class FileSupport {
  42     public boolean exists(Path path) {
  43         return Files.exists(path);
  44     }
  45 
  46     public boolean isDirectory(Path path) {
  47         return Files.isDirectory(path);
  48     }
  49 
  50     private static FileSystem makeJarFileSystem(Path path) {
  51         try {
  52             return FileSystems.newFileSystem(makeJarFileURI(path), new HashMap<>());
  53         } catch (IOException e) {
  54             throw new InternalError(e);
  55         }
  56     }
  57 
  58     private static URI makeJarFileURI(Path path) {
  59         try {
  60             String name = path.toAbsolutePath().toString();
  61             name = name.replace('\\', '/');
  62             return new URI("jar:file:///" + name + "!/");
  63         } catch (URISyntaxException e) {
  64             throw new InternalError(e);
  65         }
  66     }
  67 
  68     public ClassLoader createClassLoader(Path path, ClassLoader parent) {
  69         try {
  70             return URLClassLoader.newInstance(buildUrls(path), parent);
  71         } catch (MalformedURLException e) {
  72             throw new InternalError(e);
  73         }
  74     }
  75 
  76     public ClassLoader createClassLoader(Path path) throws MalformedURLException {
  77         return URLClassLoader.newInstance(buildUrls(path));
  78     }
  79 
  80     private static URL[] buildUrls(Path path) throws MalformedURLException {
  81         return new URL[]{path.toUri().toURL()};
  82     }
  83 
  84     public Path getJarFileSystemRoot(Path jarFile) {
  85         FileSystem fileSystem = makeJarFileSystem(jarFile);
  86         return fileSystem.getPath("/");
  87     }
  88 
  89     public boolean isAbsolute(Path entry) {
  90         return entry.isAbsolute();
  91     }
  92 
  93     public Path getSubDirectory(FileSystem fileSystem, Path root, Path path) throws IOException {
  94         DirectoryStream<Path> paths = fileSystem.provider().newDirectoryStream(root, null);
  95         for (Path entry : paths) {
  96             Path relative = root.relativize(entry);
  97             if (relative.equals(path)) {
  98                 return entry;
  99             }
 100         }
 101         return null;
 102     }
 103 }