generated from cuioss/cui-java-module-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
...re/portal-common-cdi/src/main/java/de/cuioss/portal/common/util/PortalResourceLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ortal-common-cdi/src/test/java/de/cuioss/portal/common/util/PortalResourceLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |