diff options
author | Thomas <> | 2022-05-03 16:03:59 +0200 |
---|---|---|
committer | Thomas <> | 2022-05-03 16:03:59 +0200 |
commit | 4af92cb886592bd379ed2e338a383c7f350f5104 (patch) | |
tree | 47436cd29641cf30f611da3e65733b269141e5ea /eaaf_core_utils/src/main | |
parent | 8863bbcff97c4f7ee86be063a222ec36c15b5546 (diff) | |
download | EAAF-Components-4af92cb886592bd379ed2e338a383c7f350f5104.tar.gz EAAF-Components-4af92cb886592bd379ed2e338a383c7f350f5104.tar.bz2 EAAF-Components-4af92cb886592bd379ed2e338a383c7f350f5104.zip |
feature(core): add simple implementation of a Spring based message source
Diffstat (limited to 'eaaf_core_utils/src/main')
-rw-r--r-- | eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/logging/SpringBasedBasicStatusMessager.java | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/logging/SpringBasedBasicStatusMessager.java b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/logging/SpringBasedBasicStatusMessager.java new file mode 100644 index 00000000..a62b8c9b --- /dev/null +++ b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/logging/SpringBasedBasicStatusMessager.java @@ -0,0 +1,115 @@ +package at.gv.egiz.eaaf.core.impl.logging; + +import java.text.MessageFormat; +import java.util.Locale; +import java.util.MissingResourceException; + +import org.springframework.context.MessageSource; +import org.springframework.context.MessageSourceAware; +import org.springframework.context.NoSuchMessageException; +import org.springframework.context.i18n.LocaleContextHolder; + +import at.gv.egiz.eaaf.core.api.IStatusMessenger; +import lombok.extern.slf4j.Slf4j; + +/** + * Basic {@link IStatusMessenger} implementation that operates on Spring {@link MessageSource}. + * + * @author tlenz + * + */ +@Slf4j +public class SpringBasedBasicStatusMessager implements IStatusMessenger, MessageSourceAware { + + private static final String ERROR_MESSAGES_UNAVAILABLE = "Error messages can NOT be load from application." + + " Only errorCode: {0} is availabe"; + private static final String ERROR_NO_MESSAGE = "No errormesseage for error with number.={0}"; + + private static final String MSG_WARN_NO_SOURCE = "MessageCode: {} is NOT SET for locale: {}"; + private static final String MSG_INFO = "Use locale: {} as default"; + + private MessageSource messageSource; + + @Override + public String getMessage(String messageId, Object[] parameters) { + if (messageSource == null) { + return MessageFormat.format(ERROR_MESSAGES_UNAVAILABLE, new Object[]{messageId}); + + } else { + try { + final Locale locale = LocaleContextHolder.getLocale(); + return messageSource.getMessage(messageId, parameters, locale); + + } catch (final NoSuchMessageException e) { + log.info(MSG_WARN_NO_SOURCE, messageId, LocaleContextHolder.getLocale()); + log.debug(MSG_INFO, Locale.ENGLISH); + + try { + return messageSource.getMessage(messageId, parameters, Locale.ENGLISH); + + } catch (final NoSuchMessageException e2) { + return MessageFormat.format(ERROR_NO_MESSAGE, new Object[]{messageId}); + + } + + } catch (final MissingResourceException e2) { + return MessageFormat.format(ERROR_NO_MESSAGE, new Object[]{messageId}); + + } + } + } + + @Override + public String getMessageWithoutDefault(String messageId, Object[] parameters) { + if (messageSource == null) { + return null; + + } else { + try { + final Locale locale = LocaleContextHolder.getLocale(); + return messageSource.getMessage(messageId, parameters, locale); + + } catch (final NoSuchMessageException e) { + log.info(MSG_WARN_NO_SOURCE, messageId, LocaleContextHolder.getLocale()); + log.debug(MSG_INFO, Locale.ENGLISH); + + try { + return messageSource.getMessage(messageId, parameters, Locale.ENGLISH); + + } catch (final NoSuchMessageException e2) { + log.info(MSG_WARN_NO_SOURCE, messageId, Locale.ENGLISH); + + } + + } catch (final MissingResourceException e2) { + log.warn("No message source", e2); + + } + } + + return null; + + } + + @Override + public String getResponseErrorCode(Throwable throwable) { + return IStatusMessenger.CODES_INTERNAL_ERROR_GENERIC; + + } + + @Override + public String mapInternalErrorToExternalError(String intErrorCode) { + return IStatusMessenger.CODES_EXTERNAL_ERROR_GENERIC; + + } + + @Override + public void setMessageSource(final MessageSource messageSource) { + this.messageSource = messageSource; + + log.info("Injecting '{}' into 'LogMessageProviderFactory'", this.getClass().getName()); + LogMessageProviderFactory.setStatusMessager(this); + + } + +} |