• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 io.grpc.NameResolver.Factory;
20 import java.net.InetSocketAddress;
21 import java.net.SocketAddress;
22 import java.util.Collection;
23 import java.util.Collections;
24 
25 /**
26  * Provider of name resolvers for name agnostic consumption.
27  *
28  * <p>Implementations can be automatically discovered by gRPC via Java's SPI mechanism. For
29  * automatic discovery, the implementation must have a zero-argument constructor and include
30  * a resource named {@code META-INF/services/io.grpc.NameResolverProvider} in their JAR. The
31  * file's contents should be the implementation's class name. Implementations that need arguments in
32  * their constructor can be manually registered by {@link NameResolverRegistry#register}.
33  *
34  * <p>Implementations <em>should not</em> throw. If they do, it may interrupt class loading. If
35  * exceptions may reasonably occur for implementation-specific reasons, implementations should
36  * generally handle the exception gracefully and return {@code false} from {@link #isAvailable()}.
37  */
38 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/4159")
39 public abstract class NameResolverProvider extends NameResolver.Factory {
40   /**
41    * Whether this provider is available for use, taking the current environment into consideration.
42    * If {@code false}, no other methods are safe to be called.
43    *
44    * @since 1.0.0
45    */
isAvailable()46   protected abstract boolean isAvailable();
47 
48   /**
49    * A priority, from 0 to 10 that this provider should be used, taking the current environment into
50    * consideration. 5 should be considered the default, and then tweaked based on environment
51    * detection. A priority of 0 does not imply that the provider wouldn't work; just that it should
52    * be last in line.
53    *
54    * @since 1.0.0
55    */
priority()56   protected abstract int priority();
57 
58   /**
59    * Returns the scheme associated with the provider. The provider normally should only create a
60    * {@link NameResolver} when target URI scheme matches the provider scheme. It temporarily
61    * delegates to {@link Factory#getDefaultScheme()} before {@link NameResolver.Factory} is
62    * deprecated in https://github.com/grpc/grpc-java/issues/7133.
63    *
64    * <p>The scheme should be lower-case.
65    *
66    * @since 1.40.0
67    * */
getScheme()68   protected String getScheme() {
69     return getDefaultScheme();
70   }
71 
72   /**
73    * Returns the {@link SocketAddress} types this provider's name-resolver is capable of producing.
74    * This enables selection of the appropriate {@link ManagedChannelProvider} for a channel.
75    *
76    * @return the {@link SocketAddress} types this provider's name-resolver is capable of producing.
77    */
getProducedSocketAddressTypes()78   protected Collection<Class<? extends SocketAddress>> getProducedSocketAddressTypes() {
79     return Collections.singleton(InetSocketAddress.class);
80   }
81 }
82