ServiceLoaderLocator.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *********************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  12.  * the License. You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  23.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.role.impl;

  28. import javax.annotation.Nonnull;
  29. import java.util.ServiceLoader;
  30. import java.util.Spliterators;
  31. import java.util.stream.StreamSupport;
  32. import it.tidalwave.util.LazySupplier;
  33. import it.tidalwave.util.PreferencesHandler;
  34. import it.tidalwave.role.spi.annotation.DefaultProvider;
  35. import lombok.AccessLevel;
  36. import lombok.NoArgsConstructor;
  37. import lombok.extern.slf4j.Slf4j;
  38. import static java.util.stream.Collectors.*;

  39. /***********************************************************************************************************************
  40.  *
  41.  * @author  Fabrizio Giudici
  42.  *
  43.  **********************************************************************************************************************/
  44. @Slf4j @NoArgsConstructor(access = AccessLevel.PRIVATE)
  45. public class ServiceLoaderLocator
  46.   {

  47.     /*******************************************************************************************************************
  48.      *
  49.      ******************************************************************************************************************/
  50.     @Nonnull
  51.     public static <T> T findService (@Nonnull final Class<? extends T> serviceClass)
  52.       {
  53.         final var serviceClassName = serviceClass.getSimpleName();
  54.         final var classLoader = Thread.currentThread().getContextClassLoader();
  55.         final var iterator = ServiceLoader.load(serviceClass, classLoader).iterator();
  56.         final var spliterator = Spliterators.spliteratorUnknownSize(iterator, 0);
  57.         var providers = StreamSupport.stream(spliterator, false).collect(toList());

  58.         if (providers.isEmpty())
  59.           {
  60.             throw new RuntimeException("No ServiceProvider for " + serviceClassName);
  61.           }

  62.         if (providers.size() > 1) // filter out the default one
  63.           {
  64.             log.info("Too many instances of {}, ignoring default providers ...", serviceClassName);
  65.             providers = providers.stream()
  66.                                  .filter(p -> p.getClass().getAnnotation(DefaultProvider.class) == null)
  67.                                  .collect(toList());
  68.           }

  69.         if (providers.size() > 1)
  70.           {
  71.             throw new RuntimeException("Too many instances of " + serviceClassName + " : " + providers);
  72.           }

  73.         final var provider = providers.get(0);
  74.         log.info("{} instantiated from META-INF/services: {}", serviceClassName, provider);
  75.         return provider;
  76.       }

  77.     @Nonnull
  78.     public static <T> LazySupplier<T> lazySupplierOf (@Nonnull final Class<? extends T> clazz)
  79.       {
  80.         return LazySupplier.of(() -> ServiceLoaderLocator.findService(clazz));
  81.       }
  82.   }