SpringAsDelegate.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.spring.spi;

  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.util.spi.AsDelegate;
  35. import it.tidalwave.role.spi.ContextSampler;
  36. import it.tidalwave.role.spi.RoleManager;
  37. import lombok.extern.slf4j.Slf4j;
  38. import static it.tidalwave.role.spi.impl.LogUtil.*;

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

  51.     private final ContextSampler contextSampler;

  52.     /*******************************************************************************************************************
  53.      *
  54.      * Constructor for use with subclassing.
  55.      *
  56.      ******************************************************************************************************************/
  57.     public SpringAsDelegate()
  58.       {
  59.         this.owner = this;
  60.         contextSampler = new ContextSampler(this);
  61.       }

  62.     /*******************************************************************************************************************
  63.      *
  64.      * Constructor for use with composition.
  65.      *
  66.      * @param  owner  the owner object
  67.      *
  68.      ******************************************************************************************************************/
  69.     public SpringAsDelegate (@Nonnull final Object owner)
  70.       {
  71.         this.owner = owner;
  72.         contextSampler = new ContextSampler(owner);
  73.       }

  74.     /*******************************************************************************************************************
  75.      *
  76.      * {@inheritDoc}
  77.      *
  78.      ******************************************************************************************************************/
  79.     @Override @Nonnull
  80.     public <T> Collection<? extends T> as (@Nonnull final Class<T> roleType)
  81.       {
  82.         log.trace("as({}) for {}", shortName(roleType), shortId(owner));
  83.         log.trace(">>>> contexts: {}", contextSampler);

  84.         final List<T> roles =
  85.                 new ArrayList<>(contextSampler.runWithContexts(new Task<List<? extends T>, RuntimeException>()
  86.                   {
  87.                     @Override @Nonnull
  88.                     public List<? extends T> run ()
  89.                       {
  90.                         return RoleManager.Locator.find().findRoles(owner, roleType);
  91.                       }
  92.                   }));

  93.         if (roleType.isAssignableFrom(owner.getClass()))
  94.           {
  95.             roles.add(roleType.cast(owner));
  96.           }

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

  98.         return roles;
  99.       }
  100.   }