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.core.web; 018 019 import org.apache.logging.log4j.LogManager; 020 import org.apache.logging.log4j.core.impl.ContextAnchor; 021 import org.apache.logging.log4j.core.impl.Log4jContextFactory; 022 import org.apache.logging.log4j.core.selector.ContextSelector; 023 import org.apache.logging.log4j.core.LoggerContext; 024 import org.apache.logging.log4j.core.selector.NamedContextSelector; 025 import org.apache.logging.log4j.spi.LoggerContextFactory; 026 027 import javax.servlet.Filter; 028 import javax.servlet.FilterChain; 029 import javax.servlet.FilterConfig; 030 import javax.servlet.ServletContext; 031 import javax.servlet.ServletException; 032 import javax.servlet.ServletRequest; 033 import javax.servlet.ServletResponse; 034 import javax.servlet.UnavailableException; 035 import java.io.IOException; 036 import java.net.URI; 037 038 /** 039 * ServletFilter than may be used to set up a LoggerContext for each web application. 040 */ 041 public class JNDIContextFilter implements Filter { 042 /** 043 * The Filter init parameter that defines the name of the LoggerContext. 044 */ 045 public static final String CONTEXT_NAME = "context-name"; 046 /** 047 * The Filter init parameter that defines the configuration location for the LoggerContext. 048 */ 049 public static final String CONFIG_LOCATION = "config-location"; 050 private ServletContext context; 051 private boolean created = false; 052 private String name; 053 private NamedContextSelector selector = null; 054 055 @Override 056 public void init(final FilterConfig filterConfig) throws ServletException { 057 context = filterConfig.getServletContext(); 058 name = filterConfig.getInitParameter(CONTEXT_NAME); 059 URI configLocation = null; 060 String configLocn = filterConfig.getInitParameter(CONFIG_LOCATION); 061 if (configLocn != null) { 062 try { 063 configLocation = new URI(configLocn); 064 } catch (Exception ex) { 065 context.log("Unable to convert config location " + configLocn + " to a URI: " + ex.getMessage()); 066 } 067 } 068 069 if (name == null) { 070 throw new UnavailableException("A context-name attribute is required"); 071 } 072 if (context.getAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE) == null) { 073 LoggerContext ctx; 074 final LoggerContextFactory factory = LogManager.getFactory(); 075 if (factory instanceof Log4jContextFactory) { 076 final ContextSelector sel = ((Log4jContextFactory) factory).getSelector(); 077 if (sel instanceof NamedContextSelector) { 078 selector = (NamedContextSelector) sel; 079 ctx = selector.locateContext(name, configLocation); 080 } else { 081 return; 082 } 083 } else { 084 return; 085 } 086 context.setAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE, ctx); 087 created = true; 088 context.log("Created context for " + name + " using " + ctx.getClass().getClassLoader()); 089 } 090 } 091 092 @Override 093 public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, 094 final FilterChain filterChain) 095 throws IOException, ServletException { 096 final LoggerContext ctx = (LoggerContext) context.getAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE); 097 if (ctx != null) { 098 ContextAnchor.THREAD_CONTEXT.set(ctx); 099 try { 100 filterChain.doFilter(servletRequest, servletResponse); 101 } finally { 102 ContextAnchor.THREAD_CONTEXT.remove(); 103 } 104 } else { 105 filterChain.doFilter(servletRequest, servletResponse); 106 } 107 } 108 109 @Override 110 public void destroy() { 111 final LoggerContext ctx = (LoggerContext) context.getAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE); 112 if (ctx != null && created) { 113 context.log("Removing context for " + name); 114 context.removeAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE); 115 if (selector != null) { 116 selector.removeContext(name); 117 } 118 ctx.stop(); 119 } 120 } 121 }