/*
 * Copyright © 2014 Cask Data, Inc.
 *
 * 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 co.cask.cdap.explore.client;

import co.cask.cdap.common.discovery.EndpointStrategy;
import co.cask.cdap.common.discovery.RandomEndpointStrategy;
import co.cask.cdap.common.discovery.TimeLimitEndpointStrategy;
import co.cask.cdap.explore.service.Explore;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.inject.Inject;
import org.apache.twill.discovery.DiscoveryServiceClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;

import static co.cask.cdap.common.conf.Constants.Service;

/**
 * An Explore Client that talks to a server implementing {@link Explore} over HTTP,
 * and that uses discovery to find the endpoints.
 */
public class DiscoveryExploreClient extends AbstractExploreClient {
  private static final Logger LOG = LoggerFactory.getLogger(DiscoveryExploreClient.class);

  private final Supplier<EndpointStrategy> endpointStrategySupplier;

  @Inject
  public DiscoveryExploreClient(final DiscoveryServiceClient discoveryClient) {
    this.endpointStrategySupplier = Suppliers.memoize(new Supplier<EndpointStrategy>() {
      @Override
      public EndpointStrategy get() {
        return new TimeLimitEndpointStrategy(
          new RandomEndpointStrategy(
            discoveryClient.discover(Service.EXPLORE_HTTP_USER_SERVICE)), 3L, TimeUnit.SECONDS);
      }
    });
  }

  @Override
  protected InetSocketAddress getExploreServiceAddress() {
    EndpointStrategy endpointStrategy = this.endpointStrategySupplier.get();
    if (endpointStrategy == null || endpointStrategy.pick() == null) {
      String message = String.format("Cannot discover service %s", Service.EXPLORE_HTTP_USER_SERVICE);
      LOG.debug(message);
      throw new RuntimeException(message);
    }

    return endpointStrategy.pick().getSocketAddress();
  }

  @Override
  protected String getAuthorizationToken() {
    return null;
  }
}

@@ -1014,79 +1014,70 @@
 over
-yServiceClient;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
+able;%0Aimport org.apache.twill.discovery.DiscoveryServiceClient
 ;%0A%0Ai
@@ -1422,100 +1422,8 @@
 t %7B%0A
-  private static final Logger LOG = LoggerFactory.getLogger(DiscoveryExploreClient.class);%0A%0A
   pr
@@ -2079,306 +2079,261 @@
     
-if (endpointStrategy == null %7C%7C endpointStrategy.pick() == null) %7B%0A      String message = String.format(%22Cannot discover service %25s%22, Service.EXPLORE_HTTP_USER_SERVICE);%0A      LOG.debug(message);%0A      throw new RuntimeException(message);%0A    %7D%0A%0A    return endpointStrategy.pick().getSocketAddress(
+Discoverable discoverable = endpointStrategy.pick();%0A    if (discoverable != null) %7B%0A      return discoverable.getSocketAddress();%0A    %7D%0A    throw new RuntimeException(%0A      String.format(%22Cannot discover service %25s%22, Service.EXPLORE_HTTP_USER_SERVICE)
 );%0A 

Fix explore client discovery