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