Skip to content

Commit 52c06c0

Browse files
committed
New aggregate type for ChannelCredentials
1 parent 5a54372 commit 52c06c0

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.common.collect.ImmutableList;
22+
import java.io.Closeable;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
27+
/** Tests for {@link ResourceAllocatingChannelCredentials}. */
28+
@RunWith(JUnit4.class)
29+
public class ResourceAllocatingChannelCredentialsTest {
30+
@Test
31+
public void withoutBearerTokenDelegatesCall() {
32+
ChannelCredentials channelChreds = new ChannelCredentials() {
33+
@Override
34+
public ChannelCredentials withoutBearerTokens() {
35+
return this;
36+
}
37+
};
38+
ImmutableList<Closeable> resources = ImmutableList.<Closeable>of();
39+
ChannelCredentials creds =
40+
ResourceAllocatingChannelCredentials.create(channelChreds, resources);
41+
assertThat(creds.withoutBearerTokens()).isEqualTo(channelChreds);
42+
}
43+
}

0 commit comments

Comments
 (0)