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      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      @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      * Gets the class name of the {@link org.apache.logging.log4j.spi.ThreadContextMap} implementation of this
104      * Provider.
105      *
106      * @return the class name of a ThreadContextMap implementation
107      */
108     public String getThreadContextMap() {
109         return threadContextMap;
110     }
111 
112     /**
113      * Loads the {@link org.apache.logging.log4j.spi.ThreadContextMap} class specified by this Provider.
114      *
115      * @return the ThreadContextMap implementation class or {@code null} if there was an error loading it
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      * Gets the URL containing this Provider's Log4j details.
135      *
136      * @return the URL corresponding to the Provider {@code META-INF/log4j-provider.properties} file
137      */
138     public URL getUrl() {
139         return url;
140     }
141 }