summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java
diff options
context:
space:
mode:
authorSimon Rettberg2023-02-09 15:13:55 +0100
committerSimon Rettberg2023-02-09 15:13:55 +0100
commite18e0730a9d60e3a1a93de2a1248294e1dc6cd9a (patch)
tree8f61148a1c882378ee1b0153835b3255720f1b40 /dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java
parent[client] Warn if destination directory can't be created on download (diff)
downloadtutor-module-e18e0730a9d60e3a1a93de2a1248294e1dc6cd9a.tar.gz
tutor-module-e18e0730a9d60e3a1a93de2a1248294e1dc6cd9a.tar.xz
tutor-module-e18e0730a9d60e3a1a93de2a1248294e1dc6cd9a.zip
[client] Allow Branding in external locations
Allow customizing the client via external properties and resources, instead of just the built-in resources. Built-in configuration takes highest priority, as usual, but built-in resources (images, text) take lowest priority. Any config file in the current working directory will be the first fallback. Finally, a system-wide path will be considered. On Windows, this is %ProgramData%\bwlp\, on Linux /etc/bwlp/. If a config was found in either of those places, the app will also try to load any resources (images, text) from there, only falling back to the resources withing the jar if they don't exist. You can still use --dump and --pack with these changes, but --dump will only ever dump the active configuration and the resources found within the jar, not those from the file system.
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java')
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java42
1 files changed, 37 insertions, 5 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java
index 02004cc0..a6e932cc 100644
--- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java
+++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java
@@ -9,7 +9,10 @@ import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.FileInputStream;
import java.io.InputStream;
+import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
@@ -21,6 +24,7 @@ import javax.swing.ImageIcon;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
+import org.openslx.dozmod.Branding;
import org.openslx.dozmod.Config;
/**
@@ -49,7 +53,20 @@ public class ResourceLoader
*/
public static ImageIcon getIcon( String path, String description )
{
- URL url = ResourceLoader.class.getResource( path );
+ URL url = null;
+ if ( Branding.RESOURCE_FS_DIR != null ) {
+ File file = new File( Branding.RESOURCE_FS_DIR + path );
+ try {
+ if ( file.exists() && file.isFile() && file.canRead() ) {
+ url = file.toURI().toURL();
+ }
+ } catch ( MalformedURLException e ) {
+ LOGGER.warn( "Malformed URL: " + file.toString(), e );
+ }
+ }
+ if ( url == null ) {
+ url = ResourceLoader.class.getResource( path );
+ }
if ( url == null ) {
LOGGER.error( "Resource not found: " + path );
} else {
@@ -60,6 +77,7 @@ public class ResourceLoader
icon.getImage().getScaledInstance(Math.round(icon.getIconWidth() * scalingFactor),
Math.round(icon.getIconHeight() * scalingFactor), java.awt.Image.SCALE_SMOOTH),
description);
+ // Success ^^
} catch ( Exception e ) {
LOGGER.error( "Resource not loadable: " + path );
}
@@ -135,10 +153,18 @@ public class ResourceLoader
public static String getTextFile( String path )
{
String fileContent = null;
- try ( InputStream stream = ResourceLoader.class.getResourceAsStream( path ) ) {
- fileContent = IOUtils.toString( stream, StandardCharsets.UTF_8 );
- } catch ( Exception e ) {
- LOGGER.error( "IO error while trying to load resource '" + path + "'. See trace: ", e );
+ if ( Branding.RESOURCE_FS_DIR != null ) {
+ try ( InputStream stream = new FileInputStream( Branding.RESOURCE_FS_DIR + path ) ) {
+ fileContent = IOUtils.toString( stream, StandardCharsets.UTF_8 );
+ } catch ( Exception e ) {
+ }
+ }
+ if ( fileContent == null ) {
+ try ( InputStream stream = ResourceLoader.class.getResourceAsStream( path ) ) {
+ fileContent = IOUtils.toString( stream, StandardCharsets.UTF_8 );
+ } catch ( Exception e ) {
+ LOGGER.error( "IO error while trying to load resource '" + path + "'. See trace: ", e );
+ }
}
if ( fileContent != null ) {
@@ -150,6 +176,12 @@ public class ResourceLoader
public static InputStream getStream( String path )
{
+ if ( Branding.RESOURCE_FS_DIR != null ) {
+ try {
+ return new FileInputStream( Branding.RESOURCE_FS_DIR + path );
+ } catch (Exception e) {
+ }
+ }
return ResourceLoader.class.getResourceAsStream( path );
}