You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2022/01/03 20:52:40 UTC
[logging-log4j2] 02/02: Move type converter plugins to log4j-core
This is an automated email from the ASF dual-hosted git repository.
mattsicker pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit cdf9f5003b890f3a43f3a3af79d34f3764060024
Author: Matt Sicker <ma...@apache.org>
AuthorDate: Mon Jan 3 14:52:11 2022 -0600
Move type converter plugins to log4j-core
THis avoids the need for hardcoding plugin metadata in log4j-plugins. Tests are split accordingly. As type converters are loaded as plugins, their implementations can remain in core.
Signed-off-by: Matt Sicker <ma...@apache.org>
---
.../plugins/convert/CoreTypeConvertersTest.java | 90 +-----
.../config/plugins/convert/CoreTypeConverters.java | 337 ++++++++++++++++++++-
.../core/config}/plugins/convert/HexConverter.java | 2 +-
.../plugins/convert/TypeConverterRegistryTest.java | 62 +---
log4j-plugins/src/main/java/module-info.java | 1 -
.../log4j/plugins/convert/TypeConverters.java | 331 --------------------
.../plugins/convert/plugins/Log4jPlugins.java | 55 ----
...e.logging.log4j.plugins.processor.PluginService | 1 -
8 files changed, 348 insertions(+), 531 deletions(-)
diff --git a/log4j-plugins-test/src/test/java/org/apache/logging/log4j/plugins/convert/TypeConverterRegistryTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/plugins/convert/CoreTypeConvertersTest.java
similarity index 55%
copy from log4j-plugins-test/src/test/java/org/apache/logging/log4j/plugins/convert/TypeConverterRegistryTest.java
copy to log4j-core-test/src/test/java/org/apache/logging/log4j/core/plugins/convert/CoreTypeConvertersTest.java
index bb37ba8..b5aa345 100644
--- a/log4j-plugins-test/src/test/java/org/apache/logging/log4j/plugins/convert/TypeConverterRegistryTest.java
+++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/plugins/convert/CoreTypeConvertersTest.java
@@ -14,16 +14,20 @@
* See the license for the specific language governing permissions and
* limitations under the license.
*/
-package org.apache.logging.log4j.plugins.convert;
+package org.apache.logging.log4j.core.plugins.convert;
+import org.apache.logging.log4j.core.config.plugins.convert.CoreTypeConverters;
import org.apache.logging.log4j.plugins.Plugin;
+import org.apache.logging.log4j.plugins.convert.TypeConverter;
+import org.apache.logging.log4j.plugins.convert.TypeConverterRegistry;
+import org.apache.logging.log4j.plugins.convert.TypeConverters;
import org.junit.jupiter.api.Test;
-import static org.hamcrest.MatcherAssert.*;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.jupiter.api.Assertions.*;
-public class TypeConverterRegistryTest {
+public class CoreTypeConvertersTest {
@Test
public void testFindNullConverter() {
@@ -51,7 +55,7 @@ public class TypeConverterRegistryTest {
final TypeConverter<CharSequence> converter = (TypeConverter<CharSequence>)
TypeConverterRegistry.getInstance().findCompatibleConverter(CharSequence.class);
assertNotNull(converter);
- assertThat(converter, instanceOf(TypeConverters.StringConverter.class));
+ assertThat(converter, instanceOf(CoreTypeConverters.StringConverter.class));
final CharSequence expected = "This is a test sequence of characters";
final CharSequence actual = converter.convert(expected.toString());
assertEquals(expected, actual);
@@ -81,82 +85,4 @@ public class TypeConverterRegistryTest {
assertEquals(Foo.THE, fooTypeConverter.convert("THE"));
}
- public static final class CustomTestClass1 {
-
- private CustomTestClass1() {}
-
- }
-
- @Plugin(name = "CustomTestClass1Converter1", category = TypeConverters.CATEGORY)
- public static final class CustomTestClass1Converter1
- implements TypeConverter<CustomTestClass1> {
-
- @Override
- public CustomTestClass1 convert(final String ignored) {
- return new CustomTestClass1();
- }
-
- }
-
- @SuppressWarnings("ComparableType")
- @Plugin(name = "CustomTestClass1Converter2", category = TypeConverters.CATEGORY)
- public static final class CustomTestClass1Converter2
- implements TypeConverter<CustomTestClass1>, Comparable<TypeConverter<?>> {
-
- @Override
- public CustomTestClass1 convert(final String ignored) {
- return new CustomTestClass1();
- }
-
- @Override
- public int compareTo(@SuppressWarnings("NullableProblems") final TypeConverter<?> converter) {
- return -1;
- }
-
- }
-
- @Test
- public void testMultipleComparableConverters() {
- final TypeConverter<?> converter = TypeConverterRegistry
- .getInstance()
- .findCompatibleConverter(CustomTestClass1.class);
- assertThat(converter, instanceOf(CustomTestClass1Converter2.class));
- }
-
- public static final class CustomTestClass2 {
-
- private CustomTestClass2() {}
-
- }
-
- @Plugin(name = "CustomTestClass2Converter1", category = TypeConverters.CATEGORY)
- public static final class CustomTestClass2Converter1
- implements TypeConverter<CustomTestClass2> {
-
- @Override
- public CustomTestClass2 convert(final String ignored) {
- return new CustomTestClass2();
- }
-
- }
-
- @Plugin(name = "CustomTestClass2Converter2", category = TypeConverters.CATEGORY)
- public static final class CustomTestClass2Converter2
- implements TypeConverter<CustomTestClass2> {
-
- @Override
- public CustomTestClass2 convert(final String ignored) {
- return new CustomTestClass2();
- }
-
- }
-
- @Test
- public void testMultipleIncomparableConverters() {
- final TypeConverter<?> converter = TypeConverterRegistry
- .getInstance()
- .findCompatibleConverter(CustomTestClass2.class);
- assertThat(converter, instanceOf(CustomTestClass2Converter1.class));
- }
-
}
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/CoreTypeConverters.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/CoreTypeConverters.java
index f83397b..b481b01 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/CoreTypeConverters.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/CoreTypeConverters.java
@@ -17,19 +17,187 @@
package org.apache.logging.log4j.core.config.plugins.convert;
+import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.appender.rolling.action.Duration;
import org.apache.logging.log4j.core.util.CronExpression;
import org.apache.logging.log4j.plugins.Plugin;
import org.apache.logging.log4j.plugins.convert.TypeConverter;
import org.apache.logging.log4j.plugins.convert.TypeConverters;
+import org.apache.logging.log4j.util.LoaderUtil;
+
+import java.io.File;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.net.InetAddress;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.security.Provider;
+import java.security.Security;
+import java.util.Base64;
+import java.util.UUID;
+import java.util.regex.Pattern;
/**
- * Core specific type converters.
+ * General {@link TypeConverter} implementations.
*
* @since 2.1 Moved to the {@code convert} package.
*/
public final class CoreTypeConverters {
+ private static final Base64.Decoder decoder = Base64.getDecoder();
+
+ /**
+ * Parses a {@link String} into a {@link BigDecimal}.
+ */
+ @Plugin(name = "BigDecimal", category = TypeConverters.CATEGORY)
+ public static class BigDecimalConverter implements TypeConverter<BigDecimal> {
+ @Override
+ public BigDecimal convert(final String s) {
+ return new BigDecimal(s);
+ }
+ }
+
+ /**
+ * Parses a {@link String} into a {@link BigInteger}.
+ */
+ @Plugin(name = "BigInteger", category = TypeConverters.CATEGORY)
+ public static class BigIntegerConverter implements TypeConverter<BigInteger> {
+ @Override
+ public BigInteger convert(final String s) {
+ return new BigInteger(s);
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@link Boolean}.
+ */
+ @Plugin(name = "Boolean", category = TypeConverters.CATEGORY)
+ public static class BooleanConverter implements TypeConverter<Boolean> {
+ @Override
+ public Boolean convert(final String s) {
+ return Boolean.valueOf(s);
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@code byte[]}.
+ *
+ * The supported formats are:
+ * <ul>
+ * <li>0x0123456789ABCDEF</li>
+ * <li>Base64:ABase64String</li>
+ * <li>String using {@link Charset#defaultCharset()} [TODO Should this be UTF-8 instead?]</li>
+ * </ul>
+ */
+ @Plugin(name = "ByteArray", category = TypeConverters.CATEGORY)
+ public static class ByteArrayConverter implements TypeConverter<byte[]> {
+
+ private static final String PREFIX_0x = "0x";
+ private static final String PREFIX_BASE64 = "Base64:";
+
+ @Override
+ public byte[] convert(final String value) {
+ byte[] bytes;
+ if (value == null || value.isEmpty()) {
+ bytes = new byte[0];
+ } else if (value.startsWith(PREFIX_BASE64)) {
+ final String lexicalXSDBase64Binary = value.substring(PREFIX_BASE64.length());
+ bytes = decoder.decode(lexicalXSDBase64Binary);
+ } else if (value.startsWith(PREFIX_0x)) {
+ final String lexicalXSDHexBinary = value.substring(PREFIX_0x.length());
+ bytes = HexConverter.parseHexBinary(lexicalXSDHexBinary);
+ } else {
+ bytes = value.getBytes(Charset.defaultCharset());
+ }
+ return bytes;
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@link Byte}.
+ */
+ @Plugin(name = "Byte", category = TypeConverters.CATEGORY)
+ public static class ByteConverter implements TypeConverter<Byte> {
+ @Override
+ public Byte convert(final String s) {
+ return Byte.valueOf(s);
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@link Character}.
+ */
+ @Plugin(name = "Character", category = TypeConverters.CATEGORY)
+ public static class CharacterConverter implements TypeConverter<Character> {
+ @Override
+ public Character convert(final String s) {
+ if (s.length() != 1) {
+ throw new IllegalArgumentException("Character string must be of length 1: " + s);
+ }
+ return Character.valueOf(s.toCharArray()[0]);
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@code char[]}.
+ */
+ @Plugin(name = "CharacterArray", category = TypeConverters.CATEGORY)
+ public static class CharArrayConverter implements TypeConverter<char[]> {
+ @Override
+ public char[] convert(final String s) {
+ return s.toCharArray();
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@link Charset}.
+ */
+ @Plugin(name = "Charset", category = TypeConverters.CATEGORY)
+ public static class CharsetConverter implements TypeConverter<Charset> {
+ @Override
+ public Charset convert(final String s) {
+ return Charset.forName(s);
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@link Class}.
+ */
+ @Plugin(name = "Class", category = TypeConverters.CATEGORY)
+ public static class ClassConverter implements TypeConverter<Class<?>> {
+ @Override
+ public Class<?> convert(final String s) throws ClassNotFoundException {
+ switch (s.toLowerCase()) {
+ case "boolean":
+ return boolean.class;
+ case "byte":
+ return byte.class;
+ case "char":
+ return char.class;
+ case "double":
+ return double.class;
+ case "float":
+ return float.class;
+ case "int":
+ return int.class;
+ case "long":
+ return long.class;
+ case "short":
+ return short.class;
+ case "void":
+ return void.class;
+ default:
+ return LoaderUtil.loadClass(s);
+ }
+
+ }
+ }
+
@Plugin(name = "CronExpression", category = TypeConverters.CATEGORY)
public static class CronExpressionConverter implements TypeConverter<CronExpression> {
@Override
@@ -39,6 +207,17 @@ public final class CoreTypeConverters {
}
/**
+ * Converts a {@link String} into a {@link Double}.
+ */
+ @Plugin(name = "Double", category = TypeConverters.CATEGORY)
+ public static class DoubleConverter implements TypeConverter<Double> {
+ @Override
+ public Double convert(final String s) {
+ return Double.valueOf(s);
+ }
+ }
+
+ /**
* Converts a {@link String} into a {@link Duration}.
* @since 2.5
*/
@@ -49,4 +228,160 @@ public final class CoreTypeConverters {
return Duration.parse(s);
}
}
+
+ /**
+ * Converts a {@link String} into a {@link File}.
+ */
+ @Plugin(name = "File", category = TypeConverters.CATEGORY)
+ public static class FileConverter implements TypeConverter<File> {
+ @Override
+ public File convert(final String s) {
+ return new File(s);
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@link Float}.
+ */
+ @Plugin(name = "Float", category = TypeConverters.CATEGORY)
+ public static class FloatConverter implements TypeConverter<Float> {
+ @Override
+ public Float convert(final String s) {
+ return Float.valueOf(s);
+ }
+ }
+
+ /**
+ * Converts a {@link String} into an {@link InetAddress}.
+ */
+ @Plugin(name = "InetAddress", category = TypeConverters.CATEGORY)
+ public static class InetAddressConverter implements TypeConverter<InetAddress> {
+ @Override
+ public InetAddress convert(final String s) throws Exception {
+ return InetAddress.getByName(s);
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@link Integer}.
+ */
+ @Plugin(name = "Integer", category = TypeConverters.CATEGORY)
+ public static class IntegerConverter implements TypeConverter<Integer> {
+ @Override
+ public Integer convert(final String s) {
+ return Integer.valueOf(s);
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a Log4j {@link Level}. Returns {@code null} for invalid level names.
+ */
+ @Plugin(name = "Level", category = TypeConverters.CATEGORY)
+ public static class LevelConverter implements TypeConverter<Level> {
+ @Override
+ public Level convert(final String s) {
+ return Level.valueOf(s);
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@link Long}.
+ */
+ @Plugin(name = "Long", category = TypeConverters.CATEGORY)
+ public static class LongConverter implements TypeConverter<Long> {
+ @Override
+ public Long convert(final String s) {
+ return Long.valueOf(s);
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@link Path}.
+ * @since 2.8
+ */
+ @Plugin(name = "Path", category = TypeConverters.CATEGORY)
+ public static class PathConverter implements TypeConverter<Path> {
+ @Override
+ public Path convert(final String s) throws Exception {
+ return Paths.get(s);
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@link Pattern}.
+ */
+ @Plugin(name = "Pattern", category = TypeConverters.CATEGORY)
+ public static class PatternConverter implements TypeConverter<Pattern> {
+ @Override
+ public Pattern convert(final String s) {
+ return Pattern.compile(s);
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@link Provider}.
+ */
+ @Plugin(name = "SecurityProvider", category = TypeConverters.CATEGORY)
+ public static class SecurityProviderConverter implements TypeConverter<Provider> {
+ @Override
+ public Provider convert(final String s) {
+ return Security.getProvider(s);
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@link Short}.
+ */
+ @Plugin(name = "Short", category = TypeConverters.CATEGORY)
+ public static class ShortConverter implements TypeConverter<Short> {
+ @Override
+ public Short convert(final String s) {
+ return Short.valueOf(s);
+ }
+ }
+
+ /**
+ * Returns the given {@link String}, no conversion takes place.
+ */
+ @Plugin(name = "String", category = TypeConverters.CATEGORY)
+ public static class StringConverter implements TypeConverter<String> {
+ @Override
+ public String convert(final String s) {
+ return s;
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@link URI}.
+ */
+ @Plugin(name = "URI", category = TypeConverters.CATEGORY)
+ public static class UriConverter implements TypeConverter<URI> {
+ @Override
+ public URI convert(final String s) throws URISyntaxException {
+ return new URI(s);
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@link URL}.
+ */
+ @Plugin(name = "URL", category = TypeConverters.CATEGORY)
+ public static class UrlConverter implements TypeConverter<URL> {
+ @Override
+ public URL convert(final String s) throws MalformedURLException {
+ return new URL(s);
+ }
+ }
+
+ /**
+ * Converts a {@link String} into a {@link UUID}.
+ * @since 2.8
+ */
+ @Plugin(name = "UUID", category = TypeConverters.CATEGORY)
+ public static class UuidConverter implements TypeConverter<UUID> {
+ @Override
+ public UUID convert(final String s) throws Exception {
+ return UUID.fromString(s);
+ }
+ }
}
diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/convert/HexConverter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/HexConverter.java
similarity index 95%
rename from log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/convert/HexConverter.java
rename to log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/HexConverter.java
index f70131d..fb6fa55 100644
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/convert/HexConverter.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/HexConverter.java
@@ -14,7 +14,7 @@
* See the license for the specific language governing permissions and
* limitations under the license.
*/
-package org.apache.logging.log4j.plugins.convert;
+package org.apache.logging.log4j.core.config.plugins.convert;
/**
* Converts Strings to hex. This is used in place of java.xml.bind.DataTypeConverter which is not available by
diff --git a/log4j-plugins-test/src/test/java/org/apache/logging/log4j/plugins/convert/TypeConverterRegistryTest.java b/log4j-plugins-test/src/test/java/org/apache/logging/log4j/plugins/convert/TypeConverterRegistryTest.java
index bb37ba8..bf4303b 100644
--- a/log4j-plugins-test/src/test/java/org/apache/logging/log4j/plugins/convert/TypeConverterRegistryTest.java
+++ b/log4j-plugins-test/src/test/java/org/apache/logging/log4j/plugins/convert/TypeConverterRegistryTest.java
@@ -14,72 +14,16 @@
* See the license for the specific language governing permissions and
* limitations under the license.
*/
+
package org.apache.logging.log4j.plugins.convert;
import org.apache.logging.log4j.plugins.Plugin;
import org.junit.jupiter.api.Test;
-import static org.hamcrest.MatcherAssert.*;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
-import static org.junit.jupiter.api.Assertions.*;
-
-public class TypeConverterRegistryTest {
-
- @Test
- public void testFindNullConverter() {
- assertThrows(NullPointerException.class,
- () -> TypeConverterRegistry.getInstance().findCompatibleConverter(null));
- }
-
- @Test
- public void testFindBooleanConverter() throws Exception {
- final TypeConverter<?> converter = TypeConverterRegistry.getInstance().findCompatibleConverter(Boolean.class);
- assertNotNull(converter);
- assertTrue((Boolean) converter.convert("TRUE"));
- }
-
- @Test
- public void testFindPrimitiveBooleanConverter() throws Exception {
- final TypeConverter<?> converter = TypeConverterRegistry.getInstance().findCompatibleConverter(Boolean.TYPE);
- assertNotNull(converter);
- assertTrue((Boolean) converter.convert("tRUe"));
- }
- @SuppressWarnings("unchecked")
- @Test
- public void testFindCharSequenceConverterUsingStringConverter() throws Exception {
- final TypeConverter<CharSequence> converter = (TypeConverter<CharSequence>)
- TypeConverterRegistry.getInstance().findCompatibleConverter(CharSequence.class);
- assertNotNull(converter);
- assertThat(converter, instanceOf(TypeConverters.StringConverter.class));
- final CharSequence expected = "This is a test sequence of characters";
- final CharSequence actual = converter.convert(expected.toString());
- assertEquals(expected, actual);
- }
-
- @SuppressWarnings("unchecked")
- @Test
- public void testFindNumberConverter() throws Exception {
- final TypeConverter<Number> numberTypeConverter = (TypeConverter<Number>)
- TypeConverterRegistry.getInstance().findCompatibleConverter(Number.class);
- assertNotNull(numberTypeConverter);
- // TODO: is there a specific converter this should return?
- }
-
- public enum Foo {
- I, PITY, THE
- }
-
- @SuppressWarnings("unchecked")
- @Test
- public void testFindEnumConverter() throws Exception {
- final TypeConverter<Foo> fooTypeConverter = (TypeConverter<Foo>)
- TypeConverterRegistry.getInstance().findCompatibleConverter(Foo.class);
- assertNotNull(fooTypeConverter);
- assertEquals(Foo.I, fooTypeConverter.convert("i"));
- assertEquals(Foo.PITY, fooTypeConverter.convert("pity"));
- assertEquals(Foo.THE, fooTypeConverter.convert("THE"));
- }
+class TypeConverterRegistryTest {
public static final class CustomTestClass1 {
diff --git a/log4j-plugins/src/main/java/module-info.java b/log4j-plugins/src/main/java/module-info.java
index 5926824..6e5d976 100644
--- a/log4j-plugins/src/main/java/module-info.java
+++ b/log4j-plugins/src/main/java/module-info.java
@@ -34,6 +34,5 @@ module org.apache.logging.log4j.plugins {
uses org.apache.logging.log4j.plugins.processor.PluginService;
uses org.apache.logging.log4j.plugins.di.model.PluginModule;
- provides org.apache.logging.log4j.plugins.processor.PluginService with org.apache.logging.log4j.plugins.convert.plugins.Log4jPlugins;
}
diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/convert/TypeConverters.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/convert/TypeConverters.java
index 914892c..a1110e8 100644
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/convert/TypeConverters.java
+++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/convert/TypeConverters.java
@@ -17,24 +17,9 @@
package org.apache.logging.log4j.plugins.convert;
-import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.plugins.Plugin;
import org.apache.logging.log4j.status.StatusLogger;
-import org.apache.logging.log4j.util.LoaderUtil;
-
-import java.io.File;
-import java.math.BigDecimal;
-import java.math.BigInteger;
-import java.net.*;
-import java.nio.charset.Charset;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.security.Provider;
-import java.security.Security;
-import java.util.Base64;
-import java.util.UUID;
-import java.util.regex.Pattern;
/**
* Collection of basic TypeConverter implementations. May be used to register additional TypeConverters or find
@@ -51,322 +36,6 @@ public final class TypeConverters {
*/
public static final String CATEGORY = "TypeConverter";
- private static final Base64.Decoder decoder = Base64.getDecoder();
-
- /**
- * Parses a {@link String} into a {@link BigDecimal}.
- */
- @Plugin(name = "BigDecimal", category = CATEGORY)
- public static class BigDecimalConverter implements TypeConverter<BigDecimal> {
- @Override
- public BigDecimal convert(final String s) {
- return new BigDecimal(s);
- }
- }
-
- /**
- * Parses a {@link String} into a {@link BigInteger}.
- */
- @Plugin(name = "BigInteger", category = CATEGORY)
- public static class BigIntegerConverter implements TypeConverter<BigInteger> {
- @Override
- public BigInteger convert(final String s) {
- return new BigInteger(s);
- }
- }
-
- /**
- * Converts a {@link String} into a {@link Boolean}.
- */
- @Plugin(name = "Boolean", category = CATEGORY)
- public static class BooleanConverter implements TypeConverter<Boolean> {
- @Override
- public Boolean convert(final String s) {
- return Boolean.valueOf(s);
- }
- }
-
- /**
- * Converts a {@link String} into a {@code byte[]}.
- *
- * The supported formats are:
- * <ul>
- * <li>0x0123456789ABCDEF</li>
- * <li>Base64:ABase64String</li>
- * <li>String using {@link Charset#defaultCharset()} [TODO Should this be UTF-8 instead?]</li>
- * </ul>
- */
- @Plugin(name = "ByteArray", category = CATEGORY)
- public static class ByteArrayConverter implements TypeConverter<byte[]> {
-
- private static final String PREFIX_0x = "0x";
- private static final String PREFIX_BASE64 = "Base64:";
-
- @Override
- public byte[] convert(final String value) {
- byte[] bytes;
- if (value == null || value.isEmpty()) {
- bytes = new byte[0];
- } else if (value.startsWith(PREFIX_BASE64)) {
- final String lexicalXSDBase64Binary = value.substring(PREFIX_BASE64.length());
- bytes = decoder.decode(lexicalXSDBase64Binary);
- } else if (value.startsWith(PREFIX_0x)) {
- final String lexicalXSDHexBinary = value.substring(PREFIX_0x.length());
- bytes = HexConverter.parseHexBinary(lexicalXSDHexBinary);
- } else {
- bytes = value.getBytes(Charset.defaultCharset());
- }
- return bytes;
- }
- }
-
- /**
- * Converts a {@link String} into a {@link Byte}.
- */
- @Plugin(name = "Byte", category = CATEGORY)
- public static class ByteConverter implements TypeConverter<Byte> {
- @Override
- public Byte convert(final String s) {
- return Byte.valueOf(s);
- }
- }
-
- /**
- * Converts a {@link String} into a {@link Character}.
- */
- @Plugin(name = "Character", category = CATEGORY)
- public static class CharacterConverter implements TypeConverter<Character> {
- @Override
- public Character convert(final String s) {
- if (s.length() != 1) {
- throw new IllegalArgumentException("Character string must be of length 1: " + s);
- }
- return Character.valueOf(s.toCharArray()[0]);
- }
- }
-
- /**
- * Converts a {@link String} into a {@code char[]}.
- */
- @Plugin(name = "CharacterArray", category = CATEGORY)
- public static class CharArrayConverter implements TypeConverter<char[]> {
- @Override
- public char[] convert(final String s) {
- return s.toCharArray();
- }
- }
-
- /**
- * Converts a {@link String} into a {@link Charset}.
- */
- @Plugin(name = "Charset", category = CATEGORY)
- public static class CharsetConverter implements TypeConverter<Charset> {
- @Override
- public Charset convert(final String s) {
- return Charset.forName(s);
- }
- }
-
- /**
- * Converts a {@link String} into a {@link Class}.
- */
- @Plugin(name = "Class", category = CATEGORY)
- public static class ClassConverter implements TypeConverter<Class<?>> {
- @Override
- public Class<?> convert(final String s) throws ClassNotFoundException {
- switch (s.toLowerCase()) {
- case "boolean":
- return boolean.class;
- case "byte":
- return byte.class;
- case "char":
- return char.class;
- case "double":
- return double.class;
- case "float":
- return float.class;
- case "int":
- return int.class;
- case "long":
- return long.class;
- case "short":
- return short.class;
- case "void":
- return void.class;
- default:
- return LoaderUtil.loadClass(s);
- }
-
- }
- }
-
- /**
- * Converts a {@link String} into a {@link Double}.
- */
- @Plugin(name = "Double", category = CATEGORY)
- public static class DoubleConverter implements TypeConverter<Double> {
- @Override
- public Double convert(final String s) {
- return Double.valueOf(s);
- }
- }
-
- /**
- * Converts a {@link String} into a {@link File}.
- */
- @Plugin(name = "File", category = CATEGORY)
- public static class FileConverter implements TypeConverter<File> {
- @Override
- public File convert(final String s) {
- return new File(s);
- }
- }
-
- /**
- * Converts a {@link String} into a {@link Float}.
- */
- @Plugin(name = "Float", category = CATEGORY)
- public static class FloatConverter implements TypeConverter<Float> {
- @Override
- public Float convert(final String s) {
- return Float.valueOf(s);
- }
- }
-
- /**
- * Converts a {@link String} into an {@link InetAddress}.
- */
- @Plugin(name = "InetAddress", category = CATEGORY)
- public static class InetAddressConverter implements TypeConverter<InetAddress> {
- @Override
- public InetAddress convert(final String s) throws Exception {
- return InetAddress.getByName(s);
- }
- }
-
- /**
- * Converts a {@link String} into a {@link Integer}.
- */
- @Plugin(name = "Integer", category = CATEGORY)
- public static class IntegerConverter implements TypeConverter<Integer> {
- @Override
- public Integer convert(final String s) {
- return Integer.valueOf(s);
- }
- }
-
- /**
- * Converts a {@link String} into a Log4j {@link Level}. Returns {@code null} for invalid level names.
- */
- @Plugin(name = "Level", category = CATEGORY)
- public static class LevelConverter implements TypeConverter<Level> {
- @Override
- public Level convert(final String s) {
- return Level.valueOf(s);
- }
- }
-
- /**
- * Converts a {@link String} into a {@link Long}.
- */
- @Plugin(name = "Long", category = CATEGORY)
- public static class LongConverter implements TypeConverter<Long> {
- @Override
- public Long convert(final String s) {
- return Long.valueOf(s);
- }
- }
-
- /**
- * Converts a {@link String} into a {@link Path}.
- * @since 2.8
- */
- @Plugin(name = "Path", category = CATEGORY)
- public static class PathConverter implements TypeConverter<Path> {
- @Override
- public Path convert(final String s) throws Exception {
- return Paths.get(s);
- }
- }
-
- /**
- * Converts a {@link String} into a {@link Pattern}.
- */
- @Plugin(name = "Pattern", category = CATEGORY)
- public static class PatternConverter implements TypeConverter<Pattern> {
- @Override
- public Pattern convert(final String s) {
- return Pattern.compile(s);
- }
- }
-
- /**
- * Converts a {@link String} into a {@link Provider}.
- */
- @Plugin(name = "SecurityProvider", category = CATEGORY)
- public static class SecurityProviderConverter implements TypeConverter<Provider> {
- @Override
- public Provider convert(final String s) {
- return Security.getProvider(s);
- }
- }
-
- /**
- * Converts a {@link String} into a {@link Short}.
- */
- @Plugin(name = "Short", category = CATEGORY)
- public static class ShortConverter implements TypeConverter<Short> {
- @Override
- public Short convert(final String s) {
- return Short.valueOf(s);
- }
- }
-
- /**
- * Returns the given {@link String}, no conversion takes place.
- */
- @Plugin(name = "String", category = CATEGORY)
- public static class StringConverter implements TypeConverter<String> {
- @Override
- public String convert(final String s) {
- return s;
- }
- }
-
- /**
- * Converts a {@link String} into a {@link URI}.
- */
- @Plugin(name = "URI", category = CATEGORY)
- public static class UriConverter implements TypeConverter<URI> {
- @Override
- public URI convert(final String s) throws URISyntaxException {
- return new URI(s);
- }
- }
-
- /**
- * Converts a {@link String} into a {@link URL}.
- */
- @Plugin(name = "URL", category = CATEGORY)
- public static class UrlConverter implements TypeConverter<URL> {
- @Override
- public URL convert(final String s) throws MalformedURLException {
- return new URL(s);
- }
- }
-
- /**
- * Converts a {@link String} into a {@link UUID}.
- * @since 2.8
- */
- @Plugin(name = "UUID", category = CATEGORY)
- public static class UuidConverter implements TypeConverter<UUID> {
- @Override
- public UUID convert(final String s) throws Exception {
- return UUID.fromString(s);
- }
- }
-
/**
* Converts a String to a given class if a TypeConverter is available for that class. Falls back to the provided
* default value if the conversion is unsuccessful. However, if the default value is <em>also</em> invalid, then
diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/convert/plugins/Log4jPlugins.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/convert/plugins/Log4jPlugins.java
deleted file mode 100644
index 2c11383..0000000
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/convert/plugins/Log4jPlugins.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 org.apache.logging.log4j.plugins.convert.plugins;
-
-import org.apache.logging.log4j.plugins.processor.PluginEntry;
-import org.apache.logging.log4j.plugins.processor.PluginService;
-
-/**
- * The base set of plugins.
- */
-public class Log4jPlugins extends PluginService {
-
- private static PluginEntry[] entries = new PluginEntry[] {
- new PluginEntry("bigdecimal", "org.apache.logging.log4j.plugins.convert.TypeConverters$BigDecimalConverter", "BigDecimal", false, false, "TypeConverter"),
- new PluginEntry("biginteger", "org.apache.logging.log4j.plugins.convert.TypeConverters$BigIntegerConverter", "BigInteger", false, false, "TypeConverter"),
- new PluginEntry("boolean", "org.apache.logging.log4j.plugins.convert.TypeConverters$BooleanConverter", "Boolean", false, false, "TypeConverter"),
- new PluginEntry("bytearray", "org.apache.logging.log4j.plugins.convert.TypeConverters$ByteArrayConverter", "ByteArray", false, false, "TypeConverter"),
- new PluginEntry("byte", "org.apache.logging.log4j.plugins.convert.TypeConverters$ByteConverter", "Byte", false, false, "TypeConverter"),
- new PluginEntry("character", "org.apache.logging.log4j.plugins.convert.TypeConverters$CharacterConverter", "Character", false, false, "TypeConverter"),
- new PluginEntry("characterarray", "org.apache.logging.log4j.plugins.convert.TypeConverters$CharArrayConverter", "CharacterArray", false, false, "TypeConverter"),
- new PluginEntry("charset", "org.apache.logging.log4j.plugins.convert.TypeConverters$CharsetConverter", "Charset", false, false, "TypeConverter"),
- new PluginEntry("class", "org.apache.logging.log4j.plugins.convert.TypeConverters$ClassConverter", "Class", false, false, "TypeConverter"),
- new PluginEntry("double", "org.apache.logging.log4j.plugins.convert.TypeConverters$DoubleConverter", "Double", false, false, "TypeConverter"),
- new PluginEntry("file", "org.apache.logging.log4j.plugins.convert.TypeConverters$FileConverter", "File", false, false, "TypeConverter"),
- new PluginEntry("float", "org.apache.logging.log4j.plugins.convert.TypeConverters$FloatConverter", "Float", false, false, "TypeConverter"),
- new PluginEntry("inetaddress", "org.apache.logging.log4j.plugins.convert.TypeConverters$InetAddressConverter", "InetAddress", false, false, "TypeConverter"),
- new PluginEntry("integer", "org.apache.logging.log4j.plugins.convert.TypeConverters$IntegerConverter", "Integer", false, false, "TypeConverter"),
- new PluginEntry("level", "org.apache.logging.log4j.plugins.convert.TypeConverters$LevelConverter", "Level", false, false, "TypeConverter"),
- new PluginEntry("long", "org.apache.logging.log4j.plugins.convert.TypeConverters$LongConverter", "Long", false, false, "TypeConverter"),
- new PluginEntry("path", "org.apache.logging.log4j.plugins.convert.TypeConverters$PathConverter", "Path", false, false, "TypeConverter"),
- new PluginEntry("pattern", "org.apache.logging.log4j.plugins.convert.TypeConverters$PatternConverter", "Pattern", false, false, "TypeConverter"),
- new PluginEntry("securityprovider", "org.apache.logging.log4j.plugins.convert.TypeConverters$SecurityProviderConverter", "SecurityProvider", false, false, "TypeConverter"),
- new PluginEntry("short", "org.apache.logging.log4j.plugins.convert.TypeConverters$ShortConverter", "Short", false, false, "TypeConverter"),
- new PluginEntry("string", "org.apache.logging.log4j.plugins.convert.TypeConverters$StringConverter", "String", false, false, "TypeConverter"),
- new PluginEntry("uri", "org.apache.logging.log4j.plugins.convert.TypeConverters$UriConverter", "URI", false, false, "TypeConverter"),
- new PluginEntry("url", "org.apache.logging.log4j.plugins.convert.TypeConverters$UrlConverter", "URL", false, false, "TypeConverter"),
- new PluginEntry("uuid", "org.apache.logging.log4j.plugins.convert.TypeConverters$UuidConverter", "UUID", false, false, "TypeConverter")
- };
- @Override
- public PluginEntry[] getEntries() { return entries;}
-}
diff --git a/log4j-plugins/src/main/resources/META-INF/services/org.apache.logging.log4j.plugins.processor.PluginService b/log4j-plugins/src/main/resources/META-INF/services/org.apache.logging.log4j.plugins.processor.PluginService
deleted file mode 100644
index dc436cf..0000000
--- a/log4j-plugins/src/main/resources/META-INF/services/org.apache.logging.log4j.plugins.processor.PluginService
+++ /dev/null
@@ -1 +0,0 @@
-org.apache.logging.log4j.plugins.convert.plugins.Log4jPlugins