[SDC] Onboarding 1710 rebase.
[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 <a
27  * href="http://www.slf4j.org/manual.html#mdc">MDC</a>) to other threads.</p>
28  * <p>Applicable when creating a child thread directly, or submitting tasks for potentially
29  * postponed execution via an <a href="http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executor.html">Executor</a>
30  * (including any of the <a href="http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html">executor
31  * services</a> and <a href="http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html">ForkJoinPool</a>).</p>
32  * <p>The service used by this factory must implement {@link ContextPropagationService}.</p>
33  *
34  * @author evitaliy
35  * @see ContextPropagationService
36  * @since 12/09/2016.
37  */
38 @SuppressWarnings("ThrowableInstanceNeverThrown")
39 public class TaskFactory extends BaseFactory {
40
41   private static final ContextPropagationService SERVICE;
42   private static final RuntimeException ERROR;
43
44   static {
45
46     ContextPropagationService service = null;
47     RuntimeException error = null;
48
49     try {
50       service = locateService(ContextPropagationService.class);
51     } catch (Exception ex) {
52       error = new RuntimeException("Failed to instantiate task factory", ex);
53     }
54
55     SERVICE = service;
56     ERROR = error;
57   }
58
59   /**
60    * Modify a task so that a diagnostic context is propagated to the thread when the task runs. Done
61    * in a logging-framework specific way.
62    *
63    * @param task any Runnable that will run in a thread
64    * @return modified (wrapped) original task that runs the same business logic, but also takes care
65    * of copying the diagnostic context for logging
66    */
67   public static Runnable create(Runnable task) {
68
69     if (SERVICE == null) {
70       throw ERROR;
71     }
72
73     return SERVICE.create(task);
74   }
75 }