All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.logging.log4j.spi.DefaultThreadContextMap Maven / Gradle / Ivy

There is a newer version: 2024.11.18751.20241128T090041Z-241100
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache license, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the license for the specific language governing permissions and
 * limitations under the license.
 */
package org.apache.logging.log4j.spi;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.apache.logging.log4j.util.BiConsumer;
import org.apache.logging.log4j.util.ReadOnlyStringMap;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.apache.logging.log4j.util.TriConsumer;

/**
 * The actual ThreadContext Map. A new ThreadContext Map is created each time it is updated and the Map stored is always
 * immutable. This means the Map can be passed to other threads without concern that it will be updated. Since it is
 * expected that the Map will be passed to many more log events than the number of keys it contains the performance
 * should be much better than if the Map was copied for each event.
 */
public class DefaultThreadContextMap implements ThreadContextMap, ReadOnlyStringMap {
    private static final long serialVersionUID = 8218007901108944053L;

    /**
     * Property name ({@value} ) for selecting {@code InheritableThreadLocal} (value "true") or plain
     * {@code ThreadLocal} (value is not "true") in the implementation.
     */
    public static final String INHERITABLE_MAP = "isThreadContextMapInheritable";

    private final boolean useMap;
    private final ThreadLocal> localMap;

    private static boolean inheritableMap;
    
    static {
        init();
    }

    // LOG4J2-479: by default, use a plain ThreadLocal, only use InheritableThreadLocal if configured.
    // (This method is package protected for JUnit tests.)
    static ThreadLocal> createThreadLocalMap(final boolean isMapEnabled) {
        if (inheritableMap) {
            return new InheritableThreadLocal>() {
                @Override
                protected Map childValue(final Map parentValue) {
                    return parentValue != null && isMapEnabled //
                    ? Collections.unmodifiableMap(new HashMap<>(parentValue)) //
                            : null;
                }
            };
        }
        // if not inheritable, return plain ThreadLocal with null as initial value
        return new ThreadLocal<>();
    }

    static void init() {
        inheritableMap = PropertiesUtil.getProperties().getBooleanProperty(INHERITABLE_MAP);
    }
    
    public DefaultThreadContextMap() {
        this(true);
    }

    public DefaultThreadContextMap(final boolean useMap) {
        this.useMap = useMap;
        this.localMap = createThreadLocalMap(useMap);
    }

    @Override
    public void put(final String key, final String value) {
        if (!useMap) {
            return;
        }
        Map map = localMap.get();
        map = map == null ? new HashMap<>(1) : new HashMap<>(map);
        map.put(key, value);
        localMap.set(Collections.unmodifiableMap(map));
    }

    public void putAll(final Map m) {
        if (!useMap) {
            return;
        }
        Map map = localMap.get();
        map = map == null ? new HashMap<>(m.size()) : new HashMap<>(map);
        for (final Map.Entry e : m.entrySet()) {
            map.put(e.getKey(), e.getValue());
        }
        localMap.set(Collections.unmodifiableMap(map));
    }

    @Override
    public String get(final String key) {
        final Map map = localMap.get();
        return map == null ? null : map.get(key);
    }

    @Override
    public void remove(final String key) {
        final Map map = localMap.get();
        if (map != null) {
            final Map copy = new HashMap<>(map);
            copy.remove(key);
            localMap.set(Collections.unmodifiableMap(copy));
        }
    }

    public void removeAll(final Iterable keys) {
        final Map map = localMap.get();
        if (map != null) {
            final Map copy = new HashMap<>(map);
            for (final String key : keys) {
                copy.remove(key);
            }
            localMap.set(Collections.unmodifiableMap(copy));
        }
    }

    @Override
    public void clear() {
        localMap.remove();
    }

    @Override
    public Map toMap() {
        return getCopy();
    }

    @Override
    public boolean containsKey(final String key) {
        final Map map = localMap.get();
        return map != null && map.containsKey(key);
    }

    @Override
    public  void forEach(final BiConsumer action) {
        final Map map = localMap.get();
        if (map == null) {
            return;
        }
        for (final Map.Entry entry : map.entrySet()) {
            //BiConsumer should be able to handle values of any type V. In our case the values are of type String.
            @SuppressWarnings("unchecked")
            final
            V value = (V) entry.getValue();
            action.accept(entry.getKey(), value);
        }
    }

    @Override
    public  void forEach(final TriConsumer action, final S state) {
        final Map map = localMap.get();
        if (map == null) {
            return;
        }
        for (final Map.Entry entry : map.entrySet()) {
            //TriConsumer should be able to handle values of any type V. In our case the values are of type String.
            @SuppressWarnings("unchecked")
            final
            V value = (V) entry.getValue();
            action.accept(entry.getKey(), value, state);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public  V getValue(final String key) {
        final Map map = localMap.get();
        return (V) (map == null ? null : map.get(key));
    }

    @Override
    public Map getCopy() {
        final Map map = localMap.get();
        return map == null ? new HashMap<>() : new HashMap<>(map);
    }

    @Override
    public Map getImmutableMapOrNull() {
        return localMap.get();
    }

    @Override
    public boolean isEmpty() {
        final Map map = localMap.get();
        return map == null || map.isEmpty();
    }

    @Override
    public int size() {
        final Map map = localMap.get();
        return map == null ? 0 : map.size();
    }

    @Override
    public String toString() {
        final Map map = localMap.get();
        return map == null ? "{}" : map.toString();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        final Map map = this.localMap.get();
        result = prime * result + ((map == null) ? 0 : map.hashCode());
        result = prime * result + Boolean.valueOf(this.useMap).hashCode();
        return result;
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (obj instanceof DefaultThreadContextMap) {
            final DefaultThreadContextMap other = (DefaultThreadContextMap) obj;
            if (this.useMap != other.useMap) {
                return false;
            }
        }
        if (!(obj instanceof ThreadContextMap)) {
            return false;
        }
        final ThreadContextMap other = (ThreadContextMap) obj;
        final Map map = this.localMap.get();
        final Map otherMap = other.getImmutableMapOrNull();
        return Objects.equals(map, otherMap);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy