f827fec50f744b3d99bf5a726f09700321d8e71d
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.logging.api;
18
19 import org.openecomp.sdc.logging.spi.LoggingContextService;
20
21 import java.util.Objects;
22 import java.util.concurrent.Callable;
23
24 /**
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>
30  *
31  * @author evitaliy
32  * @since 07/01/2018.
33  *
34  * @see ServiceBinder
35  * @see LoggingContextService
36  */
37 public class LoggingContext {
38
39     private static final LoggingContextService SERVICE = ServiceBinder.getContextServiceBinding().orElse(
40             new NoOpLoggingContextService());
41
42     private LoggingContext() {
43         // prevent instantiation
44     }
45
46     public static void put(String key, String value) {
47         SERVICE.put(key, value);
48     }
49
50     public static String get(String key) {
51         return SERVICE.get(key);
52     }
53
54     public static void remove(String key) {
55         SERVICE.remove(key);
56     }
57
58     public static void clear() {
59         SERVICE.clear();
60     }
61
62     public static Runnable copyToRunnable(Runnable runnable) {
63         return SERVICE.copyToRunnable(runnable);
64     }
65
66     public static <V> Callable<V> copyToCallable(Callable<V> callable) {
67         return SERVICE.copyToCallable(callable);
68     }
69
70     private static class NoOpLoggingContextService implements LoggingContextService {
71
72         private static final String KEY_CANNOT_BE_NULL = "Key cannot be null";
73
74         @Override
75         public void put(String key, String value) {
76             Objects.requireNonNull(key, KEY_CANNOT_BE_NULL);
77             // no-op
78         }
79
80         @Override
81         public String get(String key) {
82             Objects.requireNonNull(key, KEY_CANNOT_BE_NULL);
83             return null;
84         }
85
86         @Override
87         public void remove(String key) {
88             Objects.requireNonNull(key, KEY_CANNOT_BE_NULL);
89             // no-op
90         }
91
92         @Override
93         public void clear() {
94             // no-op
95         }
96
97         @Override
98         public Runnable copyToRunnable(Runnable runnable) {
99             Objects.requireNonNull(runnable, "Runnable cannot be null");
100             return runnable;
101         }
102
103         @Override
104         public <V> Callable<V> copyToCallable(Callable<V> callable) {
105             Objects.requireNonNull(callable, "Callable cannot be null");
106             return callable;
107         }
108     }
109 }