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 2021/07/11 11:42:40 UTC
[logging-log4j2] branch master updated: Reduce code duplication and
memory usage by using a singleton for an empty array of PathCondition.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push:
new aba0abe Reduce code duplication and memory usage by using a singleton for an empty array of PathCondition.
aba0abe is described below
commit aba0abe2712bdc6fc012c1853f6dc64debb2c518
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 11 07:42:35 2021 -0400
Reduce code duplication and memory usage by using a singleton for an
empty array of PathCondition.
Cherry-pick and resolve conflicts from release-2.x.
---
.../appender/rolling/DefaultRolloverStrategy.java | 2 +-
.../rolling/DirectWriteRolloverStrategy.java | 2 +-
.../rolling/action/IfAccumulatedFileCount.java | 3 +-
.../rolling/action/IfAccumulatedFileSize.java | 3 +-
.../core/appender/rolling/action/IfFileName.java | 3 +-
.../appender/rolling/action/IfLastModified.java | 3 +-
.../appender/rolling/action/PathCondition.java | 104 ++++++++++++---------
7 files changed, 66 insertions(+), 54 deletions(-)
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
index 560257e..b375801 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
@@ -525,7 +525,7 @@ public class DefaultRolloverStrategy extends AbstractRolloverStrategy {
.setBasePath(compressedName)
.setFollowLinks(false)
.setMaxDepth(1)
- .setPathConditions(new PathCondition[0])
+ .setPathConditions(PathCondition.EMPTY_ARRAY)
.setSubst(getStrSubstitutor())
.setFilePermissions(manager.getFilePermissions())
.setFileOwner(manager.getFileOwner())
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DirectWriteRolloverStrategy.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DirectWriteRolloverStrategy.java
index fe4159e..edd4428 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DirectWriteRolloverStrategy.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DirectWriteRolloverStrategy.java
@@ -336,7 +336,7 @@ public class DirectWriteRolloverStrategy extends AbstractRolloverStrategy implem
.setBasePath(compressedName)
.setFollowLinks(false)
.setMaxDepth(1)
- .setPathConditions(new PathCondition[0])
+ .setPathConditions(PathCondition.EMPTY_ARRAY)
.setSubst(getStrSubstitutor())
.setFilePermissions(manager.getFilePermissions())
.setFileOwner(manager.getFileOwner())
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileCount.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileCount.java
index 6c83a76..49878bb 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileCount.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileCount.java
@@ -45,8 +45,7 @@ public final class IfAccumulatedFileCount implements PathCondition {
throw new IllegalArgumentException("Count must be a positive integer but was " + thresholdParam);
}
this.threshold = thresholdParam;
- this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions,
- nestedConditions.length);
+ this.nestedConditions = PathCondition.copy(nestedConditions);
}
public int getThresholdCount() {
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java
index 7bad7d7..82c7798 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfAccumulatedFileSize.java
@@ -46,8 +46,7 @@ public final class IfAccumulatedFileSize implements PathCondition {
throw new IllegalArgumentException("Count must be a positive integer but was " + thresholdSize);
}
this.thresholdBytes = thresholdSize;
- this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions,
- nestedConditions.length);
+ this.nestedConditions = PathCondition.copy(nestedConditions);
}
public long getThresholdBytes() {
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
index bdf14dd..109fc36 100644
--- 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
@@ -64,8 +64,7 @@ public final class IfFileName implements PathCondition {
}
this.syntaxAndPattern = createSyntaxAndPatternString(glob, regex);
this.pathMatcher = FileSystems.getDefault().getPathMatcher(syntaxAndPattern);
- this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions,
- nestedConditions.length);
+ this.nestedConditions = PathCondition.copy(nestedConditions);
}
static String createSyntaxAndPatternString(final String glob, final String regex) {
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java
index 7aa3e73..a3b1f03 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java
@@ -47,8 +47,7 @@ public final class IfLastModified implements PathCondition {
private IfLastModified(final Duration age, final PathCondition[] nestedConditions) {
this.age = Objects.requireNonNull(age, "age");
- this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions,
- nestedConditions.length);
+ this.nestedConditions = PathCondition.copy(nestedConditions);
}
public Duration getAge() {
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/PathCondition.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/PathCondition.java
index 0fa7873..5308177 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/PathCondition.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/PathCondition.java
@@ -1,44 +1,60 @@
-/*
- * 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.Files;
-import java.nio.file.Path;
-import java.nio.file.attribute.BasicFileAttributes;
-
-/**
- * Filter that accepts or rejects a candidate {@code Path} for deletion.
- */
-public interface PathCondition {
-
- /**
- * Invoked before a new {@linkplain Files#walkFileTree(Path, java.util.Set, int, java.nio.file.FileVisitor) file
- * tree walk} is started. Stateful PathConditions can reset their state when this method is called.
- */
- void beforeFileTreeWalk();
-
- /**
- * Returns {@code true} if the specified candidate path should be deleted, {@code false} otherwise.
- *
- * @param baseDir the directory from where to start scanning for deletion candidate files
- * @param relativePath the candidate for deletion. This path is relative to the baseDir.
- * @param attrs attributes of the candidate path
- * @return whether the candidate path should be deleted
- */
- boolean accept(final Path baseDir, final Path relativePath, final BasicFileAttributes attrs);
-}
+/*
+ * 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.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.Arrays;
+
+/**
+ * Filter that accepts or rejects a candidate {@code Path} for deletion.
+ */
+public interface PathCondition {
+
+ /**
+ * Empty array.
+ */
+ static final PathCondition[] EMPTY_ARRAY = {};
+
+ /**
+ * Copies the given input.
+ *
+ * @param source What to copy
+ * @return a copy, never null.
+ */
+ static PathCondition[] copy(PathCondition[] source) {
+ return source == null ? new PathCondition[0] : Arrays.copyOf(source, source.length);
+ }
+
+ /**
+ * Invoked before a new {@linkplain Files#walkFileTree(Path, java.util.Set, int, java.nio.file.FileVisitor) file
+ * tree walk} is started. Stateful PathConditions can reset their state when this method is called.
+ */
+ void beforeFileTreeWalk();
+
+ /**
+ * Returns {@code true} if the specified candidate path should be deleted, {@code false} otherwise.
+ *
+ * @param baseDir the directory from where to start scanning for deletion candidate files
+ * @param relativePath the candidate for deletion. This path is relative to the baseDir.
+ * @param attrs attributes of the candidate path
+ * @return whether the candidate path should be deleted
+ */
+ boolean accept(final Path baseDir, final Path relativePath, final BasicFileAttributes attrs);
+}