LOG4J2-435 sort paths before executing the filtered Delete action

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

Branch: refs/heads/master
Commit: 97e22a7f496410bddd3390c4c879f9eae51fe5ed
Parents: 4c8d8a1
Author: rpopma <[email protected]>
Authored: Thu Nov 26 23:55:06 2015 +0900
Committer: rpopma <[email protected]>
Committed: Thu Nov 26 23:55:06 2015 +0900

----------------------------------------------------------------------
 .../appender/rolling/action/DeleteAction.java   | 50 ++++++++++++++++++--
 .../rolling/action/DeleteActionTest.java        |  2 +-
 2 files changed, 47 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/97e22a7f/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/DeleteAction.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/DeleteAction.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/DeleteAction.java
index 3fb625d..3abaae7 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/DeleteAction.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/DeleteAction.java
@@ -17,9 +17,11 @@
 
 package org.apache.logging.log4j.core.appender.rolling.action;
 
+import java.io.IOException;
 import java.nio.file.FileVisitor;
 import java.nio.file.Path;
 import java.util.List;
+import java.util.Objects;
 
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.plugins.Plugin;
@@ -35,6 +37,8 @@ import org.apache.logging.log4j.core.lookup.StrSubstitutor;
 @Plugin(name = "Delete", category = "Core", printObject = true)
 public class DeleteAction extends AbstractPathAction {
 
+    private PathSorter pathSorter;
+
     /**
      * Creates a new DeleteAction that starts scanning for files to delete 
from the specified base path.
      * 
@@ -43,12 +47,47 @@ public class DeleteAction extends AbstractPathAction {
      * @param maxDepth The maxDepth parameter is the maximum number of levels 
of directories to visit. A value of 0
      *            means that only the starting file is visited, unless denied 
by the security manager. A value of
      *            MAX_VALUE may be used to indicate that all levels should be 
visited.
+     * @param sorter sorts
      * @param pathFilters an array of path filters (if more than one, they all 
need to accept a path before it is
      *            deleted).
      */
-    DeleteAction(final String basePath, final boolean followSymbolicLinks, 
final int maxDepth,
+    DeleteAction(final String basePath, final boolean followSymbolicLinks, 
final int maxDepth, final PathSorter sorter,
             final PathCondition[] pathFilters, final StrSubstitutor subst) {
         super(basePath, followSymbolicLinks, maxDepth, pathFilters, subst);
+        this.pathSorter = Objects.requireNonNull(sorter, "sorter");
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see 
org.apache.logging.log4j.core.appender.rolling.action.AbstractPathAction#execute(FileVisitor)
+     */
+    @Override
+    public boolean execute(final FileVisitor<Path> visitor) throws IOException 
{
+        final List<PathWithAttributes> sortedPaths = getSortedPaths();
+
+        for (PathWithAttributes element : sortedPaths) {
+            try {
+                visitor.visitFile(element.getPath(), element.getAttributes());
+            } catch (final IOException ioex) {
+                visitor.visitFileFailed(element.getPath(), ioex);
+            }
+        }
+        // TODO return (visitor.success || ignoreProcessingFailure)
+        return true; // do not abort rollover even if processing failed
+    }
+
+    /**
+     * Returns a sorted list of all files up to maxDepth under the basePath.
+     * 
+     * @return a sorted list of files
+     * @throws IOException
+     */
+    List<PathWithAttributes> getSortedPaths() throws IOException {
+        final SortingVisitor sort = new SortingVisitor(pathSorter);
+        super.execute(sort);
+        final List<PathWithAttributes> sortedPaths = sort.getSortedPaths();
+        return sortedPaths;
     }
 
     @Override
@@ -64,7 +103,8 @@ public class DeleteAction extends AbstractPathAction {
      * @param maxDepth The maxDepth parameter is the maximum number of levels 
of directories to visit. A value of 0
      *            means that only the starting file is visited, unless denied 
by the security manager. A value of
      *            MAX_VALUE may be used to indicate that all levels should be 
visited.
-     * @param pathFilters an array of path filters (if more than one, they all 
need to accept a path before it is
+     * @param PathSorter a plugin implementing the {@link PathSorter} interface
+     * @param PathConditions an array of path conditions (if more than one, 
they all need to accept a path before it is
      *            deleted).
      * @param config The Configuration.
      * @return A DeleteAction.
@@ -75,9 +115,11 @@ public class DeleteAction extends AbstractPathAction {
             @PluginAttribute("basePath") final String basePath, //
             @PluginAttribute(value = "followLinks", defaultBoolean = false) 
final boolean followLinks,
             @PluginAttribute(value = "maxDepth", defaultInt = 1) final int 
maxDepth,
-            @PluginElement("PathFilters") final PathCondition[] pathFilters,
+            @PluginElement("PathSorter") final PathSorter sorterParameter,
+            @PluginElement("PathConditions") final PathCondition[] 
pathConditions,
             @PluginConfiguration final Configuration config) {
             // @formatter:on
-        return new DeleteAction(basePath, followLinks, maxDepth, pathFilters, 
config.getStrSubstitutor());
+        final PathSorter sorter = sorterParameter == null ? new 
PathSortByModificationTime(true) : sorterParameter;
+        return new DeleteAction(basePath, followLinks, maxDepth, sorter, 
pathConditions, config.getStrSubstitutor());
     }
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/97e22a7f/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DeleteActionTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DeleteActionTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DeleteActionTest.java
index 5b9aa1e..8793732 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DeleteActionTest.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/DeleteActionTest.java
@@ -46,7 +46,7 @@ public class DeleteActionTest {
 
     private static DeleteAction create(String path, boolean followLinks, int 
maxDepth, PathCondition[] filters) {
         Configuration config = new BasicConfigurationFactory().new 
BasicConfiguration();
-        DeleteAction delete = DeleteAction.createDeleteAction(path, 
followLinks, maxDepth, filters, config);
+        DeleteAction delete = DeleteAction.createDeleteAction(path, 
followLinks, maxDepth, null, filters, config);
         return delete;
     }
 

Reply via email to