re base code
[sdc.git] / openecomp-be / lib / openecomp-sdc-logging-lib / openecomp-sdc-logging-api / src / main / java / org / openecomp / sdc / logging / api / LoggingContext.java
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 org.openecomp.sdc.logging.spi.LoggingContextService;
20
21 import java.util.Objects;
22 import java.util.concurrent.Callable;
23
24 /**
25  * <p>Factory to hide a concrete, framework-specific implementation of diagnostic context.</p>
26  *
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
30  * logging).</p>
31  *
32  * @author evitaliy
33  * @see ServiceBinder
34  * @see LoggingContextService
35  * @since 07 Jan 2018
36  */
37 public class LoggingContext {
38
39     private static final LoggingContextService SERVICE =
40             ServiceBinder.getContextServiceBinding().orElseGet(NoOpLoggingContextService::new);
41
42     private LoggingContext() {
43         // prevent instantiation
44     }
45
46     public static void put(ContextData contextData) {
47         SERVICE.put(contextData);
48     }
49
50     public static ContextData get() {
51         return SERVICE.get();
52     }
53
54     public static void clear() {
55         SERVICE.clear();
56     }
57
58     public static Runnable copyToRunnable(Runnable runnable) {
59         return SERVICE.copyToRunnable(runnable);
60     }
61
62     public static <V> Callable<V> copyToCallable(Callable<V> callable) {
63         return SERVICE.copyToCallable(callable);
64     }
65
66     private static class NoOpLoggingContextService implements LoggingContextService {
67
68         @Override
69         public void put(ContextData contextData) {
70             Objects.requireNonNull(contextData, "Context data cannot be null");
71         }
72
73         @Override
74         public ContextData get() {
75             return EmptyContextData.INSTANCE;
76         }
77
78         @Override
79         public void clear() {
80             // no-op
81         }
82
83         @Override
84         public Runnable copyToRunnable(Runnable runnable) {
85             Objects.requireNonNull(runnable, "Runnable cannot be null");
86             return runnable;
87         }
88
89         @Override
90         public <V> Callable<V> copyToCallable(Callable<V> callable) {
91             Objects.requireNonNull(callable, "Callable cannot be null");
92             return callable;
93         }
94
95         private static class EmptyContextData {
96
97             private static final ContextData INSTANCE = ContextData.builder().build();
98
99             private EmptyContextData() {
100                 // prevent instantiation
101             }
102         }
103     }
104 }