Skip to content

Commit cd83a8c

Browse files
committedMay 22, 2024
Clean up PropertyEnvironment
We clean up `PropertyEnvironment` from all the unused public methods. Since all the classes should only use `getProperty(Class)`, the other converter methods are not necessary. We keep `getStringProperty` (renamed to `getProperty(String)`) for convenience: this allows to transform a `PropertyEnvironment` into a `PropertySource`.

File tree

18 files changed

+56
-609
lines changed

18 files changed

+56
-609
lines changed
 

‎log4j-core/src/main/java/org/apache/logging/log4j/core/util/OptionConverter.java

+1-91
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
*/
1717
package org.apache.logging.log4j.core.util;
1818

19-
import static org.apache.logging.log4j.util.Strings.toRootUpperCase;
20-
2119
import java.io.InterruptedIOException;
2220
import java.util.ArrayList;
2321
import java.util.List;
@@ -40,23 +38,12 @@ public final class OptionConverter {
4038
private static final char DELIM_STOP = '}';
4139
private static final int DELIM_START_LEN = 2;
4240
private static final int DELIM_STOP_LEN = 1;
43-
private static final int ONE_K = 1024;
4441

4542
/**
4643
* OptionConverter is a static class.
4744
*/
4845
private OptionConverter() {}
4946

50-
public static String[] concatenateArrays(final String[] l, final String[] r) {
51-
final int len = l.length + r.length;
52-
final String[] a = new String[len];
53-
54-
System.arraycopy(l, 0, a, 0, l.length);
55-
System.arraycopy(r, 0, a, l.length, r.length);
56-
57-
return a;
58-
}
59-
6047
public static String convertSpecialChars(final String s) {
6148
char c;
6249
final int len = s.length();
@@ -114,49 +101,6 @@ public static Object instantiateByKey(
114101
return OptionConverter.instantiateByClassName(className.trim(), superClass, defaultValue);
115102
}
116103

117-
/**
118-
* If <code>value</code> is "true", then {@code true} is
119-
* returned. If <code>value</code> is "false", then
120-
* {@code false} is returned. Otherwise, <code>default</code> is
121-
* returned.
122-
*
123-
* <p>Case of value is unimportant.</p>
124-
* @param value The value to convert.
125-
* @param defaultValue The default value.
126-
* @return true or false, depending on the value and/or default.
127-
*/
128-
public static boolean toBoolean(final String value, final boolean defaultValue) {
129-
if (value == null) {
130-
return defaultValue;
131-
}
132-
final String trimmedVal = value.trim();
133-
if ("true".equalsIgnoreCase(trimmedVal)) {
134-
return true;
135-
}
136-
if ("false".equalsIgnoreCase(trimmedVal)) {
137-
return false;
138-
}
139-
return defaultValue;
140-
}
141-
142-
/**
143-
* Convert the String value to an int.
144-
* @param value The value as a String.
145-
* @param defaultValue The default value.
146-
* @return The value as an int.
147-
*/
148-
public static int toInt(final String value, final int defaultValue) {
149-
if (value != null) {
150-
final String s = value.trim();
151-
try {
152-
return Integer.parseInt(s);
153-
} catch (final NumberFormatException e) {
154-
LOGGER.error("[{}] is not in proper int form.", s, e);
155-
}
156-
}
157-
return defaultValue;
158-
}
159-
160104
public static Level toLevel(String value, Level defaultValue) {
161105
if (value == null) {
162106
return defaultValue;
@@ -220,40 +164,6 @@ public static Level toLevel(String value, Level defaultValue) {
220164
return result;
221165
}
222166

223-
/**
224-
*
225-
* @param value The size of the file as a String.
226-
* @param defaultValue The default value.
227-
* @return The size of the file as a long.
228-
*/
229-
public static long toFileSize(final String value, final long defaultValue) {
230-
if (value == null) {
231-
return defaultValue;
232-
}
233-
234-
String str = toRootUpperCase(value.trim());
235-
long multiplier = 1;
236-
int index;
237-
238-
if ((index = str.indexOf("KB")) != -1) {
239-
multiplier = ONE_K;
240-
str = str.substring(0, index);
241-
} else if ((index = str.indexOf("MB")) != -1) {
242-
multiplier = ONE_K * ONE_K;
243-
str = str.substring(0, index);
244-
} else if ((index = str.indexOf("GB")) != -1) {
245-
multiplier = ONE_K * ONE_K * ONE_K;
246-
str = str.substring(0, index);
247-
}
248-
try {
249-
return Long.parseLong(str) * multiplier;
250-
} catch (final NumberFormatException e) {
251-
LOGGER.error("[{}] is not in proper int form.", str);
252-
LOGGER.error("[{}] not in expected format.", value, e);
253-
}
254-
return defaultValue;
255-
}
256-
257167
/**
258168
* Find the value corresponding to <code>key</code> in
259169
* <code>props</code>. Then perform variable substitution on the
@@ -380,7 +290,7 @@ private static String substVars(final String val, final Properties props, final
380290
j += DELIM_START_LEN;
381291
final String key = val.substring(j, k);
382292
// first try in System properties
383-
String replacement = PropertyEnvironment.getGlobal().getStringProperty(key, null);
293+
String replacement = PropertyEnvironment.getGlobal().getProperty(key);
384294
// then try props parameter
385295
if (replacement == null && props != null) {
386296
replacement = props.getProperty(key);

‎log4j-core/src/main/java/org/apache/logging/log4j/core/util/UuidUtil.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.util.Random;
2222
import java.util.UUID;
2323
import java.util.concurrent.atomic.AtomicInteger;
24-
import org.apache.logging.log4j.core.impl.CoreProperties;
24+
import org.apache.logging.log4j.core.impl.CoreProperties.UuidProperties;
2525
import org.apache.logging.log4j.kit.env.PropertyEnvironment;
2626

2727
/**
@@ -37,9 +37,8 @@ public final class UuidUtil {
3737
private static final byte VARIANT = (byte) 0x80;
3838
private static final int SEQUENCE_MASK = 0x3FFF;
3939
private static final long NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000L;
40-
private static final long INITIAL_UUID_SEQNO = PropertyEnvironment.getGlobal()
41-
.getProperty(CoreProperties.UuidProperties.class)
42-
.sequence();
40+
private static final long INITIAL_UUID_SEQNO =
41+
PropertyEnvironment.getGlobal().getProperty(UuidProperties.class).sequence();
4342

4443
private static final long LOW_MASK = 0xffffffffL;
4544
private static final long MID_MASK = 0xffff00000000L;
@@ -78,7 +77,7 @@ static long initialize(byte[] mac) {
7877
System.arraycopy(mac, index, node, 2, length);
7978
final ByteBuffer buf = ByteBuffer.wrap(node);
8079
long rand = INITIAL_UUID_SEQNO;
81-
String assigned = PropertyEnvironment.getGlobal().getStringProperty(ASSIGNED_SEQUENCES);
80+
String assigned = System.getProperty(ASSIGNED_SEQUENCES);
8281
final long[] sequences;
8382
if (assigned == null) {
8483
sequences = new long[0];

‎log4j-docker/src/main/java/org/apache/logging/log4j/docker/DockerLookup.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.fasterxml.jackson.core.type.TypeReference;
2020
import com.fasterxml.jackson.databind.ObjectMapper;
2121
import java.io.IOException;
22+
import java.net.URI;
2223
import java.net.URL;
2324
import java.util.List;
2425
import java.util.Map;
@@ -41,32 +42,28 @@
4142
public class DockerLookup extends AbstractLookup {
4243

4344
private static final Logger LOGGER = StatusLogger.getLogger();
44-
private static final String DOCKER_URI = "DOCKER_URI";
4545
private static final String HTTP = "http";
4646
private final Container container;
4747

4848
/**
4949
* Constructs a new instance.
5050
*/
5151
public DockerLookup() {
52-
String baseUri = System.getenv(DOCKER_URI);
53-
if (baseUri == null) {
54-
final PropertyEnvironment props = PropertyEnvironment.getGlobal();
55-
baseUri = props.getStringProperty(DOCKER_URI);
56-
}
52+
final URI baseUri = PropertyEnvironment.getGlobal()
53+
.getProperty(DockerProperties.class)
54+
.uri();
5755
if (baseUri == null) {
5856
LOGGER.warn("No Docker URI provided. Docker information is unavailable");
5957
container = null;
6058
return;
6159
}
6260
Container current = null;
6361
try {
64-
final URL url = new URL(baseUri + "/containers/json");
62+
final URL url = baseUri.resolve("/containers/json").toURL();
6563
if (url.getProtocol().equals(HTTP)) {
6664
final String macAddr = NetUtils.getMacAddressString();
6765
final ObjectMapper objectMapper = new ObjectMapper();
68-
final List<Container> containerList =
69-
objectMapper.readValue(url, new TypeReference<List<Container>>() {});
66+
final List<Container> containerList = objectMapper.readValue(url, new TypeReference<>() {});
7067

7168
for (final Container container : containerList) {
7269
if (macAddr != null && container.getNetworkSettings() != null) {

‎log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/condition/ConditionalOnProperty.java ‎log4j-docker/src/main/java/org/apache/logging/log4j/docker/DockerProperties.java

+8-18
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,15 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.plugins.condition;
17+
package org.apache.logging.log4j.docker;
1818

19-
import java.lang.annotation.Documented;
20-
import java.lang.annotation.ElementType;
21-
import java.lang.annotation.Retention;
22-
import java.lang.annotation.RetentionPolicy;
23-
import java.lang.annotation.Target;
19+
import java.net.URI;
20+
import org.apache.logging.log4j.kit.env.Log4jProperty;
21+
import org.jspecify.annotations.Nullable;
2422

2523
/**
26-
* Checks if a Log4j property is present or matches a specific non-empty value.
24+
* Properties for the Docker lookup.
25+
* @param uri
2726
*/
28-
@Retention(RetentionPolicy.RUNTIME)
29-
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE, ElementType.METHOD})
30-
@Documented
31-
@Conditional(OnPropertyCondition.class)
32-
public @interface ConditionalOnProperty {
33-
String name();
34-
35-
String value() default "";
36-
37-
boolean matchIfMissing() default false;
38-
}
27+
@Log4jProperty(name = "docker")
28+
public record DockerProperties(@Nullable URI uri) {}

‎log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeEmbeddedManager.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.apache.logging.log4j.util.Strings.toRootUpperCase;
2020

2121
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22+
import java.io.File;
2223
import java.security.MessageDigest;
2324
import java.security.NoSuchAlgorithmException;
2425
import java.util.HashMap;
@@ -31,13 +32,11 @@
3132
import org.apache.logging.log4j.core.appender.ManagerFactory;
3233
import org.apache.logging.log4j.core.config.ConfigurationException;
3334
import org.apache.logging.log4j.core.config.Property;
34-
import org.apache.logging.log4j.kit.env.PropertyEnvironment;
3535
import org.apache.logging.log4j.util.Strings;
3636

3737
public class FlumeEmbeddedManager extends AbstractFlumeManager {
3838

39-
private static final String FILE_SEP = PropertyEnvironment.getGlobal().getStringProperty("file.separator");
40-
39+
private static final String FILE_SEP = File.separator;
4140
private static final String IN_MEMORY = "InMemory";
4241

4342
private static final FlumeManagerFactory FACTORY = new FlumeManagerFactory();

‎log4j-jndi/src/main/java/org/apache/logging/log4j/jndi/lookup/JndiLookup.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,13 @@
2626
import org.apache.logging.log4j.core.lookup.Lookup;
2727
import org.apache.logging.log4j.jndi.JndiManager;
2828
import org.apache.logging.log4j.plugins.Plugin;
29-
import org.apache.logging.log4j.plugins.validation.constraints.RequiredProperty;
3029
import org.apache.logging.log4j.status.StatusLogger;
3130

3231
/**
3332
* Looks up keys from JNDI resources.
3433
*/
3534
@Lookup
3635
@Plugin("jndi")
37-
@RequiredProperty(
38-
name = "jndi.enableLookup",
39-
value = "true",
40-
message = "JNDI must be enabled by setting jndi.enableLookup=true")
4136
public class JndiLookup extends AbstractLookup {
4237

4338
private static final Logger LOGGER = StatusLogger.getLogger();
@@ -46,13 +41,13 @@ public class JndiLookup extends AbstractLookup {
4641
/** JNDI resource path prefix used in a J2EE container */
4742
static final String CONTAINER_JNDI_RESOURCE_PATH_PREFIX = "java:comp/env/";
4843

44+
private final boolean disabled;
45+
4946
/**
50-
* Constructs a new instance or throw IllegalStateException if this feature is disabled.
47+
* Constructs a new instance.
5148
*/
5249
public JndiLookup() {
53-
if (!JndiManager.isJndiLookupEnabled()) {
54-
throw new IllegalStateException("JNDI must be enabled by setting log4j2.enableJndiLookup=true");
55-
}
50+
this.disabled = !JndiManager.isJndiLookupEnabled();
5651
}
5752

5853
/**
@@ -64,7 +59,7 @@ public JndiLookup() {
6459
*/
6560
@Override
6661
public String lookup(final LogEvent event, final String key) {
67-
if (key == null) {
62+
if (disabled || key == null) {
6863
return null;
6964
}
7065
final String jndiName = convertJndiName(key);

‎log4j-kit/src/main/java/org/apache/logging/log4j/kit/env/PropertyEnvironment.java

+1-135
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
*/
1717
package org.apache.logging.log4j.kit.env;
1818

19-
import java.nio.charset.Charset;
20-
import java.time.Duration;
2119
import org.apache.logging.log4j.kit.env.internal.PropertiesUtilPropertyEnvironment;
2220
import org.jspecify.annotations.Nullable;
2321

@@ -34,146 +32,14 @@ static PropertyEnvironment getGlobal() {
3432
return PropertiesUtilPropertyEnvironment.INSTANCE;
3533
}
3634

37-
/**
38-
* Gets the named property as a boolean value. If the property matches the string {@code "true"} (case-insensitive),
39-
* then it is returned as the boolean value {@code true}. Any other non-{@code null} text in the property is
40-
* considered {@code false}.
41-
*
42-
* @param name the name of the property to look up
43-
* @return the boolean value of the property or {@code false} if undefined.
44-
*/
45-
default boolean getBooleanProperty(final String name) {
46-
return getBooleanProperty(name, false);
47-
}
48-
49-
/**
50-
* Gets the named property as a boolean value.
51-
*
52-
* @param name the name of the property to look up
53-
* @param defaultValue the default value to use if the property is undefined
54-
* @return the boolean value of the property or {@code defaultValue} if undefined.
55-
*/
56-
Boolean getBooleanProperty(String name, Boolean defaultValue);
57-
58-
/**
59-
* Gets the named property as a Charset value.
60-
*
61-
* @param name the name of the property to look up
62-
* @return the Charset value of the property or {@link Charset#defaultCharset()} if undefined.
63-
*/
64-
@SuppressWarnings("null")
65-
default Charset getCharsetProperty(final String name) {
66-
return getCharsetProperty(name, Charset.defaultCharset());
67-
}
68-
69-
/**
70-
* Gets the named property as a Charset value.
71-
*
72-
* @param name the name of the property to look up
73-
* @param defaultValue the default value to use if the property is undefined
74-
* @return the Charset value of the property or {@code defaultValue} if undefined.
75-
*/
76-
Charset getCharsetProperty(String name, Charset defaultValue);
77-
78-
/**
79-
* Gets the named property as a Class value.
80-
*
81-
* @param name the name of the property to look up
82-
* @param upperBound the upper bound for the class
83-
* @return the Class value of the property or {@code null} if it can not be loaded.
84-
*/
85-
<T> @Nullable Class<? extends T> getClassProperty(final String name, final Class<T> upperBound);
86-
87-
/**
88-
* Gets the named property as a subclass of {@code upperBound}.
89-
*
90-
* @param name the name of the property to look up
91-
* @param defaultValue the default value to use if the property is undefined
92-
* @param upperBound the upper bound for the class
93-
* @return the Class value of the property or {@code defaultValue} if it can not be loaded.
94-
*/
95-
<T> Class<? extends T> getClassProperty(String name, Class<? extends T> defaultValue, Class<T> upperBound);
96-
97-
/**
98-
* Gets the named property as {@link Duration}.
99-
*
100-
* @param name The property name.
101-
* @return The value of the String as a Duration or {@link Duration#ZERO} if it was undefined or could not be parsed.
102-
*/
103-
default Duration getDurationProperty(final String name) {
104-
return getDurationProperty(name, Duration.ZERO);
105-
}
106-
107-
/**
108-
* Gets the named property as {@link Duration}.
109-
*
110-
* @param name The property name.
111-
* @param defaultValue The default value.
112-
* @return The value of the String as a Duration or {@code defaultValue} if it was undefined or could not be parsed.
113-
*/
114-
Duration getDurationProperty(String name, Duration defaultValue);
115-
116-
/**
117-
* Gets the named property as an integer.
118-
*
119-
* @param name the name of the property to look up
120-
* @return the parsed integer value of the property or {@code 0} if it was undefined or could not be
121-
* parsed.
122-
*/
123-
default int getIntegerProperty(final String name) {
124-
return getIntegerProperty(name, 0);
125-
}
126-
127-
/**
128-
* Gets the named property as an integer.
129-
*
130-
* @param name the name of the property to look up
131-
* @param defaultValue the default value to use if the property is undefined
132-
* @return the parsed integer value of the property or {@code defaultValue} if it was undefined or could not be
133-
* parsed.
134-
*/
135-
Integer getIntegerProperty(String name, Integer defaultValue);
136-
137-
/**
138-
* Gets the named property as a long.
139-
*
140-
* @param name the name of the property to look up
141-
* @return the parsed long value of the property or {@code 0} if it was undefined or could not be
142-
* parsed.
143-
*/
144-
default long getLongProperty(final String name) {
145-
return getLongProperty(name, 0L);
146-
}
147-
148-
/**
149-
* Gets the named property as a long.
150-
*
151-
* @param name the name of the property to look up
152-
* @param defaultValue the default value to use if the property is undefined
153-
* @return the parsed long value of the property or {@code defaultValue} if it was undefined or could not be parsed.
154-
*/
155-
Long getLongProperty(String name, Long defaultValue);
156-
15735
/**
15836
* Gets the named property as a String.
15937
*
16038
* @param name the name of the property to look up
16139
* @return the String value of the property or {@code null} if undefined.
16240
*/
16341
@Nullable
164-
String getStringProperty(String name);
165-
166-
/**
167-
* Gets the named property as a String.
168-
*
169-
* @param name the name of the property to look up
170-
* @param defaultValue the default value to use if the property is undefined
171-
* @return the String value of the property or {@code defaultValue} if undefined.
172-
*/
173-
default String getStringProperty(final String name, final String defaultValue) {
174-
final String prop = getStringProperty(name);
175-
return (prop == null) ? defaultValue : prop;
176-
}
42+
String getProperty(String name);
17743

17844
/**
17945
* Binds properties to class {@code T}.

‎log4j-kit/src/main/java/org/apache/logging/log4j/kit/env/internal/PropertiesUtilPropertyEnvironment.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private static PropertyMapping getPropertyMapping(final Logger statusLogger) {
6363
}
6464

6565
@Override
66-
public @Nullable String getStringProperty(final String name) {
66+
public @Nullable String getProperty(final String name) {
6767
String value = propsUtil.getStringProperty(PREFIX + name);
6868
if (value == null) {
6969
final List<? extends String> legacyKeys = propertyMapping.getLegacyKeys(name);

‎log4j-kit/src/main/java/org/apache/logging/log4j/kit/env/support/BasicPropertyEnvironment.java

+16-50
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.lang.reflect.Type;
2424
import java.lang.reflect.TypeVariable;
2525
import java.lang.reflect.WildcardType;
26+
import java.net.URI;
27+
import java.net.URISyntaxException;
2628
import java.nio.charset.Charset;
2729
import java.nio.charset.IllegalCharsetNameException;
2830
import java.nio.file.InvalidPathException;
@@ -59,43 +61,7 @@ protected BasicPropertyEnvironment(final Logger statusLogger) {
5961
}
6062

6163
@Override
62-
public Boolean getBooleanProperty(final String name, final Boolean defaultValue) {
63-
return getObjectPropertyWithTypedDefault(name, this::toBoolean, defaultValue);
64-
}
65-
66-
@Override
67-
public Charset getCharsetProperty(final String name, final Charset defaultValue) {
68-
return getObjectPropertyWithTypedDefault(name, this::toCharset, defaultValue);
69-
}
70-
71-
@Override
72-
public <T> @Nullable Class<? extends T> getClassProperty(final String name, final Class<T> upperBound) {
73-
return getClassProperty(name, null, upperBound);
74-
}
75-
76-
@Override
77-
public <T> Class<? extends T> getClassProperty(
78-
final String name, final Class<? extends T> defaultValue, final Class<T> upperBound) {
79-
return getObjectPropertyWithTypedDefault(name, className -> toClass(className, upperBound), defaultValue);
80-
}
81-
82-
@Override
83-
public Duration getDurationProperty(final String name, final Duration defaultValue) {
84-
return getObjectPropertyWithTypedDefault(name, this::toDuration, defaultValue);
85-
}
86-
87-
@Override
88-
public Integer getIntegerProperty(final String name, final Integer defaultValue) {
89-
return getObjectPropertyWithTypedDefault(name, this::toInteger, defaultValue);
90-
}
91-
92-
@Override
93-
public Long getLongProperty(final String name, final Long defaultValue) {
94-
return getObjectPropertyWithTypedDefault(name, this::toLong, defaultValue);
95-
}
96-
97-
@Override
98-
public abstract @Nullable String getStringProperty(String name);
64+
public abstract @Nullable String getProperty(final String name);
9965

10066
@Override
10167
public <T> T getProperty(final Class<T> propertyClass) {
@@ -209,6 +175,15 @@ protected char[] toCharArray(final String value) {
209175
return zoneId != null ? TimeZone.getTimeZone(zoneId) : null;
210176
}
211177

178+
protected @Nullable URI toURI(final String value) {
179+
try {
180+
return new URI(value);
181+
} catch (final URISyntaxException e) {
182+
statusLogger.warn("Invalid URI value {}.", value, e);
183+
}
184+
return null;
185+
}
186+
212187
protected @Nullable ZoneId toZoneId(final String value) {
213188
try {
214189
return Log4jProperty.SYSTEM.equals(value) ? ZoneId.systemDefault() : ZoneId.of(value);
@@ -308,6 +283,9 @@ private <T> T getRecordProperty(final @Nullable String parentPrefix, final Class
308283
if (TimeZone.class.equals(clazz)) {
309284
return getObjectPropertyWithStringDefault(name, defaultValue, this::toTimeZone);
310285
}
286+
if (URI.class.equals(clazz)) {
287+
return getObjectPropertyWithStringDefault(name, defaultValue, this::toURI);
288+
}
311289
if (ZoneId.class.equals(clazz)) {
312290
return getObjectPropertyWithStringDefault(name, defaultValue, this::toZoneId);
313291
}
@@ -354,7 +332,7 @@ private String getPropertyName(final AnnotatedElement element, final Supplier<St
354332

355333
private <T> @Nullable Object getObjectPropertyWithStringDefault(
356334
final String name, final @Nullable String defaultValue, final Function<? super String, ?> converter) {
357-
final String prop = getStringProperty(name);
335+
final String prop = getProperty(name);
358336
if (prop != null) {
359337
final @Nullable Object value = converter.apply(prop);
360338
if (value != null) {
@@ -363,16 +341,4 @@ private String getPropertyName(final AnnotatedElement element, final Supplier<St
363341
}
364342
return defaultValue != null ? converter.apply(defaultValue) : null;
365343
}
366-
367-
private <T> T getObjectPropertyWithTypedDefault(
368-
final String name, final Function<? super String, ? extends @Nullable T> converter, final T defaultValue) {
369-
final String prop = getStringProperty(name);
370-
if (prop != null) {
371-
final @Nullable T value = converter.apply(prop);
372-
if (value != null) {
373-
return value;
374-
}
375-
}
376-
return defaultValue;
377-
}
378344
}

‎log4j-kit/src/main/java/org/apache/logging/log4j/kit/env/support/CompositePropertyEnvironment.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public CompositePropertyEnvironment(
4646
}
4747

4848
@Override
49-
public @Nullable String getStringProperty(final String name) {
49+
public @Nullable String getProperty(final String name) {
5050
return sources.stream()
5151
.map(source -> source.getProperty(name))
5252
.filter(Objects::nonNull)
@@ -63,7 +63,7 @@ public int getPriority() {
6363

6464
@Override
6565
public @Nullable String getProperty(final String name) {
66-
return parentEnvironment.getStringProperty(name);
66+
return parentEnvironment.getProperty(name);
6767
}
6868
}
6969
}

‎log4j-kit/src/test/java/org/apache/logging/log4j/kit/env/TestPropertyEnvironment.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public TestPropertyEnvironment(final Map<String, String> props, final Logger log
3535
}
3636

3737
@Override
38-
public String getStringProperty(final String name) {
38+
public String getProperty(final String name) {
3939
return props.get(name);
4040
}
4141
}

‎log4j-kit/src/test/java/org/apache/logging/log4j/kit/env/internal/AbstractPropertyNamesTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected Stream<String> testPropertyNames() {
3636

3737
protected void assertPropertiesAreSet(final String expected, final PropertyEnvironment environment) {
3838
Assertions.assertThat(testPropertyNames())
39-
.extracting(environment::getStringProperty)
39+
.extracting(environment::getProperty)
4040
.isSubsetOf(expected);
4141
}
4242
}

‎log4j-kit/src/test/java/org/apache/logging/log4j/kit/env/support/BasicPropertyEnvironmentTest.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static java.nio.charset.StandardCharsets.UTF_8;
2020
import static org.assertj.core.api.Assertions.assertThat;
2121

22+
import java.net.URI;
2223
import java.nio.charset.Charset;
2324
import java.nio.file.Path;
2425
import java.nio.file.Paths;
@@ -88,6 +89,7 @@ record ScalarValues(
8889
@Nullable Path pathAttr,
8990
@Nullable Locale localeAttr,
9091
@Nullable TimeZone timeZoneAttr,
92+
@Nullable URI uriAttr,
9193
@Nullable ZoneId zoneIdAttr) {}
9294

9395
@Log4jProperty
@@ -100,6 +102,7 @@ record DefaultScalarValues(
100102
@Log4jProperty(defaultValue = "app.log") Path pathAttr,
101103
@Log4jProperty(defaultValue = "en-US") Locale localeAttr,
102104
@Log4jProperty(defaultValue = "Europe/Warsaw") TimeZone timeZoneAttr,
105+
@Log4jProperty(defaultValue = "https://localhost") URI uriAttr,
103106
@Log4jProperty(defaultValue = "UTC+01:00") ZoneId zoneIdAttr) {}
104107

105108
private static final Map<String, String> SCALAR_PROPS = Map.of(
@@ -119,12 +122,14 @@ record DefaultScalarValues(
119122
"en-US",
120123
"ScalarValues.timeZoneAttr",
121124
"Europe/Warsaw",
125+
"ScalarValues.uriAttr",
126+
"https://localhost",
122127
"ScalarValues.zoneIdAttr",
123128
"UTC+01:00");
124129

125130
@Test
126131
void should_support_scalar_values() {
127-
assertMapConvertsTo(Map.of(), new ScalarValues(null, null, null, null, null, null, null, null, null));
132+
assertMapConvertsTo(Map.of(), new ScalarValues(null, null, null, null, null, null, null, null, null, null));
128133
assertMapConvertsTo(
129134
SCALAR_PROPS,
130135
new ScalarValues(
@@ -136,6 +141,7 @@ void should_support_scalar_values() {
136141
Paths.get("app.log"),
137142
Locale.forLanguageTag("en-US"),
138143
TimeZone.getTimeZone("Europe/Warsaw"),
144+
URI.create("https://localhost"),
139145
ZoneId.of("UTC+01:00")));
140146
// Default values
141147
assertMapConvertsTo(
@@ -149,6 +155,7 @@ void should_support_scalar_values() {
149155
Paths.get("app.log"),
150156
Locale.forLanguageTag("en-US"),
151157
TimeZone.getTimeZone("Europe/Warsaw"),
158+
URI.create("https://localhost"),
152159
ZoneId.of("UTC+01:00")));
153160
}
154161

‎log4j-plugins-test/src/test/java/org/apache/logging/log4j/plugins/condition/OnPropertyConditionTest.java

-108
This file was deleted.

‎log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/condition/OnPropertyCondition.java

-66
This file was deleted.

‎log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/validation/constraints/RequiredProperty.java

-48
This file was deleted.

‎log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/validation/validators/RequiredPropertyValidator.java

-58
This file was deleted.

‎src/site/antora/modules/ROOT/pages/manual/plugins.adoc

-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ can be created as well.
155155
and empty `Map` instances.
156156
`RequiredClass`::
157157
This annotation validates that a class name can be loaded. This is useful for plugins that should only be loaded when an optional class is present.
158-
`RequiredProperty`::
159-
This annotation validates that a system property is set to some value.
160158
`ValidHost`::
161159
This annotation validates that a value corresponds to a valid
162160
hostname. This uses the same validation as

0 commit comments

Comments
 (0)
Please sign in to comment.