-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathBootClassPath.java
More file actions
132 lines (113 loc) · 4.94 KB
/
BootClassPath.java
File metadata and controls
132 lines (113 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright 2013-2026 chronicle.software; SPDX-License-Identifier: Apache-2.0
*/
package net.openhft.affinity;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
enum BootClassPath {
INSTANCE;
private final Set<String> bootClassPathResources = Collections.unmodifiableSet(getResourcesOnBootClasspath());
private static Set<String> getResourcesOnBootClasspath() {
final Logger logger = LoggerFactory.getLogger(BootClassPath.class);
final Set<String> resources = new HashSet<>();
final String bootClassPath = System.getProperty("sun.boot.class.path", "");
if (!bootClassPath.isEmpty()) {
logger.trace("Boot class-path is: {}", bootClassPath);
final String pathSeparator = File.pathSeparator;
logger.trace("Path separator is: '{}'", pathSeparator);
final String[] pathElements = bootClassPath.split(pathSeparator);
for (final String pathElement : pathElements) {
resources.addAll(findResources(Paths.get(pathElement), logger));
}
} else {
resources.addAll(findResourcesInJrt(logger));
}
return resources;
}
private static Set<String> findResourcesInJrt(final Logger logger) {
final Set<String> jrtResources = new HashSet<>();
try {
FileSystem fs;
try {
fs = FileSystems.getFileSystem(URI.create("jrt:/"));
} catch (FileSystemNotFoundException | ProviderNotFoundException e) {
fs = FileSystems.newFileSystem(URI.create("jrt:/"), Collections.emptyMap());
}
final Path modules = fs.getPath("/modules");
Files.walkFileTree(modules, new SimpleFileVisitor<Path>() {
@Override
public @NotNull FileVisitResult visitFile(final @NotNull Path file,
final @NotNull BasicFileAttributes attrs) throws IOException {
if (file.getFileName().toString().endsWith(".class")) {
Path relative = modules.relativize(file);
if (relative.getNameCount() > 1) {
Path classPath = relative.subpath(1, relative.getNameCount());
jrtResources.add(classPath.toString());
}
}
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
logger.warn("Error walking jrt filesystem", e);
}
return jrtResources;
}
private static Set<String> findResources(final Path path, final Logger logger) {
if (!Files.exists(path)) {
return Collections.emptySet();
}
if (Files.isDirectory(path)) {
return findResourcesInDirectory(path, logger);
}
return findResourcesInJar(path, logger);
}
private static Set<String> findResourcesInJar(final Path path, final Logger logger) {
final Set<String> jarResources = new HashSet<>();
try (final JarFile jarFile = new JarFile(path.toFile())) {
final Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
final JarEntry jarEntry = entries.nextElement();
if (jarEntry.getName().endsWith(".class")) {
jarResources.add(jarEntry.getName());
}
}
} catch (IOException e) {
logger.warn("Not a jar file: {}", path);
}
return jarResources;
}
private static Set<String> findResourcesInDirectory(final Path path, final Logger logger) {
final Set<String> dirResources = new HashSet<>();
try {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public @NotNull FileVisitResult visitFile(final @NotNull Path file, final @NotNull BasicFileAttributes attrs) throws IOException {
if (file.getFileName().toString().endsWith(".class")) {
dirResources.add(path.relativize(file).toString());
}
return super.visitFile(file, attrs);
}
});
} catch (IOException e) {
logger.warn("Error walking dir: {}", path, e);
}
return dirResources;
}
public final boolean has(String binaryClassName) {
final String resourceClassName = binaryClassName.replace('.', '/').concat(".class");
return bootClassPathResources.contains(resourceClassName);
}
}