org.apache.webbeans.configurator.BeanConfiguratorImpl 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.webbeans.configurator;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanAttributes;
import javax.enterprise.inject.spi.CDI;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.PassivationCapable;
import javax.enterprise.inject.spi.Producer;
import javax.enterprise.inject.spi.configurator.BeanConfigurator;
import javax.enterprise.util.TypeLiteral;
import javax.inject.Named;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;
import org.apache.webbeans.annotation.AnyLiteral;
import org.apache.webbeans.annotation.DefaultLiteral;
import org.apache.webbeans.component.OwbBean;
import org.apache.webbeans.component.WebBeansType;
import org.apache.webbeans.component.creation.BeanAttributesBuilder;
import org.apache.webbeans.config.WebBeansContext;
import org.apache.webbeans.exception.WebBeansConfigurationException;
import org.apache.webbeans.util.AnnotationUtil;
import org.apache.webbeans.util.GenericsUtil;
import static java.util.stream.Collectors.joining;
//X TODO finish. Impossible to implement right now as the spec is ambiguous
//X TODO producer part
public class BeanConfiguratorImpl implements BeanConfigurator
{
private final WebBeansContext webBeansContext;
private Class> beanClass;
private String passivationId;
private Class extends Annotation> scope = Dependent.class;
private String name;
private boolean alternative;
private Set typeClosures = new LinkedHashSet<>();
private Set injectionPoints = new HashSet<>();
private Set qualifiers = new HashSet<>();
private Set> stereotypes = new HashSet<>();
private Function, ?> createWithCallback;
private BiConsumer> destroyWithCallback;
private Function, ? extends T> produceWithCallback;
private BiConsumer> disposeWithCallback;
public BeanConfiguratorImpl(WebBeansContext webBeansContext)
{
this.webBeansContext = webBeansContext;
}
@Override
public BeanConfigurator beanClass(Class> beanClass)
{
this.beanClass = beanClass;
return this;
}
@Override
public BeanConfigurator addInjectionPoint(InjectionPoint injectionPoint)
{
this.injectionPoints.add(injectionPoint);
return this;
}
@Override
public BeanConfigurator addInjectionPoints(InjectionPoint... injectionPoints)
{
this.injectionPoints.addAll(Arrays.asList(injectionPoints));
return this;
}
@Override
public BeanConfigurator addInjectionPoints(Set injectionPoints)
{
this.injectionPoints.addAll(injectionPoints);
return this;
}
@Override
public BeanConfigurator injectionPoints(InjectionPoint... injectionPoints)
{
this.injectionPoints.clear();
addInjectionPoints(injectionPoints);
return this;
}
@Override
public BeanConfigurator injectionPoints(Set injectionPoints)
{
this.injectionPoints.clear();
addInjectionPoints(injectionPoints);
return this;
}
@Override
public BeanConfigurator id(String id)
{
this.passivationId = id;
return this;
}
@Override
public BeanConfigurator createWith(Function, U> callback)
{
this.createWithCallback = (Function) callback;
return (BeanConfigurator) this;
}
@Override
public BeanConfigurator produceWith(Function, U> callback)
{
this.produceWithCallback = callback;
return (BeanConfigurator) this;
}
@Override
public BeanConfigurator destroyWith(BiConsumer> callback)
{
this.destroyWithCallback = callback;
return this;
}
@Override
public BeanConfigurator disposeWith(BiConsumer> callback)
{
this.disposeWithCallback = callback;
return this;
}
@Override
public BeanConfigurator read(AnnotatedType type)
{
read(BeanAttributesBuilder.forContext(webBeansContext).newBeanAttibutes(type).build());
return (BeanConfigurator) this;
}
@Override
public BeanConfigurator read(BeanAttributes> beanAttributes)
{
this.stereotypes.addAll(beanAttributes.getStereotypes());
this.scope = beanAttributes.getScope();
this.name = beanAttributes.getName();
this.alternative = beanAttributes.isAlternative();
types(beanAttributes.getTypes());
qualifiers(beanAttributes.getQualifiers());
stereotypes(beanAttributes.getStereotypes());
return this;
}
@Override
public BeanConfigurator addType(Type type)
{
this.typeClosures.add(type);
return this;
}
@Override
public BeanConfigurator addType(TypeLiteral> typeLiteral)
{
this.typeClosures.add(typeLiteral.getType());
return this;
}
@Override
public BeanConfigurator addTypes(Type... types)
{
for (Type type : types)
{
addType(type);
}
return this;
}
@Override
public BeanConfigurator addTypes(Set types)
{
for (Type type : types)
{
addType(type);
}
return this;
}
@Override
public BeanConfigurator addTransitiveTypeClosure(Type type)
{
Set typeClosure = GenericsUtil.getTypeClosure(type, type);
addTypes(typeClosure);
return this;
}
@Override
public BeanConfigurator types(Type... types)
{
this.typeClosures.clear();
addTypes(types);
return this;
}
@Override
public BeanConfigurator types(Set types)
{
this.typeClosures.clear();
addTypes(types);
return this;
}
@Override
public BeanConfigurator scope(Class extends Annotation> scope)
{
this.scope = scope;
return this;
}
@Override
public BeanConfigurator addQualifier(Annotation qualifier)
{
this.qualifiers.add(qualifier);
return this;
}
@Override
public BeanConfigurator addQualifiers(Annotation... qualifiers)
{
this.qualifiers.addAll(Arrays.asList(qualifiers));
return this;
}
@Override
public BeanConfigurator addQualifiers(Set qualifiers)
{
this.qualifiers.addAll(qualifiers);
return this;
}
@Override
public BeanConfigurator qualifiers(Annotation... qualifiers)
{
this.qualifiers.clear();
addQualifiers(qualifiers);
return this;
}
@Override
public BeanConfigurator qualifiers(Set qualifiers)
{
this.qualifiers.clear();
addQualifiers(qualifiers);
return this;
}
@Override
public BeanConfigurator addStereotype(Class extends Annotation> stereotype)
{
this.stereotypes.add(stereotype);
return this;
}
@Override
public BeanConfigurator addStereotypes(Set> stereotypes)
{
this.stereotypes.addAll(stereotypes);
return this;
}
@Override
public BeanConfigurator stereotypes(Set> stereotypes)
{
this.stereotypes.clear();
addStereotypes(stereotypes);
return this;
}
@Override
public BeanConfigurator name(String name)
{
this.name = name;
return this;
}
@Override
public BeanConfigurator alternative(boolean alternative)
{
this.alternative = alternative;
return this;
}
public Bean> getBean()
{
updateQualifiers();
setPassivationIdIfNeeded();
return new ConstructedBean();
}
private void setPassivationIdIfNeeded()
{
if (!webBeansContext.getBeanManagerImpl().isPassivatingScope(scope) || passivationId != null)
{
return;
}
final StringBuilder sb = new StringBuilder("CONFIGURATOR#");
sb.append(beanClass != null ? beanClass : typeClosures.stream().filter(Class.class::isInstance).findFirst()
.filter(it -> it != Object.class)
.orElse(Object.class)).append('#').append(qualifiers.stream().map(Annotation::toString).collect(
joining(",")));
for (final Annotation qualifier : qualifiers)
{
sb.append(qualifier.toString()).append(',');
}
passivationId = sb.toString();
}
// same as org.apache.webbeans.component.creation.BeanAttributesBuilder#defineQualifiers
private void updateQualifiers()
{
if (qualifiers.isEmpty())
{
qualifiers.add(DefaultLiteral.INSTANCE);
}
else if (qualifiers.size() == 1)
{
final Class extends Annotation> annotationType = qualifiers.iterator().next().annotationType();
if (annotationType == Named.class || annotationType == Any.class)
{
qualifiers.add(DefaultLiteral.INSTANCE);
}
}
else if (qualifiers.size() == 2 && qualifiers.stream().allMatch(it -> it.annotationType() == Named.class))
{
qualifiers.add(DefaultLiteral.INSTANCE);
}
if (AnnotationUtil.getAnnotation(qualifiers, Any.class) == null)
{
qualifiers.add(AnyLiteral.INSTANCE);
}
}
/**
* 1:1 with the BeanConfigurator.
*/
public class ConstructedBean implements OwbBean, PassivationCapable
{
private final Class returnType;
private final boolean dependent;
private boolean specialized;
private boolean enabled = true;
public ConstructedBean()
{
//X TODO calculate return type from the typeClosures properly
this.returnType = beanClass != null ? Class.class.cast(beanClass) : (typeClosures.isEmpty() ? null :
Class.class.cast(typeClosures.stream().filter(Class.class::isInstance).findFirst()
.filter(it -> it != Object.class)
.orElse(Object.class)));
dependent = !webBeansContext.getBeanManagerImpl().isNormalScope(scope);
if (createWithCallback == null && produceWithCallback == null)
{
WebBeansConfigurationException e = new WebBeansConfigurationException("Either a createCallback or a produceCallback must be set " + toString());
webBeansContext.getBeanManagerImpl().getErrorStack().pushError(e);
}
if (createWithCallback != null && produceWithCallback != null)
{
WebBeansConfigurationException e = new WebBeansConfigurationException("Only exactly one of createCallback and produceCallback must be set " + toString());
webBeansContext.getBeanManagerImpl().getErrorStack().pushError(e);
}
}
@Override
public Set getTypes()
{
return typeClosures;
}
@Override
public Set getInjectionPoints()
{
return injectionPoints;
}
@Override
public Set getQualifiers()
{
return qualifiers;
}
@Override
public Producer getProducer()
{
//X TODO
return new Producer()
{
private final Instance
© 2015 - 2025 Weber Informatics LLC | Privacy Policy