forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalInterceptors.java
106 lines (94 loc) · 4.06 KB
/
GlobalInterceptors.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright 2022 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/** The collection of global interceptors and global server stream tracers. */
@Internal
final class GlobalInterceptors {
private static List<ClientInterceptor> clientInterceptors = Collections.emptyList();
private static List<ServerInterceptor> serverInterceptors = Collections.emptyList();
private static List<ServerStreamTracer.Factory> serverStreamTracerFactories =
Collections.emptyList();
private static boolean isGlobalInterceptorsTracersSet;
private static boolean isGlobalInterceptorsTracersGet;
// Prevent instantiation
private GlobalInterceptors() {}
static boolean isIsGlobalInterceptorsTracersSet() {
return isGlobalInterceptorsTracersSet;
}
/**
* Sets the list of global interceptors and global server stream tracers.
*
* <p>If {@code setInterceptorsTracers()} is called again, this method will throw {@link
* IllegalStateException}.
*
* <p>It is only safe to call early. This method throws {@link IllegalStateException} after any of
* the get calls [{@link #getClientInterceptors()}, {@link #getServerInterceptors()} or {@link
* #getServerStreamTracerFactories()}] has been called, in order to limit changes to the result of
* {@code setInterceptorsTracers()}.
*
* @param clientInterceptorList list of {@link ClientInterceptor} that make up global Client
* Interceptors.
* @param serverInterceptorList list of {@link ServerInterceptor} that make up global Server
* Interceptors.
* @param serverStreamTracerFactoryList list of {@link ServerStreamTracer.Factory} that make up
* global ServerStreamTracer factories.
*/
static synchronized void setInterceptorsTracers(
List<ClientInterceptor> clientInterceptorList,
List<ServerInterceptor> serverInterceptorList,
List<ServerStreamTracer.Factory> serverStreamTracerFactoryList) {
if (isGlobalInterceptorsTracersGet) {
throw new IllegalStateException("Set cannot be called after any get call");
}
if (isGlobalInterceptorsTracersSet) {
throw new IllegalStateException("Global interceptors and tracers are already set");
}
if (clientInterceptorList != null) {
clientInterceptors = Collections.unmodifiableList(new ArrayList<>(clientInterceptorList));
}
if (serverInterceptorList != null) {
serverInterceptors = Collections.unmodifiableList(new ArrayList<>(serverInterceptorList));
}
if (serverStreamTracerFactoryList != null) {
serverStreamTracerFactories =
Collections.unmodifiableList(new ArrayList<>(serverStreamTracerFactoryList));
}
isGlobalInterceptorsTracersSet = true;
}
/**
* Returns the list of global {@link ClientInterceptor}. If not set, this returns am empty list.
*/
static synchronized List<ClientInterceptor> getClientInterceptors() {
isGlobalInterceptorsTracersGet = true;
return clientInterceptors;
}
/** Returns list of global {@link ServerInterceptor}. If not set, this returns an empty list. */
static synchronized List<ServerInterceptor> getServerInterceptors() {
isGlobalInterceptorsTracersGet = true;
return serverInterceptors;
}
/**
* Returns list of global {@link ServerStreamTracer.Factory}. If not set, this returns an empty
* list.
*/
static synchronized List<ServerStreamTracer.Factory> getServerStreamTracerFactories() {
isGlobalInterceptorsTracersGet = true;
return serverStreamTracerFactories;
}
}