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.netty; 18 19 import io.grpc.ChannelCredentials; 20 import io.grpc.Internal; 21 import io.grpc.ManagedChannelProvider; 22 import java.net.InetSocketAddress; 23 import java.net.SocketAddress; 24 import java.util.Collection; 25 import java.util.Collections; 26 27 /** Provider for {@link NettyChannelBuilder} instances. */ 28 @Internal 29 public final class NettyChannelProvider extends ManagedChannelProvider { 30 @Override isAvailable()31 public boolean isAvailable() { 32 return true; 33 } 34 35 @Override priority()36 public int priority() { 37 return 5; 38 } 39 40 @Override builderForAddress(String name, int port)41 public NettyChannelBuilder builderForAddress(String name, int port) { 42 return NettyChannelBuilder.forAddress(name, port); 43 } 44 45 @Override builderForTarget(String target)46 public NettyChannelBuilder builderForTarget(String target) { 47 return NettyChannelBuilder.forTarget(target); 48 } 49 50 @Override newChannelBuilder(String target, ChannelCredentials creds)51 public NewChannelBuilderResult newChannelBuilder(String target, ChannelCredentials creds) { 52 ProtocolNegotiators.FromChannelCredentialsResult result = ProtocolNegotiators.from(creds); 53 if (result.error != null) { 54 return NewChannelBuilderResult.error(result.error); 55 } 56 return NewChannelBuilderResult.channelBuilder( 57 new NettyChannelBuilder(target, creds, result.callCredentials, result.negotiator)); 58 } 59 60 @Override getSupportedSocketAddressTypes()61 protected Collection<Class<? extends SocketAddress>> getSupportedSocketAddressTypes() { 62 return Collections.singleton(InetSocketAddress.class); 63 } 64 } 65