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.lang.reflect.Constructor; 020import java.util.Map; 021import java.util.Map.Entry; 022 023import org.apache.logging.log4j.core.ContextDataInjector; 024import org.apache.logging.log4j.core.LogEvent; 025import org.apache.logging.log4j.core.util.Loader; 026import org.apache.logging.log4j.util.IndexedStringMap; 027import org.apache.logging.log4j.util.PropertiesUtil; 028import org.apache.logging.log4j.util.ReadOnlyStringMap; 029import org.apache.logging.log4j.util.SortedArrayStringMap; 030import org.apache.logging.log4j.util.StringMap; 031 032/** 033 * Factory for creating the StringMap instances used to initialize LogEvents' {@linkplain LogEvent#getContextData() 034 * context data}. When context data is {@linkplain ContextDataInjector injected} into the log event, these StringMap 035 * instances may be either populated with key-value pairs from the context, or completely replaced altogether. 036 * <p> 037 * By default returns {@code SortedArrayStringMap} objects. Can be configured by setting system property 038 * {@code "log4j2.ContextData"} to the fully qualified class name of a class implementing the {@code StringMap} 039 * interface. The class must have a public default constructor, and if possible should also have a public constructor 040 * that takes a single {@code int} argument for the initial capacity. 041 * </p> 042 * 043 * @see LogEvent#getContextData() 044 * @see ContextDataInjector 045 * @see SortedArrayStringMap 046 * @since 2.7 047 */ 048public class ContextDataFactory { 049 private static final String CLASS_NAME = PropertiesUtil.getProperties().getStringProperty("log4j2.ContextData"); 050 private static final Class<? extends StringMap> CACHED_CLASS = createCachedClass(CLASS_NAME); 051 052 /** 053 * In LOG4J2-2649 (https://issues.apache.org/jira/browse/LOG4J2-2649), 054 * the reporter said some reason about using graalvm to static compile. 055 * In graalvm doc (https://github.com/oracle/graal/blob/master/substratevm/LIMITATIONS.md), 056 * graalvm is not support MethodHandle now, so the Constructor need not to return MethodHandle. 057 */ 058 private static final Constructor<?> DEFAULT_CONSTRUCTOR = createDefaultConstructor(CACHED_CLASS); 059 private static final Constructor<?> INITIAL_CAPACITY_CONSTRUCTOR = createInitialCapacityConstructor(CACHED_CLASS); 060 061 private static final StringMap EMPTY_STRING_MAP = createContextData(0); 062 063 static { 064 EMPTY_STRING_MAP.freeze(); 065 } 066 067 private static Class<? extends StringMap> createCachedClass(final String className) { 068 if (className == null) { 069 return null; 070 } 071 try { 072 return Loader.loadClass(className).asSubclass(IndexedStringMap.class); 073 } catch (final Exception any) { 074 return null; 075 } 076 } 077 078 private static Constructor<?> createDefaultConstructor(final Class<? extends StringMap> cachedClass){ 079 if (cachedClass == null) { 080 return null; 081 } 082 try { 083 return cachedClass.getConstructor(); 084 } catch (final NoSuchMethodException | IllegalAccessError ignored) { 085 return null; 086 } 087 } 088 089 private static Constructor<?> createInitialCapacityConstructor(final Class<? extends StringMap> cachedClass){ 090 if (cachedClass == null) { 091 return null; 092 } 093 try { 094 return cachedClass.getConstructor(int.class); 095 } catch (final NoSuchMethodException | IllegalAccessError ignored) { 096 return null; 097 } 098 } 099 100 public static StringMap createContextData() { 101 if (DEFAULT_CONSTRUCTOR == null) { 102 return new SortedArrayStringMap(); 103 } 104 try { 105 return (IndexedStringMap) DEFAULT_CONSTRUCTOR.newInstance(); 106 } catch (final Throwable ignored) { 107 return new SortedArrayStringMap(); 108 } 109 } 110 111 public static StringMap createContextData(final int initialCapacity) { 112 if (INITIAL_CAPACITY_CONSTRUCTOR == null) { 113 return new SortedArrayStringMap(initialCapacity); 114 } 115 try { 116 return (IndexedStringMap) INITIAL_CAPACITY_CONSTRUCTOR.newInstance(initialCapacity); 117 } catch (final Throwable ignored) { 118 return new SortedArrayStringMap(initialCapacity); 119 } 120 } 121 122 public static StringMap createContextData(final Map<String, String> context) { 123 final StringMap contextData = createContextData(context.size()); 124 for (final Entry<String, String> entry : context.entrySet()) { 125 contextData.putValue(entry.getKey(), entry.getValue()); 126 } 127 return contextData; 128 } 129 130 public static StringMap createContextData(final ReadOnlyStringMap readOnlyStringMap) { 131 final StringMap contextData = createContextData(readOnlyStringMap.size()); 132 contextData.putAll(readOnlyStringMap); 133 return contextData; 134 } 135 136 /** 137 * An empty pre-frozen IndexedStringMap. The returned object may be shared. 138 * 139 * @return an empty pre-frozen IndexedStringMap 140 */ 141 public static StringMap emptyFrozenContextData() { 142 return EMPTY_STRING_MAP; 143 } 144 145}