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.context;
19 import org.openecomp.sdc.logging.api.context.ContextPropagationService;
25 * Propagates the <a href="http://www.slf4j.org/manual.html#mdc">SLF4J Mapped Diagnostic Context (MDC)</a>
26 * of a thread onto a runnable created by that thread, so that the context is available when the runnable is executed
32 public class MDCPropagationService implements ContextPropagationService {
34 public Runnable create(Runnable task) {
35 return new MDCCopyingWrapper(task);
38 private static class MDCCopyingWrapper implements Runnable {
40 private final Runnable task;
41 private final Map<String, String> context;
43 private MDCCopyingWrapper(Runnable task) {
45 this.context = MDC.getCopyOfContextMap();
51 Map<String, String> oldContext = MDC.getCopyOfContextMap();
52 replaceMDC(this.context);
57 replaceMDC(oldContext);
61 private static void replaceMDC(Map<String, String> context) {
63 if (context == null) {
66 MDC.setContextMap(context);