From def56feafa0c1e91eba0949a9eab71f479a37d16 Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Thu, 11 Jul 2024 16:44:37 +0200 Subject: [PATCH] implement and document fallback strategy --- .../common/NamedServiceProvider.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/cryptomator/integrations/common/NamedServiceProvider.java b/src/main/java/org/cryptomator/integrations/common/NamedServiceProvider.java index d9b47e2..c9344a0 100644 --- a/src/main/java/org/cryptomator/integrations/common/NamedServiceProvider.java +++ b/src/main/java/org/cryptomator/integrations/common/NamedServiceProvider.java @@ -1,5 +1,8 @@ package org.cryptomator.integrations.common; +import org.slf4j.LoggerFactory; + +import java.util.MissingResourceException; import java.util.ResourceBundle; /** @@ -11,15 +14,22 @@ public interface NamedServiceProvider { * Get the name of this service provider. * * @return The name of the service provider - * @implNote The default implementation looks first for a {@link LocalizedDisplayName} and loads the name from the specified resource bundle/key. If the annotation is not present, the code looks for {@link DisplayName} and uses its value. If none of the former annotations are present, it falls back to the qualified class name. + * @implNote The default implementation looks first for a {@link LocalizedDisplayName} and loads the name from the specified resource bundle/key. If the annotation is not present or loading the resource throws an exception, the code looks for {@link DisplayName} and uses its value. If none of the former annotations are present, it falls back to the qualified class name. * @see DisplayName * @see LocalizedDisplayName */ default String getName() { var localizedDisplayName = this.getClass().getAnnotation(LocalizedDisplayName.class); if (localizedDisplayName != null) { - return ResourceBundle.getBundle(localizedDisplayName.bundle()) // - .getString(localizedDisplayName.key()); + try { + return ResourceBundle.getBundle(localizedDisplayName.bundle()) // + .getString(localizedDisplayName.key()); + } catch (MissingResourceException e) { + var clazz = this.getClass(); + var logger = LoggerFactory.getLogger(clazz); + logger.warn("Failed to load localized display name for {}. Falling back to not-localized display name/class name.", clazz.getName()); + logger.debug("Reason for failure of {}.", clazz.getName(), e); + } } var displayName = this.getClass().getAnnotation(DisplayName.class);