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 com.google.common.base.Preconditions; 20 import io.grpc.ManagedChannelProvider.ProviderNotFoundException; 21 22 /** 23 * Provider of servers for transport agnostic consumption. 24 * 25 * <p>Implementations can be automatically discovered by gRPC via Java's SPI mechanism. For 26 * automatic discovery, the implementation must have a zero-argument constructor and include 27 * a resource named {@code META-INF/services/io.grpc.ServerProvider} in their JAR. The 28 * file's contents should be the implementation's class name. 29 * 30 * <p>Implementations <em>should not</em> throw. If they do, it may interrupt class loading. If 31 * exceptions may reasonably occur for implementation-specific reasons, implementations should 32 * generally handle the exception gracefully and return {@code false} from {@link #isAvailable()}. 33 */ 34 @Internal 35 public abstract class ServerProvider { 36 /** 37 * Returns the ClassLoader-wide default server. 38 * 39 * @throws ProviderNotFoundException if no provider is available 40 */ provider()41 public static ServerProvider provider() { 42 ServerProvider provider = ServerRegistry.getDefaultRegistry().provider(); 43 if (provider == null) { 44 throw new ProviderNotFoundException("No functional server found. " 45 + "Try adding a dependency on the grpc-netty or grpc-netty-shaded artifact"); 46 } 47 return provider; 48 } 49 50 /** 51 * Whether this provider is available for use, taking the current environment into consideration. 52 * If {@code false}, no other methods are safe to be called. 53 */ isAvailable()54 protected abstract boolean isAvailable(); 55 56 /** 57 * A priority, from 0 to 10 that this provider should be used, taking the current environment into 58 * consideration. 5 should be considered the default, and then tweaked based on environment 59 * detection. A priority of 0 does not imply that the provider wouldn't work; just that it should 60 * be last in line. 61 */ priority()62 protected abstract int priority(); 63 64 /** 65 * Creates a new builder with the given port. 66 */ builderForPort(int port)67 protected abstract ServerBuilder<?> builderForPort(int port); 68 69 /** 70 * Creates a new builder with the given port and credentials. Returns an error-string result if 71 * unable to understand the credentials. 72 */ newServerBuilderForPort(int port, ServerCredentials creds)73 protected NewServerBuilderResult newServerBuilderForPort(int port, ServerCredentials creds) { 74 return NewServerBuilderResult.error("ServerCredentials are unsupported"); 75 } 76 77 public static final class NewServerBuilderResult { 78 private final ServerBuilder<?> serverBuilder; 79 private final String error; 80 NewServerBuilderResult(ServerBuilder<?> serverBuilder, String error)81 private NewServerBuilderResult(ServerBuilder<?> serverBuilder, String error) { 82 this.serverBuilder = serverBuilder; 83 this.error = error; 84 } 85 serverBuilder(ServerBuilder<?> builder)86 public static NewServerBuilderResult serverBuilder(ServerBuilder<?> builder) { 87 return new NewServerBuilderResult(Preconditions.checkNotNull(builder), null); 88 } 89 error(String error)90 public static NewServerBuilderResult error(String error) { 91 return new NewServerBuilderResult(null, Preconditions.checkNotNull(error)); 92 } 93 getServerBuilder()94 public ServerBuilder<?> getServerBuilder() { 95 return serverBuilder; 96 } 97 getError()98 public String getError() { 99 return error; 100 } 101 } 102 } 103