// Path: repository/src/main/java/de/acosix/alfresco/mtsupport/repo/beans/TenantBeanUtils.java
// public final class TenantBeanUtils
// {
// 
//     public static final String TENANT_BEAN_NAME_PATTERN = ".tenant-";
// 
//     public static final String TENANT_BEAN_TEMPLATE_SUFFIX = ".template";
// 
//     public static final String TENANT_PLACEHOLDER = "tenant";
// 
//     public static final String TENANT_PLACEHOLDER_IN_PLACEHOLDER = ".thisTenant.";
// 
//     private TenantBeanUtils()
//     {
//         // NO-OP
//     }
// 
//     /**
//      * Retrieves a specific bean for a specific tenant from the provided application context. This operation should only be used when each
//      * tenant domain is backed by its own implementation bean for a feature and calls need to be delegated according to the tenant domain.
//      *
//      * @param applicationContext
//      *            the application context from which to retrieve the bean
//      * @param baseBeanName
//      *            the base bean name
//      * @param tenantDomain
//      *            the tenant domain for which to retrieve the bean
//      * @return the bean
//      */
//     public static Object getBeanForTenant(final ApplicationContext applicationContext, final String baseBeanName, final String tenantDomain)
//     {
//         ParameterCheck.mandatory("applicationContext", applicationContext);
//         ParameterCheck.mandatoryString("baseBeanName", baseBeanName);
//         ParameterCheck.mandatoryString("tenantDomain", tenantDomain);
// 
//         final String expectedBeanName = baseBeanName + TENANT_BEAN_NAME_PATTERN + tenantDomain;
//         final Object bean = applicationContext.getBean(expectedBeanName);
//         return bean;
//     }
// 
//     /**
//      * Retrieves a specific bean definition for a specific tenant from the provided registry. This operation should only be used when each
//      * tenant domain is backed by its own implementation bean for a feature.
//      *
//      * @param registry
//      *            the bean definition registry from which to retrieve the bean definition
//      * @param baseBeanName
//      *            the base bean name
//      * @param tenantDomain
//      *            the tenant domain for which to retrieve the bean definition
//      * @return the bean definition
//      */
//     public static BeanDefinition getBeanDefinitionForTenant(final BeanDefinitionRegistry registry, final String baseBeanName,
//             final String tenantDomain)
//     {
//         ParameterCheck.mandatory("registry", registry);
//         ParameterCheck.mandatoryString("baseBeanName", baseBeanName);
//         ParameterCheck.mandatoryString("tenantDomain", tenantDomain);
// 
//         final String expectedBeanName = baseBeanName + TENANT_BEAN_NAME_PATTERN + tenantDomain;
//         final BeanDefinition beanDefinition = registry.getBeanDefinition(expectedBeanName);
//         return beanDefinition;
//     }
// 
//     /**
//      * Retrieves a specific bean for a specific tenant from the provided application context. This operation should only be used when each
//      * tenant domain is backed by its own implementation bean for a feature and calls need to be delegated according to the tenant domain.
//      *
//      * @param applicationContext
//      *            the application context from which to retrieve the bean
//      * @param baseBeanName
//      *            the base bean name
//      * @param tenantDomain
//      *            the tenant domain for which to retrieve the bean
//      * @param expectedType
//      *            the expected class or interface to which the bean must conform
//      * @return the bean
//      */
//     public static <T> T getBeanForTenant(final ApplicationContext applicationContext, final String baseBeanName, final String tenantDomain,
//             final Class<T> expectedType)
//     {
//         ParameterCheck.mandatory("applicationContext", applicationContext);
//         ParameterCheck.mandatoryString("baseBeanName", baseBeanName);
//         ParameterCheck.mandatoryString("tenantDomain", tenantDomain);
//         ParameterCheck.mandatory("expectedType", expectedType);
// 
//         final String expectedBeanName = baseBeanName + TENANT_BEAN_NAME_PATTERN + tenantDomain;
//         final T bean = applicationContext.getBean(expectedBeanName, expectedType);
//         return bean;
//     }
// }


import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.alfresco.repo.management.subsystems.ActivateableBean;
import org.alfresco.repo.security.authentication.AbstractAuthenticationComponent;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.security.authentication.AuthenticationException;
import org.alfresco.repo.tenant.TenantAdminService;
import org.alfresco.repo.tenant.TenantService;
import org.alfresco.repo.tenant.TenantUtil;
import org.alfresco.util.ParameterCheck;
import org.alfresco.util.PropertyCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import de.acosix.alfresco.mtsupport.repo.beans.TenantBeanUtils;

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isActive()
    {
        final AtomicBoolean isActive = new AtomicBoolean(false);

        LOGGER.trace("Checking isActive for enabled tenants (until first active tenant)");
        for (final String tenantDomain : this.enabledTenants)
        {
            if (!isActive.get())
            {
                isActive.set(this.isActive(tenantDomain));
            }
        }
        LOGGER.trace("Component is active: {}", isActive.get());

        return isActive.get();
    }

    protected boolean isActive(final String tenantDomain)
    {
        boolean isActive = false;

        LOGGER.trace("Checking isActive for tenant {}", tenantDomain);
        if (this.enabledTenants.contains(tenantDomain) && (TenantUtil.DEFAULT_TENANT.equals(tenantDomain)
                || (this.tenantAdminService.existsTenant(tenantDomain) && this.tenantAdminService.isEnabledTenant(tenantDomain))))
        {
// Path: repository/src/main/java/de/acosix/alfresco/mtsupport/repo/beans/TenantBeanUtils.java
// public final class TenantBeanUtils
// {
// 
//     public static final String TENANT_BEAN_NAME_PATTERN = ".tenant-";
// 
//     public static final String TENANT_BEAN_TEMPLATE_SUFFIX = ".template";
// 
//     public static final String TENANT_PLACEHOLDER = "tenant";
// 
//     public static final String TENANT_PLACEHOLDER_IN_PLACEHOLDER = ".thisTenant.";
// 
//     private TenantBeanUtils()
//     {
//         // NO-OP
//     }
// 
//     /**
//      * Retrieves a specific bean for a specific tenant from the provided application context. This operation should only be used when each
//      * tenant domain is backed by its own implementation bean for a feature and calls need to be delegated according to the tenant domain.
//      *
//      * @param applicationContext
//      *            the application context from which to retrieve the bean
//      * @param baseBeanName
//      *            the base bean name
//      * @param tenantDomain
//      *            the tenant domain for which to retrieve the bean
//      * @return the bean
//      */
//     public static Object getBeanForTenant(final ApplicationContext applicationContext, final String baseBeanName, final String tenantDomain)
//     {
//         ParameterCheck.mandatory("applicationContext", applicationContext);
//         ParameterCheck.mandatoryString("baseBeanName", baseBeanName);
//         ParameterCheck.mandatoryString("tenantDomain", tenantDomain);
// 
//         final String expectedBeanName = baseBeanName + TENANT_BEAN_NAME_PATTERN + tenantDomain;
//         final Object bean = applicationContext.getBean(expectedBeanName);
//         return bean;
//     }
// 
//     /**
//      * Retrieves a specific bean definition for a specific tenant from the provided registry. This operation should only be used when each
//      * tenant domain is backed by its own implementation bean for a feature.
//      *
//      * @param registry
//      *            the bean definition registry from which to retrieve the bean definition
//      * @param baseBeanName
//      *            the base bean name
//      * @param tenantDomain
//      *            the tenant domain for which to retrieve the bean definition
//      * @return the bean definition
//      */
//     public static BeanDefinition getBeanDefinitionForTenant(final BeanDefinitionRegistry registry, final String baseBeanName,
//             final String tenantDomain)
//     {
//         ParameterCheck.mandatory("registry", registry);
//         ParameterCheck.mandatoryString("baseBeanName", baseBeanName);
//         ParameterCheck.mandatoryString("tenantDomain", tenantDomain);
// 
//         final String expectedBeanName = baseBeanName + TENANT_BEAN_NAME_PATTERN + tenantDomain;
//         final BeanDefinition beanDefinition = registry.getBeanDefinition(expectedBeanName);
//         return beanDefinition;
//     }
// 
//     /**
//      * Retrieves a specific bean for a specific tenant from the provided application context. This operation should only be used when each
//      * tenant domain is backed by its own implementation bean for a feature and calls need to be delegated according to the tenant domain.
//      *
//      * @param applicationContext
//      *            the application context from which to retrieve the bean
//      * @param baseBeanName
//      *            the base bean name
//      * @param tenantDomain
//      *            the tenant domain for which to retrieve the bean
//      * @param expectedType
//      *            the expected class or interface to which the bean must conform
//      * @return the bean
//      */
//     public static <T> T getBeanForTenant(final ApplicationContext applicationContext, final String baseBeanName, final String tenantDomain,
//             final Class<T> expectedType)
//     {
//         ParameterCheck.mandatory("applicationContext", applicationContext);
//         ParameterCheck.mandatoryString("baseBeanName", baseBeanName);
//         ParameterCheck.mandatoryString("tenantDomain", tenantDomain);
//         ParameterCheck.mandatory("expectedType", expectedType);
// 
//         final String expectedBeanName = baseBeanName + TENANT_BEAN_NAME_PATTERN + tenantDomain;
//         final T bean = applicationContext.getBean(expectedBeanName, expectedType);
//         return bean;
//     }
// }


// Path: repository/src/main/java/de/acosix/alfresco/mtsupport/repo/auth/TenantRoutingAuthenticationComponentFacade.java
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.alfresco.repo.management.subsystems.ActivateableBean;
import org.alfresco.repo.security.authentication.AbstractAuthenticationComponent;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.security.authentication.AuthenticationException;
import org.alfresco.repo.tenant.TenantAdminService;
import org.alfresco.repo.tenant.TenantService;
import org.alfresco.repo.tenant.TenantUtil;
import org.alfresco.util.ParameterCheck;
import org.alfresco.util.PropertyCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import de.acosix.alfresco.mtsupport.repo.beans.TenantBeanUtils;


    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isActive()
    {
        final AtomicBoolean isActive = new AtomicBoolean(false);

        LOGGER.trace("Checking isActive for enabled tenants (until first active tenant)");
        for (final String tenantDomain : this.enabledTenants)
        {
            if (!isActive.get())
            {
                isActive.set(this.isActive(tenantDomain));
            }
        }
        LOGGER.trace("Component is active: {}", isActive.get());

        return isActive.get();
    }

    protected boolean isActive(final String tenantDomain)
    {
        boolean isActive = false;

        LOGGER.trace("Checking isActive for tenant {}", tenantDomain);
        if (this.enabledTenants.contains(tenantDomain) && (TenantUtil.DEFAULT_TENANT.equals(tenantDomain)
                || (this.tenantAdminService.existsTenant(tenantDomain) && this.tenantAdminService.isEnabledTenant(tenantDomain))))
        {
            final AuthenticationComponent authenticationComponent = TenantBeanUtils.getBeanForTenant(this.applicationContext, this.beanName,