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 public void init(final FilterConfig filterConfig) throws ServletException { 056 context = filterConfig.getServletContext(); 057 name = filterConfig.getInitParameter(CONTEXT_NAME); 058 URI configLocation = null; 059 String configLocn = filterConfig.getInitParameter(CONFIG_LOCATION); 060 if (configLocn != null) { 061 try { 062 configLocation = new URI(configLocn); 063 } catch (Exception ex) { 064 context.log("Unable to convert config location " + configLocn + " to a URI: " + ex.getMessage()); 065 } 066 } 067 068 if (name == null) { 069 throw new UnavailableException("A context-name attribute is required"); 070 } 071 if (context.getAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE) == null) { 072 LoggerContext ctx; 073 final LoggerContextFactory factory = LogManager.getFactory(); 074 if (factory instanceof Log4jContextFactory) { 075 final ContextSelector sel = ((Log4jContextFactory) factory).getSelector(); 076 if (sel instanceof NamedContextSelector) { 077 selector = (NamedContextSelector) sel; 078 ctx = selector.locateContext(name, configLocation); 079 } else { 080 return; 081 } 082 } else { 083 return; 084 } 085 context.setAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE, ctx); 086 created = true; 087 context.log("Created context for " + name + " using " + ctx.getClass().getClassLoader()); 088 } 089 } 090 091 public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, 092 final FilterChain filterChain) 093 throws IOException, ServletException { 094 final LoggerContext ctx = (LoggerContext) context.getAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE); 095 if (ctx != null) { 096 ContextAnchor.THREAD_CONTEXT.set(ctx); 097 try { 098 filterChain.doFilter(servletRequest, servletResponse); 099 } finally { 100 ContextAnchor.THREAD_CONTEXT.remove(); 101 } 102 } else { 103 filterChain.doFilter(servletRequest, servletResponse); 104 } 105 } 106 107 public void destroy() { 108 final LoggerContext ctx = (LoggerContext) context.getAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE); 109 if (ctx != null && created) { 110 context.log("Removing context for " + name); 111 context.removeAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE); 112 if (selector != null) { 113 selector.removeContext(name); 114 } 115 ctx.stop(); 116 } 117 } 118 }