001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2014-2015 ForgeRock AS.
025 */
026package org.opends.server.loggers.slf4j;
027
028import java.util.concurrent.ConcurrentHashMap;
029import java.util.concurrent.ConcurrentMap;
030
031import org.slf4j.ILoggerFactory;
032import org.slf4j.Logger;
033
034/**
035 * Factory to retrieve an openDJ implementation of SLF4J Logger.
036 */
037public final class OpenDJLoggerFactory implements ILoggerFactory {
038
039    private final ConcurrentMap<String, Logger> loggerMap;
040
041    /** Create the factory. */
042    public OpenDJLoggerFactory() {
043        loggerMap = new ConcurrentHashMap<>();
044    }
045
046    /** {@inheritDoc} */
047    public Logger getLogger(String name) {
048        if (name.equalsIgnoreCase(Logger.ROOT_LOGGER_NAME)) {
049            name = "org.forgerock";
050        }
051
052        Logger slf4jLogger = loggerMap.get(name);
053        if (slf4jLogger != null) {
054            return slf4jLogger;
055        }
056        Logger newInstance = new OpenDJLoggerAdapter(name);
057        Logger oldInstance = loggerMap.putIfAbsent(name, newInstance);
058        return oldInstance == null ? newInstance : oldInstance;
059    }
060}