Repository: logging-log4j2 Updated Branches: refs/heads/master bda86100d -> 1b980d882
[LOG4J2-1930] Add forEach() methods to org.apache.logging.log4j.message.MapMessage. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/1b980d88 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/1b980d88 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/1b980d88 Branch: refs/heads/master Commit: 1b980d8827fa0f568a515765dea7715a89bf28ff Parents: bda8610 Author: Gary Gregory <[email protected]> Authored: Thu Jun 1 10:55:33 2017 -0700 Committer: Gary Gregory <[email protected]> Committed: Thu Jun 1 10:55:33 2017 -0700 ---------------------------------------------------------------------- .../logging/log4j/message/MapMessage.java | 54 ++++++++++++++++++++ .../log4j/core/appender/mom/JmsManager.java | 23 ++++++--- src/changes/changes.xml | 3 ++ 3 files changed, 72 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1b980d88/log4j-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java index 1e17b53..1bc23bc 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java @@ -20,14 +20,17 @@ import java.util.Collections; import java.util.Map; import java.util.TreeMap; +import org.apache.logging.log4j.util.BiConsumer; import org.apache.logging.log4j.util.EnglishEnums; import org.apache.logging.log4j.util.IndexedReadOnlyStringMap; import org.apache.logging.log4j.util.IndexedStringMap; import org.apache.logging.log4j.util.PerformanceSensitive; +import org.apache.logging.log4j.util.ReadOnlyStringMap; import org.apache.logging.log4j.util.SortedArrayStringMap; import org.apache.logging.log4j.util.StringBuilderFormattable; import org.apache.logging.log4j.util.StringBuilders; import org.apache.logging.log4j.util.Strings; +import org.apache.logging.log4j.util.TriConsumer; /** * Represents a Message that consists of a Map. @@ -214,6 +217,57 @@ public class MapMessage implements MultiformatMessage, StringBuilderFormattable } /** + * Performs the given action for each key-value pair in this data structure + * until all entries have been processed or the action throws an exception. + * <p> + * Some implementations may not support structural modifications (adding new elements or removing elements) while + * iterating over the contents. In such implementations, attempts to add or remove elements from the + * {@code BiConsumer}'s {@link BiConsumer#accept(Object, Object)} accept} method may cause a + * {@code ConcurrentModificationException} to be thrown. + * </p> + * + * @param action The action to be performed for each key-value pair in this collection + * @param <V> type of the value + * @throws java.util.ConcurrentModificationException some implementations may not support structural modifications + * to this data structure while iterating over the contents with {@link #forEach(BiConsumer)} or + * {@link #forEach(TriConsumer, Object)}. + * @see ReadOnlyStringMap#forEach(BiConsumer) + * @since 2.9 + */ + public <V> void forEach(final BiConsumer<String, ? super V> action) { + data.forEach(action); + } + + /** + * Performs the given action for each key-value pair in this data structure + * until all entries have been processed or the action throws an exception. + * <p> + * The third parameter lets callers pass in a stateful object to be modified with the key-value pairs, + * so the TriConsumer implementation itself can be stateless and potentially reusable. + * </p> + * <p> + * Some implementations may not support structural modifications (adding new elements or removing elements) while + * iterating over the contents. In such implementations, attempts to add or remove elements from the + * {@code TriConsumer}'s {@link TriConsumer#accept(Object, Object, Object) accept} method may cause a + * {@code ConcurrentModificationException} to be thrown. + * </p> + * + * @param action The action to be performed for each key-value pair in this collection + * @param state the object to be passed as the third parameter to each invocation on the specified + * triconsumer + * @param <V> type of the value + * @param <S> type of the third parameter + * @throws java.util.ConcurrentModificationException some implementations may not support structural modifications + * to this data structure while iterating over the contents with {@link #forEach(BiConsumer)} or + * {@link #forEach(TriConsumer, Object)}. + * @see ReadOnlyStringMap#forEach(TriConsumer, Object) + * @since 2.9 + */ + public <V, S> void forEach(final TriConsumer<String, ? super V, S> action, final S state) { + data.forEach(action, state); + } + + /** * Format the Structured data as described in RFC 5424. * * @param format The format identifier. Ignored in this implementation. http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1b980d88/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsManager.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsManager.java index 6556eac..cddf791 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsManager.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsManager.java @@ -18,7 +18,6 @@ package org.apache.logging.log4j.core.appender.mom; import java.io.Serializable; -import java.util.Map; import java.util.concurrent.TimeUnit; import javax.jms.Connection; @@ -37,6 +36,7 @@ import org.apache.logging.log4j.core.appender.AbstractManager; import org.apache.logging.log4j.core.appender.ManagerFactory; import org.apache.logging.log4j.core.net.JndiManager; import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.util.BiConsumer; /** * JMS connection and session manager. Can be used to access MessageProducer, MessageConsumer, and Message objects @@ -139,16 +139,23 @@ public class JmsManager extends AbstractManager { return this.session.createObjectMessage(object); } - private MapMessage map(final org.apache.logging.log4j.message.MapMessage log4jMapMessage, final MapMessage jmsMapMessage) - throws JMSException { - // Call getData() only once. - final Map<String, String> data = log4jMapMessage.getData(); - for (Map.Entry<String, String> entry : data.entrySet()) { - jmsMapMessage.setString(entry.getKey(), entry.getValue()); - } + private MapMessage map(final org.apache.logging.log4j.message.MapMessage log4jMapMessage, final MapMessage jmsMapMessage) { + // Map without calling rg.apache.logging.log4j.message.MapMessage#getData() which makes a copy of the map. + log4jMapMessage.forEach(new BiConsumer<String, String>() { + @Override + public void accept(final String key, final String value) { + try { + jmsMapMessage.setString(key, value); + } catch (JMSException e) { + throw new IllegalArgumentException(String.format("%s mapping key '%s' to value '%s': %s", + e.getClass(), key, value, e.getLocalizedMessage()), e); + } + } + }); return jmsMapMessage; } + @Override protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) { boolean closed = true; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1b980d88/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 9be1872..61c0496 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -40,6 +40,9 @@ <action issue="LOG4J2-1442" dev="mikes" type="add"> Generic HTTP appender. </action> + <action issue="LOG4J2-1930" dev="ggregory" type="add"> + Add forEach() methods to org.apache.logging.log4j.message.MapMessage. + </action> <action issue="LOG4J2-1917" dev="rgoers" type="update"> Support using java.util.ServiceLoader to locate Log4j 2 API providers. </action>
