View Javadoc
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      /**
32       * Property name to set for a Log4j 2 provider to specify the priority of this implementation.
33       */
34      public static final String FACTORY_PRIORITY = "FactoryPriority";
35      /**
36       * Property name to set to the implementation of {@link org.apache.logging.log4j.spi.ThreadContextMap}.
37       */
38      public static final String THREAD_CONTEXT_MAP = "ThreadContextMap";
39      /**
40       * Property name to set to the implementation of {@link org.apache.logging.log4j.spi.LoggerContextFactory}.
41       */
42      public static final String LOGGER_CONTEXT_FACTORY = "LoggerContextFactory";
43  
44      private static final Integer DEFAULT_PRIORITY = Integer.valueOf(-1);
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 Provider.
103      *
104      * @return the class name of a ThreadContextMap implementation
105      */
106     public String getThreadContextMap() {
107         return threadContextMap;
108     }
109 
110     /**
111      * Loads the {@link org.apache.logging.log4j.spi.ThreadContextMap} class specified by this Provider.
112      *
113      * @return the ThreadContextMap implementation class or {@code null} if there was an error loading it
114      */
115     public Class<? extends ThreadContextMap> loadThreadContextMap() {
116         if (threadContextMap == null) {
117             return null;
118         }
119         try {
120             final Class<?> clazz = classLoader.loadClass(threadContextMap);
121             if (ThreadContextMap.class.isAssignableFrom(clazz)) {
122                 return clazz.asSubclass(ThreadContextMap.class);
123             }
124         } catch (final Exception e) {
125             LOGGER.error("Unable to create class {} specified in {}", threadContextMap, url.toString(), e);
126         }
127         return null;
128     }
129 
130     /**
131      * Gets the URL containing this Provider's Log4j details.
132      *
133      * @return the URL corresponding to the Provider {@code META-INF/log4j-provider.properties} file
134      */
135     public URL getUrl() {
136         return url;
137     }
138 }