2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.core.logging.context;
23 import org.openecomp.core.logging.api.context.ContextPropagationService;
29 * Propagates the <a href="http://www.slf4j.org/manual.html#mdc">SLF4J Mapped Diagnostic Context
30 * (MDC)</a> of a thread onto a runnable created by that thread, so that the context is available
31 * when the runnable is executed in a new thread.
33 public class MdcPropagationService implements ContextPropagationService {
35 public Runnable create(Runnable task) {
36 return new MdcCopyingWrapper(task);
39 private static class MdcCopyingWrapper implements Runnable {
41 private final Runnable task;
42 private final Map<String, String> context;
44 private MdcCopyingWrapper(Runnable task) {
46 this.context = MDC.getCopyOfContextMap();
49 private static void replaceMdc(Map<String, String> context) {
51 if (context == null) {
54 MDC.setContextMap(context);
61 Map<String, String> oldContext = MDC.getCopyOfContextMap();
62 replaceMdc(this.context);
67 replaceMdc(oldContext);