org.glassfish.jersey.inject.cdi.se.RequestScopeBean Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jersey-cdi2-se Show documentation
Show all versions of jersey-cdi2-se Show documentation
CDI 4 SE InjectionManager implementation
/*
* Copyright (c) 2017, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.inject.cdi.se;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.enterprise.inject.spi.InjectionTargetFactory;
import javax.enterprise.util.AnnotationLiteral;
import javax.inject.Singleton;
import org.glassfish.jersey.process.internal.RequestScope;
/**
* CDI Class Bean represents {@link CdiRequestScope}.
*
* @author Petr Bouda
*/
public class RequestScopeBean implements Bean {
private final InjectionTarget injectionTarget;
/**
* Creates a new Jersey-specific {@link javax.enterprise.inject.spi.Bean} instance.
*/
public RequestScopeBean(BeanManager beanManager) {
AnnotatedType annotatedType = beanManager.createAnnotatedType(CdiRequestScope.class);
InjectionTargetFactory injectionTargetFactory = beanManager.getInjectionTargetFactory(annotatedType);
this.injectionTarget = injectionTargetFactory.createInjectionTarget(null);
}
@Override
public boolean isNullable() {
return false;
}
@Override
@SuppressWarnings("unchecked")
public CdiRequestScope create(CreationalContext context) {
CdiRequestScope instance = injectionTarget.produce(context);
injectionTarget.inject(instance, context);
injectionTarget.postConstruct(instance);
return instance;
}
@Override
public void destroy(CdiRequestScope instance, CreationalContext context) {
injectionTarget.preDestroy(instance);
injectionTarget.dispose(instance);
context.release();
}
@Override
public Set getTypes() {
return Collections.singleton(RequestScope.class);
}
@Override
public Set getInjectionPoints() {
return Collections.emptySet();
}
@Override
public Set getQualifiers() {
Set qualifiers = new HashSet<>();
qualifiers.add(new AnnotationLiteral() {});
qualifiers.add(new AnnotationLiteral() {});
return qualifiers;
}
@Override
public Class extends Annotation> getScope() {
return Singleton.class;
}
@Override
public String getName() {
return CdiRequestScope.class.getName();
}
@Override
public Set> getStereotypes() {
return Collections.emptySet();
}
@Override
public boolean isAlternative() {
return false;
}
@Override
public Class> getBeanClass() {
return CdiRequestScope.class;
}
}