View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.util;
20  
21  import java.io.Closeable;
22  import java.io.File;
23  import java.io.IOException;
24  import java.net.URI;
25  import java.net.URL;
26  import java.net.URLClassLoader;
27  import java.nio.file.DirectoryStream;
28  import java.nio.file.FileSystem;
29  import java.nio.file.FileSystems;
30  import java.nio.file.Files;
31  import java.nio.file.Path;
32  import java.nio.file.Paths;
33  import java.util.ArrayList;
34  import java.util.Collections;
35  import java.util.List;
36  import java.util.Map;
37  
38  import org.apache.commons.io.IOUtils;
39  
40  /**
41   * Wraps a Java 9 JEP 220 modular runtime image. Requires the JRT NIO file system.
42   *
43   * @since 6.3
44   */
45  public class ModularRuntimeImage implements Closeable {
46  
47      static final String MODULES_PATH = File.separator + "modules";
48      static final String PACKAGES_PATH = File.separator + "packages";
49  
50      private final URLClassLoader classLoader;
51      private final FileSystem fileSystem;
52  
53      /**
54       * Constructs a default instance.
55       */
56      @SuppressWarnings("resource") // See #close()
57      public ModularRuntimeImage() {
58          this(null, FileSystems.getFileSystem(URI.create("jrt:/")));
59      }
60  
61      /**
62       * Constructs an instance using the JRT file system implementation from a specific Java Home.
63       *
64       * @param javaHome Path to a Java 9 or greater home.
65       * @throws IOException an I/O error occurs accessing the file system
66       */
67      public ModularRuntimeImage(final String javaHome) throws IOException {
68          final Map<String, ?> emptyMap = Collections.emptyMap();
69          final Path jrePath = Paths.get(javaHome);
70          final Path jrtFsPath = jrePath.resolve("lib").resolve("jrt-fs.jar");
71          this.classLoader = URLClassLoader.newInstance(new URL[] {jrtFsPath.toUri().toURL()});
72          this.fileSystem = FileSystems.newFileSystem(URI.create("jrt:/"), emptyMap, classLoader);
73      }
74  
75      private ModularRuntimeImage(final URLClassLoader cl, final FileSystem fs) {
76          this.classLoader = cl;
77          this.fileSystem = fs;
78      }
79  
80      @Override
81      public void close() throws IOException {
82          IOUtils.close(classLoader);
83          if (fileSystem != null) {
84              try {
85                  fileSystem.close();
86              } catch (final UnsupportedOperationException e) {
87                  // do nothing
88              }
89          }
90      }
91  
92      public FileSystem getFileSystem() {
93          return fileSystem;
94      }
95  
96      /**
97       * Lists all entries in the given directory.
98       *
99       * @param dirPath directory path.
100      * @return a list of dir entries if an I/O error occurs
101      * @throws IOException an I/O error occurs accessing the file system
102      */
103     public List<Path> list(final Path dirPath) throws IOException {
104         final List<Path> list = new ArrayList<>();
105         try (DirectoryStream<Path> ds = Files.newDirectoryStream(dirPath)) {
106             ds.forEach(list::add);
107         }
108         return list;
109     }
110 
111     /**
112      * Lists all entries in the given directory.
113      *
114      * @param dirName directory path.
115      * @return a list of dir entries if an I/O error occurs
116      * @throws IOException an I/O error occurs accessing the file system
117      */
118     public List<Path> list(final String dirName) throws IOException {
119         return list(fileSystem.getPath(dirName));
120     }
121 
122     /**
123      * Lists all modules.
124      *
125      * @return a list of modules
126      * @throws IOException an I/O error occurs accessing the file system
127      */
128     public List<Path> modules() throws IOException {
129         return list(MODULES_PATH);
130     }
131 
132     /**
133      * Lists all packages.
134      *
135      * @return a list of modules
136      * @throws IOException an I/O error occurs accessing the file system
137      */
138     public List<Path> packages() throws IOException {
139         return list(PACKAGES_PATH);
140     }
141 
142 }