AnnotationSpringRoleManager.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 javax.annotation.PostConstruct;
  30. import javax.inject.Inject;
  31. import org.springframework.beans.factory.BeanFactory;
  32. import org.springframework.beans.factory.annotation.Configurable;
  33. import it.tidalwave.util.NotFoundException;
  34. import it.tidalwave.util.spring.ClassScanner;
  35. import it.tidalwave.role.spi.RoleManagerSupport;
  36. import it.tidalwave.dci.annotation.DciContext;
  37. import it.tidalwave.dci.annotation.DciRole;
  38. import lombok.extern.slf4j.Slf4j;

  39. /***********************************************************************************************************************
  40.  *
  41.  * A specialization of {@link RoleManagerSupport} for a Spring context that uses annotations ({@link DciRole} and
  42.  * {@link DciContext}) for retrieving role metadata and is capable to inject Spring beans into created roles.
  43.  *
  44.  * @author  Fabrizio Giudici
  45.  *
  46.  **********************************************************************************************************************/
  47. @Slf4j @Configurable
  48. public class AnnotationSpringRoleManager extends RoleManagerSupport
  49.   {
  50.     @Inject @Nonnull
  51.     private BeanFactory beanFactory;

  52.     /*******************************************************************************************************************
  53.      *
  54.      *
  55.      *
  56.      ******************************************************************************************************************/
  57.     @PostConstruct
  58.     /* package */ void initialize()
  59.       {
  60.         log.debug("scanning classes with {} annotation...", DciRole.class);
  61.         scan(new ClassScanner().withAnnotationFilter(DciRole.class).findClasses());
  62.       }

  63.     /*******************************************************************************************************************
  64.      *
  65.      * {@inheritDoc}
  66.      *
  67.      ******************************************************************************************************************/
  68.     @Override @Nonnull
  69.     protected <T> T getBean (@Nonnull final Class<T> beanType)
  70.       {
  71.         return beanFactory.getBean(beanType);
  72.       }

  73.     /*******************************************************************************************************************
  74.      *
  75.      * {@inheritDoc}
  76.      *
  77.      ******************************************************************************************************************/
  78.     @Override @Nonnull
  79.     protected Class<?> findContextTypeForRole (@Nonnull final Class<?> roleImplementationType)
  80.       throws NotFoundException
  81.       {
  82.         final var contextClass = roleImplementationType.getAnnotation(DciRole.class).context();

  83.         if (contextClass == DciRole.NoContext.class)
  84.           {
  85.             throw new NotFoundException("No context");
  86.           }

  87.         return contextClass;
  88.       }

  89.     /*******************************************************************************************************************
  90.      *
  91.      * {@inheritDoc}
  92.      *
  93.      ******************************************************************************************************************/
  94.     @Override @Nonnull
  95.     protected Class<?>[] findDatumTypesForRole (@Nonnull final Class<?> roleImplementationType)
  96.       {
  97.         return roleImplementationType.getAnnotation(DciRole.class).datumType();
  98.       }
  99.   }