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.net.URL;
20 import java.util.Properties;
21
22 import org.apache.logging.log4j.Logger;
23 import org.apache.logging.log4j.status.StatusLogger;
24
25
26
27
28
29
30 public class Provider {
31 private static final Integer DEFAULT_PRIORITY = Integer.valueOf(-1);
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 Logger LOGGER = StatusLogger.getLogger();
46
47 private final Integer priority;
48 private final String className;
49 private final String threadContextMap;
50 private final URL url;
51 private final ClassLoader classLoader;
52
53 public Provider(final Properties props, final URL url, final ClassLoader classLoader) {
54 this.url = url;
55 this.classLoader = classLoader;
56 final String weight = props.getProperty(FACTORY_PRIORITY);
57 priority = weight == null ? DEFAULT_PRIORITY : Integer.valueOf(weight);
58 className = props.getProperty(LOGGER_CONTEXT_FACTORY);
59 threadContextMap = props.getProperty(THREAD_CONTEXT_MAP);
60 }
61
62
63
64
65
66
67 public Integer getPriority() {
68 return priority;
69 }
70
71
72
73
74
75
76
77 public String getClassName() {
78 return className;
79 }
80
81
82
83
84
85
86 public Class<? extends LoggerContextFactory> loadLoggerContextFactory() {
87 if (className == null) {
88 return null;
89 }
90 try {
91 final Class<?> clazz = classLoader.loadClass(className);
92 if (LoggerContextFactory.class.isAssignableFrom(clazz)) {
93 return clazz.asSubclass(LoggerContextFactory.class);
94 }
95 } catch (final Exception e) {
96 LOGGER.error("Unable to create class {} specified in {}", className, url.toString(), e);
97 }
98 return null;
99 }
100
101
102
103
104
105
106
107 public String getThreadContextMap() {
108 return threadContextMap;
109 }
110
111
112
113
114
115
116 public Class<? extends ThreadContextMap> loadThreadContextMap() {
117 if (threadContextMap == null) {
118 return null;
119 }
120 try {
121 final Class<?> clazz = classLoader.loadClass(threadContextMap);
122 if (ThreadContextMap.class.isAssignableFrom(clazz)) {
123 return clazz.asSubclass(ThreadContextMap.class);
124 }
125 } catch (final Exception e) {
126 LOGGER.error("Unable to create class {} specified in {}", threadContextMap, url.toString(), e);
127 }
128 return null;
129 }
130
131
132
133
134
135
136 public URL getUrl() {
137 return url;
138 }
139 }