1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.util;
18
19 import java.io.Closeable;
20 import java.io.File;
21 import java.io.IOException;
22 import java.net.URI;
23 import java.net.URL;
24 import java.net.URLClassLoader;
25 import java.nio.file.DirectoryStream;
26 import java.nio.file.FileSystem;
27 import java.nio.file.FileSystems;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Map;
35
36
37
38
39
40
41 public class ModularRuntimeImage implements Closeable {
42
43 static final String MODULES_PATH = File.separator + "modules";
44 static final String PACKAGES_PATH = File.separator + "packages";
45
46 private final URLClassLoader classLoader;
47 private final FileSystem fileSystem;
48
49
50
51
52 @SuppressWarnings("resource")
53 public ModularRuntimeImage() {
54 this(null, FileSystems.getFileSystem(URI.create("jrt:/")));
55 }
56
57
58
59
60
61
62
63
64 public ModularRuntimeImage(final String javaHome) throws IOException {
65 final Map<String, ?> emptyMap = Collections.emptyMap();
66 final Path jrePath = Paths.get(javaHome);
67 final Path jrtFsPath = jrePath.resolve("lib").resolve("jrt-fs.jar");
68 this.classLoader = URLClassLoader.newInstance(new URL[] {jrtFsPath.toUri().toURL()});
69 this.fileSystem = FileSystems.newFileSystem(URI.create("jrt:/"), emptyMap, classLoader);
70 }
71
72 private ModularRuntimeImage(final URLClassLoader cl, final FileSystem fs) {
73 this.classLoader = cl;
74 this.fileSystem = fs;
75 }
76
77 @Override
78 public void close() throws IOException {
79 if (classLoader != null) {
80 classLoader.close();
81 }
82 if (fileSystem != null) {
83 fileSystem.close();
84 }
85 }
86
87 public FileSystem getFileSystem() {
88 return fileSystem;
89 }
90
91
92
93
94
95
96
97
98 public List<Path> list(final Path dirPath) throws IOException {
99 final List<Path> list = new ArrayList<>();
100 try (DirectoryStream<Path> ds = Files.newDirectoryStream(dirPath)) {
101 ds.forEach(list::add);
102 }
103 return list;
104 }
105
106
107
108
109
110
111
112
113 public List<Path> list(final String dirName) throws IOException {
114 return list(fileSystem.getPath(dirName));
115 }
116
117
118
119
120
121
122
123 public List<Path> modules() throws IOException {
124 return list(MODULES_PATH);
125 }
126
127
128
129
130
131
132
133 public List<Path> packages() throws IOException {
134 return list(PACKAGES_PATH);
135 }
136
137 }