1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
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 * Model class for a Log4j 2 provider. The properties in this class correspond to the properties used in a
27 * {@code META-INF/log4j-provider.properties} file. Note that this class is automatically created by Log4j and should
28 * not be used by providers.
29 */
30 public class Provider {
31 private static final Integer DEFAULT_PRIORITY = Integer.valueOf(-1);
32 /**
33 * Property name to set for a Log4j 2 provider to specify the priority of this implementation.
34 */
35 public static final String FACTORY_PRIORITY = "FactoryPriority";
36 /**
37 * Property name to set to the implementation of {@link org.apache.logging.log4j.spi.ThreadContextMap}.
38 */
39 public static final String THREAD_CONTEXT_MAP = "ThreadContextMap";
40 /**
41 * Property name to set to the implementation of {@link org.apache.logging.log4j.spi.LoggerContextFactory}.
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 * Gets the priority (natural ordering) of this Provider.
64 *
65 * @return the priority of this Provider
66 */
67 public Integer getPriority() {
68 return priority;
69 }
70
71 /**
72 * Gets the class name of the {@link org.apache.logging.log4j.spi.LoggerContextFactory} implementation of this
73 * Provider.
74 *
75 * @return the class name of a LoggerContextFactory implementation
76 */
77 public String getClassName() {
78 return className;
79 }
80
81 /**
82 * Loads the {@link org.apache.logging.log4j.spi.LoggerContextFactory} class specified by this Provider.
83 *
84 * @return the LoggerContextFactory implementation class or {@code null} if there was an error loading it
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 * Gets the class name of the {@link org.apache.logging.log4j.spi.ThreadContextMap} implementation of this
103 * Provider.
104 *
105 * @return the class name of a ThreadContextMap implementation
106 */
107 public String getThreadContextMap() {
108 return threadContextMap;
109 }
110
111 /**
112 * Loads the {@link org.apache.logging.log4j.spi.ThreadContextMap} class specified by this Provider.
113 *
114 * @return the ThreadContextMap implementation class or {@code null} if there was an error loading it
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 * Gets the URL containing this Provider's Log4j details.
133 *
134 * @return the URL corresponding to the Provider {@code META-INF/log4j-provider.properties} file
135 */
136 public URL getUrl() {
137 return url;
138 }
139 }