
Author: seb Date: Tue Jan 30 15:48:50 2007 New Revision: 1292 Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/LoggerContextFilter.java Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/Configurator.java logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/ConfiguratorMBean.java logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextJNDISelector.java logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextSelector.java logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/DefaultContextSelector.java logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/ContextDetachingSCL.java Log: Added a LoggerContextFilter to accelerate the retrieval of the logger context when in JNDI environment Added the LB license to some classes Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/Configurator.java ============================================================================== --- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/Configurator.java (original) +++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/Configurator.java Tue Jan 30 15:48:50 2007 @@ -1,3 +1,12 @@ +/** + * Logback: the reliable, generic, fast and flexible logging framework. + * + * Copyright (C) 1999-2006, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ package ch.qos.logback.classic.jmx; import java.net.URL; Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/ConfiguratorMBean.java ============================================================================== --- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/ConfiguratorMBean.java (original) +++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/ConfiguratorMBean.java Tue Jan 30 15:48:50 2007 @@ -1,3 +1,12 @@ +/** + * Logback: the reliable, generic, fast and flexible logging framework. + * + * Copyright (C) 1999-2006, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ package ch.qos.logback.classic.jmx; import java.net.URL; Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextJNDISelector.java ============================================================================== --- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextJNDISelector.java (original) +++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextJNDISelector.java Tue Jan 30 15:48:50 2007 @@ -1,3 +1,12 @@ +/** + * Logback: the reliable, generic, fast and flexible logging framework. + * + * Copyright (C) 1999-2006, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ package ch.qos.logback.classic.selector; import static ch.qos.logback.classic.ClassicGlobal.JNDI_CONFIGURATION_RESOURCE; @@ -39,6 +48,8 @@ private final Map<String, LoggerContext> contextMap; private final LoggerContext defaultContext; + private static final ThreadLocal<LoggerContext> threadLocal = new ThreadLocal<LoggerContext>(); + public ContextJNDISelector(LoggerContext context) { contextMap = Collections .synchronizedMap(new HashMap<String, LoggerContext>()); @@ -49,6 +60,12 @@ String contextName = null; Context ctx = null; + // First check if ThreadLocal has been set already + LoggerContext lc = threadLocal.get(); + if (lc != null) { + return lc; + } + try { // We first try to find the name of our // environment's LoggerContext @@ -110,20 +127,19 @@ + " does not lead to a valid file"); } } - + public List<String> getContextNames() { List<String> list = new ArrayList<String>(); list.addAll(contextMap.keySet()); return list; } - + public LoggerContext getLoggerContext(String name) { return contextMap.get(name); } - + /** - * Returns the number of managed contexts - * Used for testing purposes + * Returns the number of managed contexts Used for testing purposes * * @return the number of managed contexts */ @@ -131,4 +147,20 @@ return contextMap.size(); } + /** + * These methods are used by the LoggerContextFilter. + * + * They provide a way to tell the selector which context to use, thus saving + * the cost of a JNDI call at each new request. + * + * @param context + */ + public void setLocalContext(LoggerContext context) { + threadLocal.set(context); + } + + public void removeLocalContext() { + threadLocal.remove(); + } + } Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextSelector.java ============================================================================== --- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextSelector.java (original) +++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextSelector.java Tue Jan 30 15:48:50 2007 @@ -1,3 +1,12 @@ +/** + * Logback: the reliable, generic, fast and flexible logging framework. + * + * Copyright (C) 1999-2006, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ package ch.qos.logback.classic.selector; import java.util.List; Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/DefaultContextSelector.java ============================================================================== --- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/DefaultContextSelector.java (original) +++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/DefaultContextSelector.java Tue Jan 30 15:48:50 2007 @@ -1,3 +1,12 @@ +/** + * Logback: the reliable, generic, fast and flexible logging framework. + * + * Copyright (C) 1999-2006, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ package ch.qos.logback.classic.selector; import java.util.Arrays; Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/ContextDetachingSCL.java ============================================================================== --- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/ContextDetachingSCL.java (original) +++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/ContextDetachingSCL.java Tue Jan 30 15:48:50 2007 @@ -1,3 +1,13 @@ +/** + * Logback: the reliable, generic, fast and flexible logging framework. + * + * Copyright (C) 1999-2006, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ + package ch.qos.logback.classic.selector.servlet; import static ch.qos.logback.classic.ClassicGlobal.JNDI_CONTEXT_NAME; Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/LoggerContextFilter.java ============================================================================== --- (empty file) +++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/LoggerContextFilter.java Tue Jan 30 15:48:50 2007 @@ -0,0 +1,79 @@ +/** + * Logback: the reliable, generic, fast and flexible logging framework. + * + * Copyright (C) 1999-2006, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ + +package ch.qos.logback.classic.selector.servlet; + +import java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; + +import org.slf4j.LoggerFactory; + +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.selector.ContextJNDISelector; +import ch.qos.logback.classic.selector.ContextSelector; + +/** + * A servlet filter that puts the environment-dependend + * LoggerContext in a Threadlocal variable. + * + * It removes it after the request is processed. + * + * To use it, add the following lines to a web.xml file + * + * <filter> + * <filter-name>LoggerContextFilter</filter-name> + * <filter-class> + * ch.qos.userApp.LoggerContextFilter + * </filter-class> + * </filter> + * <filter-mapping> + * <filter-name>LoggerContextFilter</filter-name> + * <url-pattern>/*</url-pattern> + * </filter-mapping> + * + * @author Sébastien Pennec + */ +public class LoggerContextFilter implements Filter { + + public void destroy() { + //do nothing + } + + public void doFilter(ServletRequest request, ServletResponse response, + FilterChain chain) throws IOException, ServletException { + + LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); + ContextSelector selector = LoggerFactory.getContextSelector(); + ContextJNDISelector sel = null; + + if (selector instanceof ContextJNDISelector) { + sel = (ContextJNDISelector)selector; + sel.setLocalContext(lc); + } + + try { + chain.doFilter(request, response); + } finally { + if (sel != null) { + sel.removeLocalContext(); + } + } + } + + public void init(FilterConfig arg0) throws ServletException { + //do nothing + } +}