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.api;
19 import org.openecomp.sdc.logging.spi.LoggingContextService;
21 import java.util.Objects;
22 import java.util.concurrent.Callable;
25 * <a>Factory to hide a concrete, framework-specific implementation of diagnostic context.</a>
26 * <p>The service used by this factory must implement {@link LoggingContextService}. If no
27 * implementation has been configured or could be instantiated, a <b>no-op context service</b> will be
28 * used, and <b>no context</b> will be stored or propagated. No errors will be generated, so that the application can
29 * still work (albeit without proper logging).</p>
35 * @see LoggingContextService
37 public class LoggingContext {
39 private static final LoggingContextService SERVICE = ServiceBinder.getContextServiceBinding().orElse(
40 new NoOpLoggingContextService());
42 private LoggingContext() {
43 // prevent instantiation
46 public static void put(String key, String value) {
47 SERVICE.put(key, value);
50 public static String get(String key) {
51 return SERVICE.get(key);
54 public static void remove(String key) {
58 public static void clear() {
62 public static Runnable copyToRunnable(Runnable runnable) {
63 return SERVICE.copyToRunnable(runnable);
66 public static <V> Callable<V> copyToCallable(Callable<V> callable) {
67 return SERVICE.copyToCallable(callable);
70 private static class NoOpLoggingContextService implements LoggingContextService {
72 private static final String KEY_CANNOT_BE_NULL = "Key cannot be null";
75 public void put(String key, String value) {
76 Objects.requireNonNull(key, KEY_CANNOT_BE_NULL);
81 public String get(String key) {
82 Objects.requireNonNull(key, KEY_CANNOT_BE_NULL);
87 public void remove(String key) {
88 Objects.requireNonNull(key, KEY_CANNOT_BE_NULL);
98 public Runnable copyToRunnable(Runnable runnable) {
99 Objects.requireNonNull(runnable, "Runnable cannot be null");
104 public <V> Callable<V> copyToCallable(Callable<V> callable) {
105 Objects.requireNonNull(callable, "Callable cannot be null");