Java Code Examples for org.apache.logging.log4j.util.StringMap#freeze()

The following examples show how to use org.apache.logging.log4j.util.StringMap#freeze() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CopyOnWriteSortedArrayThreadContextMap.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private ThreadLocal<StringMap> createThreadLocalMap() {
    if (inheritableMap) {
        return new InheritableThreadLocal<StringMap>() {
            @Override
            protected StringMap childValue(final StringMap parentValue) {
                if (parentValue == null) {
                    return null;
                }
                StringMap stringMap = createStringMap(parentValue);
                stringMap.freeze();
                return stringMap;
            }
        };
    }
    // if not inheritable, return plain ThreadLocal with null as initial value
    return new ThreadLocal<>();
}
 
Example 2
Source File: CopyOnWriteSortedArrayThreadContextMap.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void putValue(final String key, final Object value) {
    StringMap map = localMap.get();
    map = map == null ? createStringMap() : createStringMap(map);
    map.putValue(key, value);
    map.freeze();
    localMap.set(map);
}
 
Example 3
Source File: CopyOnWriteSortedArrayThreadContextMap.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void putAll(final Map<String, String> values) {
    if (values == null || values.isEmpty()) {
        return;
    }
    StringMap map = localMap.get();
    map = map == null ? createStringMap() : createStringMap(map);
    for (final Map.Entry<String, String> entry : values.entrySet()) {
        map.putValue(entry.getKey(), entry.getValue());
    }
    map.freeze();
    localMap.set(map);
}
 
Example 4
Source File: CopyOnWriteSortedArrayThreadContextMap.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public <V> void putAllValues(final Map<String, V> values) {
    if (values == null || values.isEmpty()) {
        return;
    }
    StringMap map = localMap.get();
    map = map == null ? createStringMap() : createStringMap(map);
    for (final Map.Entry<String, V> entry : values.entrySet()) {
        map.putValue(entry.getKey(), entry.getValue());
    }
    map.freeze();
    localMap.set(map);
}
 
Example 5
Source File: CopyOnWriteSortedArrayThreadContextMap.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void remove(final String key) {
    final StringMap map = localMap.get();
    if (map != null) {
        final StringMap copy = createStringMap(map);
        copy.remove(key);
        copy.freeze();
        localMap.set(copy);
    }
}
 
Example 6
Source File: CopyOnWriteSortedArrayThreadContextMap.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void removeAll(final Iterable<String> keys) {
    final StringMap map = localMap.get();
    if (map != null) {
        final StringMap copy = createStringMap(map);
        for (final String key : keys) {
            copy.remove(key);
        }
        copy.freeze();
        localMap.set(copy);
    }
}
 
Example 7
Source File: ThreadContextDataInjector.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Puts key-value pairs from both the specified list of properties as well as the thread context into the
 * specified reusable StringMap.
 *
 * @param props list of configuration properties, may be {@code null}
 * @param contextData a {@code StringMap} instance from the log event
 * @return a {@code StringMap} combining configuration properties with thread context data
 */
@Override
public StringMap injectContextData(final List<Property> props, final StringMap contextData) {

    final Map<String, String> copy;

    if (providers.size() == 1) {
        copy = providers.get(0).supplyContextData();
    } else {
        copy = new HashMap<>();
        for (ContextDataProvider provider : providers) {
            copy.putAll(provider.supplyContextData());
        }
    }

    // The DefaultThreadContextMap stores context data in a Map<String, String>.
    // This is a copy-on-write data structure so we are sure ThreadContext changes will not affect our copy.
    // If there are no configuration properties or providers returning a thin wrapper around the copy
    // is faster than copying the elements into the LogEvent's reusable StringMap.
    if ((props == null || props.isEmpty())) {
        // this will replace the LogEvent's context data with the returned instance.
        // NOTE: must mark as frozen or downstream components may attempt to modify (UnsupportedOperationEx)
        return copy.isEmpty() ? ContextDataFactory.emptyFrozenContextData() : frozenStringMap(copy);
    }
    // If the list of Properties is non-empty we need to combine the properties and the ThreadContext
    // data. Note that we cannot reuse the specified StringMap: some Loggers may have properties defined
    // and others not, so the LogEvent's context data may have been replaced with an immutable copy from
    // the ThreadContext - this will throw an UnsupportedOperationException if we try to modify it.
    final StringMap result = new JdkMapAdapterStringMap(new HashMap<>(copy));
    for (int i = 0; i < props.size(); i++) {
        final Property prop = props.get(i);
        if (!copy.containsKey(prop.getName())) {
            result.putValue(prop.getName(), prop.getValue());
        }
    }
    result.freeze();
    return result;
}
 
Example 8
Source File: ServiceTalkTracingThreadContextMap.java    From servicetalk with Apache License 2.0 4 votes vote down vote up
@Override
public StringMap getReadOnlyContextData() {
    StringMap map = new JdkMapAdapterStringMap(getCopy());
    map.freeze();
    return map;
}