1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache license, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the license for the specific language governing permissions and 15 * limitations under the license. 16 */ 17 package org.apache.logging.log4j.spi; 18 19 import java.util.Map; 20 21 import org.apache.logging.log4j.ThreadContext; 22 23 /** 24 * Service provider interface to implement custom MDC behavior for {@link org.apache.logging.log4j.ThreadContext}. 25 * <p> 26 * Since 2.8, {@code ThreadContextMap} implementations that implement the {@link ReadOnlyThreadContextMap} interface 27 * are accessible to applications via the {@link ThreadContext#getThreadContextMap()} method. 28 * </p> 29 */ 30 public interface ThreadContextMap { 31 32 /** 33 * Clears the context. 34 */ 35 void clear(); 36 37 /** 38 * Determines if the key is in the context. 39 * @param key The key to locate. 40 * @return True if the key is in the context, false otherwise. 41 */ 42 boolean containsKey(final String key); 43 44 /** 45 * Gets the context identified by the <code>key</code> parameter. 46 * 47 * <p>This method has no side effects.</p> 48 * @param key The key to locate. 49 * @return The value associated with the key or null. 50 */ 51 String get(final String key); 52 53 /** 54 * Gets a non-{@code null} mutable copy of current thread's context Map. 55 * @return a mutable copy of the context. 56 */ 57 Map<String, String> getCopy(); 58 59 /** 60 * Returns an immutable view on the context Map or {@code null} if the context map is empty. 61 * @return an immutable context Map or {@code null}. 62 */ 63 Map<String, String> getImmutableMapOrNull(); 64 65 /** 66 * Returns true if the Map is empty. 67 * @return true if the Map is empty, false otherwise. 68 */ 69 boolean isEmpty(); 70 71 /** 72 * Puts a context value (the <code>o</code> parameter) as identified 73 * with the <code>key</code> parameter into the current thread's 74 * context map. 75 * 76 * <p>If the current thread does not have a context map it is 77 * created as a side effect.</p> 78 * @param key The key name. 79 * @param value The key value. 80 */ 81 void put(final String key, final String value); 82 83 /** 84 * Removes the context identified by the <code>key</code> 85 * parameter. 86 * @param key The key to remove. 87 */ 88 void remove(final String key); 89 }