• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 The gRPC Authors
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 io.grpc;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 
25 /** The collection of global interceptors and global server stream tracers. */
26 @Internal
27 final class GlobalInterceptors {
28   private static List<ClientInterceptor> clientInterceptors = null;
29   private static List<ServerInterceptor> serverInterceptors = null;
30   private static List<ServerStreamTracer.Factory> serverStreamTracerFactories =
31       null;
32   private static boolean isGlobalInterceptorsTracersSet;
33   private static boolean isGlobalInterceptorsTracersGet;
34 
35   // Prevent instantiation
GlobalInterceptors()36   private GlobalInterceptors() {}
37 
38   /**
39    * Sets the list of global interceptors and global server stream tracers.
40    *
41    * <p>If {@code setInterceptorsTracers()} is called again, this method will throw {@link
42    * IllegalStateException}.
43    *
44    * <p>It is only safe to call early. This method throws {@link IllegalStateException} after any of
45    * the get calls [{@link #getClientInterceptors()}, {@link #getServerInterceptors()} or {@link
46    * #getServerStreamTracerFactories()}] has been called, in order to limit changes to the result of
47    * {@code setInterceptorsTracers()}.
48    *
49    * @param clientInterceptorList list of {@link ClientInterceptor} that make up global Client
50    *     Interceptors.
51    * @param serverInterceptorList list of {@link ServerInterceptor} that make up global Server
52    *     Interceptors.
53    * @param serverStreamTracerFactoryList list of {@link ServerStreamTracer.Factory} that make up
54    *     global ServerStreamTracer factories.
55    */
setInterceptorsTracers( List<ClientInterceptor> clientInterceptorList, List<ServerInterceptor> serverInterceptorList, List<ServerStreamTracer.Factory> serverStreamTracerFactoryList)56   static synchronized void setInterceptorsTracers(
57       List<ClientInterceptor> clientInterceptorList,
58       List<ServerInterceptor> serverInterceptorList,
59       List<ServerStreamTracer.Factory> serverStreamTracerFactoryList) {
60     if (isGlobalInterceptorsTracersGet) {
61       throw new IllegalStateException("Set cannot be called after any get call");
62     }
63     if (isGlobalInterceptorsTracersSet) {
64       throw new IllegalStateException("Global interceptors and tracers are already set");
65     }
66     checkNotNull(clientInterceptorList);
67     checkNotNull(serverInterceptorList);
68     checkNotNull(serverStreamTracerFactoryList);
69     clientInterceptors = Collections.unmodifiableList(new ArrayList<>(clientInterceptorList));
70     serverInterceptors = Collections.unmodifiableList(new ArrayList<>(serverInterceptorList));
71     serverStreamTracerFactories =
72           Collections.unmodifiableList(new ArrayList<>(serverStreamTracerFactoryList));
73     isGlobalInterceptorsTracersSet = true;
74   }
75 
76   /** Returns the list of global {@link ClientInterceptor}. If not set, this returns null. */
getClientInterceptors()77   static synchronized List<ClientInterceptor> getClientInterceptors() {
78     isGlobalInterceptorsTracersGet = true;
79     return clientInterceptors;
80   }
81 
82   /** Returns list of global {@link ServerInterceptor}. If not set, this returns null. */
getServerInterceptors()83   static synchronized List<ServerInterceptor> getServerInterceptors() {
84     isGlobalInterceptorsTracersGet = true;
85     return serverInterceptors;
86   }
87 
88   /** Returns list of global {@link ServerStreamTracer.Factory}. If not set, this returns null. */
getServerStreamTracerFactories()89   static synchronized List<ServerStreamTracer.Factory> getServerStreamTracerFactories() {
90     isGlobalInterceptorsTracersGet = true;
91     return serverStreamTracerFactories;
92   }
93 }
94