55import io .grpc .ChannelCredentials ;
66import io .grpc .Grpc ;
77import io .grpc .ManagedChannel ;
8+ import org .apache .commons .lang3 .StringUtils ;
89import org .slf4j .Logger ;
910import org .slf4j .LoggerFactory ;
1011
@@ -33,8 +34,6 @@ public abstract class ChannelManager implements AutoCloseable {
3334 public static final String MAX_INBOUND_MESSAGE_BYTE_SIZE = GRPC_CHANNEL_PREFIX + "MAX_INBOUND_MESSAGE_BYTE_SIZE" ;
3435 public static final String MAX_INBOUND_METADATA_BYTE_SIZE = GRPC_CHANNEL_PREFIX + "MAX_INBOUND_METADATA_BYTE_SIZE" ;
3536
36- protected static final String LOCALHOST = "localhost" ;
37- protected static final String DNS_PREFIX = "dns:///" ;
3837 protected static final int MAX_PORT_NUMBER = 0xFFFF ;
3938
4039 protected final Logger logger ;
@@ -65,9 +64,9 @@ public abstract class ChannelManager implements AutoCloseable {
6564 protected ChannelManager (String host , int port , Configurator configG , ChannelCredentials credentials ) {
6665 this .logger = LoggerFactory .getLogger (this .getClass ().getName ());
6766
68- this .host = validateHostName ( host ) ;
69- this .port = validatePortNumber ( port ) ;
70- this .target = createTarget (host , port );
67+ this .host = host ;
68+ this .port = port ;
69+ this .target = createTarget ();
7170
7271 // How often (in milliseconds) to send pings when the connection is idle
7372 this .keepAliveMillis = configG .findLongEntry (KEEP_ALIVE_MILLIS , 60000L );
@@ -91,23 +90,14 @@ protected ChannelManager(String host, int port, Configurator configG, ChannelCre
9190 this .channelCredentials = credentials ;
9291 }
9392
94- protected String validateHostName ( String host ) {
95- if (host . equals ( LOCALHOST ) || host . startsWith ( DNS_PREFIX )) {
96- return host ;
93+ private String createTarget ( ) {
94+ if (StringUtils . isEmpty ( host )) {
95+ throw new IllegalArgumentException ( "Missing required gRPC host configuration" ) ;
9796 }
98- throw new IllegalArgumentException (
99- String .format ("Expected \" %s\" or DNS URI prefix \" %s\" but got \" %s\" " , LOCALHOST , DNS_PREFIX , host ));
100- }
101-
102- protected int validatePortNumber (int port ) {
103- if (port > 0 && port <= MAX_PORT_NUMBER ) {
104- return port ;
97+ if (port <= 0 || port > MAX_PORT_NUMBER ) {
98+ throw new IllegalArgumentException (
99+ String .format ("Port \" %d\" is outside valid range [1, %d]" , port , MAX_PORT_NUMBER ));
105100 }
106- throw new IllegalArgumentException (
107- String .format ("Port \" %d\" is outside valid range [1, %d]" , port , MAX_PORT_NUMBER ));
108- }
109-
110- protected String createTarget (String host , int port ) {
111101 return host + ":" + port ;
112102 }
113103
0 commit comments