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 */ 017package org.apache.logging.log4j.core.async; 018 019import org.apache.logging.log4j.core.LoggerContext; 020import org.apache.logging.log4j.core.impl.ContextAnchor; 021import org.apache.logging.log4j.core.selector.ContextSelector; 022 023import java.net.URI; 024import java.util.ArrayList; 025import java.util.Collections; 026import java.util.List; 027import java.util.concurrent.TimeUnit; 028 029/** 030 * Returns either this Thread's context or the default {@link AsyncLoggerContext}. 031 * Single-application instances should prefer this implementation over the {@link AsyncLoggerContextSelector} 032 * due the the reduced overhead avoiding classloader lookups. 033 */ 034public class BasicAsyncLoggerContextSelector implements ContextSelector { 035 036 private static final AsyncLoggerContext CONTEXT = new AsyncLoggerContext("AsyncDefault"); 037 038 @Override 039 public void shutdown(String fqcn, ClassLoader loader, boolean currentContext, boolean allContexts) { 040 LoggerContext ctx = getContext(fqcn, loader, currentContext); 041 if (ctx != null && ctx.isStarted()) { 042 ctx.stop(DEFAULT_STOP_TIMEOUT, TimeUnit.MILLISECONDS); 043 } 044 } 045 046 @Override 047 public boolean hasContext(String fqcn, ClassLoader loader, boolean currentContext) { 048 LoggerContext ctx = getContext(fqcn, loader, currentContext); 049 return ctx != null && ctx.isStarted(); 050 } 051 052 @Override 053 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext) { 054 final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get(); 055 return ctx != null ? ctx : CONTEXT; 056 } 057 058 059 @Override 060 public LoggerContext getContext( 061 final String fqcn, 062 final ClassLoader loader, 063 final boolean currentContext, 064 final URI configLocation) { 065 final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get(); 066 return ctx != null ? ctx : CONTEXT; 067 } 068 069 @Override 070 public void removeContext(final LoggerContext context) { 071 // does not remove anything 072 } 073 074 @Override 075 public boolean isClassLoaderDependent() { 076 return false; 077 } 078 079 @Override 080 public List<LoggerContext> getLoggerContexts() { 081 return Collections.singletonList(CONTEXT); 082 } 083 084}