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.core.util;
18  
19  import org.apache.logging.log4j.core.time.PreciseClock;
20  import org.apache.logging.log4j.status.StatusLogger;
21  import org.apache.logging.log4j.util.PropertiesUtil;
22  import org.apache.logging.log4j.util.Supplier;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  /**
28   * Factory for {@code Clock} objects.
29   */
30  public final class ClockFactory {
31  
32      /**
33       * Name of the system property that can be used to specify a {@code Clock}
34       * implementation class. The value of this property is {@value}.
35       */
36      public static final String PROPERTY_NAME = "log4j.Clock";
37      private static final StatusLogger LOGGER = StatusLogger.getLogger();
38  
39      // private static final Clock clock = createClock();
40  
41      private ClockFactory() {
42      }
43  
44      /**
45       * Returns a {@code Clock} instance depending on the value of system
46       * property {@link #PROPERTY_NAME}.
47       * <p>
48       * If system property {@code log4j.Clock=CachedClock} is specified,
49       * this method returns an instance of {@link CachedClock}. If system
50       * property {@code log4j.Clock=CoarseCachedClock} is specified, this
51       * method returns an instance of {@link CoarseCachedClock}.
52       * </p>
53       * <p>
54       * If another value is specified, this value is taken as the fully qualified
55       * class name of a class that implements the {@code Clock} interface. An
56       * object of this class is instantiated and returned.
57       * </p>
58       * <p>
59       * If no value is specified, or if the specified value could not correctly
60       * be instantiated or did not implement the {@code Clock} interface, then an
61       * instance of {@link SystemClock} is returned.
62       * </p>
63       *
64       * @return a {@code Clock} instance
65       */
66      public static Clock getClock() {
67          return createClock();
68      }
69  
70      private static Map<String, Supplier<Clock>> aliases() {
71          final Map<String, Supplier<Clock>> result = new HashMap<>();
72          result.put("SystemClock",       new Supplier<Clock>() { @Override public Clock get() { return new SystemClock(); } });
73          result.put("SystemMillisClock", new Supplier<Clock>() { @Override public Clock get() { return new SystemMillisClock(); } });
74          result.put("CachedClock",       new Supplier<Clock>() { @Override public Clock get() { return CachedClock.instance(); } });
75          result.put("CoarseCachedClock", new Supplier<Clock>() { @Override public Clock get() { return CoarseCachedClock.instance(); } });
76          result.put("org.apache.logging.log4j.core.util.CachedClock", new Supplier<Clock>() { @Override public Clock get() { return CachedClock.instance(); } });
77          result.put("org.apache.logging.log4j.core.util.CoarseCachedClock", new Supplier<Clock>() { @Override public Clock get() { return CoarseCachedClock.instance(); } });
78          return result;
79      }
80  
81      private static Clock createClock() {
82          final String userRequest = PropertiesUtil.getProperties().getStringProperty(PROPERTY_NAME);
83          if (userRequest == null) {
84              LOGGER.trace("Using default SystemClock for timestamps.");
85              return logSupportedPrecision(new SystemClock());
86          }
87          final Supplier<Clock> specified = aliases().get(userRequest);
88          if (specified != null) {
89              LOGGER.trace("Using specified {} for timestamps.", userRequest);
90              return logSupportedPrecision(specified.get());
91          }
92          try {
93              final Clock result = Loader.newCheckedInstanceOf(userRequest, Clock.class);
94              LOGGER.trace("Using {} for timestamps.", result.getClass().getName());
95              return logSupportedPrecision(result);
96          } catch (final Exception e) {
97              final String fmt = "Could not create {}: {}, using default SystemClock for timestamps.";
98              LOGGER.error(fmt, userRequest, e);
99              return logSupportedPrecision(new SystemClock());
100         }
101     }
102 
103     private static Clock logSupportedPrecision(final Clock clock) {
104         final String support = clock instanceof PreciseClock ? "supports" : "does not support";
105         LOGGER.debug("{} {} precise timestamps.", clock.getClass().getName(), support);
106         return clock;
107     }
108 }