Skip to content

Commit

Permalink
Temp changes
Browse files Browse the repository at this point in the history
  • Loading branch information
stejbac committed Dec 5, 2021
1 parent 628ac27 commit c8d0c49
Showing 1 changed file with 67 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import bisq.core.dao.state.model.blockchain.Block;

import bisq.common.config.Config;
import bisq.common.file.FileUtil;
import bisq.common.proto.persistable.PersistenceProtoResolver;

import protobuf.BaseBlock;
Expand All @@ -29,14 +30,21 @@
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.commons.io.FileUtils;

import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;

import java.nio.charset.StandardCharsets;

import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;

import lombok.Getter;
Expand Down Expand Up @@ -116,27 +124,29 @@ void copyFromResources(String postFix) {
return;
}

URL dirUrl = getClass().getClassLoader().getResource(resourceDir);
if (dirUrl == null) {
log.info("Directory {} in resources does not exist.", resourceDir);
return;
}
File dir = new File(dirUrl.toURI());
String[] fileNames = dir.list();
if (fileNames == null) {
log.info("No files in directory. {}", dir.getAbsolutePath());
return;
}
List<String> fileNames = getFilenamesForDirnameFromCP(resourceDir);
// URL dirUrl = getClass().getClassLoader().getResource(resourceDir);
// if (dirUrl == null) {
// log.info("Directory {} in resources does not exist.", resourceDir);
// return;
// }
// File dir = new File(dirUrl.toURI());
// String[] fileNames = dir.list();
// if (fileNames == null) {
// log.info("No files in directory. {}", dir.getAbsolutePath());
// return;
// }
if (!storageDir.exists()) {
storageDir.mkdir();
}
for (String fileName : fileNames) {
URL url = getClass().getClassLoader().getResource(resourceDir + File.separator + fileName);
File resourceFile = new File(url.toURI());
// URL url = getClass().getClassLoader().getResource(resourceDir + File.separator + fileName);
// File resourceFile = new File(url.toURI());
File destinationFile = new File(storageDir, fileName);
FileUtils.copyFile(resourceFile, destinationFile);
// FileUtils.copyFile(resourceFile, destinationFile);
FileUtil.resourceToFile(resourceDir + "/" + fileName, destinationFile);
}
log.info("Copying {} resource files took {} ms", fileNames.length, System.currentTimeMillis() - ts);
log.info("Copying {} resource files took {} ms", fileNames.size(), System.currentTimeMillis() - ts);
} catch (Throwable e) {
e.printStackTrace();
}
Expand All @@ -159,4 +169,44 @@ public void removeBlocksInDirectory() {
storageDir.mkdir();
}
}

// adapted from https://stackoverflow.com/questions/3923129/get-a-list-of-resources-from-classpath-directory/48190582#48190582
private static List<String> getFilenamesForDirnameFromCP(String directoryName) throws URISyntaxException, IOException {
List<String> filenames = new ArrayList<>();

URL url = Thread.currentThread().getContextClassLoader().getResource(directoryName);
if (url != null) {
if (url.getProtocol().equals("file")) {
// File file = Paths.get(url.toURI()).toFile();
// File[] files = file.listFiles();
// if (files != null) {
// for (File filename : files) {
// filenames.add(filename.toString());
// }
// }
File dir = new File(url.toURI());
String[] filenames0 = dir.list();
if (filenames0 != null) {
filenames.addAll(List.of(filenames0));
}
} else if (url.getProtocol().equals("jar")) {
String dirname = directoryName + "/";
String path = url.getPath();
String jarPath = path.substring(5, path.indexOf("!"));
try (JarFile jar = new JarFile(URLDecoder.decode(jarPath, StandardCharsets.UTF_8.name()))) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if (name.startsWith(dirname) && !dirname.equals(name)) {
// URL resource = Thread.currentThread().getContextClassLoader().getResource(name);
// filenames.add(resource.toString());
filenames.add(name.substring(dirname.length()));
}
}
}
}
}
return filenames;
}
}

0 comments on commit c8d0c49

Please sign in to comment.