Skip to content

Commit

Permalink
Updated code to accept a class parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
gurpreet- committed Aug 30, 2019
1 parent aa9c973 commit f3c3356
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ plugins {
ext {
artifactId = "resource-loader"
groupId = "co.libly"
version = '1.3.1'
version = '1.3.2'
description = "Resource Loader gives you the functions to load resource files inside or outside JARs."
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/co/libly/resourceloader/FileLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public static FileLoader get() {
* @return The file your directory.
* @throws IOException
*/
public File load(String relativePath) throws IOException {
return load(relativePath, new HashSet<>());
public File load(String relativePath, Class outsideClass) throws IOException {
return load(relativePath, new HashSet<>(), outsideClass);
}

/**
Expand All @@ -49,12 +49,12 @@ public File load(String relativePath) throws IOException {
* @return The file your directory.
* @throws IOException
*/
public File load(String relativePath, Set<PosixFilePermission> permissions) throws IOException {
return loadFromRelativePath(relativePath, permissions);
public File load(String relativePath, Set<PosixFilePermission> permissions, Class outsideClass) throws IOException {
return loadFromRelativePath(relativePath, permissions, outsideClass);
}

private File loadFromRelativePath(String relativePath, Set<PosixFilePermission> filePermissions) throws IOException {
File file = copyToTempDirectory(relativePath);
private File loadFromRelativePath(String relativePath, Set<PosixFilePermission> filePermissions, Class outsideClass) throws IOException {
File file = copyToTempDirectory(relativePath, outsideClass);
setPermissions(file, filePermissions);
return file;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/co/libly/resourceloader/ResourceLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class ResourceLoader {
* @return The file or directory you want to load.
* @throws IOException
*/
public File copyToTempDirectory(String relativePath) throws IOException {
public File copyToTempDirectory(String relativePath, Class outsideClass) throws IOException {
// If the file does not start with a separator,
// then let's make sure it does!
if (!relativePath.startsWith(File.separator)) {
Expand All @@ -68,7 +68,7 @@ public File copyToTempDirectory(String relativePath) throws IOException {
mainTempDir.mkdirs();

try {
URL jarUrl = getThisJarPath();
URL jarUrl = getThisJarPath(outsideClass);
// Is the user loading this in a JAR?
if (jarUrl.toString().endsWith(".jar")) {
// If so the get the file/directory
Expand Down Expand Up @@ -363,7 +363,7 @@ protected boolean isPosixCompliant() {
* using this method.
* @return URL of this JAR.
*/
public URL getThisJarPath() {
return ResourceLoader.class.getProtectionDomain().getCodeSource().getLocation();
public URL getThisJarPath(Class outsideClass) {
return outsideClass.getProtectionDomain().getCodeSource().getLocation();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public File load(String relativePath, Class clzz) {
public File load(String relativePath, List<Class> classes) {
synchronized (lock) {
try {
File library = copyToTempDirectory(relativePath);
File library = copyToTempDirectory(relativePath, classes.get(0));
setPermissions(library);
if (library.isDirectory()) {
throw new IOException("Please supply a relative path to a file and not a directory.");
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/co/libly/resourceloader/FileLoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public void loadLoader() {

@Test
public void loadFile() throws IOException {
File file = fileLoader.load("test1.txt");
File file = fileLoader.load("test1.txt", FileLoaderTest.class);
assertThat(file)
.as("Load a file")
.isNotNull();
}

@Test
public void loadFileCheckContents() throws IOException {
File file = fileLoader.load("test1.txt");
File file = fileLoader.load("test1.txt", FileLoaderTest.class);

assertThat(file)
.as("Load a file testing the content")
Expand All @@ -44,7 +44,7 @@ public void loadFileCheckContents() throws IOException {

@Test
public void loadWholeFolders() throws IOException {
File file = fileLoader.load("folder2");
File file = fileLoader.load("folder2", FileLoaderTest.class);

assertThat(file)
.as("Load a directory with children")
Expand Down

0 comments on commit f3c3356

Please sign in to comment.