1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.impl;
18
19 import java.net.URI;
20
21 import org.apache.logging.log4j.core.LoggerContext;
22 import org.apache.logging.log4j.core.config.Configuration;
23 import org.apache.logging.log4j.core.config.ConfigurationFactory;
24 import org.apache.logging.log4j.core.helpers.Constants;
25 import org.apache.logging.log4j.core.helpers.Loader;
26 import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector;
27 import org.apache.logging.log4j.core.selector.ContextSelector;
28 import org.apache.logging.log4j.spi.LoggerContextFactory;
29 import org.apache.logging.log4j.status.StatusLogger;
30 import org.apache.logging.log4j.util.PropertiesUtil;
31
32
33
34
35 public class Log4jContextFactory implements LoggerContextFactory {
36
37 private static final StatusLogger LOGGER = StatusLogger.getLogger();
38
39 private ContextSelector selector;
40
41
42
43
44 public Log4jContextFactory() {
45 final String sel = PropertiesUtil.getProperties().getStringProperty(Constants.LOG4J_CONTEXT_SELECTOR);
46 if (sel != null) {
47 try {
48 final Class<?> clazz = Loader.loadClass(sel);
49 if (clazz != null && ContextSelector.class.isAssignableFrom(clazz)) {
50 selector = (ContextSelector) clazz.newInstance();
51 }
52 } catch (final Exception ex) {
53 LOGGER.error("Unable to create context " + sel, ex);
54 }
55 }
56 if (selector == null) {
57 selector = new ClassLoaderContextSelector();
58 }
59 }
60
61
62
63
64
65 public ContextSelector getSelector() {
66 return selector;
67 }
68
69
70
71
72
73
74
75
76
77
78 @Override
79 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
80 final boolean currentContext) {
81 final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext);
82 ctx.setExternalContext(externalContext);
83 if (ctx.getStatus() == LoggerContext.Status.INITIALIZED) {
84 ctx.start();
85 }
86 return ctx;
87 }
88
89
90
91
92
93
94
95
96
97
98
99 @Override
100 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
101 final boolean currentContext, final URI configLocation, final String name) {
102 final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext, configLocation);
103 if (externalContext != null && ctx.getExternalContext() == null) {
104 ctx.setExternalContext(externalContext);
105 }
106 if (ctx.getStatus() == LoggerContext.Status.INITIALIZED) {
107 if (configLocation != null || name != null) {
108 ContextAnchor.THREAD_CONTEXT.set(ctx);
109 final Configuration config = ConfigurationFactory.getInstance().getConfiguration(name, configLocation);
110 ctx.start(config);
111 ContextAnchor.THREAD_CONTEXT.remove();
112 } else {
113 ctx.start();
114 }
115 }
116 return ctx;
117 }
118
119
120
121
122
123
124
125
126
127
128
129 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
130 final boolean currentContext, final ConfigurationFactory.ConfigurationSource source) {
131 final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext, null);
132 if (externalContext != null && ctx.getExternalContext() == null) {
133 ctx.setExternalContext(externalContext);
134 }
135 if (ctx.getStatus() == LoggerContext.Status.INITIALIZED) {
136 if (source != null) {
137 ContextAnchor.THREAD_CONTEXT.set(ctx);
138 final Configuration config = ConfigurationFactory.getInstance().getConfiguration(source);
139 ctx.start(config);
140 ContextAnchor.THREAD_CONTEXT.remove();
141 } else {
142 ctx.start();
143 }
144 }
145 return ctx;
146 }
147
148
149
150
151
152
153
154
155 @Override
156 public void removeContext(final org.apache.logging.log4j.spi.LoggerContext context) {
157 if (context instanceof LoggerContext) {
158 selector.removeContext((LoggerContext) context);
159 }
160 }
161 }