From c98cf31dc51876343609244406bbde4381c1f5d9 Mon Sep 17 00:00:00 2001 From: Stephan Reichhelm Date: Thu, 6 Jun 2024 12:01:12 +0200 Subject: [PATCH] * disallow by default * change documentation * formatting --- .../deegree/commons/font/WorkspaceFonts.java | 131 ++++++++++-------- .../commons/config/WorkspaceFontTest.java | 17 +-- .../src/main/asciidoc/appendix.adoc | 2 + .../src/main/asciidoc/basics.adoc | 4 +- 4 files changed, 85 insertions(+), 69 deletions(-) diff --git a/deegree-core/deegree-core-commons/src/main/java/org/deegree/commons/font/WorkspaceFonts.java b/deegree-core/deegree-core-commons/src/main/java/org/deegree/commons/font/WorkspaceFonts.java index 23a8766947..75d76272f6 100644 --- a/deegree-core/deegree-core-commons/src/main/java/org/deegree/commons/font/WorkspaceFonts.java +++ b/deegree-core/deegree-core-commons/src/main/java/org/deegree/commons/font/WorkspaceFonts.java @@ -37,6 +37,8 @@ ----------------------------------------------------------------------------*/ package org.deegree.commons.font; +import static org.deegree.commons.utils.TunableParameter.get; + import java.awt.Font; import java.awt.GraphicsEnvironment; import java.io.File; @@ -50,64 +52,73 @@ import org.slf4j.LoggerFactory; public class WorkspaceFonts implements Initializable { - protected static final Set PROCESSED_FILES = new HashSet<>(); - - private static final Logger LOG = LoggerFactory.getLogger( WorkspaceFonts.class ); - - private static final String FONT_DIR = "fonts"; - - @Override - public void init( Workspace workspace ) { - LOG.info( "--------------------------------------------------------------------------------" ); - LOG.info( "Fonts in workspace." ); - LOG.info( "--------------------------------------------------------------------------------" ); - if ( loadFontsFromWorkspace( workspace ) ) { - LOG.info( "Fonts successfully loaded from workspace." ); - return; - } - LOG.info( "No Fonts to register" ); - } - - private boolean loadFontsFromWorkspace( final Workspace ws ) { - File fontDir = new File( ( (DefaultWorkspace) ws ).getLocation(), FONT_DIR ); - if ( !fontDir.isDirectory() ) { - return false; - } - boolean loaded = false; - for ( File f : FileUtils.listFiles( fontDir, new String[] { "ttf" }, false ) ) { - loaded = true; - registerOnce( f ); - } - return loaded; - } - - /** - * Load font and register it in the local {@link GraphicsEnvironment} - * - * Note: If a file has already been processed, it wont be loaded or registered again - * - * @param fontFile - * font to be processed - */ - static void registerOnce( File fontFile ) { - if ( fontFile == null ) { - return; - } - final String fileKey = fontFile.getAbsolutePath(); - if ( PROCESSED_FILES.contains( fileKey ) ) { - // do not re-register fonts, as fonts can not be deregistered in GraphicsEnvironment - LOG.info( "Skip file '{}' because it was already processed.", fontFile.getName() ); - return; - } - PROCESSED_FILES.add( fileKey ); - try { - Font f = Font.createFont( Font.TRUETYPE_FONT, fontFile ); - GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont( f ); - LOG.info( "Loaded Font: {} (face: {}, family: {}, file: {})", f.getName(), f.getFontName(), f.getFamily(), - fontFile.getName() ); - } catch ( Exception e ) { - LOG.warn( "Font '{}' could not be loaded: {}", e.getMessage() ); - LOG.trace( "Exception", e ); - } - } + + protected static final Set PROCESSED_FILES = new HashSet<>(); + + private static final boolean ENABLED = get("deegree.workspace.allow-font-loading", false); + + private static final Logger LOG = LoggerFactory.getLogger(WorkspaceFonts.class); + + private static final String FONT_DIR = "fonts"; + + @Override + public void init(Workspace workspace) { + if (!ENABLED) { + LOG.debug( + "Loading fonts from workspace is disabled, set deegree.workspace.allow-font-loading=true to enable it."); + return; + } + LOG.info("--------------------------------------------------------------------------------"); + LOG.info("Fonts in workspace."); + LOG.info("--------------------------------------------------------------------------------"); + if (loadFontsFromWorkspace(workspace)) { + LOG.info("Fonts successfully loaded from workspace."); + return; + } + LOG.info("No Fonts to register"); + } + + private boolean loadFontsFromWorkspace(final Workspace ws) { + File fontDir = new File(((DefaultWorkspace) ws).getLocation(), FONT_DIR); + if (!fontDir.isDirectory()) { + return false; + } + boolean loaded = false; + for (File f : FileUtils.listFiles(fontDir, new String[] { "ttf" }, false)) { + loaded = true; + registerOnce(f); + } + return loaded; + } + + /** + * Load font and register it in the local {@link GraphicsEnvironment} + * + * Note: If a file has already been processed, it wont be loaded or registered again + * @param fontFile font to be processed + */ + static void registerOnce(File fontFile) { + if (fontFile == null) { + return; + } + final String fileKey = fontFile.getAbsolutePath(); + if (PROCESSED_FILES.contains(fileKey)) { + // do not re-register fonts, as fonts can not be deregistered in + // GraphicsEnvironment + LOG.info("Skip file '{}' because it was already processed.", fontFile.getName()); + return; + } + PROCESSED_FILES.add(fileKey); + try { + Font f = Font.createFont(Font.TRUETYPE_FONT, fontFile); + GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(f); + LOG.info("Loaded Font: {} (face: {}, family: {}, file: {})", f.getName(), f.getFontName(), f.getFamily(), + fontFile.getName()); + } + catch (Exception e) { + LOG.warn("Font '{}' could not be loaded: {}", e.getMessage()); + LOG.trace("Exception", e); + } + } + } diff --git a/deegree-core/deegree-core-commons/src/test/java/org/deegree/commons/config/WorkspaceFontTest.java b/deegree-core/deegree-core-commons/src/test/java/org/deegree/commons/config/WorkspaceFontTest.java index 13239e6d28..671e190fa9 100644 --- a/deegree-core/deegree-core-commons/src/test/java/org/deegree/commons/config/WorkspaceFontTest.java +++ b/deegree-core/deegree-core-commons/src/test/java/org/deegree/commons/config/WorkspaceFontTest.java @@ -11,14 +11,15 @@ public class WorkspaceFontTest { - private static final File TEST_DIR = new File( "src/test/resources/org/deegree/commons/fontworkspace" ); + private static final File TEST_DIR = new File("src/test/resources/org/deegree/commons/fontworkspace"); - private static Workspace ws = new DefaultWorkspace( TEST_DIR ); + private static Workspace ws = new DefaultWorkspace(TEST_DIR); + + @Test + public void testFontLoader() { + WorkspaceFonts wsf = new WorkspaceFonts(); + wsf.init(ws); + assertThat(WorkspaceFonts.PROCESSED_FILES, not(empty())); + } - @Test - public void testFontLoader() { - WorkspaceFonts wsf = new WorkspaceFonts(); - wsf.init( ws ); - assertThat( WorkspaceFonts.PROCESSED_FILES, not( empty() ) ); - } } diff --git a/deegree-services/deegree-webservices-handbook/src/main/asciidoc/appendix.adoc b/deegree-services/deegree-webservices-handbook/src/main/asciidoc/appendix.adoc index 909a6e1a86..94619cee50 100644 --- a/deegree-services/deegree-webservices-handbook/src/main/asciidoc/appendix.adoc +++ b/deegree-services/deegree-webservices-handbook/src/main/asciidoc/appendix.adoc @@ -59,6 +59,8 @@ f |deegree.config.apikey.warn-when-disabled |java.lang.Boolean |true |Log warning if security on REST api is disabled by specifying `*` in _config.apikey_. +|deegree.workspace.allow-font-loading |java.lang.Boolean |false |Allow font registration on workspace startup (disabled by default). + |=== === Interception points diff --git a/deegree-services/deegree-webservices-handbook/src/main/asciidoc/basics.adoc b/deegree-services/deegree-webservices-handbook/src/main/asciidoc/basics.adoc index d85e217346..077a49717b 100644 --- a/deegree-services/deegree-webservices-handbook/src/main/asciidoc/basics.adoc +++ b/deegree-services/deegree-webservices-handbook/src/main/asciidoc/basics.adoc @@ -202,7 +202,9 @@ Some common ones are: |=== ____ -NOTE: Font files are processed only once per file and are not +NOTE: Font registration on workspace startup is not allowed by default +and has to be enabled with a parameter, see <> +for details. Font files are processed only once per file and are not deregistered when a workspace is changed, stopped or reloaded. To remove a font, remove the font file from the folder and restart the container.