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.impl; 018 019import java.util.ArrayList; 020import java.util.Collection; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024import java.util.ServiceLoader; 025import java.util.concurrent.ConcurrentLinkedDeque; 026import java.util.concurrent.locks.Lock; 027import java.util.concurrent.locks.ReentrantLock; 028 029import org.apache.logging.log4j.Logger; 030import org.apache.logging.log4j.ThreadContext; 031import org.apache.logging.log4j.core.ContextDataInjector; 032import org.apache.logging.log4j.core.config.Property; 033import org.apache.logging.log4j.core.util.ContextDataProvider; 034import org.apache.logging.log4j.spi.ReadOnlyThreadContextMap; 035import org.apache.logging.log4j.status.StatusLogger; 036import org.apache.logging.log4j.util.LoaderUtil; 037import org.apache.logging.log4j.util.ReadOnlyStringMap; 038import org.apache.logging.log4j.util.StringMap; 039 040/** 041 * {@code ThreadContextDataInjector} contains a number of strategies for copying key-value pairs from the various 042 * {@code ThreadContext} map implementations into a {@code StringMap}. In the case of duplicate keys, 043 * thread context values overwrite configuration {@code Property} values. 044 * <p> 045 * These are the default {@code ContextDataInjector} objects returned by the {@link ContextDataInjectorFactory}. 046 * </p> 047 * 048 * @see org.apache.logging.log4j.ThreadContext 049 * @see Property 050 * @see ReadOnlyStringMap 051 * @see ContextDataInjector 052 * @see ContextDataInjectorFactory 053 * @since 2.7 054 */ 055public class ThreadContextDataInjector { 056 057 private static final Logger LOGGER = StatusLogger.getLogger(); 058 059 /** 060 * ContextDataProviders loaded via OSGi. 061 */ 062 public static Collection<ContextDataProvider> contextDataProviders = 063 new ConcurrentLinkedDeque<>(); 064 065 private static volatile List<ContextDataProvider> serviceProviders = null; 066 private static final Lock providerLock = new ReentrantLock(); 067 068 public static void initServiceProviders() { 069 if (serviceProviders == null) { 070 providerLock.lock(); 071 try { 072 if (serviceProviders == null) { 073 serviceProviders = getServiceProviders(); 074 } 075 } finally { 076 providerLock.unlock(); 077 } 078 } 079 } 080 081 private static List<ContextDataProvider> getServiceProviders() { 082 List<ContextDataProvider> providers = new ArrayList<>(); 083 for (final ClassLoader classLoader : LoaderUtil.getClassLoaders()) { 084 try { 085 for (final ContextDataProvider provider : ServiceLoader.load(ContextDataProvider.class, classLoader)) { 086 if (providers.stream().noneMatch(p -> p.getClass().isAssignableFrom(provider.getClass()))) { 087 providers.add(provider); 088 } 089 } 090 } catch (final Throwable ex) { 091 LOGGER.debug("Unable to access Context Data Providers {}", ex.getMessage()); 092 } 093 } 094 return providers; 095 } 096 097 /** 098 * Default {@code ContextDataInjector} for the legacy {@code Map<String, String>}-based ThreadContext (which is 099 * also the ThreadContext implementation used for web applications). 100 * <p> 101 * This injector always puts key-value pairs into the specified reusable StringMap. 102 */ 103 public static class ForDefaultThreadContextMap implements ContextDataInjector { 104 105 private final List<ContextDataProvider> providers; 106 107 public ForDefaultThreadContextMap() { 108 providers = getProviders(); 109 } 110 111 /** 112 * Puts key-value pairs from both the specified list of properties as well as the thread context into the 113 * specified reusable StringMap. 114 * 115 * @param props list of configuration properties, may be {@code null} 116 * @param contextData a {@code StringMap} instance from the log event 117 * @return a {@code StringMap} combining configuration properties with thread context data 118 */ 119 @Override 120 public StringMap injectContextData(final List<Property> props, final StringMap contextData) { 121 122 final Map<String, String> copy; 123 124 if (providers.size() == 1) { 125 copy = providers.get(0).supplyContextData(); 126 } else { 127 copy = new HashMap<>(); 128 for (ContextDataProvider provider : providers) { 129 copy.putAll(provider.supplyContextData()); 130 } 131 } 132 133 // The DefaultThreadContextMap stores context data in a Map<String, String>. 134 // This is a copy-on-write data structure so we are sure ThreadContext changes will not affect our copy. 135 // If there are no configuration properties or providers returning a thin wrapper around the copy 136 // is faster than copying the elements into the LogEvent's reusable StringMap. 137 if ((props == null || props.isEmpty())) { 138 // this will replace the LogEvent's context data with the returned instance. 139 // NOTE: must mark as frozen or downstream components may attempt to modify (UnsupportedOperationEx) 140 return copy.isEmpty() ? ContextDataFactory.emptyFrozenContextData() : frozenStringMap(copy); 141 } 142 // If the list of Properties is non-empty we need to combine the properties and the ThreadContext 143 // data. Note that we cannot reuse the specified StringMap: some Loggers may have properties defined 144 // and others not, so the LogEvent's context data may have been replaced with an immutable copy from 145 // the ThreadContext - this will throw an UnsupportedOperationException if we try to modify it. 146 final StringMap result = new JdkMapAdapterStringMap(new HashMap<>(copy)); 147 for (int i = 0; i < props.size(); i++) { 148 final Property prop = props.get(i); 149 if (!copy.containsKey(prop.getName())) { 150 result.putValue(prop.getName(), prop.getValue()); 151 } 152 } 153 result.freeze(); 154 return result; 155 } 156 157 private static JdkMapAdapterStringMap frozenStringMap(final Map<String, String> copy) { 158 final JdkMapAdapterStringMap result = new JdkMapAdapterStringMap(copy); 159 result.freeze(); 160 return result; 161 } 162 163 @Override 164 public ReadOnlyStringMap rawContextData() { 165 final ReadOnlyThreadContextMap map = ThreadContext.getThreadContextMap(); 166 if (map instanceof ReadOnlyStringMap) { 167 return (ReadOnlyStringMap) map; 168 } 169 // note: default ThreadContextMap is null 170 final Map<String, String> copy = ThreadContext.getImmutableContext(); 171 return copy.isEmpty() ? ContextDataFactory.emptyFrozenContextData() : new JdkMapAdapterStringMap(copy); 172 } 173 } 174 175 /** 176 * The {@code ContextDataInjector} used when the ThreadContextMap implementation is a garbage-free 177 * StringMap-based data structure. 178 * <p> 179 * This injector always puts key-value pairs into the specified reusable StringMap. 180 */ 181 public static class ForGarbageFreeThreadContextMap implements ContextDataInjector { 182 private final List<ContextDataProvider> providers; 183 184 public ForGarbageFreeThreadContextMap() { 185 this.providers = getProviders(); 186 } 187 188 /** 189 * Puts key-value pairs from both the specified list of properties as well as the thread context into the 190 * specified reusable StringMap. 191 * 192 * @param props list of configuration properties, may be {@code null} 193 * @param reusable a {@code StringMap} instance that may be reused to avoid creating temporary objects 194 * @return a {@code StringMap} combining configuration properties with thread context data 195 */ 196 @Override 197 public StringMap injectContextData(final List<Property> props, final StringMap reusable) { 198 // When the ThreadContext is garbage-free, we must copy its key-value pairs into the specified reusable 199 // StringMap. We cannot return the ThreadContext's internal data structure because it may be modified later 200 // and such modifications should not be reflected in the log event. 201 copyProperties(props, reusable); 202 for (int i = 0; i < providers.size(); ++i) { 203 reusable.putAll(providers.get(i).supplyStringMap()); 204 } 205 return reusable; 206 } 207 208 @Override 209 public ReadOnlyStringMap rawContextData() { 210 return ThreadContext.getThreadContextMap().getReadOnlyContextData(); 211 } 212 } 213 214 /** 215 * The {@code ContextDataInjector} used when the ThreadContextMap implementation is a copy-on-write 216 * StringMap-based data structure. 217 * <p> 218 * If there are no configuration properties, this injector will return the thread context's internal data 219 * structure. Otherwise the configuration properties are combined with the thread context key-value pairs into the 220 * specified reusable StringMap. 221 */ 222 public static class ForCopyOnWriteThreadContextMap implements ContextDataInjector { 223 private final List<ContextDataProvider> providers; 224 225 public ForCopyOnWriteThreadContextMap() { 226 this.providers = getProviders(); 227 } 228 /** 229 * If there are no configuration properties, this injector will return the thread context's internal data 230 * structure. Otherwise the configuration properties are combined with the thread context key-value pairs into the 231 * specified reusable StringMap. 232 * 233 * @param props list of configuration properties, may be {@code null} 234 * @param ignore a {@code StringMap} instance from the log event 235 * @return a {@code StringMap} combining configuration properties with thread context data 236 */ 237 @Override 238 public StringMap injectContextData(final List<Property> props, final StringMap ignore) { 239 // If there are no configuration properties we want to just return the ThreadContext's StringMap: 240 // it is a copy-on-write data structure so we are sure ThreadContext changes will not affect our copy. 241 if (providers.size() == 1 && (props == null || props.isEmpty())) { 242 // this will replace the LogEvent's context data with the returned instance 243 return providers.get(0).supplyStringMap(); 244 } 245 int count = props == null ? 0 : props.size(); 246 StringMap[] maps = new StringMap[providers.size()]; 247 for (int i = 0; i < providers.size(); ++i) { 248 maps[i] = providers.get(i).supplyStringMap(); 249 count += maps[i].size(); 250 } 251 // However, if the list of Properties is non-empty we need to combine the properties and the ThreadContext 252 // data. Note that we cannot reuse the specified StringMap: some Loggers may have properties defined 253 // and others not, so the LogEvent's context data may have been replaced with an immutable copy from 254 // the ThreadContext - this will throw an UnsupportedOperationException if we try to modify it. 255 final StringMap result = ContextDataFactory.createContextData(count); 256 copyProperties(props, result); 257 for (StringMap map : maps) { 258 result.putAll(map); 259 } 260 return result; 261 } 262 263 @Override 264 public ReadOnlyStringMap rawContextData() { 265 return ThreadContext.getThreadContextMap().getReadOnlyContextData(); 266 } 267 } 268 269 /** 270 * Copies key-value pairs from the specified property list into the specified {@code StringMap}. 271 * 272 * @param properties list of configuration properties, may be {@code null} 273 * @param result the {@code StringMap} object to add the key-values to. Must be non-{@code null}. 274 */ 275 public static void copyProperties(final List<Property> properties, final StringMap result) { 276 if (properties != null) { 277 for (int i = 0; i < properties.size(); i++) { 278 final Property prop = properties.get(i); 279 result.putValue(prop.getName(), prop.getValue()); 280 } 281 } 282 } 283 284 private static List<ContextDataProvider> getProviders() { 285 initServiceProviders(); 286 final List<ContextDataProvider> providers = new ArrayList<>(contextDataProviders); 287 if (serviceProviders != null) { 288 providers.addAll(serviceProviders); 289 } 290 return providers; 291 } 292}