2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.logging.slf4j;
19 import java.util.EnumMap;
21 import org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField;
25 * Because we don't know which information should be carried over from MDC, and which shouldn't, copy just the keys that
26 * the logging service uses.
31 abstract class BaseMDCCopyingWrapper {
33 private final Map<ContextField, String> context;
35 BaseMDCCopyingWrapper() {
36 this.context = fromMdc();
39 final Map<ContextField, String> replace() {
40 Map<ContextField, String> old = fromMdc();
45 final void revert(Map<ContextField, String> old) {
49 private Map<ContextField, String> fromMdc() {
51 Map<ContextField, String> copy = new EnumMap<>(ContextField.class);
52 for (ContextField k : ContextField.values()) {
53 String v = MDC.get(k.asKey());
62 private static void toMdc(Map<ContextField, String> context) {
64 for (ContextField k : ContextField.values()) {
65 String v = context.get(k);
67 MDC.put(k.asKey(), v);
69 MDC.remove(k.asKey());