Repository: qpid-broker-j Updated Branches: refs/heads/6.1.x bc81238bf -> b1eecae10
QPID-7892: [Java Broker] Escape regexp special characters in logback file name pattern for rolled log files Cherry picked from master 8c26b53. Project: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/commit/b3a65d50 Tree: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/tree/b3a65d50 Diff: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/diff/b3a65d50 Branch: refs/heads/6.1.x Commit: b3a65d505a510c1b6ecf55db644d5bab33eba7ab Parents: bc81238 Author: Alex Rudyy <[email protected]> Authored: Thu Aug 31 13:42:24 2017 +0100 Committer: Keith Wall <[email protected]> Committed: Wed Nov 22 15:48:03 2017 +0000 ---------------------------------------------------------------------- .../logging/logback/RollingPolicyDecorator.java | 67 ++++++++++++++------ 1 file changed, 47 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/b3a65d50/broker-plugins/logging-logback/src/main/java/org/apache/qpid/server/logging/logback/RollingPolicyDecorator.java ---------------------------------------------------------------------- diff --git a/broker-plugins/logging-logback/src/main/java/org/apache/qpid/server/logging/logback/RollingPolicyDecorator.java b/broker-plugins/logging-logback/src/main/java/org/apache/qpid/server/logging/logback/RollingPolicyDecorator.java index 1f8e49a..0d20a22 100644 --- a/broker-plugins/logging-logback/src/main/java/org/apache/qpid/server/logging/logback/RollingPolicyDecorator.java +++ b/broker-plugins/logging-logback/src/main/java/org/apache/qpid/server/logging/logback/RollingPolicyDecorator.java @@ -74,8 +74,9 @@ public class RollingPolicyDecorator implements RollingPolicy String filePathPattern = _decorated.getFileNamePattern(); String filePathRegExp = new FileNamePattern(filePathPattern, _decorated.getContext()).toRegex(); - _rolledFilesBaseFolder = getRolledFilesBaseFolderFromRegExp(filePathRegExp); - _rolledFileRegExp = Pattern.compile(filePathRegExp); + FilePathBaseFolderAndPatternPair pair = new FilePathBaseFolderAndPatternPair(filePathRegExp); + _rolledFilesBaseFolder = pair.getBaseFolder(); + _rolledFileRegExp = pair.getPattern(); _currentScanTask = null; } @@ -159,24 +160,6 @@ public class RollingPolicyDecorator implements RollingPolicy return task; } - private Path getRolledFilesBaseFolderFromRegExp(String fileNamePattern) - { - int firstDigitPatternPosition= fileNamePattern.indexOf("\\d"); - if (firstDigitPatternPosition == -1) - { - throw new RuntimeException("Rolling policy file pattern does not seem to contain date or integer token"); - } - int slashBeforeDigitPatternPosition = fileNamePattern.lastIndexOf("/", firstDigitPatternPosition); - if (slashBeforeDigitPatternPosition != -1) - { - return new File(fileNamePattern.substring(0, slashBeforeDigitPatternPosition)).toPath().toAbsolutePath(); - } - else - { - return new File(System.getProperty("user.dir")).toPath().toAbsolutePath(); - } - } - private class ScanTask implements Runnable { private int _rescanCounter; @@ -309,4 +292,48 @@ public class RollingPolicyDecorator implements RollingPolicy } } + + private static class FilePathBaseFolderAndPatternPair + { + private static Pattern REGEX_SPECIAL_CHARACTERS = Pattern.compile("[{}()\\[\\].+*?^$\\\\|]"); + private final Path _baseFolder; + private final Pattern _pattern; + + public FilePathBaseFolderAndPatternPair(String fileNamePattern) + { + String path; + int firstDigitPatternPosition= fileNamePattern.indexOf("\\d"); + if (firstDigitPatternPosition == -1) + { + throw new RuntimeException("Rolling policy file pattern does not seem to contain date or integer token"); + } + int slashBeforeDigitPatternPosition = fileNamePattern.lastIndexOf("/", firstDigitPatternPosition); + if (slashBeforeDigitPatternPosition != -1) + { + path = fileNamePattern.substring(0, slashBeforeDigitPatternPosition); + fileNamePattern = fileNamePattern.substring( slashBeforeDigitPatternPosition + 1); + } + else + { + path = System.getProperty("user.dir"); + } + _baseFolder = new File(path).toPath().toAbsolutePath(); + _pattern = Pattern.compile(escape(path) + "/" + fileNamePattern); + } + + private String escape(String string) + { + return REGEX_SPECIAL_CHARACTERS.matcher(string).replaceAll("\\\\$0"); + } + + public Path getBaseFolder() + { + return _baseFolder; + } + + public Pattern getPattern() + { + return _pattern; + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
