org.apache.deltaspike.core.impl.exclude.extension.ExcludeExtension Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.deltaspike.core.impl.exclude.extension;
import org.apache.deltaspike.core.api.config.ConfigResolver;
import org.apache.deltaspike.core.api.exclude.Exclude;
import org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder;
import org.apache.deltaspike.core.impl.exclude.CustomProjectStageBeanFilter;
import org.apache.deltaspike.core.impl.exclude.GlobalAlternative;
import org.apache.deltaspike.core.spi.activation.Deactivatable;
import org.apache.deltaspike.core.api.interpreter.ExpressionInterpreter;
import org.apache.deltaspike.core.api.projectstage.ProjectStage;
import org.apache.deltaspike.core.impl.interpreter.PropertyExpressionInterpreter;
import org.apache.deltaspike.core.util.ClassDeactivationUtils;
import org.apache.deltaspike.core.util.ClassUtils;
import org.apache.deltaspike.core.util.ProjectStageProducer;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Alternative;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.util.Nonbinding;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This class implements the logic for handling
* {@link org.apache.deltaspike.core.api.exclude.Exclude} annotations.
*
* Further details see {@link org.apache.deltaspike.core.api.exclude.Exclude}
*/
public class ExcludeExtension implements Extension, Deactivatable
{
private static final String GLOBAL_ALTERNATIVES = "globalAlternatives.";
private static final Logger LOG = Logger.getLogger(ExcludeExtension.class.getName());
private boolean isActivated = true;
private boolean isGlobalAlternativeActivated = true;
private boolean isCustomProjectStageBeanFilterActivated = true;
/**
* Contains the globalAlternatives which should get used
* KEY=Interface class name
* VALUE=Implementation class name
*/
private Map globalAlternatives = new HashMap();
@SuppressWarnings("UnusedDeclaration")
protected void init(@Observes BeforeBeanDiscovery beforeBeanDiscovery, BeanManager beanManager)
{
isActivated =
ClassDeactivationUtils.isActivated(getClass());
isCustomProjectStageBeanFilterActivated =
ClassDeactivationUtils.isActivated(CustomProjectStageBeanFilter.class);
isGlobalAlternativeActivated =
ClassDeactivationUtils.isActivated(GlobalAlternative.class);
if (isGlobalAlternativeActivated)
{
Map allProperties = ConfigResolver.getAllProperties();
for (Map.Entry property : allProperties.entrySet())
{
if (property.getKey().startsWith(GLOBAL_ALTERNATIVES))
{
String interfaceName = property.getKey().substring(GLOBAL_ALTERNATIVES.length());
String implementation = property.getValue();
if (LOG.isLoggable(Level.FINE))
{
LOG.fine("Enabling global alternative for interface " + interfaceName + ": " + implementation);
}
globalAlternatives.put(interfaceName, implementation);
}
}
if (globalAlternatives.isEmpty())
{
isGlobalAlternativeActivated = false;
}
}
}
/**
* triggers initialization in any case
* @param afterDeploymentValidation observed event
*/
@SuppressWarnings("UnusedDeclaration")
protected void initProjectStage(@Observes AfterDeploymentValidation afterDeploymentValidation)
{
ProjectStageProducer.getInstance();
}
/**
* Observer which is vetoing beans based on {@link Exclude}
* @param processAnnotatedType observed event
*/
@SuppressWarnings("UnusedDeclaration")
protected void vetoBeans(@Observes ProcessAnnotatedType processAnnotatedType, BeanManager beanManager)
{
//we need to do it before the exclude logic to keep the @Exclude support for global alternatives
if (isGlobalAlternativeActivated)
{
activateGlobalAlternatives(processAnnotatedType, beanManager);
}
if (isCustomProjectStageBeanFilterActivated)
{
vetoCustomProjectStageBeans(processAnnotatedType);
}
if (!isActivated)
{
return;
}
//TODO needs further discussions for a different feature CodiStartupBroadcaster.broadcastStartup();
//also forces deterministic project-stage initialization
ProjectStage projectStage = ProjectStageProducer.getInstance().getProjectStage();
Exclude exclude = extractExcludeAnnotation(processAnnotatedType.getAnnotatedType().getJavaClass());
if (exclude == null)
{
return;
}
if (!evalExcludeWithoutCondition(processAnnotatedType, exclude))
{
return; //veto called already
}
if (!evalExcludeInProjectStage(processAnnotatedType, exclude, projectStage))
{
return; //veto called already
}
if (!evalExcludeNotInProjectStage(processAnnotatedType, exclude, projectStage))
{
return; //veto called already
}
evalExcludeWithExpression(processAnnotatedType, exclude);
}
//only support the physical usage and inheritance if @Exclude comes from an abstract class
//TODO re-visit the impact of java.lang.annotation.Inherited (for @Exclude) for the available use-cases
protected Exclude extractExcludeAnnotation(Class currentClass)
{
Exclude result = currentClass.getAnnotation(Exclude.class);
if (result != null)
{
return result;
}
currentClass = currentClass.getSuperclass();
while (!Object.class.equals(currentClass) && currentClass != null)
{
if (Modifier.isAbstract(currentClass.getModifiers()))
{
result = currentClass.getAnnotation(Exclude.class);
}
if (result != null)
{
return result;
}
currentClass = currentClass.getSuperclass();
}
return null;
}
protected void vetoCustomProjectStageBeans(ProcessAnnotatedType processAnnotatedType)
{
//currently there is a veto for all project-stage implementations,
//but we still need @Typed() for the provided implementations in case of the deactivation of this behaviour
if (ProjectStage.class.isAssignableFrom(processAnnotatedType.getAnnotatedType().getJavaClass()))
{
processAnnotatedType.veto();
}
}
private void activateGlobalAlternatives(ProcessAnnotatedType processAnnotatedType,
BeanManager beanManager)
{
Class