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.sdc.logging.context;
23 import org.openecomp.sdc.logging.api.context.ContextPropagationService;
29 * Propagates the <a href="http://www.slf4j.org/manual.html#mdc">SLF4J Mapped Diagnostic Context (MDC)</a>
30 * of a thread onto a runnable created by that thread, so that the context is available when the runnable is executed
36 public class MDCPropagationService implements ContextPropagationService {
38 public Runnable create(Runnable task) {
39 return new MDCCopyingWrapper(task);
42 private static class MDCCopyingWrapper implements Runnable {
44 private final Runnable task;
45 private final Map<String, String> context;
47 private MDCCopyingWrapper(Runnable task) {
49 this.context = MDC.getCopyOfContextMap();
55 Map<String, String> oldContext = MDC.getCopyOfContextMap();
56 replaceMDC(this.context);
61 replaceMDC(oldContext);
65 private static void replaceMDC(Map<String, String> context) {
67 if (context == null) {
70 MDC.setContextMap(context);