001/*
002 * Copyright (C) 2019 Michael N. Lipp (http://www.mnl.de)
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package de.mnl.osgi.log4j2osgi;
018
019import de.mnl.osgi.lf4osgi.core.LoggerGroup;
020
021import org.apache.logging.log4j.message.MessageFactory;
022import org.apache.logging.log4j.spi.ExtendedLogger;
023import org.apache.logging.log4j.spi.LoggerContext;
024import org.apache.logging.log4j.spi.LoggerRegistry;
025import org.osgi.framework.Bundle;
026
027public class OsgiLoggerContext implements LoggerGroup, LoggerContext {
028
029    private final Bundle bundle;
030    private final LoggerRegistry<OsgiLogger> loggerRegistry;
031
032    public OsgiLoggerContext(Bundle bundle) {
033        this.bundle = bundle;
034        loggerRegistry = new LoggerRegistry<>();
035    }
036
037    @Override
038    public Bundle getBundle() {
039        return bundle;
040    }
041
042    @Override
043    public OsgiLogger getLogger(final String name) {
044        if (!loggerRegistry.hasLogger(name)) {
045            loggerRegistry.putIfAbsent(name, null,
046                new OsgiLogger(this, name));
047        }
048        return loggerRegistry.getLogger(name);
049    }
050
051    @Override
052    public ExtendedLogger getLogger(final String name,
053            final MessageFactory messageFactory) {
054        if (!loggerRegistry.hasLogger(name, messageFactory)) {
055            loggerRegistry.putIfAbsent(name, messageFactory,
056                new OsgiLogger(this, name, messageFactory));
057        }
058        return loggerRegistry.getLogger(name, messageFactory);
059    }
060
061    @Override
062    public boolean hasLogger(final String name) {
063        return loggerRegistry.hasLogger(name);
064    }
065
066    @Override
067    public boolean hasLogger(final String name,
068            final MessageFactory messageFactory) {
069        return loggerRegistry.hasLogger(name, messageFactory);
070    }
071
072    @Override
073    public boolean hasLogger(final String name,
074            final Class<? extends MessageFactory> messageFactoryClass) {
075        return loggerRegistry.hasLogger(name, messageFactoryClass);
076    }
077
078    @Override
079    public Object getExternalContext() {
080        return null;
081    }
082
083}