Skip to content

Commit

Permalink
Adding PortalResourceLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Dec 20, 2023
1 parent 9b7d497 commit 62ecd3a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
*
*/
package de.cuioss.portal.common.util;

import static de.cuioss.tools.collect.MoreCollections.requireNotEmpty;
import static java.util.Objects.requireNonNull;

import java.net.URL;
import java.util.Optional;

import de.cuioss.tools.logging.CuiLogger;
import lombok.experimental.UtilityClass;

/**
* Helper class that streamlines loading Resources from the classpath. This is
* needed because Quarkus modules behave differently.
*/
@UtilityClass
public class PortalResourceLoader {

private static final CuiLogger LOGGER = new CuiLogger(PortalResourceLoader.class);

/**
* Loads a certain resource
*
* @param resourcePath identifying the concrete resource, must nor be null
* @param callingClass must not be null
* @return The {@link Optional} {@link URL} identifying the resource
*/
public static Optional<URL> getRessource(String resourcePath, Class<?> callingClass) {
requireNotEmpty(resourcePath);
requireNonNull(callingClass);
var result = Optional.ofNullable(callingClass.getResource(resourcePath));
if (result.isEmpty()) {
LOGGER.debug("Simple class loading for resource '%s' and type '%s' did not work, using context-classloader",
resourcePath, callingClass);
result = Optional.ofNullable(Thread.currentThread().getContextClassLoader().getResource(resourcePath));
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.cuioss.portal.common.util;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class PortalResourceLoaderTest {

@Test
void shouldLoadExisitingResource() {
assertTrue(PortalResourceLoader.getRessource("/de/cuioss/portal/l18n/messages/high1.properties", getClass())
.isPresent());
}

@Test
void shouldHAndleNotExisitingResource() {
assertTrue(PortalResourceLoader.getRessource("/not/ther", getClass()).isEmpty());
}

}

0 comments on commit 62ecd3a

Please sign in to comment.