LOG4J2-435 Renamed FileNameFilter (plugin name "File") to IfFileName (plugin name "IfFileName": the FileAppender plugin also has plugin name "File"
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/c4b0b630 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c4b0b630 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c4b0b630 Branch: refs/heads/master Commit: c4b0b63012e29cdcde82af93907f77fd09ff4333 Parents: 68953fe Author: rpopma <[email protected]> Authored: Sun Nov 15 21:05:18 2015 +0900 Committer: rpopma <[email protected]> Committed: Sun Nov 15 21:05:18 2015 +0900 ---------------------------------------------------------------------- .../appender/rolling/action/FileNameFilter.java | 144 ----------------- .../appender/rolling/action/IfFileName.java | 154 +++++++++++++++++++ .../rolling/action/FileNameFilterTest.java | 106 ------------- .../appender/rolling/action/IfFileNameTest.java | 106 +++++++++++++ 4 files changed, 260 insertions(+), 250 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c4b0b630/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/FileNameFilter.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/FileNameFilter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/FileNameFilter.java deleted file mode 100644 index cbde2a9..0000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/FileNameFilter.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. - */ -package org.apache.logging.log4j.core.appender.rolling.action; - -import java.nio.file.Path; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.apache.logging.log4j.core.config.plugins.PluginAttribute; -import org.apache.logging.log4j.core.config.plugins.PluginFactory; - -/** - * PathFilter that accepts files for deletion if their relative path matches either a path pattern or a regular - * expression. - * <p> - * If both a regular expression and a path pattern are specified the path pattern is used and the regular expression is - * ignored. - * <p> - * The path pattern may contain '?' and '*' wildcarts. - */ -@Plugin(name = "File", category = "Core", printObject = true) -public final class FileNameFilter implements PathFilter { - - private final Pattern regex; - private final String pathPattern; - - /** - * Constructs a FileNameFilter filter. If both a regular expression and a path pattern are specified the path - * pattern is used and the regular expression is ignored. - * - * @param path the baseDir-relative path pattern of the files to delete (may contain '*' and '?' wildcarts) - * @param regex the regular expression that matches the baseDir-relative path of the file(s) to delete - */ - private FileNameFilter(final String path, final String regex) { - if (regex == null && path == null) { - throw new IllegalArgumentException("Specify either a path or a regular expression. Both cannot be null."); - } - this.regex = regex != null ? Pattern.compile(regex) : null; - this.pathPattern = path; - } - - /** - * Returns the compiled regular expression that matches the baseDir-relative path of the file(s) to delete, or - * {@code null} if no regular expression was specified. - * - * @return the compiled regular expression, or {@code null} - */ - public Pattern getRegex() { - return regex; - } - - /** - * Returns the baseDir-relative path pattern of the files to delete, or {@code null} if not specified. This path - * pattern may contain '*' and '?' wildcarts. - * - * @return relative path of the file(s) to delete (may contain '*' and '?' wildcarts) - */ - public String getPathPattern() { - return pathPattern; - } - - /* - * (non-Javadoc) - * - * @see org.apache.logging.log4j.core.appender.rolling.action.PathFilter#accept(java.nio.file.Path, - * java.nio.file.Path) - */ - @Override - public boolean accept(final Path baseDir, final Path relativePath, final BasicFileAttributes attrs) { - if (pathPattern != null) { - return isMatch(relativePath.toString(), pathPattern); - } else { - Matcher matcher = regex.matcher(relativePath.toString()); - return (matcher.matches()); - } - } - - // package protected for unit tests - static boolean isMatch(final String text, final String pattern) { - int i = 0; - int j = 0; - int starIndex = -1; - int iIndex = -1; - - while (i < text.length()) { - if (j < pattern.length() && (pattern.charAt(j) == '?' || pattern.charAt(j) == text.charAt(i))) { - ++i; - ++j; - } else if (j < pattern.length() && pattern.charAt(j) == '*') { - starIndex = j; - iIndex = i; - j++; - } else if (starIndex != -1) { - j = starIndex + 1; - i = iIndex + 1; - iIndex++; - } else { - return false; - } - } - - while (j < pattern.length() && pattern.charAt(j) == '*') { - ++j; - } - return j == pattern.length(); - } - - /** - * Creates a FileNameFilter filter. If both a regular expression and a path pattern are specified the path pattern - * is used and the regular expression is ignored. - * - * @param path the baseDir-relative path pattern of the files to delete (may contain '*' and '?' wildcarts) - * @param regex the regular expression that matches the baseDir-relative path of the file(s) to delete - * @return A FileNameFilter filter. - */ - @PluginFactory - public static FileNameFilter createNameFilter( // - @PluginAttribute("path") final String path, // - @PluginAttribute("regex") final String regex) { - return new FileNameFilter(path, regex); - } - - @Override - public String toString() { - final String pattern = regex == null ? "null" : regex.pattern(); - return "FileNameFilter(regex=" + pattern + ", name=" + pathPattern + ")"; - } -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c4b0b630/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfFileName.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfFileName.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfFileName.java new file mode 100644 index 0000000..9b3810c --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfFileName.java @@ -0,0 +1,154 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.core.appender.rolling.action; + +import java.nio.file.Path; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.plugins.PluginAttribute; +import org.apache.logging.log4j.core.config.plugins.PluginFactory; +import org.apache.logging.log4j.status.StatusLogger; + +/** + * PathFilter that accepts files for deletion if their relative path matches either a path pattern or a regular + * expression. + * <p> + * If both a regular expression and a path pattern are specified the path pattern is used and the regular expression is + * ignored. + * <p> + * The path pattern may contain '?' and '*' wildcarts. + */ +@Plugin(name = "FileNameFilter", category = "Core", printObject = true) +public final class IfFileName implements PathCondition { + private static final Logger LOGGER = StatusLogger.getLogger(); + private final Pattern regex; + private final String pathPattern; + + /** + * Constructs a FileNameFilter filter. If both a regular expression and a path pattern are specified the path + * pattern is used and the regular expression is ignored. + * + * @param path the baseDir-relative path pattern of the files to delete (may contain '*' and '?' wildcarts) + * @param regex the regular expression that matches the baseDir-relative path of the file(s) to delete + */ + private IfFileName(final String path, final String regex) { + if (regex == null && path == null) { + throw new IllegalArgumentException("Specify either a path or a regular expression. Both cannot be null."); + } + this.regex = regex != null ? Pattern.compile(regex) : null; + this.pathPattern = path; + } + + /** + * Returns the compiled regular expression that matches the baseDir-relative path of the file(s) to delete, or + * {@code null} if no regular expression was specified. + * + * @return the compiled regular expression, or {@code null} + */ + public Pattern getRegex() { + return regex; + } + + /** + * Returns the baseDir-relative path pattern of the files to delete, or {@code null} if not specified. This path + * pattern may contain '*' and '?' wildcarts. + * + * @return relative path of the file(s) to delete (may contain '*' and '?' wildcarts) + */ + public String getPathPattern() { + return pathPattern; + } + + /* + * (non-Javadoc) + * + * @see org.apache.logging.log4j.core.appender.rolling.action.PathFilter#accept(java.nio.file.Path, + * java.nio.file.Path) + */ + @Override + public boolean accept(final Path basePath, final Path relativePath, final BasicFileAttributes attrs) { + if (pathPattern != null) { + final boolean result = isMatch(relativePath.toString(), pathPattern); + final String match = result ? "" : "not "; + LOGGER.trace("FileNameFilter: pathPattern '{}' does {}match relative path '{}'", pathPattern, match, + relativePath); + return result; + } else { + final Matcher matcher = regex.matcher(relativePath.toString()); + final boolean result = matcher.matches(); + final String match = result ? "" : "not "; + LOGGER.trace("FileNameFilter: regex '{}' does {}match relative path '{}'", regex.pattern(), match, + relativePath); + return result; + } + } + + // package protected for unit tests + static boolean isMatch(final String text, final String pattern) { + int i = 0; + int j = 0; + int starIndex = -1; + int iIndex = -1; + + while (i < text.length()) { + if (j < pattern.length() && (pattern.charAt(j) == '?' || pattern.charAt(j) == text.charAt(i))) { + ++i; + ++j; + } else if (j < pattern.length() && pattern.charAt(j) == '*') { + starIndex = j; + iIndex = i; + j++; + } else if (starIndex != -1) { + j = starIndex + 1; + i = iIndex + 1; + iIndex++; + } else { + return false; + } + } + + while (j < pattern.length() && pattern.charAt(j) == '*') { + ++j; + } + return j == pattern.length(); + } + + /** + * Creates a FileNameFilter filter. If both a regular expression and a path pattern are specified the path pattern + * is used and the regular expression is ignored. + * + * @param path the baseDir-relative path pattern of the files to delete (may contain '*' and '?' wildcarts) + * @param regex the regular expression that matches the baseDir-relative path of the file(s) to delete + * @return A FileNameFilter filter. + */ + @PluginFactory + public static IfFileName createNameFilter( // + @PluginAttribute("path") final String path, // + @PluginAttribute("regex") final String regex) { + return new IfFileName(path, regex); + } + + @Override + public String toString() { + final String pattern = regex == null ? "null" : regex.pattern(); + return "FileNameFilter(regex=" + pattern + ", path=" + pathPattern + ")"; + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c4b0b630/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FileNameFilterTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FileNameFilterTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FileNameFilterTest.java deleted file mode 100644 index 45469a4..0000000 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/FileNameFilterTest.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. - */ - -package org.apache.logging.log4j.core.appender.rolling.action; - -import java.nio.file.FileSystems; -import java.nio.file.Path; - -import org.apache.logging.log4j.core.appender.rolling.action.FileNameFilter; -import org.junit.Test; - -import static org.junit.Assert.*; - -public class FileNameFilterTest { - - @Test(expected = IllegalArgumentException.class) - public void testCreateNameFilterFailsIfBothRegexAndPathAreNull() { - FileNameFilter.createNameFilter(null, null); - } - - @Test() - public void testCreateNameFilterAcceptsIfEitherRegexOrPathOrBothAreNonNull() { - FileNameFilter.createNameFilter("bar", null); - FileNameFilter.createNameFilter(null, "foo"); - FileNameFilter.createNameFilter("bar", "foo"); - } - - @Test - public void testGetRegexReturnsConstructorValue() { - assertEquals("bar", FileNameFilter.createNameFilter(null, "bar").getRegex().pattern()); - assertEquals(null, FileNameFilter.createNameFilter("path", null).getRegex()); - } - - @Test - public void testGetPathReturnsConstructorValue() { - assertEquals("path", FileNameFilter.createNameFilter("path", null).getPathPattern()); - assertEquals(null, FileNameFilter.createNameFilter(null, "bar").getPathPattern()); - } - - @Test - public void testAcceptUsesPathPatternIfExists() { - final FileNameFilter filter = FileNameFilter.createNameFilter("path", "regex"); - final Path relativePath = FileSystems.getDefault().getPath("path"); - assertTrue(filter.accept(null, relativePath, null)); - - final Path pathMatchingRegex = FileSystems.getDefault().getPath("regex"); - assertFalse(filter.accept(null, pathMatchingRegex, null)); - } - - @Test - public void testAcceptUsesRegexIfNoPathPatternExists() { - final FileNameFilter regexFilter = FileNameFilter.createNameFilter(null, "regex"); - final Path pathMatchingRegex = FileSystems.getDefault().getPath("regex"); - assertTrue(regexFilter.accept(null, pathMatchingRegex, null)); - - final Path noMatch = FileSystems.getDefault().getPath("nomatch"); - assertFalse(regexFilter.accept(null, noMatch, null)); - } - - @Test - public void testAcceptIgnoresBasePathAndAttributes() { - final FileNameFilter pathFilter = FileNameFilter.createNameFilter("path", null); - final Path relativePath = FileSystems.getDefault().getPath("path"); - assertTrue(pathFilter.accept(null, relativePath, null)); - - final FileNameFilter regexFilter = FileNameFilter.createNameFilter(null, "regex"); - final Path pathMatchingRegex = FileSystems.getDefault().getPath("regex"); - assertTrue(regexFilter.accept(null, pathMatchingRegex, null)); - } - - @Test - public void testIsMatch() { - assertTrue(FileNameFilter.isMatch("abc", "???")); - assertTrue(FileNameFilter.isMatch("abc", "a??")); - assertTrue(FileNameFilter.isMatch("abc", "?b?")); - assertTrue(FileNameFilter.isMatch("abc", "??c")); - assertTrue(FileNameFilter.isMatch("abc", "ab?")); - assertTrue(FileNameFilter.isMatch("abc", "?bc")); - assertTrue(FileNameFilter.isMatch("abc", "*")); - assertTrue(FileNameFilter.isMatch("abc", "*bc")); - assertTrue(FileNameFilter.isMatch("abc", "*c")); - assertTrue(FileNameFilter.isMatch("abc", "*c*")); - assertTrue(FileNameFilter.isMatch("abc", "a*")); - assertTrue(FileNameFilter.isMatch("abc", "*a*")); - assertTrue(FileNameFilter.isMatch("abc", "*b*")); - - assertFalse(FileNameFilter.isMatch("abc", "????")); - assertFalse(FileNameFilter.isMatch("abc", "b*")); - assertFalse(FileNameFilter.isMatch("abc", "*b")); - } - -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c4b0b630/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfFileNameTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfFileNameTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfFileNameTest.java new file mode 100644 index 0000000..afb8fd2 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/action/IfFileNameTest.java @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package org.apache.logging.log4j.core.appender.rolling.action; + +import java.nio.file.FileSystems; +import java.nio.file.Path; + +import org.apache.logging.log4j.core.appender.rolling.action.IfFileName; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class IfFileNameTest { + + @Test(expected = IllegalArgumentException.class) + public void testCreateNameFilterFailsIfBothRegexAndPathAreNull() { + IfFileName.createNameFilter(null, null); + } + + @Test() + public void testCreateNameFilterAcceptsIfEitherRegexOrPathOrBothAreNonNull() { + IfFileName.createNameFilter("bar", null); + IfFileName.createNameFilter(null, "foo"); + IfFileName.createNameFilter("bar", "foo"); + } + + @Test + public void testGetRegexReturnsConstructorValue() { + assertEquals("bar", IfFileName.createNameFilter(null, "bar").getRegex().pattern()); + assertEquals(null, IfFileName.createNameFilter("path", null).getRegex()); + } + + @Test + public void testGetPathReturnsConstructorValue() { + assertEquals("path", IfFileName.createNameFilter("path", null).getPathPattern()); + assertEquals(null, IfFileName.createNameFilter(null, "bar").getPathPattern()); + } + + @Test + public void testAcceptUsesPathPatternIfExists() { + final IfFileName filter = IfFileName.createNameFilter("path", "regex"); + final Path relativePath = FileSystems.getDefault().getPath("path"); + assertTrue(filter.accept(null, relativePath, null)); + + final Path pathMatchingRegex = FileSystems.getDefault().getPath("regex"); + assertFalse(filter.accept(null, pathMatchingRegex, null)); + } + + @Test + public void testAcceptUsesRegexIfNoPathPatternExists() { + final IfFileName regexFilter = IfFileName.createNameFilter(null, "regex"); + final Path pathMatchingRegex = FileSystems.getDefault().getPath("regex"); + assertTrue(regexFilter.accept(null, pathMatchingRegex, null)); + + final Path noMatch = FileSystems.getDefault().getPath("nomatch"); + assertFalse(regexFilter.accept(null, noMatch, null)); + } + + @Test + public void testAcceptIgnoresBasePathAndAttributes() { + final IfFileName pathFilter = IfFileName.createNameFilter("path", null); + final Path relativePath = FileSystems.getDefault().getPath("path"); + assertTrue(pathFilter.accept(null, relativePath, null)); + + final IfFileName regexFilter = IfFileName.createNameFilter(null, "regex"); + final Path pathMatchingRegex = FileSystems.getDefault().getPath("regex"); + assertTrue(regexFilter.accept(null, pathMatchingRegex, null)); + } + + @Test + public void testIsMatch() { + assertTrue(IfFileName.isMatch("abc", "???")); + assertTrue(IfFileName.isMatch("abc", "a??")); + assertTrue(IfFileName.isMatch("abc", "?b?")); + assertTrue(IfFileName.isMatch("abc", "??c")); + assertTrue(IfFileName.isMatch("abc", "ab?")); + assertTrue(IfFileName.isMatch("abc", "?bc")); + assertTrue(IfFileName.isMatch("abc", "*")); + assertTrue(IfFileName.isMatch("abc", "*bc")); + assertTrue(IfFileName.isMatch("abc", "*c")); + assertTrue(IfFileName.isMatch("abc", "*c*")); + assertTrue(IfFileName.isMatch("abc", "a*")); + assertTrue(IfFileName.isMatch("abc", "*a*")); + assertTrue(IfFileName.isMatch("abc", "*b*")); + + assertFalse(IfFileName.isMatch("abc", "????")); + assertFalse(IfFileName.isMatch("abc", "b*")); + assertFalse(IfFileName.isMatch("abc", "*b")); + } + +}
