Skip to content

Commit d3a7b8b

Browse files
committed
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`.
1 parent 9d0b681 commit d3a7b8b

File tree

18 files changed

+56
-514
lines changed

18 files changed

+56
-514
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/NamedLoggerContextPropertiesTest.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
import org.apache.logging.log4j.core.config.Configuration;
2626
import org.apache.logging.log4j.core.selector.BasicContextSelector;
2727
import org.apache.logging.log4j.core.test.junit.ContextSelectorType;
28+
import org.apache.logging.log4j.kit.env.Log4jProperty;
2829
import org.apache.logging.log4j.kit.env.PropertyEnvironment;
2930
import org.apache.logging.log4j.plugins.Inject;
3031
import org.apache.logging.log4j.plugins.di.ConfigurableInstanceFactory;
32+
import org.jspecify.annotations.Nullable;
3133
import org.junit.jupiter.api.Test;
3234
import org.junitpioneer.jupiter.DisabledUntil;
3335

@@ -43,7 +45,7 @@ public void testProperties() {
4345
assertEquals(LifeCycle.State.STARTED, context.getState());
4446
final PropertyEnvironment props = context.getEnvironment();
4547
assertNotNull(props, "Logger Context Properties were not loaded");
46-
final String scriptLanguages = props.getStringProperty("Script.enableLanguages");
48+
final String scriptLanguages = props.getProperty(Script.class).enableLanguages();
4749
assertEquals("Groovy,JavaScript", scriptLanguages);
4850
final Configuration config = context.getConfiguration();
4951
assertNotNull(config, "Configuration was not created");
@@ -52,6 +54,9 @@ public void testProperties() {
5254
assertEquals(LifeCycle.State.STOPPED, context.getState());
5355
}
5456

57+
@Log4jProperty
58+
private record Script(@Nullable String enableLanguages) {}
59+
5560
public static class TestContextSelector extends BasicContextSelector {
5661

5762
@Inject

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
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();

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);

0 commit comments

Comments
 (0)