LOG4J2-2428: extract code setting an event property as a separate method
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/commit/dc83a07e Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/tree/dc83a07e Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/diff/dc83a07e Branch: refs/heads/master Commit: dc83a07e668cc8326c29a6bb72fb1ce13845b24d Parents: bf04a1d Author: Andrei Ivanov <[email protected]> Authored: Mon Oct 1 00:07:40 2018 +0300 Committer: Andrei Ivanov <[email protected]> Committed: Mon Oct 1 00:07:40 2018 +0300 ---------------------------------------------------------------------- .../logging/log4j/audit/LogEventFactory.java | 82 ++++++++++---------- 1 file changed, 42 insertions(+), 40 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/blob/dc83a07e/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/LogEventFactory.java ---------------------------------------------------------------------- diff --git a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/LogEventFactory.java b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/LogEventFactory.java index 4105b97..9174b67 100644 --- a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/LogEventFactory.java +++ b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/LogEventFactory.java @@ -25,12 +25,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.EventLogger; -import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.MarkerManager; import org.apache.logging.log4j.ThreadContext; import org.apache.logging.log4j.audit.annotation.Constraint; import org.apache.logging.log4j.audit.annotation.Constraints; @@ -184,7 +180,7 @@ public class LogEventFactory { } } - public static List<String> getPropertyNames(String className) { + public static List<String> getPropertyNames(String className) { Class<?> intrface = getClass(className); List<String> names; if (intrface != null) { @@ -277,6 +273,7 @@ public class LogEventFactory { logEvent(msg, auditExceptionHandler); return null; } + if (method.getName().equals("setCompletionStatus")) { if (objects == null || objects[0] == null) { throw new IllegalArgumentException("Missing completion status"); @@ -285,6 +282,7 @@ public class LogEventFactory { msg.put(name, objects[0].toString()); return null; } + if (method.getName().equals("setAuditExceptionHandler")) { if (objects == null || objects[0] == null) { auditExceptionHandler = NOOP_EXCEPTION_HANDLER; @@ -295,47 +293,51 @@ public class LogEventFactory { } return null; } + if (method.getName().startsWith("set")) { - String name = NamingUtils.lowerFirst(NamingUtils.getMethodShortName(method.getName())); - if (objects == null || objects[0] == null) { - throw new IllegalArgumentException("No value to be set for " + name); - } + setProperty(method, objects); + return null; + } - Annotation[] annotations = method.getDeclaredAnnotations(); - Class<?> returnType = method.getReturnType(); - StringBuilder errors = new StringBuilder(); - for (Annotation annotation : annotations) { + return null; + } - if (annotation instanceof Constraints) { - Constraints constraints = (Constraints) annotation; - validateConstraints(false, constraints.value(), name, objects[0].toString(), - errors); - } else if (annotation instanceof Constraint) { - Constraint constraint = (Constraint) annotation; - constraintPlugins.validateConstraint(false, constraint.constraintType(), - name, objects[0].toString(), constraint.constraintValue(), errors); - } - } - if (errors.length() > 0) { - throw new ConstraintValidationException(errors.toString()); - } - String result; - if (objects[0] instanceof List) { - result = StringUtils.join(objects, ", "); - } else if (objects[0] instanceof Map) { - StructuredDataMessage extra = new StructuredDataMessage(name, null, null); - extra.putAll((Map)objects[0]); - msg.addContent(name, extra); - return null; - } else { - result = objects[0].toString(); - } + private void setProperty(Method method, Object[] objects) { + String name = NamingUtils.lowerFirst(NamingUtils.getMethodShortName(method.getName())); + if (objects == null || objects[0] == null) { + throw new IllegalArgumentException("No value to be set for " + name); + } - msg.put(name, result); - return null; + StringBuilder errors = new StringBuilder(); + Annotation[] annotations = method.getDeclaredAnnotations(); + for (Annotation annotation : annotations) { + if (annotation instanceof Constraints) { + Constraints constraints = (Constraints) annotation; + validateConstraints(false, constraints.value(), name, objects[0].toString(), + errors); + } else if (annotation instanceof Constraint) { + Constraint constraint = (Constraint) annotation; + constraintPlugins.validateConstraint(false, constraint.constraintType(), + name, objects[0].toString(), constraint.constraintValue(), errors); + } + } + if (errors.length() > 0) { + throw new ConstraintValidationException(errors.toString()); } - return null; + String result; + if (objects[0] instanceof List) { + result = StringUtils.join(objects, ", "); + } else if (objects[0] instanceof Map) { + StructuredDataMessage extra = new StructuredDataMessage(name, null, null); + extra.putAll((Map) objects[0]); + msg.addContent(name, extra); + return; + } else { + result = objects[0].toString(); + } + + msg.put(name, result); } }
