|
| 1 | +/* |
| 2 | + * Copyright 2025 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 com.google.common.base.Preconditions; |
| 20 | +import com.google.common.collect.ImmutableList; |
| 21 | +import java.io.Closeable; |
| 22 | + |
| 23 | +/** |
| 24 | + * {@code ChannelCredentials} which holds allocated resources (e.g. file watchers) upon |
| 25 | + * instantiation of a given {@code ChannelCredentials} object, which must be closed once |
| 26 | + * mentioned {@code ChannelCredentials} are no longer in use. |
| 27 | + */ |
| 28 | +public final class ResourceAllocatingChannelCredentials extends ChannelCredentials { |
| 29 | + public static ChannelCredentials create( |
| 30 | + ChannelCredentials channelCreds, ImmutableList<Closeable> resources) { |
| 31 | + return new ResourceAllocatingChannelCredentials(channelCreds, resources); |
| 32 | + } |
| 33 | + |
| 34 | + private final ChannelCredentials channelCreds; |
| 35 | + private final ImmutableList<Closeable> resources; |
| 36 | + |
| 37 | + private ResourceAllocatingChannelCredentials( |
| 38 | + ChannelCredentials channelCreds, ImmutableList<Closeable> resources) { |
| 39 | + this.channelCreds = Preconditions.checkNotNull(channelCreds, "channelCreds"); |
| 40 | + this.resources = Preconditions.checkNotNull(resources, "resources"); |
| 41 | + } |
| 42 | + |
| 43 | + public ChannelCredentials getChannelCredentials() { |
| 44 | + return channelCreds; |
| 45 | + } |
| 46 | + |
| 47 | + public ImmutableList<Closeable> getAllocatedResources() { |
| 48 | + return resources; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public ChannelCredentials withoutBearerTokens() { |
| 53 | + return channelCreds.withoutBearerTokens(); |
| 54 | + } |
| 55 | +} |
0 commit comments