All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cerner.beadledom.client.BeadledomClientBuilderProvider Maven / Gradle / Ivy

The newest version!
package com.cerner.beadledom.client;

import com.cerner.beadledom.guice.dynamicbindings.DynamicBindingProvider;
import com.google.common.base.Optional;
import com.google.inject.Binding;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.inject.Provider;
import javax.ws.rs.core.Feature;

/**
 * A Guice provider for {@link BeadledomClientBuilder}.
 *
 * @author John Leacox
 * @author Sundeep Paruvu
 * @author Nimesh Subramanian
 * @since 2.0
 * @deprecated As of 3.6, use Retrofit (https://github.com/square/retrofit) instead.
 */
@Deprecated
class BeadledomClientBuilderProvider implements Provider {
  private final Class clientBindingAnnotation;
  private DynamicBindingProvider clientBuilderFactoryProvider;
  private DynamicBindingProvider> beadledomConfigProvider;

  @SuppressWarnings("unchecked")
  private final List> excludedBindings = Arrays.asList(
      BeadledomClient.class,
      BeadledomClientBuilder.class,
      BeadledomClientLifecycleHook.class);

  private Injector injector;
  private String correlationIdHeader;

  BeadledomClientBuilderProvider(Class clientBindingAnnotation) {
    this.clientBindingAnnotation = clientBindingAnnotation;
  }

  @Inject
  void init(
      Injector injector,
      @CorrelationIdClientHeader String correlationIdHeaderOpt,
      DynamicBindingProvider> beadledomConfigProvider,
      DynamicBindingProvider clientBuilderFactoryProvider) {
    this.injector = injector;
    this.correlationIdHeader = correlationIdHeaderOpt;
    this.beadledomConfigProvider = beadledomConfigProvider;
    this.clientBuilderFactoryProvider = clientBuilderFactoryProvider;
  }

  @Override
  public BeadledomClientBuilder get() {
    Optional beadledomClientConfigOpt = beadledomConfigProvider.get(
        clientBindingAnnotation);
    BeadledomClientBuilder clientBuilder =
        clientBuilderFactoryProvider.get(clientBindingAnnotation).create();
    clientBuilder.setCorrelationIdName(correlationIdHeader);
    if (beadledomClientConfigOpt.isPresent()) {
      // When there is custom client config
      BeadledomClientConfiguration config = beadledomClientConfigOpt.get();

      if (config.correlationIdName() != null) {
        clientBuilder.setCorrelationIdName(config.correlationIdName());
      }

      clientBuilder.setConnectionPoolSize(config.connectionPoolSize());
      clientBuilder.setMaxPooledPerRouteSize(config.maxPooledPerRouteSize());
      clientBuilder.setSocketTimeout(config.socketTimeoutMillis(), TimeUnit.MILLISECONDS);
      clientBuilder.setConnectionTimeout(config.connectionTimeoutMillis(), TimeUnit.MILLISECONDS);
      clientBuilder.setTtl(config.ttlMillis(), TimeUnit.MILLISECONDS);

      if (config.sslContext() != null) {
        clientBuilder.sslContext(config.sslContext());
      }
      if (config.trustStore() != null) {
        clientBuilder.trustStore(config.trustStore());
      }
    }

    Injector tempInjector = getInjector();
    processInjector(tempInjector, clientBuilder);
    while (tempInjector.getParent() != null) {
      tempInjector = tempInjector.getParent();
      processInjector(tempInjector, clientBuilder);
    }
    return clientBuilder;
  }

  private Injector getInjector() {
    return injector;
  }

  private void processInjector(Injector injector, BeadledomClientBuilder builder) {
    for (Map.Entry, Binding> bindingEntry : injector.getBindings().entrySet()) {
      if (isExcludedBinding(bindingEntry.getKey())) {
        continue;
      }

      Binding binding = bindingEntry.getValue();
      if (isJaxrsClientProviderBinding(binding)) {
        Object object = binding.getProvider().get();
        if (isJaxrsFeatureOrProvider(object)) {
          builder.register(object);
        }
      }
    }
  }

  /**
   * This is to prevent circular dependency during binding resolution in the provider.
   * As we loop through every binding in the injector, we need to exclude
   * certain bindings as they havent been created yet.
   */
  private boolean isExcludedBinding(Key key) {
    Class rawType = key.getTypeLiteral().getRawType();
    for (Class excludedClass : excludedBindings) {
      if (excludedClass.isAssignableFrom(rawType)) {
        return true;
      }
    }

    return false;
  }

  private boolean isJaxrsClientProviderBinding(Binding binding) {
    Class bindingAnnotation = binding.getKey().getAnnotationType();
    return bindingAnnotation != null && bindingAnnotation.equals(clientBindingAnnotation);
  }

  private boolean isJaxrsFeatureOrProvider(Object object) {
    Class clazz = object.getClass();
    boolean isFeature = Feature.class.isAssignableFrom(clazz);
    boolean isProvider = clazz.isAnnotationPresent(javax.ws.rs.ext.Provider.class);
    return isFeature || isProvider;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy