Skip to content

Deprecate o.a.l.l.c.a.r.a.Duration class for removal #2425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.file.attribute.FileTime;
import java.time.Duration;
import java.util.List;
import org.apache.logging.log4j.core.config.Node;
import org.apache.logging.log4j.core.config.NullConfiguration;
Expand All @@ -43,15 +44,10 @@
@SetSystemProperty(key = "log4j2.status.entries", value = "10")
class IfLastModifiedTest {

@Test
public void testGetDurationReturnsConstructorValue() {
final IfLastModified filter = IfLastModified.createAgeCondition(Duration.parse("P7D"));
assertEquals(0, filter.getAge().compareTo(Duration.parse("P7D")));
}

@Test
public void testAcceptsIfFileAgeEqualToDuration() {
final IfLastModified filter = IfLastModified.createAgeCondition(Duration.parse("PT33S"));
final IfLastModified filter =
IfLastModified.newBuilder().setAge(Duration.parse("PT33S")).build();
final DummyFileAttributes attrs = new DummyFileAttributes();
final long age = 33 * 1000;
attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - age);
Expand All @@ -60,7 +56,8 @@ public void testAcceptsIfFileAgeEqualToDuration() {

@Test
public void testAcceptsIfFileAgeExceedsDuration() {
final IfLastModified filter = IfLastModified.createAgeCondition(Duration.parse("PT33S"));
final IfLastModified filter =
IfLastModified.newBuilder().setAge(Duration.parse("PT33S")).build();
final DummyFileAttributes attrs = new DummyFileAttributes();
final long age = 33 * 1000 + 5;
attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - age);
Expand All @@ -69,7 +66,8 @@ public void testAcceptsIfFileAgeExceedsDuration() {

@Test
public void testDoesNotAcceptIfFileAgeLessThanDuration() {
final IfLastModified filter = IfLastModified.createAgeCondition(Duration.parse("PT33S"));
final IfLastModified filter =
IfLastModified.newBuilder().setAge(Duration.parse("PT33S")).build();
final DummyFileAttributes attrs = new DummyFileAttributes();
final long age = 33 * 1000 - 5;
attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - age);
Expand All @@ -79,7 +77,10 @@ public void testDoesNotAcceptIfFileAgeLessThanDuration() {
@Test
public void testAcceptCallsNestedConditionsOnlyIfPathAccepted() {
final CountingCondition counter = new CountingCondition(true);
final IfLastModified filter = IfLastModified.createAgeCondition(Duration.parse("PT33S"), counter);
final IfLastModified filter = IfLastModified.newBuilder()
.setAge(Duration.parse("PT33S"))
.setNestedConditions(counter)
.build();
final DummyFileAttributes attrs = new DummyFileAttributes();
final long oldEnough = 33 * 1000 + 5;
attrs.lastModified = FileTime.fromMillis(System.currentTimeMillis() - oldEnough);
Expand All @@ -104,16 +105,19 @@ public void testAcceptCallsNestedConditionsOnlyIfPathAccepted() {
@Test
public void testBeforeTreeWalk() {
final CountingCondition counter = new CountingCondition(true);
final IfLastModified filter =
IfLastModified.createAgeCondition(Duration.parse("PT33S"), counter, counter, counter);
final IfLastModified filter = IfLastModified.newBuilder()
.setAge(Duration.parse("PT33S"))
.setNestedConditions(counter, counter, counter)
.build();
filter.beforeFileTreeWalk();
assertEquals(3, counter.getBeforeFileTreeWalkCount());
}

@Test
public void testCreateAgeConditionCalledProgrammaticallyThrowsNPEWhenAgeIsNotSpecified() {
Duration age = null;
assertThrows(NullPointerException.class, () -> IfLastModified.createAgeCondition(age));
assertThrows(
NullPointerException.class, () -> IfLastModified.newBuilder().setAge(age));
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
* This implementation does not support fractions or negative values.
*
* @see #parse(CharSequence)
* @deprecated since 2.24.0 use {@link java.time.Duration} instead.
*/
@Deprecated
public class Duration implements Serializable, Comparable<Duration> {
private static final long serialVersionUID = -3756810052716342061L;

Expand Down Expand Up @@ -66,6 +68,13 @@ public class Duration implements Serializable, Comparable<Duration> {
private static final Pattern PATTERN = Pattern.compile(
"P?(?:([0-9]+)D)?" + "(T?(?:([0-9]+)H)?(?:([0-9]+)M)?(?:([0-9]+)?S)?)?", Pattern.CASE_INSENSITIVE);

/**
* @since 2.24.0
*/
public static Duration ofMillis(final long millis) {
return new Duration(millis / 1000L);
}

/**
* The number of seconds in the duration.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,25 @@
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Core;
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.PluginBuilderAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
import org.apache.logging.log4j.core.util.Clock;
import org.apache.logging.log4j.core.util.ClockFactory;
import org.apache.logging.log4j.status.StatusLogger;

/**
* PathCondition that accepts paths that are older than the specified duration.
* @since 2.5
*/
@Plugin(name = "IfLastModified", category = Core.CATEGORY_NAME, printObject = true)
public final class IfLastModified implements PathCondition {
Expand All @@ -50,20 +52,18 @@ private IfLastModified(final Duration age, final PathCondition[] nestedCondition
this.nestedConditions = PathCondition.copy(nestedConditions);
}

public Duration getAge() {
return age;
/**
* @deprecated since 2.24.0. In 3.0.0 the signature will change.
*/
@Deprecated
public org.apache.logging.log4j.core.appender.rolling.action.Duration getAge() {
return org.apache.logging.log4j.core.appender.rolling.action.Duration.ofMillis(age.toMillis());
}

public List<PathCondition> getNestedConditions() {
return Collections.unmodifiableList(Arrays.asList(nestedConditions));
}

/*
* (non-Javadoc)
*
* @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path,
* java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes)
*/
@Override
public boolean accept(final Path basePath, final Path relativePath, final BasicFileAttributes attrs) {
final FileTime fileTime = attrs.lastModifiedTime();
Expand All @@ -79,35 +79,62 @@ public boolean accept(final Path basePath, final Path relativePath, final BasicF
return result;
}

/*
* (non-Javadoc)
*
* @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk()
*/
@Override
public void beforeFileTreeWalk() {
IfAll.beforeFileTreeWalk(nestedConditions);
}

/**
* Create an IfLastModified condition.
*
* @param age The path age that is accepted by this condition. Must be a valid Duration.
* @param nestedConditions nested conditions to evaluate if this condition accepts a path
* @return An IfLastModified condition.
* @deprecated since 2.24.0 use {@link #newBuilder()} instead.
*/
@PluginFactory
@Deprecated
public static IfLastModified createAgeCondition(
// @formatter:off
@PluginAttribute("age") @Required(message = "No age provided for IfLastModified") final Duration age,
@PluginElement("PathConditions") final PathCondition... nestedConditions) {
// @formatter:on
return new IfLastModified(age, nestedConditions);
final org.apache.logging.log4j.core.appender.rolling.action.Duration age,
final PathCondition... pathConditions) {
return newBuilder()
.setAge(Duration.ofMillis(age.toMillis()))
.setNestedConditions(pathConditions)
.build();
}

@Override
public String toString() {
final String nested = nestedConditions.length == 0 ? "" : " AND " + Arrays.toString(nestedConditions);
return "IfLastModified(age=" + age + nested + ")";
}

/**
* @since 2.24.0
*/
@PluginBuilderFactory
public static Builder newBuilder() {
return new Builder();
}

/**
* @since 2.24.0
*/
public static final class Builder implements org.apache.logging.log4j.core.util.Builder<IfLastModified> {
@PluginBuilderAttribute
@Required(message = "No age provided for IfLastModified")
private org.apache.logging.log4j.core.appender.rolling.action.Duration age;

@PluginElement("nestedConditions")
private PathCondition[] nestedConditions;

public Builder setAge(final Duration age) {
this.age = org.apache.logging.log4j.core.appender.rolling.action.Duration.ofMillis(age.toMillis());
return this;
}

public Builder setNestedConditions(final PathCondition... nestedConditions) {
this.nestedConditions = nestedConditions;
return this;
}

@Override
public IfLastModified build() {
return isValid() ? new IfLastModified(Duration.ofMillis(age.toMillis()), nestedConditions) : null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Support classes for the Rolling File Appender.
*/
@Export
@Version("2.20.2")
@Version("2.24.0")
package org.apache.logging.log4j.core.appender.rolling.action;

import org.osgi.annotation.bundle.Export;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.regex.Pattern;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.appender.rolling.action.Duration;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.util.CronExpression;
import org.apache.logging.log4j.status.StatusLogger;
Expand Down Expand Up @@ -224,14 +223,17 @@ public Double convert(final String s) {
}

/**
* Converts a {@link String} into a {@link Duration}.
* Converts a {@link String} into a {@link org.apache.logging.log4j.core.appender.rolling.action.Duration}.
* @since 2.5
* @deprecated since 2.24.0. A {@link java.time.Duration} converter will be available in 3.0.0.
*/
@Plugin(name = "Duration", category = CATEGORY)
public static class DurationConverter implements TypeConverter<Duration> {
@Deprecated
public static class DurationConverter
implements TypeConverter<org.apache.logging.log4j.core.appender.rolling.action.Duration> {
@Override
public Duration convert(final String s) {
return Duration.parse(s);
public org.apache.logging.log4j.core.appender.rolling.action.Duration convert(final String s) {
return org.apache.logging.log4j.core.appender.rolling.action.Duration.parse(s);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* attributes in plugin factory methods.
*/
@Export
@Version("2.20.2")
@Version("2.24.0")
package org.apache.logging.log4j.core.config.plugins.convert;

import org.osgi.annotation.bundle.Export;
Expand Down
7 changes: 7 additions & 0 deletions src/changelog/.2.x.x/deprecate_duration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://logging.apache.org/log4j/changelog"
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.3.xsd"
type="changed">
<description format="asciidoc">Deprecate `org.apache.logging.log4j.core.appender.rolling.action.Duration` class for removal.</description>
</entry>