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