3ad999f2728f9a713761f40af920ede96e3f6daa
[sdc.git] / openecomp-be / lib / openecomp-logging-lib / openecomp-sdc-logging-api / src / main / java / org / openecomp / sdc / logging / api / context / TaskFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.logging.api.context;
22
23 import org.openecomp.sdc.logging.api.BaseFactory;
24
25 /**
26  * <p>Should be used to propagate a diagnostic context (for instance
27  * <a href="http://www.slf4j.org/manual.html#mdc">MDC</a>) to other threads.</p>
28  *
29  * <p>Applicable when creating a child thread directly, or submitting tasks for potentially postponed execution via
30  * an <a href="http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executor.html">Executor</a> (including
31  * any of the <a href="http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html">executor
32  * services</a> and
33  * <a href="http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html">ForkJoinPool</a>).</p>
34  *
35  * <p>The service used by this factory must implement {@link ContextPropagationService}.</p>
36  *
37  * @author evitaliy
38  * @since 12/09/2016.
39  *
40  * @see ContextPropagationService
41  */
42 @SuppressWarnings("ThrowableInstanceNeverThrown")
43 public class TaskFactory extends BaseFactory {
44
45     private static final ContextPropagationService SERVICE;
46     private static final RuntimeException ERROR;
47
48     static {
49
50         ContextPropagationService service = null;
51         RuntimeException error = null;
52
53         try {
54             service = locateService(ContextPropagationService.class);
55         } catch (Throwable e) {
56             error = new RuntimeException("Failed to instantiate task factory", e);
57         }
58
59         SERVICE = service;
60         ERROR = error;
61     }
62
63     /**
64      * Modify a task so that a diagnostic context is propagated to the thread when the task runs. Done in a
65      * logging-framework specific way.
66      *
67      * @param task any Runnable that will run in a thread
68      * @return modified (wrapped) original task that runs the same business logic, but also takes care of copying the
69      *         diagnostic context for logging
70      */
71     public static Runnable create(Runnable task) {
72
73         if (SERVICE == null) {
74             throw ERROR;
75         }
76
77         return SERVICE.create(task);
78     }
79 }