• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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.ManagedChannelProvider.ProviderNotFoundException;
20 import io.grpc.ServiceProviders.PriorityAccessor;
21 import java.util.Collections;
22 
23 /**
24  * Provider of servers for transport agnostic consumption.
25  *
26  * <p>Implementations <em>should not</em> throw. If they do, it may interrupt class loading. If
27  * exceptions may reasonably occur for implementation-specific reasons, implementations should
28  * generally handle the exception gracefully and return {@code false} from {@link #isAvailable()}.
29  */
30 @Internal
31 public abstract class ServerProvider {
32   private static final ServerProvider provider = ServiceProviders.load(
33       ServerProvider.class,
34       Collections.<Class<?>>emptyList(),
35       ServerProvider.class.getClassLoader(),
36       new PriorityAccessor<ServerProvider>() {
37         @Override
38         public boolean isAvailable(ServerProvider provider) {
39           return provider.isAvailable();
40         }
41 
42         @Override
43         public int getPriority(ServerProvider provider) {
44           return provider.priority();
45         }
46       });
47 
48   /**
49    * Returns the ClassLoader-wide default server.
50    *
51    * @throws ProviderNotFoundException if no provider is available
52    */
provider()53   public static ServerProvider provider() {
54     if (provider == null) {
55       throw new ProviderNotFoundException("No functional server found. "
56           + "Try adding a dependency on the grpc-netty or grpc-netty-shaded artifact");
57     }
58     return provider;
59   }
60 
61   /**
62    * Whether this provider is available for use, taking the current environment into consideration.
63    * If {@code false}, no other methods are safe to be called.
64    */
isAvailable()65   protected abstract boolean isAvailable();
66 
67   /**
68    * A priority, from 0 to 10 that this provider should be used, taking the current environment into
69    * consideration. 5 should be considered the default, and then tweaked based on environment
70    * detection. A priority of 0 does not imply that the provider wouldn't work; just that it should
71    * be last in line.
72    */
priority()73   protected abstract int priority();
74 
75   /**
76    * Creates a new builder with the given port.
77    */
builderForPort(int port)78   protected abstract ServerBuilder<?> builderForPort(int port);
79 }
80