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  
18  package org.apache.logging.log4j.core.config;
19  
20  import org.apache.logging.log4j.status.StatusLogger;
21  import org.apache.logging.log4j.util.PropertiesUtil;
22  
23  /**
24   * Factory for ReliabilityStrategies.
25   */
26  public class ReliabilityStrategyFactory {
27      private ReliabilityStrategyFactory() {
28      }
29  
30      /**
31       * Returns a new {@code ReliabilityStrategy} instance based on the value of system property
32       * {@code log4j.ReliabilityStrategy}. If not value was specified this method returns a new
33       * {@code AwaitUnconditionallyReliabilityStrategy}.
34       * <p>
35       * Valid values for this system property are {@code "AwaitUnconditionally"} (use
36       * {@code AwaitUnconditionallyReliabilityStrategy}), {@code "Locking"} (use {@code LockingReliabilityStrategy}) and
37       * {@code "AwaitCompletion"} (use the default {@code AwaitCompletionReliabilityStrategy}).
38       * <p>
39       * Users may also use this system property to specify the fully qualified class name of a class that implements the
40       * {@code ReliabilityStrategy} and has a constructor that accepts a single {@code LoggerConfig} argument.
41       * 
42       * @param loggerConfig the LoggerConfig the resulting {@code ReliabilityStrategy} is associated with
43       * @return a ReliabilityStrategy that helps the specified LoggerConfig to log events reliably during or after a
44       *         configuration change
45       */
46      public static ReliabilityStrategy getReliabilityStrategy(final LoggerConfig loggerConfig) {
47  
48          String strategy = PropertiesUtil.getProperties().getStringProperty("log4j.ReliabilityStrategy",
49                  "AwaitCompletion");
50          if ("AwaitCompletion".equals(strategy)) {
51              return new AwaitCompletionReliabilityStrategy(loggerConfig);
52          }
53          if ("AwaitUnconditionally".equals(strategy)) {
54              return new AwaitUnconditionallyReliabilityStrategy(loggerConfig);
55          }
56          if ("Locking".equals(strategy)) {
57              return new LockingReliabilityStrategy(loggerConfig);
58          }
59          try {
60              @SuppressWarnings("unchecked")
61              Class<? extends ReliabilityStrategy> cls = (Class<? extends ReliabilityStrategy>) Class.forName(strategy);
62              return cls.getConstructor(LoggerConfig.class).newInstance(loggerConfig);
63          } catch (Exception dynamicFailed) {
64              StatusLogger.getLogger().warn(
65                      "Could not create ReliabilityStrategy for '{}', using default AwaitCompletionReliabilityStrategy");
66              return new AwaitCompletionReliabilityStrategy(loggerConfig);
67          }
68      }
69  }