Skip to content

Commit d99a5a7

Browse files
committed
Use Lazy in some places from 3.0
1 parent 4243f7c commit d99a5a7

File tree

4 files changed

+17
-27
lines changed

4 files changed

+17
-27
lines changed

log4j-api/src/main/java/org/apache/logging/log4j/message/ThreadDumpMessage.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import aQute.bnd.annotation.Resolution;
2828
import aQute.bnd.annotation.spi.ServiceConsumer;
2929
import org.apache.logging.log4j.message.ThreadDumpMessage.ThreadInfoFactory;
30+
import org.apache.logging.log4j.util.Lazy;
3031
import org.apache.logging.log4j.util.ServiceLoaderUtil;
3132
import org.apache.logging.log4j.util.StringBuilderFormattable;
3233
import org.apache.logging.log4j.util.Strings;
@@ -40,7 +41,7 @@
4041
@ServiceConsumer(value = ThreadInfoFactory.class, resolution = Resolution.OPTIONAL, cardinality = Cardinality.SINGLE)
4142
public class ThreadDumpMessage implements Message, StringBuilderFormattable {
4243
private static final long serialVersionUID = -1103400781608841088L;
43-
private static ThreadInfoFactory FACTORY;
44+
private static final Lazy<ThreadInfoFactory> FACTORY = Lazy.lazy(ThreadDumpMessage::initFactory);
4445

4546
private volatile Map<ThreadInformation, StackTraceElement[]> threads;
4647
private final String title;
@@ -52,21 +53,14 @@ public class ThreadDumpMessage implements Message, StringBuilderFormattable {
5253
*/
5354
public ThreadDumpMessage(final String title) {
5455
this.title = title == null ? Strings.EMPTY : title;
55-
threads = getFactory().createThreadInfo();
56+
threads = FACTORY.get().createThreadInfo();
5657
}
5758

5859
private ThreadDumpMessage(final String formattedMsg, final String title) {
5960
this.formattedMessage = formattedMsg;
6061
this.title = title == null ? Strings.EMPTY : title;
6162
}
6263

63-
private static ThreadInfoFactory getFactory() {
64-
if (FACTORY == null) {
65-
FACTORY = initFactory();
66-
}
67-
return FACTORY;
68-
}
69-
7064
private static ThreadInfoFactory initFactory() {
7165
return ServiceLoaderUtil.loadServices(ThreadInfoFactory.class, MethodHandles.lookup(), false)
7266
.findFirst()

log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public final class PropertiesUtil {
5757

5858
private static final String LOG4J_PROPERTIES_FILE_NAME = "log4j2.component.properties";
5959
private static final String LOG4J_SYSTEM_PROPERTIES_FILE_NAME = "log4j2.system.properties";
60-
private static final PropertiesUtil LOG4J_PROPERTIES = new PropertiesUtil(LOG4J_PROPERTIES_FILE_NAME, false);
60+
private static final Lazy<PropertiesUtil> COMPONENT_PROPERTIES =
61+
Lazy.lazy(() -> new PropertiesUtil(LOG4J_PROPERTIES_FILE_NAME, false));
6162

6263
private final Environment environment;
6364

@@ -123,7 +124,7 @@ static Properties loadClose(final InputStream in, final Object source) {
123124
* @return the main Log4j PropertiesUtil instance.
124125
*/
125126
public static PropertiesUtil getProperties() {
126-
return LOG4J_PROPERTIES;
127+
return COMPONENT_PROPERTIES.get();
127128
}
128129

129130
/**

log4j-core/src/main/java/org/apache/logging/log4j/core/selector/BasicContextSelector.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@
1717
package org.apache.logging.log4j.core.selector;
1818

1919
import java.net.URI;
20-
import java.util.ArrayList;
2120
import java.util.Collections;
2221
import java.util.List;
2322
import java.util.concurrent.TimeUnit;
2423

2524
import org.apache.logging.log4j.core.LoggerContext;
2625
import org.apache.logging.log4j.core.impl.ContextAnchor;
26+
import org.apache.logging.log4j.util.Lazy;
2727

2828
/**
2929
* Returns either this Thread's context or the default LoggerContext.
3030
*/
3131
public class BasicContextSelector implements ContextSelector {
3232

33-
private static final LoggerContext CONTEXT = new LoggerContext("Default");
33+
private final Lazy<LoggerContext> defaultLoggerContext = Lazy.lazy(() -> new LoggerContext("Default"));
3434

3535
@Override
3636
public void shutdown(final String fqcn, final ClassLoader loader, final boolean currentContext, final boolean allContexts) {
@@ -49,7 +49,7 @@ public boolean hasContext(final String fqcn, final ClassLoader loader, final boo
4949
@Override
5050
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext) {
5151
final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
52-
return ctx != null ? ctx : CONTEXT;
52+
return ctx != null ? ctx : defaultLoggerContext.get();
5353
}
5454

5555

@@ -58,11 +58,11 @@ public LoggerContext getContext(final String fqcn, final ClassLoader loader, fin
5858
final URI configLocation) {
5959

6060
final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
61-
return ctx != null ? ctx : CONTEXT;
61+
return ctx != null ? ctx : defaultLoggerContext.get();
6262
}
6363

6464
public LoggerContext locateContext(final String name, final String configLocation) {
65-
return CONTEXT;
65+
return defaultLoggerContext.get();
6666
}
6767

6868
@Override
@@ -77,9 +77,7 @@ public boolean isClassLoaderDependent() {
7777

7878
@Override
7979
public List<LoggerContext> getLoggerContexts() {
80-
final List<LoggerContext> list = new ArrayList<>();
81-
list.add(CONTEXT);
82-
return Collections.unmodifiableList(list);
80+
return Collections.singletonList(defaultLoggerContext.get());
8381
}
8482

8583
}

log4j-core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.logging.log4j.core.impl.ContextAnchor;
3333
import org.apache.logging.log4j.spi.LoggerContextShutdownAware;
3434
import org.apache.logging.log4j.status.StatusLogger;
35+
import org.apache.logging.log4j.util.Lazy;
3536
import org.apache.logging.log4j.util.StackLocatorUtil;
3637

3738
/**
@@ -47,13 +48,14 @@
4748
*/
4849
public class ClassLoaderContextSelector implements ContextSelector, LoggerContextShutdownAware {
4950

50-
private static final AtomicReference<LoggerContext> DEFAULT_CONTEXT = new AtomicReference<>();
51-
5251
protected static final StatusLogger LOGGER = StatusLogger.getLogger();
5352

5453
protected static final ConcurrentMap<String, AtomicReference<WeakReference<LoggerContext>>> CONTEXT_MAP =
5554
new ConcurrentHashMap<>();
5655

56+
private final Lazy<LoggerContext> defaultLoggerContext =
57+
Lazy.lazy(() -> createContext(defaultContextName(), null));
58+
5759
@Override
5860
public void shutdown(final String fqcn, final ClassLoader loader, final boolean currentContext,
5961
final boolean allContexts) {
@@ -259,12 +261,7 @@ protected String toContextMapKey(final ClassLoader loader) {
259261
}
260262

261263
protected LoggerContext getDefault() {
262-
final LoggerContext ctx = DEFAULT_CONTEXT.get();
263-
if (ctx != null) {
264-
return ctx;
265-
}
266-
DEFAULT_CONTEXT.compareAndSet(null, createContext(defaultContextName(), null));
267-
return DEFAULT_CONTEXT.get();
264+
return defaultLoggerContext.get();
268265
}
269266

270267
protected String defaultContextName() {

0 commit comments

Comments
 (0)