894dd2c00c1ce3c98b80dc14f7f1e691cad1af53
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 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 java.util.Objects;
20 import java.util.concurrent.Callable;
21 import org.openecomp.sdc.logging.spi.LoggingContextService;
22
23 /**
24  * <p>Factory to hide a concrete, framework-specific implementation of diagnostic context.</p>
25  *
26  * <p>The service used by this factory must implement {@link LoggingContextService}. If no implementation has been
27  * configured or could be instantiated, a <b>no-op context service</b> will be used, and <b>no context</b> will be
28  * stored or propagated. No errors will be generated, so that the application can still work (albeit without proper
29  * logging).</p>
30  *
31  * @author evitaliy
32  * @see ServiceBinder
33  * @see LoggingContextService
34  * @since 07 Jan 2018
35  */
36 public class LoggingContext {
37
38     private static final LoggingContextService SERVICE =
39             ServiceBinder.getContextServiceBinding().orElseGet(NoOpLoggingContextService::new);
40
41     private LoggingContext() {
42         // prevent instantiation
43     }
44
45     public static void put(ContextData contextData) {
46         SERVICE.put(contextData);
47     }
48
49     public static ContextData get() {
50         return SERVICE.get();
51     }
52
53     public static void clear() {
54         SERVICE.clear();
55     }
56
57     public static Runnable copyToRunnable(Runnable runnable) {
58         return SERVICE.copyToRunnable(runnable);
59     }
60
61     public static <V> Callable<V> copyToCallable(Callable<V> callable) {
62         return SERVICE.copyToCallable(callable);
63     }
64
65     private static class NoOpLoggingContextService implements LoggingContextService {
66
67         @Override
68         public void put(ContextData contextData) {
69             Objects.requireNonNull(contextData, "Context data cannot be null");
70         }
71
72         @Override
73         public ContextData get() {
74             return EmptyContextData.INSTANCE;
75         }
76
77         @Override
78         public void clear() {
79             // no-op
80         }
81
82         @Override
83         public Runnable copyToRunnable(Runnable runnable) {
84             Objects.requireNonNull(runnable, "Runnable cannot be null");
85             return runnable;
86         }
87
88         @Override
89         public <V> Callable<V> copyToCallable(Callable<V> callable) {
90             Objects.requireNonNull(callable, "Callable cannot be null");
91             return callable;
92         }
93
94         private static class EmptyContextData {
95
96             private static final ContextData INSTANCE = ContextData.builder().build();
97
98             private EmptyContextData() {
99                 // prevent instantiation
100             }
101         }
102     }
103 }