    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements. See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache license, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License. You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the license for the specific language governing permissions and
    015     * limitations under the license.
    016     */
    017    package org.apache.logging.log4j.core.util;
    018
    019    import org.apache.logging.log4j.status.StatusLogger;
    020    import org.apache.logging.log4j.util.PropertiesUtil;
    021
    022    /**
    023     * Factory for {@code Clock} objects.
    024     */
    025    public final class ClockFactory {
    026
    027        /**
    028         * Name of the system property that can be used to specify a {@code Clock}
    029         * implementation class. The value of this property is {@value}.
    030         */
    031        public static final String PROPERTY_NAME = "log4j.Clock";
    032        private static final StatusLogger LOGGER = StatusLogger.getLogger();
    033
    034        // private static final Clock clock = createClock();
    035
    036        private ClockFactory() {
    037        }
    038
    039        /**
    040         * Returns a {@code Clock} instance depending on the value of system
    041         * property {@link #PROPERTY_NAME}.
    042         * <p>
    043         * If system property {@code log4j.Clock=CachedClock} is specified,
    044         * this method returns an instance of {@link CachedClock}. If system
    045         * property {@code log4j.Clock=CoarseCachedClock} is specified, this
    046         * method returns an instance of {@link CoarseCachedClock}.
    047         * </p>
    048         * <p>
    049         * If another value is specified, this value is taken as the fully qualified
    050         * class name of a class that implements the {@code Clock} interface. An
    051         * object of this class is instantiated and returned.
    052         * </p>
    053         * <p>
    054         * If no value is specified, or if the specified value could not correctly
    055         * be instantiated or did not implement the {@code Clock} interface, then an
    056         * instance of {@link SystemClock} is returned.
    057         * </p>
    058         *
    059         * @return a {@code Clock} instance
    060         */
    061        public static Clock getClock() {
    062            return createClock();
    063        }
    064
    065        private static Clock createClock() {
    066            final String userRequest = PropertiesUtil.getProperties().getStringProperty(PROPERTY_NAME);
    067            if (userRequest == null || "SystemClock".equals(userRequest)) {
    068                LOGGER.debug("Using default SystemClock for timestamps");
    069                return new SystemClock();
    070            }
    071            if (CachedClock.class.getName().equals(userRequest)
    072                    || "CachedClock".equals(userRequest)) {
    073                LOGGER.debug("Using specified CachedClock for timestamps");
    074                return CachedClock.instance();
    075            }
    076            if (CoarseCachedClock.class.getName().equals(userRequest)
    077                    || "CoarseCachedClock".equals(userRequest)) {
    078                LOGGER.debug("Using specified CoarseCachedClock for timestamps");
    079                return CoarseCachedClock.instance();
    080            }
    081            try {
    082                final Clock result = Loader.newCheckedInstanceOf(userRequest, Clock.class);
    083                LOGGER.debug("Using {} for timestamps", result.getClass().getName());
    084                return result;
    085            } catch (final Exception e) {
    086                final String fmt = "Could not create {}: {}, using default SystemClock for timestamps";
    087                LOGGER.error(fmt, userRequest, e);
    088                return new SystemClock();
    089            }
    090        }
    091    }