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

ro.isdc.wro.util.provider.ProviderFinder Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
package ro.isdc.wro.util.provider;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.imageio.spi.ServiceRegistry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ro.isdc.wro.WroRuntimeException;


/**
 * Helps to find available providers of any supported type.
 * 
 * @author Alex Objelean
 * @created 16 Jun 2012
 * @since 1.4.7
 */
public class ProviderFinder {
  private static final Logger LOG = LoggerFactory.getLogger(ProviderFinder.class);
  
  private final Class type;
  
  /**
   * @VisibleForTesting.
   * @param type
   *          the type of providers to find.
   */
  ProviderFinder(final Class type) {
    this.type = type;
  }
  
  /**
   * Creates a {@link ProviderFinder} which will find providers of type provided as argument..
   * 
   * @param type
   *          the type of providers to search.
   * @return {@link ProviderFinder} handling providers lookup.
   */
  public static  ProviderFinder of(final Class type) {
    return new ProviderFinder(type);
  }
  
  /**
   * @return the list of all providers found in classpath.
   */
  public List find() {
    final List providers = new ArrayList();
    try {
      final Iterator iterator = lookupProviders(type);
      for (; iterator.hasNext();) {
        final T provider = iterator.next();
        LOG.debug("found provider: {}", provider);
        providers.add(provider);
      }
      collectConfigurableProviders(providers);
    } catch (Exception e) {
      LOG.error("Failed to discover providers using ServiceRegistry. Cannot continue...", e);
      throw WroRuntimeException.wrap(e);
    }
    return providers;
  }
  
  /**
   * Collects also providers of type {@link ConfigurableProvider} if the T type is a supertype of
   * {@link ConfigurableProvider}.
   * 
   * @param providers
   *          the list where found providers will be added.
   */
  @SuppressWarnings("unchecked")
  private void collectConfigurableProviders(final List providers) {
    if (type.isAssignableFrom(ConfigurableProvider.class)) {
      final Iterator iterator = lookupProviders(ConfigurableProvider.class);
      for (; iterator.hasNext();) {
        T provider = (T) iterator.next();
        LOG.debug("found provider: {}", provider);
        providers.add(provider);
      }
    }
  }
  
  /**
   * This method is useful for mocking the lookup operation.
   * 
   * @param clazz
   *          the class of the provider to lookup.
   * @VisibleForTesting
   * @return the iterator of found providers.
   */
  

Iterator

lookupProviders(final Class

clazz) { LOG.debug("searching for providers of type : {}", clazz); return ServiceRegistry.lookupProviders(clazz); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy