/*
 * 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 {
  private static final BigtableOptions options = BigtableOptions.builder().build();
  private static ClientInterceptor[] interceptors;

  static {
    try {
      final ClientInterceptor interceptor =
          CredentialInterceptorCache.getInstance()
              .getCredentialsInterceptor(options.getCredentialOptions(), options.getRetryOptions());

      // If credentials are unset (i.e. via local emulator), CredentialsInterceptor will return null
      if (interceptor == null) {
        interceptors = new ClientInterceptor[] {};
      } else {
        interceptors = new ClientInterceptor[] {interceptor};
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

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

@@ -898,17 +898,16 @@
 ceptor;%0A
-%0A
 import j
@@ -925,16 +925,73 @@
 ception;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Apublic
@@ -1045,69 +1045,73 @@
 nal 
-BigtableOptions options = BigtableOptions.builder().build(
+Logger LOG = LoggerFactory.getLogger(ChannelPoolCreator.class
 );%0A
+%0A
   pr
@@ -1143,17 +1143,26 @@
 eptor%5B%5D 
-i
+getClientI
 ntercept
@@ -1168,19 +1168,39 @@
 tors
-;%0A%0A  static
+(final BigtableOptions options)
  %7B%0A 
@@ -1404,17 +1404,16 @@
 ons());%0A
-%0A
       //
@@ -1542,38 +1542,30 @@
 ) %7B%0A        
-interceptors =
+return
  new ClientI
@@ -1608,22 +1608,14 @@
     
-interceptors =
+return
  new
@@ -1696,27 +1696,159 @@
     
-e.printStackTrace()
+LOG.error(%0A          %22Failed to get credentials interceptor. No interceptor will be used for the channel.%22, e);%0A      return new ClientInterceptor%5B%5D %7B%7D
 ;%0A  
@@ -1947,16 +1947,94 @@
 ption %7B%0A
+    final ClientInterceptor%5B%5D interceptors = getClientInterceptors(options);%0A%0A
     retu

Remove unnecessary cache of client interceptors (#3091)