@@ -887,7 +887,9 @@ protected SpannerOptions(Builder builder) {
887887 "Number of channels must fall in the range [1, %s], found: %s" ,
888888 MAX_CHANNELS ,
889889 numChannels );
890-
890+ Preconditions .checkArgument (
891+ builder .instanceType != InstanceType .OMNI || !Strings .isNullOrEmpty (builder .host ),
892+ "Host must be set for connecting to Spanner Omni instances" );
891893 transportChannelExecutorThreadNameFormat = builder .transportChannelExecutorThreadNameFormat ;
892894 channelProvider = builder .channelProvider ;
893895 channelEndpointCacheFactory = builder .channelEndpointCacheFactory ;
@@ -1808,8 +1810,19 @@ public Builder setDecodeMode(DecodeMode decodeMode) {
18081810
18091811 @ Override
18101812 public Builder setHost (String host ) {
1811- super .setHost (host );
18121813 this .host = host ;
1814+ if (this .instanceType == InstanceType .OMNI
1815+ && !Strings .isNullOrEmpty (this .host )
1816+ && this .usePlainText ) {
1817+ Preconditions .checkArgument (
1818+ !this .host .startsWith ("https:" ),
1819+ "Please remove the 'https:' protocol prefix from the host string when using plain text"
1820+ + " communication" );
1821+ if (!this .host .startsWith ("http" )) {
1822+ this .host = "http://" + this .host ;
1823+ }
1824+ }
1825+ super .setHost (this .host );
18131826 // Setting a host should override any SPANNER_EMULATOR_HOST setting.
18141827 setEmulatorHost (null );
18151828 return this ;
@@ -1820,10 +1833,10 @@ public Builder setHost(String host) {
18201833 @ ObsoleteApi ("Use setHost(String).setType(InstanceType.OMNI) instead" )
18211834 @ ExperimentalApi ("https://github.com/googleapis/java-spanner/pull/3676" )
18221835 public Builder setExperimentalHost (String host ) {
1823- setHost (host );
18241836 if (!Strings .isNullOrEmpty (host )) {
18251837 setType (InstanceType .OMNI );
18261838 }
1839+ setHost (host );
18271840 return this ;
18281841 }
18291842
@@ -1835,19 +1848,6 @@ public Builder setExperimentalHost(String host) {
18351848 public Builder setType (InstanceType instanceType ) {
18361849 this .instanceType = instanceType ;
18371850 if (instanceType == InstanceType .OMNI ) {
1838- if (Strings .isNullOrEmpty (this .host )) {
1839- throw new IllegalStateException ("Host must be set before setting Type to OMNI" );
1840- }
1841- if (this .usePlainText ) {
1842- Preconditions .checkArgument (
1843- !this .host .startsWith ("https:" ),
1844- "Please remove the 'https:' protocol prefix from the host string when using plain text"
1845- + " communication" );
1846- if (!this .host .startsWith ("http" )) {
1847- this .host = "http://" + this .host ;
1848- setHost (this .host );
1849- }
1850- }
18511851 setBuiltInMetricsEnabled (false );
18521852 super .setProjectId (SPANNER_OMNI_PROJECT_ID );
18531853 setSessionPoolOption (SessionPoolOptions .newBuilder ().setExperimentalHost ().build ());
@@ -1981,9 +1981,9 @@ public Builder usePlainText() {
19811981 this .usePlainText = true ;
19821982 this .setChannelConfigurator (ManagedChannelBuilder ::usePlaintext )
19831983 .setCredentials (NoCredentials .getInstance ());
1984- if (this .instanceType == InstanceType .OMNI && ! Strings . isNullOrEmpty ( this . host ) ) {
1984+ if (this .instanceType == InstanceType .OMNI ) {
19851985 // Re-apply host settings to ensure http:// is prepended.
1986- setType ( InstanceType . OMNI );
1986+ setHost ( this . host );
19871987 }
19881988 return this ;
19891989 }
0 commit comments