1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.spi;
18
19 import java.lang.ref.WeakReference;
20 import java.net.URL;
21 import java.util.Properties;
22
23 import org.apache.logging.log4j.Logger;
24 import org.apache.logging.log4j.status.StatusLogger;
25
26
27
28
29
30
31 public class Provider {
32
33
34
35 public static final String FACTORY_PRIORITY = "FactoryPriority";
36
37
38
39 public static final String THREAD_CONTEXT_MAP = "ThreadContextMap";
40
41
42
43 public static final String LOGGER_CONTEXT_FACTORY = "LoggerContextFactory";
44
45 private static final Integer DEFAULT_PRIORITY = Integer.valueOf(-1);
46 private static final Logger LOGGER = StatusLogger.getLogger();
47
48 private final Integer priority;
49 private final String className;
50 private final String threadContextMap;
51 private final URL url;
52 private final WeakReference<ClassLoader> classLoader;
53
54 public Provider(final Properties props, final URL url, final ClassLoader classLoader) {
55 this.url = url;
56 this.classLoader = new WeakReference<>(classLoader);
57 final String weight = props.getProperty(FACTORY_PRIORITY);
58 priority = weight == null ? DEFAULT_PRIORITY : Integer.valueOf(weight);
59 className = props.getProperty(LOGGER_CONTEXT_FACTORY);
60 threadContextMap = props.getProperty(THREAD_CONTEXT_MAP);
61 }
62
63
64
65
66
67
68 public Integer getPriority() {
69 return priority;
70 }
71
72
73
74
75
76
77
78 public String getClassName() {
79 return className;
80 }
81
82
83
84
85
86
87 public Class<? extends LoggerContextFactory> loadLoggerContextFactory() {
88 if (className == null) {
89 return null;
90 }
91 final ClassLoader loader = classLoader.get();
92 if (loader == null) {
93 return null;
94 }
95 try {
96 final Class<?> clazz = loader.loadClass(className);
97 if (LoggerContextFactory.class.isAssignableFrom(clazz)) {
98 return clazz.asSubclass(LoggerContextFactory.class);
99 }
100 } catch (final Exception e) {
101 LOGGER.error("Unable to create class {} specified in {}", className, url.toString(), e);
102 }
103 return null;
104 }
105
106
107
108
109
110
111 public String getThreadContextMap() {
112 return threadContextMap;
113 }
114
115
116
117
118
119
120 public Class<? extends ThreadContextMap> loadThreadContextMap() {
121 if (threadContextMap == null) {
122 return null;
123 }
124 final ClassLoader loader = classLoader.get();
125 if (loader == null) {
126 return null;
127 }
128 try {
129 final Class<?> clazz = loader.loadClass(threadContextMap);
130 if (ThreadContextMap.class.isAssignableFrom(clazz)) {
131 return clazz.asSubclass(ThreadContextMap.class);
132 }
133 } catch (final Exception e) {
134 LOGGER.error("Unable to create class {} specified in {}", threadContextMap, url.toString(), e);
135 }
136 return null;
137 }
138
139
140
141
142
143
144 public URL getUrl() {
145 return url;
146 }
147
148 @Override
149 public String toString() {
150 String result = "Provider[";
151 if (priority != DEFAULT_PRIORITY) {
152 result += "priority=" + priority + ", ";
153 }
154 if (threadContextMap != null) {
155 result += "threadContextMap=" + threadContextMap + ", ";
156 }
157 if (className != null) {
158 result += "className=" + className + ", ";
159 }
160 result += "url=" + url;
161 final ClassLoader loader = classLoader.get();
162 if (loader == null) {
163 result += ", classLoader=null(not reachable)";
164 } else {
165 result += ", classLoader=" + loader;
166 }
167 result += "]";
168 return result;
169 }
170 }