2 * Copyright © 2016-2018 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.api;
19 import org.openecomp.sdc.logging.spi.LoggingContextService;
21 import java.util.Objects;
22 import java.util.concurrent.Callable;
25 * <p>Factory to hide a concrete, framework-specific implementation of diagnostic context.</p>
27 * <p>The service used by this factory must implement {@link LoggingContextService}. If no implementation has been
28 * configured or could be instantiated, a <b>no-op context service</b> will be used, and <b>no context</b> will be
29 * stored or propagated. No errors will be generated, so that the application can still work (albeit without proper
34 * @see LoggingContextService
37 public class LoggingContext {
39 private static final LoggingContextService SERVICE =
40 ServiceBinder.getContextServiceBinding().orElseGet(NoOpLoggingContextService::new);
42 private LoggingContext() {
43 // prevent instantiation
46 public static void put(ContextData contextData) {
47 SERVICE.put(contextData);
50 public static ContextData get() {
54 public static void clear() {
58 public static Runnable copyToRunnable(Runnable runnable) {
59 return SERVICE.copyToRunnable(runnable);
62 public static <V> Callable<V> copyToCallable(Callable<V> callable) {
63 return SERVICE.copyToCallable(callable);
66 private static class NoOpLoggingContextService implements LoggingContextService {
69 public void put(ContextData contextData) {
70 Objects.requireNonNull(contextData, "Context data cannot be null");
74 public ContextData get() {
75 return EmptyContextData.INSTANCE;
84 public Runnable copyToRunnable(Runnable runnable) {
85 Objects.requireNonNull(runnable, "Runnable cannot be null");
90 public <V> Callable<V> copyToCallable(Callable<V> callable) {
91 Objects.requireNonNull(callable, "Callable cannot be null");
95 private static class EmptyContextData {
97 private static final ContextData INSTANCE = ContextData.builder().build();
99 private EmptyContextData() {
100 // prevent instantiation