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 @SuppressWarnings("unchecked")
87 public Class<? extends LoggerContextFactory> loadLoggerContextFactory() {
88 if (className == null) {
89 return null;
90 }
91 try {
92 final Class<?> clazz = classLoader.loadClass(className);
93 if (LoggerContextFactory.class.isAssignableFrom(clazz)) {
94 return (Class<? extends LoggerContextFactory>) clazz;
95 }
96 } catch (final Exception e) {
97 LOGGER.error("Unable to create class {} specified in {}", className, url.toString(), e);
98 }
99 return null;
100 }
101
102
103
104
105
106
107
108 public String getThreadContextMap() {
109 return threadContextMap;
110 }
111
112
113
114
115
116
117 @SuppressWarnings("unchecked")
118 public Class<? extends ThreadContextMap> loadThreadContextMap() {
119 if (threadContextMap == null) {
120 return null;
121 }
122 try {
123 final Class<?> clazz = classLoader.loadClass(threadContextMap);
124 if (ThreadContextMap.class.isAssignableFrom(clazz)) {
125 return (Class<? extends ThreadContextMap>) clazz;
126 }
127 } catch (final Exception e) {
128 LOGGER.error("Unable to create class {} specified in {}", threadContextMap, url.toString(), e);
129 }
130 return null;
131 }
132
133
134
135
136
137
138 public URL getUrl() {
139 return url;
140 }
141 }