/*
 * Copyright 2016 Spotify AB.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */


package com.spotify.scio.bigtable;

import com.google.cloud.bigtable.config.BigtableOptions;
import com.google.cloud.bigtable.grpc.BigtableSession;
import com.google.cloud.bigtable.grpc.io.ChannelPool;
import com.google.cloud.bigtable.grpc.io.CredentialInterceptorCache;
import io.grpc.ClientInterceptor;

import java.io.IOException;

public class ChannelPoolCreator {
  public static final BigtableOptions options = new BigtableOptions.Builder().build();
  public static ClientInterceptor interceptor;

  static {
    try {
      interceptor = CredentialInterceptorCache.getInstance()
              .getCredentialsInterceptor(options.getCredentialOptions(), options.getRetryOptions());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static ChannelPool createPool(final BigtableOptions options) throws IOException {
    return new ChannelPool(() ->
        BigtableSession.createNettyChannel(options.getAdminHost(), options, interceptor), 1);
  }
}

@@ -962,29 +962,30 @@
 reator %7B%0A  p
-ublic
+rivate
  static fina
@@ -1011,20 +1011,16 @@
 ptions =
- new
  Bigtabl
@@ -1028,17 +1028,17 @@
 Options.
-B
+b
 uilder()
@@ -1046,29 +1046,30 @@
 build();%0A  p
-ublic
+rivate
  static Clie
@@ -1081,16 +1081,18 @@
 erceptor
+%5B%5D
  interce
@@ -1095,16 +1095,17 @@
 erceptor
+s
 ;%0A%0A  sta
@@ -1126,16 +1126,40 @@
 %7B%0A      
+final ClientInterceptor 
 intercep
@@ -1190,16 +1190,25 @@
 torCache
+%0A        
 .getInst
@@ -1214,22 +1214,16 @@
 tance()%0A
-      
         
@@ -1308,16 +1308,289 @@
 ions());
+%0A%0A      // If credentials are unset (i.e. via local emulator), CredentialsInterceptor will return null%0A      if (interceptor == null) %7B%0A        interceptors = new ClientInterceptor%5B%5D %7B%7D;%0A      %7D else %7B%0A        interceptors = new ClientInterceptor%5B%5D %7B interceptor %7D;%0A      %7D
 %0A    %7D c
@@ -1859,16 +1859,17 @@
 erceptor
+s
 ), 1);%0A 

fix NPE in scio-bigtable's ChannelPoolCreator when credentials aren't set (#2317)