[LOG4J2-1730]: Add supporting configuration plugins

* ColumnMapping can be used for generic column mapping.
* SocketAddress can be used for host and port configurations.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/de0cfbb1
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/de0cfbb1
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/de0cfbb1

Branch: refs/heads/master
Commit: de0cfbb1c5f674522a6bc6dd3afbc7e2a95e98ba
Parents: 2c504ec
Author: Matt Sicker <[email protected]>
Authored: Sat Dec 31 18:41:31 2016 -0600
Committer: Matt Sicker <[email protected]>
Committed: Sat Dec 31 18:41:31 2016 -0600

----------------------------------------------------------------------
 .../log4j/core/appender/db/ColumnMapping.java   | 122 +++++++++++++++++++
 .../logging/log4j/core/net/SocketAddress.java   |  83 +++++++++++++
 2 files changed, 205 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/de0cfbb1/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/ColumnMapping.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/ColumnMapping.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/ColumnMapping.java
new file mode 100644
index 0000000..c4d6b45
--- /dev/null
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/ColumnMapping.java
@@ -0,0 +1,122 @@
+package org.apache.logging.log4j.core.appender.db;
+
+import org.apache.logging.log4j.core.Core;
+import org.apache.logging.log4j.core.StringLayout;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
+import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
+import org.apache.logging.log4j.core.config.plugins.PluginElement;
+import 
org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
+import org.apache.logging.log4j.core.layout.PatternLayout;
+import org.apache.logging.log4j.spi.ThreadContextMap;
+import org.apache.logging.log4j.spi.ThreadContextStack;
+
+/**
+ * A configuration element for specifying a database column name mapping.
+ *
+ * @since 2.8
+ */
+@Plugin(name = "ColumnMapping", category = Core.CATEGORY_NAME, printObject = 
true)
+public class ColumnMapping {
+
+    /**
+     * Column name.
+     */
+    private final String name;
+    /**
+     * Layout of value to write to database (before type conversion). Not 
applicable if {@link #type} is a collection.
+     */
+    private final StringLayout layout;
+    /**
+     * Class to convert value to before storing in database. If the type is a 
{@link ThreadContextMap}, then the
+     * MDC will be used. If the type is a {@link ThreadContextStack}, then the 
NDC will be used.
+     */
+    private final Class<?> type;
+
+    private ColumnMapping(final String name, final StringLayout layout, final 
Class<?> type) {
+        this.name = name;
+        this.layout = layout;
+        this.type = type;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public StringLayout getLayout() {
+        return layout;
+    }
+
+    public Class<?> getType() {
+        return type;
+    }
+
+    @PluginBuilderFactory
+    public static Builder newBuilder() {
+        return new Builder();
+    }
+
+    public static class Builder implements 
org.apache.logging.log4j.core.util.Builder<ColumnMapping> {
+
+        @PluginBuilderAttribute
+        @Required(message = "No column name provided")
+        private String name;
+
+        @PluginElement("Layout")
+        private StringLayout layout;
+
+        @PluginBuilderAttribute
+        private String pattern;
+
+        @PluginBuilderAttribute
+        @Required(message = "No conversion type provided")
+        private Class<?> type = String.class;
+
+        @PluginConfiguration
+        private Configuration configuration;
+
+        public Builder setName(final String name) {
+            this.name = name;
+            return this;
+        }
+
+        public Builder setLayout(final StringLayout layout) {
+            this.layout = layout;
+            return this;
+        }
+
+        public Builder setPattern(final String pattern) {
+            this.pattern = pattern;
+            return this;
+        }
+
+        public Builder setType(final Class<?> type) {
+            this.type = type;
+            return this;
+        }
+
+        public Builder setConfiguration(final Configuration configuration) {
+            this.configuration = configuration;
+            return this;
+        }
+
+        @Override
+        public ColumnMapping build() {
+            if (pattern != null) {
+                layout = PatternLayout.newBuilder()
+                    .withPattern(pattern)
+                    .withConfiguration(configuration)
+                    .build();
+            }
+            if (!(layout != null
+                || ThreadContextMap.class.isAssignableFrom(type)
+                || ThreadContextStack.class.isAssignableFrom(type))) {
+                throw new IllegalStateException(
+                    "No layout specified and type (" + type + ") is not 
compatible with ThreadContextMap or ThreadContextStack");
+            }
+            return new ColumnMapping(name, layout, type);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/de0cfbb1/log4j-core/src/main/java/org/apache/logging/log4j/core/net/SocketAddress.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/SocketAddress.java 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/SocketAddress.java
new file mode 100644
index 0000000..6bfa817
--- /dev/null
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/SocketAddress.java
@@ -0,0 +1,83 @@
+package org.apache.logging.log4j.core.net;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+
+import org.apache.logging.log4j.core.config.Node;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
+import 
org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
+import 
org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
+
+/**
+ * Plugin to hold a hostname and port (socket address).
+ *
+ * @since 2.8
+ */
+@Plugin(name = "SocketAddress", category = Node.CATEGORY, printObject = true)
+public class SocketAddress {
+
+    /**
+     * Creates a SocketAddress corresponding to {@code localhost:0}.
+     *
+     * @return a SocketAddress for {@code localhost:0}
+     */
+    public static SocketAddress getLoopback() {
+        return new SocketAddress(InetAddress.getLoopbackAddress(), 0);
+    }
+
+    private final InetSocketAddress socketAddress;
+
+    private SocketAddress(final InetAddress host, final int port) {
+        this.socketAddress = new InetSocketAddress(host, port);
+    }
+
+    public InetSocketAddress getSocketAddress() {
+        return socketAddress;
+    }
+
+    public int getPort() {
+        return socketAddress.getPort();
+    }
+
+    public InetAddress getAddress() {
+        return socketAddress.getAddress();
+    }
+
+    public String getHostName() {
+        return socketAddress.getHostName();
+    }
+
+    @PluginBuilderFactory
+    public static Builder newBuilder() {
+        return new Builder();
+    }
+
+    public static class Builder implements 
org.apache.logging.log4j.core.util.Builder<SocketAddress> {
+
+        @PluginBuilderAttribute
+        @ValidHost
+        private InetAddress host;
+
+        @PluginBuilderAttribute
+        @ValidPort
+        private int port;
+
+        public Builder setHost(final InetAddress host) {
+            this.host = host;
+            return this;
+        }
+
+        public Builder setPort(final int port) {
+            this.port = port;
+            return this;
+        }
+
+        @Override
+        public SocketAddress build() {
+            return new SocketAddress(host, port);
+        }
+    }
+
+}

Reply via email to