• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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.googleapis;
18 
19 import com.google.common.base.Preconditions;
20 import io.grpc.Internal;
21 import io.grpc.NameResolver;
22 import io.grpc.NameResolver.Args;
23 import io.grpc.NameResolverProvider;
24 import io.grpc.internal.GrpcUtil;
25 import io.grpc.xds.InternalSharedXdsClientPoolProvider;
26 import java.net.InetSocketAddress;
27 import java.net.SocketAddress;
28 import java.net.URI;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.Map;
32 
33 /**
34  * A provider for {@link GoogleCloudToProdNameResolver}.
35  */
36 @Internal
37 public final class GoogleCloudToProdNameResolverProvider extends NameResolverProvider {
38 
39   private static final String SCHEME = "google-c2p";
40 
41   private final String scheme;
42 
GoogleCloudToProdNameResolverProvider()43   public GoogleCloudToProdNameResolverProvider() {
44     this(SCHEME);
45   }
46 
GoogleCloudToProdNameResolverProvider(String scheme)47   GoogleCloudToProdNameResolverProvider(String scheme) {
48     this.scheme = Preconditions.checkNotNull(scheme, "scheme");
49   }
50 
51   @Override
newNameResolver(URI targetUri, Args args)52   public NameResolver newNameResolver(URI targetUri, Args args) {
53     if (scheme.equals(targetUri.getScheme())) {
54       return new GoogleCloudToProdNameResolver(
55           targetUri, args, GrpcUtil.SHARED_CHANNEL_EXECUTOR,
56           new SharedXdsClientPoolProviderBootstrapSetter());
57     }
58     return null;
59   }
60 
61   @Override
getDefaultScheme()62   public String getDefaultScheme() {
63     return scheme;
64   }
65 
66   @Override
isAvailable()67   protected boolean isAvailable() {
68     return true;
69   }
70 
71   @Override
priority()72   protected int priority() {
73     return 4;
74   }
75 
76   @Override
getProducedSocketAddressTypes()77   protected Collection<Class<? extends SocketAddress>> getProducedSocketAddressTypes() {
78     return Collections.singleton(InetSocketAddress.class);
79   }
80 
81   private static final class SharedXdsClientPoolProviderBootstrapSetter
82       implements GoogleCloudToProdNameResolver.BootstrapSetter {
83     @Override
setBootstrap(Map<String, ?> bootstrap)84     public void setBootstrap(Map<String, ?> bootstrap) {
85       InternalSharedXdsClientPoolProvider.setDefaultProviderBootstrapOverride(bootstrap);
86     }
87   }
88 }
89