879d9cf09830ece45515af779fd9415d24d10151
[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 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().orElse(
41             new NoOpLoggingContextService());
42
43     private LoggingContext() {
44         // prevent instantiation
45     }
46
47     public static void putRequestId(String requestId) {
48         SERVICE.putRequestId(requestId);
49     }
50
51     public static void putServiceName(String serviceName) {
52         SERVICE.putServiceName(serviceName);
53     }
54
55     public static void putPartnerName(String partnerName) {
56         SERVICE.putPartnerName(partnerName);
57     }
58
59     public static void clear() {
60         SERVICE.clear();
61     }
62
63     public static Runnable copyToRunnable(Runnable runnable) {
64         return SERVICE.copyToRunnable(runnable);
65     }
66
67     public static <V> Callable<V> copyToCallable(Callable<V> callable) {
68         return SERVICE.copyToCallable(callable);
69     }
70
71     private static class NoOpLoggingContextService implements LoggingContextService {
72
73         @Override
74         public void putRequestId(String requestId) {
75             Objects.requireNonNull(requestId, "Request ID cannot be null");
76         }
77
78         @Override
79         public void putServiceName(String serviceName) {
80             Objects.requireNonNull(serviceName, "Service name cannot be null");
81         }
82
83         @Override
84         public void putPartnerName(String partnerName) {
85             Objects.requireNonNull(partnerName, "Partner name cannot be null");
86         }
87
88         @Override
89         public void clear() {
90             // no-op
91         }
92
93         @Override
94         public Runnable copyToRunnable(Runnable runnable) {
95             Objects.requireNonNull(runnable, "Runnable cannot be null");
96             return runnable;
97         }
98
99         @Override
100         public <V> Callable<V> copyToCallable(Callable<V> callable) {
101             Objects.requireNonNull(callable, "Callable cannot be null");
102             return callable;
103         }
104     }
105 }