DefaultOwnerRoleFactory.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2024 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 the License.
  12.  * 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 an "AS IS" BASIS, WITHOUT WARRANTIES OR
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  22.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.role.impl;

  27. import javax.annotation.Nonnull;
  28. import java.util.ArrayList;
  29. import java.util.Collection;
  30. import java.util.List;
  31. import it.tidalwave.util.As;
  32. import it.tidalwave.util.Task;
  33. import it.tidalwave.role.spi.OwnerRoleFactory;
  34. import it.tidalwave.role.spi.SystemRoleFactory;
  35. import lombok.extern.slf4j.Slf4j;
  36. import static it.tidalwave.util.ShortNames.*;

  37. /***************************************************************************************************************************************************************
  38.  *
  39.  * An implementation for {@link As} based on Spring.
  40.  *
  41.  * @author  Fabrizio Giudici
  42.  *
  43.  **************************************************************************************************************************************************************/
  44. @Slf4j
  45. class DefaultOwnerRoleFactory implements OwnerRoleFactory
  46.   {
  47.     @Nonnull
  48.     private final Object owner;

  49.     private final ContextSnapshot contextSnapshot;

  50.     /***********************************************************************************************************************************************************
  51.      * Constructor for use with composition.
  52.      *
  53.      * @param  owner  the owner object
  54.      **********************************************************************************************************************************************************/
  55.     public DefaultOwnerRoleFactory (@Nonnull final Object owner)
  56.       {
  57.         this.owner = owner;
  58.         contextSnapshot = new ContextSnapshot(owner);
  59.       }

  60.     /***********************************************************************************************************************************************************
  61.      * {@inheritDoc}
  62.      **********************************************************************************************************************************************************/
  63.     @Override @Nonnull
  64.     public <T> Collection<T> findRoles (@Nonnull final Class<? extends T> roleType)
  65.       {
  66.         log.trace("as({}) for {}", shortName(roleType), shortId(owner));
  67.         log.trace(">>>> contexts: {}", contextSnapshot);

  68.         final var roles =
  69.                 new ArrayList<T>(contextSnapshot.runWithContexts(new Task<List<? extends T>, RuntimeException>()
  70.                   {
  71.                     @Override
  72.                     @Nonnull
  73.                     public List<? extends T> run ()
  74.                       {
  75.                         return SystemRoleFactory.getInstance().findRoles(owner, roleType);
  76.                       }
  77.                   }));

  78.         if (roleType.isAssignableFrom(owner.getClass()))
  79.           {
  80.             roles.add(roleType.cast(owner));
  81.           }

  82.         log.trace(">>>> as() returning {}", shortIds(roles));

  83.         return roles;
  84.       }
  85.   }