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.jcl; 018 019 import org.apache.commons.logging.Log; 020 import org.apache.commons.logging.LogConfigurationException; 021 import org.apache.commons.logging.LogFactory; 022 import org.apache.logging.log4j.LogManager; 023 import org.apache.logging.log4j.spi.AbstractLogger; 024 import org.apache.logging.log4j.spi.LoggerContext; 025 026 import java.util.Map; 027 import java.util.WeakHashMap; 028 import java.util.concurrent.ConcurrentHashMap; 029 import java.util.concurrent.ConcurrentMap; 030 031 /** 032 * 033 */ 034 public class LogFactoryImpl extends LogFactory { 035 036 private final Map<LoggerContext, ConcurrentMap<String, Log>> contextMap = 037 new WeakHashMap<LoggerContext, ConcurrentMap<String, Log>>(); 038 039 private final ConcurrentMap<String, Object> attributes = new ConcurrentHashMap<String, Object>(); 040 041 @Override 042 public Log getInstance(final String name) throws LogConfigurationException { 043 final LoggerContext context = PrivateManager.getContext(); 044 final ConcurrentMap<String, Log> loggers = getLoggersMap(); 045 if (loggers.containsKey(name)) { 046 return loggers.get(name); 047 } 048 final org.apache.logging.log4j.Logger logger = PrivateManager.getLogger(name); 049 if (logger instanceof AbstractLogger) { 050 loggers.putIfAbsent(name, new Log4JLog((AbstractLogger) logger, name)); 051 return loggers.get(name); 052 } 053 throw new LogConfigurationException( 054 "Commons Logging Adapter requires base logging system to extend Log4J AbstractLogger"); 055 } 056 057 private ConcurrentMap<String, Log> getLoggersMap() { 058 final LoggerContext context = PrivateManager.getContext(); 059 synchronized (contextMap) { 060 ConcurrentMap<String, Log> map = contextMap.get(context); 061 if (map == null) { 062 map = new ConcurrentHashMap<String, Log>(); 063 contextMap.put(context, map); 064 } 065 return map; 066 } 067 } 068 069 @Override 070 public Object getAttribute(final String name) { 071 return attributes.get(name); 072 } 073 074 @Override 075 public String[] getAttributeNames() { 076 return attributes.keySet().toArray(new String[attributes.size()]); 077 } 078 079 @Override 080 public Log getInstance(final Class clazz) throws LogConfigurationException { 081 return getInstance(clazz.getName()); 082 } 083 084 /** 085 * This method is supposed to clear all loggers. In this implementation it will clear all the logger 086 * wrappers but the loggers managed by the underlying logger context will not be. 087 */ 088 @Override 089 public void release() { 090 getLoggersMap().clear(); 091 } 092 093 @Override 094 public void removeAttribute(final String name) { 095 attributes.remove(name); 096 } 097 098 @Override 099 public void setAttribute(final String name, final Object value) { 100 if (value != null) { 101 attributes.put(name, value); 102 } else { 103 removeAttribute(name); 104 } 105 } 106 107 /** 108 * The real bridge between commons logging and Log4j. 109 */ 110 private static class PrivateManager extends LogManager { 111 private static final String FQCN = LogFactory.class.getName(); 112 113 public static LoggerContext getContext() { 114 return getContext(FQCN, false); 115 } 116 117 public static org.apache.logging.log4j.Logger getLogger(final String name) { 118 return getLogger(FQCN, name); 119 } 120 } 121 122 }