Open Kilda Java Documentation
RequestCorrelationId.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.northbound.utils;
17 
19 
20 import java.io.Closeable;
21 import java.util.Optional;
22 
26 public final class RequestCorrelationId {
27 
28  private static final InheritableThreadLocal<String> ID = new InheritableThreadLocal<>();
29 
30  public static String getId() {
31  return Optional.ofNullable(ID.get()).orElse(DEFAULT_CORRELATION_ID);
32  }
33 
41  public static RequestCorrelationClosable create(String correlationId) {
42  ID.set(correlationId);
43 
44  return new RequestCorrelationClosable();
45  }
46 
47  public static String chain(String inner, String outer) {
48  return String.join(" : ", inner, outer);
49  }
50 
51  public static class RequestCorrelationClosable implements Closeable {
52 
53  public void close() {
54  ID.remove();
55  }
56  }
57 
58  private RequestCorrelationId() {
59  }
60 }
static RequestCorrelationClosable create(String correlationId)
static final String DEFAULT_CORRELATION_ID
Definition: Utils.java:69
static String chain(String inner, String outer)