DefaultOwnerRoleFactory.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.ArrayList;
  30. import java.util.Collection;
  31. import java.util.List;
  32. import it.tidalwave.util.As;
  33. import it.tidalwave.util.Task;
  34. import it.tidalwave.role.spi.OwnerRoleFactory;
  35. import it.tidalwave.role.spi.SystemRoleFactory;
  36. import lombok.extern.slf4j.Slf4j;
  37. import static it.tidalwave.util.ShortNames.*;

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

  50.     private final ContextSnapshot contextSnapshot;

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

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

  73.         final var roles =
  74.                 new ArrayList<T>(contextSnapshot.runWithContexts(new Task<List<? extends T>, RuntimeException>()
  75.                   {
  76.                     @Override
  77.                     @Nonnull
  78.                     public List<? extends T> run ()
  79.                       {
  80.                         return SystemRoleFactory.getInstance().findRoles(owner, roleType);
  81.                       }
  82.                   }));

  83.         if (roleType.isAssignableFrom(owner.getClass()))
  84.           {
  85.             roles.add(roleType.cast(owner));
  86.           }

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

  88.         return roles;
  89.       }
  90.   }