You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2014/09/09 15:05:25 UTC

git commit: [LOG4J2-800] All life cycle implementations should be serializable. Implement missing hashCode() and equals(Object) methods.

Repository: logging-log4j2
Updated Branches:
  refs/heads/master bbf3d3484 -> eaf61bf1c


[LOG4J2-800] All life cycle implementations should be serializable.
Implement missing hashCode() and equals(Object) methods.

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

Branch: refs/heads/master
Commit: eaf61bf1c65eb472fe3f18c91b91eddf4b0b00be
Parents: bbf3d34
Author: Gary Gregory <ga...@gmail.com>
Authored: Tue Sep 9 09:05:20 2014 -0400
Committer: Gary Gregory <ga...@gmail.com>
Committed: Tue Sep 9 09:05:20 2014 -0400

----------------------------------------------------------------------
 .../logging/log4j/core/AbstractLifeCycle.java   |  30 +++-
 .../log4j/core/filter/AbstractFilter.java       |  82 +++++++----
 .../core/filter/DynamicThresholdFilter.java     | 136 +++++++++++++------
 3 files changed, 175 insertions(+), 73 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/eaf61bf1/log4j-core/src/main/java/org/apache/logging/log4j/core/AbstractLifeCycle.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/AbstractLifeCycle.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/AbstractLifeCycle.java
index 6b3bd10..92acc50 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/AbstractLifeCycle.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/AbstractLifeCycle.java
@@ -28,19 +28,45 @@ import org.apache.logging.log4j.status.StatusLogger;
  */
 public class AbstractLifeCycle implements LifeCycle, Serializable {
 
-    private static final long serialVersionUID = 1L;
-
     /**
      * Allow subclasses access to the status logger without creating another instance.
      */
     protected static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
 
+    private static final long serialVersionUID = 1L;
+
     private volatile LifeCycle.State state = LifeCycle.State.INITIALIZED;
 
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        AbstractLifeCycle other = (AbstractLifeCycle) obj;
+        if (state != other.state) {
+            return false;
+        }
+        return true;
+    }
+
     public LifeCycle.State getState() {
         return this.state;
     }
 
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((state == null) ? 0 : state.hashCode());
+        return result;
+    }
+
     public boolean isInitialized() {
         return this.state == LifeCycle.State.INITIALIZED;
     }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/eaf61bf1/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/AbstractFilter.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/AbstractFilter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/AbstractFilter.java
index e12e5a6..b958d37 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/AbstractFilter.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/AbstractFilter.java
@@ -61,27 +61,35 @@ public abstract class AbstractFilter extends AbstractLifeCycle implements Filter
         this.onMismatch = onMismatch == null ? Result.DENY : onMismatch;
     }
 
-    /**
-     * Returns the Result to be returned when a match does not occur.
-     * @return the onMismatch Result.
-     */
     @Override
-    public final Result getOnMismatch() {
-        return onMismatch;
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        AbstractFilter other = (AbstractFilter) obj;
+        if (onMatch != other.onMatch) {
+            return false;
+        }
+        if (onMismatch != other.onMismatch) {
+            return false;
+        }
+        return true;
     }
 
     /**
-     * Returns the Result to be returned when a match occurs.
-     * @return the onMatch Result.
+     * Context Filter method. The default returns NEUTRAL.
+     * @param event The LogEvent.
+     * @return The Result of filtering.
      */
     @Override
-    public final Result getOnMatch() {
-        return onMatch;
-    }
-
-    @Override
-    public String toString() {
-        return this.getClass().getSimpleName();
+    public Result filter(final LogEvent event) {
+        return Result.NEUTRAL;
     }
 
     /**
@@ -90,12 +98,12 @@ public abstract class AbstractFilter extends AbstractLifeCycle implements Filter
      * @param level The logging Level.
      * @param marker The Marker, if any.
      * @param msg The message, if present.
-     * @param params An array of parameters or null.
+     * @param t A throwable or null.
      * @return The Result of filtering.
      */
     @Override
-    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
-                         final Object... params) {
+    public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
+                         final Throwable t) {
         return Result.NEUTRAL;
     }
 
@@ -120,22 +128,44 @@ public abstract class AbstractFilter extends AbstractLifeCycle implements Filter
      * @param level The logging Level.
      * @param marker The Marker, if any.
      * @param msg The message, if present.
-     * @param t A throwable or null.
+     * @param params An array of parameters or null.
      * @return The Result of filtering.
      */
     @Override
-    public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
-                         final Throwable t) {
+    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
+                         final Object... params) {
         return Result.NEUTRAL;
     }
 
     /**
-     * Context Filter method. The default returns NEUTRAL.
-     * @param event The LogEvent.
-     * @return The Result of filtering.
+     * Returns the Result to be returned when a match occurs.
+     * @return the onMatch Result.
      */
     @Override
-    public Result filter(final LogEvent event) {
-        return Result.NEUTRAL;
+    public final Result getOnMatch() {
+        return onMatch;
+    }
+
+    /**
+     * Returns the Result to be returned when a match does not occur.
+     * @return the onMismatch Result.
+     */
+    @Override
+    public final Result getOnMismatch() {
+        return onMismatch;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = super.hashCode();
+        result = prime * result + ((onMatch == null) ? 0 : onMatch.hashCode());
+        result = prime * result + ((onMismatch == null) ? 0 : onMismatch.hashCode());
+        return result;
+    }
+
+    @Override
+    public String toString() {
+        return this.getClass().getSimpleName();
     }
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/eaf61bf1/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/DynamicThresholdFilter.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/DynamicThresholdFilter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/DynamicThresholdFilter.java
index ddb811e..dd89796 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/DynamicThresholdFilter.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/DynamicThresholdFilter.java
@@ -39,10 +39,34 @@ public final class DynamicThresholdFilter extends AbstractFilter {
     
     private static final long serialVersionUID = 1L;
 
-    private Map<String, Level> levelMap = new HashMap<String, Level>();
+    /**
+     * Create the DynamicThresholdFilter.
+     * @param key The name of the key to compare.
+     * @param pairs An array of value and Level pairs.
+     * @param defaultThreshold The default Level.
+     * @param onMatch The action to perform if a match occurs.
+     * @param onMismatch The action to perform if no match occurs.
+     * @return The DynamicThresholdFilter.
+     */
+    @PluginFactory
+    public static DynamicThresholdFilter createFilter(
+            @PluginAttribute("key") final String key,
+            @PluginElement("Pairs") final KeyValuePair[] pairs,
+            @PluginAttribute("defaultThreshold") final Level defaultThreshold,
+            @PluginAttribute("onMatch") final Result onMatch,
+            @PluginAttribute("onMismatch") final Result onMismatch) {
+        final Map<String, Level> map = new HashMap<String, Level>();
+        for (final KeyValuePair pair : pairs) {
+            map.put(pair.getKey(), Level.toLevel(pair.getValue()));
+        }
+        final Level level = defaultThreshold == null ? Level.ERROR : defaultThreshold;
+        return new DynamicThresholdFilter(key, map, level, onMatch, onMismatch);
+    }
     private Level defaultThreshold = Level.ERROR;
     private final String key;
 
+    private Map<String, Level> levelMap = new HashMap<String, Level>();
+
     private DynamicThresholdFilter(final String key, final Map<String, Level> pairs, final Level defaultLevel,
                                    final Result onMatch, final Result onMismatch) {
         super(onMatch, onMismatch);
@@ -54,44 +78,80 @@ public final class DynamicThresholdFilter extends AbstractFilter {
         this.defaultThreshold = defaultLevel;
     }
 
-    public String getKey() {
-        return this.key;
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        DynamicThresholdFilter other = (DynamicThresholdFilter) obj;
+        if (defaultThreshold == null) {
+            if (other.defaultThreshold != null) {
+                return false;
+            }
+        } else if (!defaultThreshold.equals(other.defaultThreshold)) {
+            return false;
+        }
+        if (key == null) {
+            if (other.key != null) {
+                return false;
+            }
+        } else if (!key.equals(other.key)) {
+            return false;
+        }
+        if (levelMap == null) {
+            if (other.levelMap != null) {
+                return false;
+            }
+        } else if (!levelMap.equals(other.levelMap)) {
+            return false;
+        }
+        return true;
+    }
+
+    private Result filter(final Level level) {
+        final Object value = ThreadContext.get(key);
+        if (value != null) {
+            Level ctxLevel = levelMap.get(value);
+            if (ctxLevel == null) {
+                ctxLevel = defaultThreshold;
+            }
+            return level.isMoreSpecificThan(ctxLevel) ? onMatch : onMismatch;
+        }
+        return Result.NEUTRAL;
+
     }
 
     @Override
-    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
-                         final Object... params) {
-        return filter(level);
+    public Result filter(final LogEvent event) {
+        return filter(event.getLevel());
     }
 
     @Override
-    public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
+    public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
                          final Throwable t) {
         return filter(level);
     }
 
     @Override
-    public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
+    public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
                          final Throwable t) {
         return filter(level);
     }
 
     @Override
-    public Result filter(final LogEvent event) {
-        return filter(event.getLevel());
+    public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
+                         final Object... params) {
+        return filter(level);
     }
 
-    private Result filter(final Level level) {
-        final Object value = ThreadContext.get(key);
-        if (value != null) {
-            Level ctxLevel = levelMap.get(value);
-            if (ctxLevel == null) {
-                ctxLevel = defaultThreshold;
-            }
-            return level.isMoreSpecificThan(ctxLevel) ? onMatch : onMismatch;
-        }
-        return Result.NEUTRAL;
-
+    public String getKey() {
+        return this.key;
     }
 
     public Map<String, Level> getLevelMap() {
@@ -99,6 +159,16 @@ public final class DynamicThresholdFilter extends AbstractFilter {
     }
 
     @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = super.hashCode();
+        result = prime * result + ((defaultThreshold == null) ? 0 : defaultThreshold.hashCode());
+        result = prime * result + ((key == null) ? 0 : key.hashCode());
+        result = prime * result + ((levelMap == null) ? 0 : levelMap.hashCode());
+        return result;
+    }
+
+    @Override
     public String toString() {
         final StringBuilder sb = new StringBuilder();
         sb.append("key=").append(key);
@@ -117,28 +187,4 @@ public final class DynamicThresholdFilter extends AbstractFilter {
         }
         return sb.toString();
     }
-
-    /**
-     * Create the DynamicThresholdFilter.
-     * @param key The name of the key to compare.
-     * @param pairs An array of value and Level pairs.
-     * @param defaultThreshold The default Level.
-     * @param onMatch The action to perform if a match occurs.
-     * @param onMismatch The action to perform if no match occurs.
-     * @return The DynamicThresholdFilter.
-     */
-    @PluginFactory
-    public static DynamicThresholdFilter createFilter(
-            @PluginAttribute("key") final String key,
-            @PluginElement("Pairs") final KeyValuePair[] pairs,
-            @PluginAttribute("defaultThreshold") final Level defaultThreshold,
-            @PluginAttribute("onMatch") final Result onMatch,
-            @PluginAttribute("onMismatch") final Result onMismatch) {
-        final Map<String, Level> map = new HashMap<String, Level>();
-        for (final KeyValuePair pair : pairs) {
-            map.put(pair.getKey(), Level.toLevel(pair.getValue()));
-        }
-        final Level level = defaultThreshold == null ? Level.ERROR : defaultThreshold;
-        return new DynamicThresholdFilter(key, map, level, onMatch, onMismatch);
-    }
 }