RoleManager.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.spi;

  28. import javax.annotation.Nonnull;
  29. import java.util.List;
  30. import java.util.Objects;
  31. import java.util.ServiceLoader;
  32. import it.tidalwave.util.LazySupplier;
  33. import lombok.AccessLevel;
  34. import lombok.NoArgsConstructor;
  35. import lombok.extern.slf4j.Slf4j;

  36. /***********************************************************************************************************************
  37.  *
  38.  * A service which retrieves DCI Roles for a given object.
  39.  *
  40.  * @author  Fabrizio Giudici
  41.  *
  42.  **********************************************************************************************************************/
  43. public interface RoleManager
  44.   {
  45.     /*******************************************************************************************************************
  46.      *
  47.      * A locator for the {@link RoleManager} which uses the {@link ServiceLoader} facility to be independent of
  48.      * any DI framework.
  49.      *
  50.      ******************************************************************************************************************/
  51.     @Slf4j @NoArgsConstructor(access = AccessLevel.PRIVATE)
  52.     public static final class Locator
  53.       {
  54.         private static final LazySupplier<RoleManager> ROLE_MANAGER_REF =
  55.                 LazySupplier.of(RoleManager.Locator::findRoleManager);

  56.         private static final LazySupplier<RoleManagerProvider> ROLE_MANAGER_PROVIDER_REF =
  57.                 LazySupplier.of(RoleManager.Locator::findRoleManagerProvider);

  58.         /***************************************************************************************************************
  59.          *
  60.          **************************************************************************************************************/
  61.         @Nonnull
  62.         public static RoleManager find()
  63.           {
  64.             return ROLE_MANAGER_REF.get();
  65.           }

  66.         /***************************************************************************************************************
  67.          *
  68.          **************************************************************************************************************/
  69.         @Nonnull
  70.         private static RoleManager findRoleManager()
  71.           {
  72.             return Objects.requireNonNull(ROLE_MANAGER_PROVIDER_REF.get().getRoleManager(),
  73.                                           "Cannot find RoleManager");
  74.           }

  75.         /***************************************************************************************************************
  76.          *
  77.          **************************************************************************************************************/
  78.         @SuppressWarnings("ConstantValue")
  79.         @Nonnull
  80.         private static RoleManagerProvider findRoleManagerProvider()
  81.           {
  82.             final var classLoader = Thread.currentThread().getContextClassLoader();
  83.             final var i = ServiceLoader.load(RoleManagerProvider.class, classLoader).iterator();

  84.             if (!i.hasNext())
  85.               {
  86.                 throw new RuntimeException("No ServiceProvider for RoleManagerProvider");
  87.               }

  88.             final var roleManagerProvider = Objects.requireNonNull(i.next(),
  89.                                                                    "roleManagerProvider is null");
  90.             assert roleManagerProvider != null; // for SpotBugs
  91.             log.info("RoleManagerProvider instantiated from META-INF: {}", roleManagerProvider);
  92.             return roleManagerProvider;
  93.           }
  94.       }

  95.     /*******************************************************************************************************************
  96.      *
  97.      * Retrieves the roles of the given class for the given owner object.
  98.      *
  99.      * @param <T>           the static type of the roles
  100.      * @param   owner       the owner object
  101.      * @param   roleType    the dynamic type of the roles
  102.      * @return              a list of roles
  103.      *
  104.      ******************************************************************************************************************/
  105.     @Nonnull
  106.     public <T> List<T> findRoles (@Nonnull Object owner, @Nonnull Class<? extends T> roleType);
  107.   }