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