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 Thrown if an I/O error occurs while 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 /**
93 * Gets the file system.
94 *
95 * @return The file system.
96 */
97 public FileSystem getFileSystem() {
98 return fileSystem;
99 }
100
101 /**
102 * Lists all entries in the given directory.
103 *
104 * @param dirPath directory path.
105 * @return A list of dir entries if an I/O error occurs.
106 * @throws IOException Thrown if an I/O error occurs while accessing the file system.
107 */
108 public List<Path> list(final Path dirPath) throws IOException {
109 final List<Path> list = new ArrayList<>();
110 try (DirectoryStream<Path> ds = Files.newDirectoryStream(dirPath)) {
111 ds.forEach(list::add);
112 }
113 return list;
114 }
115
116 /**
117 * Lists all entries in the given directory.
118 *
119 * @param dirName directory path.
120 * @return A list of dir entries if an I/O error occurs.
121 * @throws IOException Thrown if an I/O error occurs while accessing the file system.
122 */
123 public List<Path> list(final String dirName) throws IOException {
124 return list(fileSystem.getPath(dirName));
125 }
126
127 /**
128 * Lists all modules.
129 *
130 * @return A list of modules.
131 * @throws IOException Thrown if an I/O error occurs while accessing the file system.
132 */
133 public List<Path> modules() throws IOException {
134 return list(MODULES_PATH);
135 }
136
137 /**
138 * Lists all packages.
139 *
140 * @return A list of modules.
141 * @throws IOException Thrown if an I/O error occurs while accessing the file system.
142 */
143 public List<Path> packages() throws IOException {
144 return list(PACKAGES_PATH);
145 }
146
147 }