Unlike most singleton-based filters, each StatefulFilter object has a distinct identity.
| 33 | * Unlike most singleton-based filters, each StatefulFilter object has a distinct identity. |
| 34 | */ |
| 35 | class StatefulFilter implements Filter { |
| 36 | |
| 37 | static final String DEFAULT_TYPE_URL = "type.googleapis.com/grpc.test.StatefulFilter"; |
| 38 | private final AtomicBoolean shutdown = new AtomicBoolean(); |
| 39 | |
| 40 | final int idx; |
| 41 | @Nullable volatile String lastCfg = null; |
| 42 | |
| 43 | public StatefulFilter(int idx) { |
| 44 | this.idx = idx; |
| 45 | } |
| 46 | |
| 47 | public boolean isShutdown() { |
| 48 | return shutdown.get(); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void close() { |
| 53 | if (!shutdown.compareAndSet(false, true)) { |
| 54 | throw new ConcurrentModificationException( |
| 55 | "Unexpected: StatefulFilter#close called multiple times"); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | @Nullable |
| 60 | @Override |
| 61 | public ServerInterceptor buildServerInterceptor( |
| 62 | FilterConfig config, |
| 63 | @Nullable FilterConfig overrideConfig) { |
| 64 | Config cfg = (Config) config; |
| 65 | // TODO(sergiitk): to be replaced when name argument passed to the constructor. |
| 66 | lastCfg = cfg.getConfig(); |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public String toString() { |
| 72 | StringBuilder sb = new StringBuilder().append("StatefulFilter{") |
| 73 | .append("idx=").append(idx); |
| 74 | if (lastCfg != null) { |
| 75 | sb.append(", name=").append(lastCfg); |
| 76 | } |
| 77 | return sb.append("}").toString(); |
| 78 | } |
| 79 | |
| 80 | static final class Provider implements Filter.Provider { |
| 81 | |
| 82 | private final String typeUrl; |
| 83 | private final ConcurrentMap<Integer, StatefulFilter> instances = new ConcurrentHashMap<>(); |
| 84 | |
| 85 | volatile int counter; |
| 86 | |
| 87 | Provider() { |
| 88 | this(DEFAULT_TYPE_URL); |
| 89 | } |
| 90 | |
| 91 | Provider(String typeUrl) { |
| 92 | this.typeUrl = typeUrl; |
nothing calls this directly
no outgoing calls
no test coverage detected