in java/main/src/main/java/com/epam/deltix/utilities/ResourceLoader.java [367:512]
private void listResources() throws URISyntaxException, IOException {
_maxResourceLength = -1;
_totalResourceLength = 0;
_resources = new ArrayList<>();
_resourcesRoot = "";
final String dllExt;
final VariablesMapper vm = getVariablesMapper();
if (vm.isOsWindows())
dllExt = ".dll";
else if (vm.isOsLinux())
dllExt = ".so";
else if (vm.isOsDarwin())
dllExt = ".dylib";
else
throw new RuntimeException("Can't determine dynamic library extension. Unsupported operations system: " + vm.getOs());
// Based on the https://stackoverflow.com/questions/1429172/how-to-list-the-files-inside-a-jar-file/28057735
final CodeSource src = _class.getProtectionDomain().getCodeSource();
if (src == null)
throw new RuntimeException("Can't get class '" + _class + "' code source.");
final URL srcLocation = src.getLocation();
final URL classResource = _class.getResource("");
if (classResource == null)
throw new RuntimeException("Can't get class '" + _class + "' resource.");
String classLocation = classResource.toString();
final String classPackageDir = _class.getPackage().getName().replace('.', '/') + '/';
if (!classLocation.endsWith(classPackageDir))
throw new RuntimeException("Can't determine source location for resource '" + classResource + "' of class '" + _class + "'.");
classLocation = classLocation.substring(0, classLocation.length() - classPackageDir.length());
log("Resources location: %s", srcLocation);
final String srcScheme = new URI(classLocation).getScheme(); // srcLocation.toURI().getScheme();
if (srcScheme.equalsIgnoreCase("jar")) {
try (final ZipInputStream zip = new ZipInputStream(srcLocation.openStream())) {
if (srcLocation.toURI().getScheme().equals("jar"))
_resourcesRoot = srcLocation.toString();
ZipEntry zipEntry;
while ((zipEntry = zip.getNextEntry()) != null) {
final String name = zipEntry.getName();
if (name.endsWith("/"))
continue;
addResource(name, zipEntry.getSize(), dllExt);
}
}
} else if (srcScheme.equalsIgnoreCase("file")) {
// https://stackoverflow.com/questions/3923129/get-a-list-of-resources-from-classpath-directory
final String classPath = System.getProperty("java.class.path", ".");
final String[] classPathElements = classPath.split(System.getProperty("path.separator"));
for (final String classPathElement : classPathElements) {
final Path classPathElementPath = Paths.get(classPathElement);
if (classPathElementPath.toFile().isDirectory()) {
final Path resPath = classPathElementPath.toAbsolutePath();
final int resPathLength = resPath.toString().length() + 1; // Add last separator character
Files.walkFileTree(resPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
final String resPath = file.toAbsolutePath().toString().substring(resPathLength);
addResource(resPath, Files.size(file), dllExt);
return FileVisitResult.CONTINUE;
}
});
}
else if (classPathElement.toLowerCase(Locale.ROOT).endsWith(".jar")) {
try (final ZipInputStream zip = new ZipInputStream(Files.newInputStream(classPathElementPath))) {
ZipEntry zipEntry;
while ((zipEntry = zip.getNextEntry()) != null) {
final String name = zipEntry.getName();
if (name.endsWith("/"))
continue;
addResource(name, zipEntry.getSize(), dllExt);
}
}
}
}
} else {
throw new RuntimeException("Unsupported URI scheme: " + srcScheme);
}
if (_resources.size() < 1)
throw argException("No resource files were found at the specified path.");
//Shorten destination path
if (_compactDestinationPath) {
final List<Path> commonPart = new ArrayList<>();
{
final Path resDstPath = _resources.get(0).destinationPathNullable;
if (resDstPath != null) {
for (int pi = 0, pe = resDstPath.getNameCount(); pi < pe; ++pi)
commonPart.add(resDstPath.getName(pi));
}
}
for (int ri = 1, re = _resources.size(); !commonPart.isEmpty() && ri < re; ++ri) {
final Path resDstPath = _resources.get(ri).destinationPathNullable;
if (resDstPath == null) {
commonPart.clear();
break;
}
final int resNameCount = resDstPath.getNameCount();
while (resNameCount < commonPart.size())
commonPart.remove(commonPart.size() - 1);
for (int pi = 0, pe = commonPart.size(); pi < pe; ++pi) {
if (!resDstPath.getName(pi).equals(commonPart.get(pi))) {
while (pi < commonPart.size())
commonPart.remove(commonPart.size() - 1);
break;
}
}
}
if (!commonPart.isEmpty()) {
final int removeParts = commonPart.size();
for (final Resource res : _resources) {
final int resNameCount = res.destinationPathNullable.getNameCount();
res.destinationPathNullable = removeParts < resNameCount
? res.destinationPathNullable.subpath(removeParts, resNameCount) : null;
}
}
}
if (logLevelLeast(DBG)) {
// Show predefined deployment order
// Will be later sorted before calling LoadLibrary
Collections.sort(_resources);
final Path fakeRoot = Paths.get("");
for (final Resource resource : _resources)
log("%s: %s", resource.order, resource.getFullPath(fakeRoot));
}
}