Skip to content

Commit

Permalink
implement and document fallback strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
infeo committed Jul 11, 2024
1 parent de3997a commit def56fe
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.cryptomator.integrations.common;

import org.slf4j.LoggerFactory;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
Expand All @@ -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);
Expand Down

0 comments on commit def56fe

Please sign in to comment.