
com.streamsets.pipeline.api.impl.LocalizableMessage Maven / Gradle / Ivy
/*
* Copyright 2017 StreamSets Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.streamsets.pipeline.api.impl;
import com.streamsets.pipeline.api.ErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class LocalizableMessage implements LocalizableString {
private static final Logger LOG = LoggerFactory.getLogger(LocalizableMessage.class);
private static final Object[] NULL_ONE_ARG = {null};
private static final Set MISSING_BUNDLE_WARNS = Collections.newSetFromMap(new ConcurrentHashMap<>());
private static final Set MISSING_KEY_WARNS = Collections.newSetFromMap(new ConcurrentHashMap<>());
private final ClassLoader classLoader;
private final String bundle;
private final String errorCode;
private final String humanReadableMessage;
private String technicalMessage = "";
private String remediationSteps = "";
private final Object[] args;
public LocalizableMessage(ClassLoader classLoader, String bundle, String id, String defaultTemplate, Object[] args) {
this.classLoader = Utils.checkNotNull(classLoader, "classLoader");
this.bundle = bundle;
this.errorCode = id;
this.humanReadableMessage = defaultTemplate;
// we need to do this trick because of the ... syntax sugar resolution when a single value 'null' is used
this.args = (args != null) ? args : NULL_ONE_ARG;
}
public LocalizableMessage(ClassLoader classLoader, String bundle, ErrorCode errorCode, Object[] args) {
this.classLoader = Utils.checkNotNull(classLoader, "classLoader");
this.bundle = bundle;
this.errorCode = errorCode.getCode();
this.humanReadableMessage = errorCode.getMessage();
this.technicalMessage = errorCode.getTechnicalMessage();
this.remediationSteps = errorCode.getRemediationSteps();
// we need to do this trick because of the ... syntax sugar resolution when a single value 'null' is used
this.args = (args != null) ? args : NULL_ONE_ARG;
}
public LocalizableMessage(String bundle, String id, String defaultTemplate, Object[] args) {
this(Thread.currentThread().getContextClassLoader(), bundle, id, defaultTemplate, args);
}
@Override
public String getNonLocalized() {
return Utils.format(humanReadableMessage, args);
}
@Override
public String getLocalized() {
String templateToUse = humanReadableMessage;
if (bundle != null) {
Locale locale = LocaleInContext.get();
if (locale != null) {
try {
ResourceBundle rb = ResourceBundle.getBundle(bundle, locale, classLoader);
if (rb.containsKey(errorCode)) {
templateToUse = rb.getString(errorCode);
} else if (!MISSING_KEY_WARNS.contains(bundle + " " + errorCode)) {
MISSING_KEY_WARNS.add(bundle + " " + errorCode);
LOG.warn("ResourceBundle '{}' does not have key '{}' via ClassLoader '{}'", bundle, errorCode, classLoader);
}
} catch (MissingResourceException ex) {
if (!MISSING_BUNDLE_WARNS.contains(bundle)) {
MISSING_BUNDLE_WARNS.add(bundle);
LOG.trace("ResourceBundle '{}' not found via ClassLoader '{}'", bundle, classLoader);
}
}
}
}
return Utils.format(templateToUse, args);
}
@Override
public String getHumanReadableMessage() {
return Utils.format(humanReadableMessage, args);
}
@Override
public String getErrorCode() {
return errorCode;
}
@Override
public String getTechnicalMessage() {
return Utils.format(technicalMessage, args);
}
@Override
public String getRemediationSteps() {
return remediationSteps;
}
@Override
public String toString() {
return getNonLocalized();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy