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.spi; 018 019import java.util.Map; 020 021import org.apache.logging.log4j.ThreadContext; 022import org.apache.logging.log4j.util.StringMap; 023 024/** 025 * Read-only view of the data structure that implements MDC behavior for {@link org.apache.logging.log4j.ThreadContext}. 026 * <p> 027 * {@code ThreadContextMap} implementations that also implement this interface can be accessed 028 * by applications via the {@link ThreadContext#getThreadContextMap()} method. 029 * </p> 030 * 031 * @see ThreadContext#getThreadContextMap() 032 * @since 2.8 033 */ 034public interface ReadOnlyThreadContextMap { 035 036 /** 037 * Clears the context. 038 */ 039 void clear(); 040 041 /** 042 * Determines if the key is in the context. 043 * @param key The key to locate. 044 * @return True if the key is in the context, false otherwise. 045 */ 046 boolean containsKey(final String key); 047 048 /** 049 * Gets the context identified by the <code>key</code> parameter. 050 * 051 * <p>This method has no side effects.</p> 052 * @param key The key to locate. 053 * @return The value associated with the key or null. 054 */ 055 String get(final String key); 056 057 /** 058 * Gets a non-{@code null} mutable copy of current thread's context Map. 059 * @return a mutable copy of the context. 060 */ 061 Map<String, String> getCopy(); 062 063 /** 064 * Returns an immutable view on the context Map or {@code null} if the context map is empty. 065 * @return an immutable context Map or {@code null}. 066 */ 067 Map<String, String> getImmutableMapOrNull(); 068 069 /** 070 * Returns the context data for reading. Note that regardless of whether the returned context data has been 071 * {@linkplain StringMap#freeze() frozen} (made read-only) or not, callers should not attempt to modify 072 * the returned data structure. 073 * <p> 074 * <b>Thread safety note:</b> 075 * </p> 076 * <p> 077 * If this {@code ReadOnlyThreadContextMap} implements {@link CopyOnWrite}, then the returned {@code StringMap} can 078 * safely be passed to another thread: future changes in the underlying context data will not be reflected in the 079 * returned {@code StringMap}. 080 * </p><p> 081 * Otherwise, if this {@code ReadOnlyThreadContextMap} does <em>not</em> implement {@link CopyOnWrite}, then it is 082 * not safe to pass the returned {@code StringMap} to another thread because changes in the underlying context may 083 * be reflected in the returned object. It is the responsibility of the caller to make a copy to pass to another 084 * thread. 085 * </p> 086 * 087 * @return a {@code StringMap} containing context data key-value pairs 088 */ 089 StringMap getReadOnlyContextData(); 090 091 /** 092 * Returns true if the Map is empty. 093 * @return true if the Map is empty, false otherwise. 094 */ 095 boolean isEmpty(); 096}