Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add i18n messages and logging #288

Merged
merged 1 commit into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions implementation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,23 @@
<groupId>io.smallrye.common</groupId>
<artifactId>smallrye-common-expression</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.common</groupId>
<artifactId>smallrye-common-constraint</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-processor</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
Expand Down
17 changes: 17 additions & 0 deletions implementation/src/main/java/io/smallrye/config/ConfigLogging.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.smallrye.config;

import org.jboss.logging.BasicLogger;
import org.jboss.logging.Logger;
import org.jboss.logging.annotations.Cause;
import org.jboss.logging.annotations.LogMessage;
import org.jboss.logging.annotations.Message;
import org.jboss.logging.annotations.MessageLogger;

@MessageLogger(projectCode = "SRCFG", length = 5)
public interface ConfigLogging extends BasicLogger {
ConfigLogging log = Logger.getMessageLogger(ConfigLogging.class, ConfigLogging.class.getPackage().getName());

@LogMessage(level = Logger.Level.WARN)
@Message(id = 1000, value = "Unable to get context classloader instance")
void failedToRetrieveClassloader(@Cause Throwable cause);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.smallrye.config;

import java.io.InvalidObjectException;
import java.util.NoSuchElementException;
import java.util.regex.Pattern;

import org.eclipse.microprofile.config.spi.Converter;
import org.jboss.logging.Messages;
import org.jboss.logging.annotations.Cause;
import org.jboss.logging.annotations.Message;
import org.jboss.logging.annotations.MessageBundle;

@MessageBundle(projectCode = "SRCFG", length = 5)
interface ConfigMessages {
ConfigMessages msg = Messages.getBundle(ConfigMessages.class);

@Message(id = 0, value = "The file %s was not found")
IllegalStateException fileNotFound(String fileName);

@Message(id = 1, value = "Failure while loading microprofile-config.properties files")
IllegalStateException failedToLoadConfig(@Cause Throwable throwable);

@Message(id = 2, value = "%s can not be converted to a Character")
IllegalArgumentException failedCharacterConversion(String value);

@Message(id = 3, value = "Converter %s must be parameterized with a single type")
IllegalStateException singleTypeConverter(String className);

@Message(id = 4, value = "%s is not an array type")
IllegalArgumentException notArrayType(String arrayType);

@Message(id = 5, value = "Value does not match pattern %s (value was \"%s\")")
IllegalArgumentException valueNotMatchPattern(Pattern pattern, String value);

@Message(id = 6, value = "Value must not be less than %s (value was \"%s\")")
IllegalArgumentException lessThanMinimumValue(Object minimum, String value);

@Message(id = 7, value = "Value must not be less than or equal to %s (value was \"%s\")")
IllegalArgumentException lessThanEqualToMinimumValue(Object minimum, String value);

@Message(id = 8, value = "Value must not be greater than %s (value was \"%s\")")
IllegalArgumentException greaterThanMaximumValue(Object maximum, String value);

@Message(id = 9, value = "Value must not be greater than or equal to %s (value was \"%s\")")
IllegalArgumentException greaterThanEqualToMaximumValue(Object maximum, String value);

@Message(id = 10, value = "Unknown converter ID: %s")
InvalidObjectException unknownConverterId(int id);

@Message(id = 11, value = "Could not expand value %s in property %s")
NoSuchElementException expandingElementNotFound(String key, String valueName);

@Message(id = 12, value = "Can not add converter %s that is not parameterized with a type")
IllegalStateException unableToAddConverter(Converter<?> converter);

@Message(id = 13, value = "No Converter registered for %s")
IllegalArgumentException noRegisteredConverter(Class<?> type);

@Message(id = 14, value = "Property %s not found")
NoSuchElementException propertyNotFound(String name);

@Message(id = 15, value = "No configuration is available for this class loader")
IllegalStateException noConfigForClassloader();

@Message(id = 16, value = "config cannot be null")
IllegalArgumentException configIsNull();

@Message(id = 17, value = "Configuration already registered for the given class loader")
IllegalStateException configAlreadyRegistered();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;

import org.eclipse.microprofile.config.spi.ConfigSource;

import io.smallrye.common.constraint.Assert;

/**
* A {@link Map Map&lt;String, String>} which is backed by a {@link ConfigSource}.
* This should not be used to implement {@link ConfigSource#getProperties()} on {@code ConfigSource}
Expand All @@ -55,7 +56,7 @@ public class ConfigSourceMap extends AbstractMap<String, String> implements Map<
* @param delegate the delegate configuration source (must not be {@code null})
*/
public ConfigSourceMap(final ConfigSource delegate) {
this.delegate = Objects.requireNonNull(delegate, "delegate must not be null");
this.delegate = Assert.checkNotNullParam("delegate", delegate);
}

public int size() {
Expand Down
28 changes: 9 additions & 19 deletions implementation/src/main/java/io/smallrye/config/Converters.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.smallrye.config;

import java.io.InvalidObjectException;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.lang.reflect.Array;
Expand Down Expand Up @@ -52,8 +51,6 @@ public final class Converters {
private Converters() {
}

private static final String ILLEGAL_ARGUMENT_EXCEPTION_FORMAT = "%s %s (value was \"%s\")";

static final Converter<String> STRING_CONVERTER = BuiltInConverter.of(0, newEmptyValueConverter(value -> value));

static final Converter<Boolean> BOOLEAN_CONVERTER = BuiltInConverter.of(1, newTrimmingConverter(newEmptyValueConverter(
Expand Down Expand Up @@ -112,7 +109,7 @@ private Converters() {
if (value.length() == 1) {
return Character.valueOf(value.charAt(0));
}
throw new IllegalArgumentException(value + " can not be converted to a Character");
throw ConfigMessages.msg.failedCharacterConversion(value);
}));

static final Converter<Short> SHORT_CONVERTER = BuiltInConverter.of(12,
Expand Down Expand Up @@ -192,7 +189,7 @@ public static Type getConverterType(Class<?> clazz) {
if (pt.getRawType().equals(Converter.class)) {
Type[] typeArguments = pt.getActualTypeArguments();
if (typeArguments.length != 1) {
throw new IllegalStateException("Converter " + clazz + " must be parameterized with a single type");
throw ConfigMessages.msg.singleTypeConverter(clazz.getName());
}
return typeArguments[0];
}
Expand Down Expand Up @@ -238,7 +235,7 @@ public static <T, C extends Collection<T>> Converter<C> newCollectionConverter(C
*/
public static <A, T> Converter<A> newArrayConverter(Converter<? extends T> itemConverter, Class<A> arrayType) {
if (!arrayType.isArray()) {
throw new IllegalArgumentException(arrayType.toString() + " is not an array type");
throw ConfigMessages.msg.notArrayType(arrayType.toString());
}
return new ArrayConverter<>(itemConverter, arrayType);
}
Expand Down Expand Up @@ -560,8 +557,7 @@ public T convert(final String value) {
if (pattern.matcher(value).matches()) {
return delegate.convert(value);
}
throw new IllegalArgumentException(
String.format(ILLEGAL_ARGUMENT_EXCEPTION_FORMAT, "Value does not match pattern", pattern, value));
throw ConfigMessages.msg.valueNotMatchPattern(pattern, value);
}
}

Expand Down Expand Up @@ -595,29 +591,23 @@ public T convert(final String value) {
final int cmp = comparator.compare(result, min);
if (minInclusive) {
if (cmp < 0) {
throw new IllegalArgumentException(
String.format(ILLEGAL_ARGUMENT_EXCEPTION_FORMAT, "Value must not be less than", min, value));
throw ConfigMessages.msg.lessThanMinimumValue(min, value);
}
} else {
if (cmp <= 0) {
throw new IllegalArgumentException(
String.format(ILLEGAL_ARGUMENT_EXCEPTION_FORMAT, "Value must not be less than or equal to", max,
value));
throw ConfigMessages.msg.lessThanEqualToMinimumValue(min, value);
}
}
}
if (max != null) {
final int cmp = comparator.compare(result, max);
if (maxInclusive) {
if (cmp > 0) {
throw new IllegalArgumentException(
String.format(ILLEGAL_ARGUMENT_EXCEPTION_FORMAT, "Value must not be greater than", max, value));
throw ConfigMessages.msg.greaterThanMaximumValue(max, value);
}
} else {
if (cmp >= 0) {
throw new IllegalArgumentException(
String.format(ILLEGAL_ARGUMENT_EXCEPTION_FORMAT, "Value must not be greater than or equal to",
max, value));
throw ConfigMessages.msg.greaterThanEqualToMaximumValue(max, value);
}
}
}
Expand Down Expand Up @@ -871,7 +861,7 @@ Object readResolve() throws ObjectStreamException {
case 13:
return BYTE_CONVERTER;
default:
throw new InvalidObjectException("Unknown converter ID");
throw ConfigMessages.msg.unknownConverterId(id);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import static io.smallrye.common.expression.Expression.Flag.LENIENT_SYNTAX;
import static io.smallrye.common.expression.Expression.Flag.NO_TRIM;

import java.util.NoSuchElementException;

import javax.annotation.Priority;

import io.smallrye.common.expression.Expression;
Expand Down Expand Up @@ -33,8 +31,7 @@ public ConfigValue getValue(final ConfigSourceInterceptorContext context, final
} else if (resolveContext.hasDefault()) {
resolveContext.expandDefault();
} else {
throw new NoSuchElementException(
"Could not expand value " + resolveContext.getKey() + " in property " + configValue.getName());
throw ConfigMessages.msg.expandingElementNotFound(resolveContext.getKey(), configValue.getName());
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public PropertiesConfigSourceProvider(String propertyFileName, boolean optional,
Enumeration<URL> propertyFileUrls = classLoader.getResources(propertyFileName);

if (!optional && !propertyFileUrls.hasMoreElements()) {
throw new IllegalStateException(propertyFileName + " wasn't found.");
throw ConfigMessages.msg.fileNotFound(propertyFileName);
}

while (propertyFileUrls.hasMoreElements()) {
URL propertyFileUrl = propertyFileUrls.nextElement();
configSources.add(new PropertiesConfigSource(propertyFileUrl));
}
} catch (IOException ioe) {
throw new IllegalStateException("problem while loading microprofile-config.properties files", ioe);
throw ConfigMessages.msg.failedToLoadConfig(ioe);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@
import java.security.AccessController;
import java.security.PrivilegedAction;

import org.jboss.logging.Logger;

/**
* @author <a href="http://jmesnil.net/">Jeff Mesnil</a> (c) 2018 Red Hat inc.
*/
class SecuritySupport {
private static final Logger LOG = Logger.getLogger("io.smallrye.config");

private SecuritySupport() {
}

Expand All @@ -39,7 +35,7 @@ static ClassLoader getContextClassLoader() {
try {
tccl = Thread.currentThread().getContextClassLoader();
} catch (SecurityException ex) {
LOG.warn("Unable to get context classloader instance.", ex);
ConfigLogging.log.failedToRetrieveClassloader(ex);
}
return tccl;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -110,8 +109,7 @@ private Map<Type, Converter<?>> buildConverters(final SmallRyeConfigBuilder buil
for (Converter converter : builder.discoverConverters()) {
Type type = Converters.getConverterType(converter.getClass());
if (type == null) {
throw new IllegalStateException(
"Can not add converter " + converter + " that is not parameterized with a type");
throw ConfigMessages.msg.unableToAddConverter(converter);
}
SmallRyeConfigBuilder.addConverter(type, converter, convertersToBuild);
}
Expand Down Expand Up @@ -189,11 +187,11 @@ public <T> T getValue(String name, Converter<T> converter) {
try {
converted = converter.convert("");
} catch (IllegalArgumentException ignored) {
throw propertyNotFound(name);
throw ConfigMessages.msg.propertyNotFound(name);
}
}
if (converted == null) {
throw propertyNotFound(name);
throw ConfigMessages.msg.propertyNotFound(name);
}
return converted;
}
Expand Down Expand Up @@ -294,13 +292,9 @@ public <T> Converter<T> getConverter(Class<T> asType) {
return (Converter<T>) converters.computeIfAbsent(asType, clazz -> {
final Converter<?> conv = ImplicitConverters.getConverter((Class<?>) clazz);
if (conv == null) {
throw new IllegalArgumentException("No Converter registered for " + asType);
throw ConfigMessages.msg.noRegisteredConverter(asType);
}
return conv;
});
}

private static NoSuchElementException propertyNotFound(final String name) {
return new NoSuchElementException("Property " + name + " not found");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ public SmallRyeConfigBuilder withConverters(Converter<?>[] converters) {
for (Converter<?> converter : converters) {
Type type = Converters.getConverterType(converter.getClass());
if (type == null) {
throw new IllegalStateException(
"Can not add converter " + converter + " that is not parameterized with a type");
throw ConfigMessages.msg.unableToAddConverter(converter);
}
addConverter(type, getPriority(converter), converter, this.converters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public Config getConfig(ClassLoader classLoader) {
config = getFactoryFor(realClassLoader, false).getConfigFor(this, classLoader);
// don't cache null, as that would leak class loaders
if (config == null) {
throw new IllegalStateException("No configuration is available for this class loader");
throw ConfigMessages.msg.noConfigForClassloader();
}
configsForClassLoader.put(realClassLoader, config);
}
Expand Down Expand Up @@ -118,14 +118,14 @@ public SmallRyeConfigBuilder getBuilder() {
@Override
public void registerConfig(Config config, ClassLoader classLoader) {
if (config == null) {
throw new IllegalArgumentException("config cannot be null");
throw ConfigMessages.msg.configIsNull();
}
final ClassLoader realClassLoader = getRealClassLoader(classLoader);
final Map<ClassLoader, Config> configsForClassLoader = this.configsForClassLoader;
synchronized (configsForClassLoader) {
final Config existing = configsForClassLoader.putIfAbsent(realClassLoader, config);
if (existing != null) {
throw new IllegalStateException("Configuration already registered for the given class loader");
throw ConfigMessages.msg.configAlreadyRegistered();
}
}
}
Expand Down
Loading