use of jakarta.enterprise.context.spi.Context in project core by weld.
the class GetContextUtilMethodsTest method getActiveContextsTest.
@Test
public void getActiveContextsTest() {
try (WeldContainer container = new Weld().initialize()) {
// TheLoneBean is just to have some bean in the archive
container.select(TheLoneBean.class).get().ping();
WeldManager manager = container.select(WeldManager.class).get();
// there are 7 scopes by default in SE, only 3 have active contexts by default
// it is dependent, singleton and application
Collection<Context> activeContexts = manager.getActiveContexts();
Assert.assertEquals(3, activeContexts.size());
Set<Class<? extends Annotation>> scopes = activeContexts.stream().map(t -> t.getScope()).collect(Collectors.toSet());
Assert.assertTrue(scopes.contains(Dependent.class));
Assert.assertTrue(scopes.contains(Singleton.class));
Assert.assertTrue(scopes.contains(ApplicationScoped.class));
}
}
use of jakarta.enterprise.context.spi.Context in project core by weld.
the class BeanManagerImpl method getInjectableReference.
/**
* Get a reference, registering the injection point used.
*
* @param injectionPoint the injection point to register
* @param resolvedBean the bean to get a reference to
* @param creationalContext the creationalContext
* @return the injectable reference
*/
public Object getInjectableReference(InjectionPoint injectionPoint, Bean<?> resolvedBean, CreationalContext<?> creationalContext) {
Preconditions.checkArgumentNotNull(resolvedBean, "resolvedBean");
Preconditions.checkArgumentNotNull(creationalContext, CREATIONAL_CONTEXT);
boolean registerInjectionPoint = isRegisterableInjectionPoint(injectionPoint);
boolean delegateInjectionPoint = injectionPoint != null && injectionPoint.isDelegate();
final ThreadLocalStackReference<InjectionPoint> stack = currentInjectionPoint.pushConditionally(injectionPoint, registerInjectionPoint);
try {
Type requestedType = null;
if (injectionPoint != null) {
requestedType = injectionPoint.getType();
}
if (clientProxyOptimization && injectionPoint != null && injectionPoint.getBean() != null) {
// For certain combinations of scopes, the container is permitted to optimize an injectable reference lookup
// This should also partially solve circular @PostConstruct invocation
CreationalContextImpl<?> weldCreationalContext = null;
Bean<?> bean = injectionPoint.getBean();
// Do not optimize for self injection
if (!bean.equals(resolvedBean)) {
if (creationalContext instanceof CreationalContextImpl) {
weldCreationalContext = (CreationalContextImpl<?>) creationalContext;
}
if (weldCreationalContext != null && Dependent.class.equals(bean.getScope()) && isNormalScope(resolvedBean.getScope())) {
bean = findNormalScopedDependant(weldCreationalContext);
}
if (InjectionPoints.isInjectableReferenceLookupOptimizationAllowed(bean, resolvedBean)) {
if (weldCreationalContext != null) {
final Object incompleteInstance = weldCreationalContext.getIncompleteInstance(resolvedBean);
if (incompleteInstance != null) {
return incompleteInstance;
}
}
Context context = internalGetContext(resolvedBean.getScope());
if (context != null) {
@SuppressWarnings({ "unchecked", "rawtypes" }) final Object existinInstance = context.get(Reflections.<Contextual>cast(resolvedBean));
if (existinInstance != null) {
return existinInstance;
}
}
}
}
}
return getReference(resolvedBean, requestedType, creationalContext, delegateInjectionPoint);
} finally {
stack.pop();
}
}
use of jakarta.enterprise.context.spi.Context in project core by weld.
the class JsonObjects method inspectContext.
private static JsonArrayBuilder inspectContext(Class<? extends Annotation> scope, BeanManagerImpl beanManager, Probe probe) {
Context context;
try {
context = beanManager.getContext(scope);
} catch (ContextNotActiveException e) {
return null;
}
JsonArrayBuilder builder = Json.arrayBuilder();
for (Bean<?> bean : probe.getBeans()) {
if (bean.getScope().equals(scope)) {
Object contextualInstance = context.get(bean);
if (contextualInstance != null) {
JsonObjectBuilder instanceBuilder = createSimpleBeanJson(bean, probe);
instanceBuilder.add(OBJECT_TO_STRING, contextualInstance.getClass().getName() + "@" + Integer.toHexString(contextualInstance.hashCode()));
instanceBuilder.add(AS_STRING, Strings.abbreviate(contextualInstance.toString(), CONTEXTUAL_INSTANCE_TO_STRING_LIMIT));
builder.add(instanceBuilder);
}
}
}
return builder;
}
use of jakarta.enterprise.context.spi.Context in project core by weld.
the class SuicidalInterceptor method destroyInstance.
private void destroyInstance() throws Exception {
Context context = manager.getContext(bean.getScope());
if (!(context instanceof AlterableContext)) {
throw new IllegalStateException("Context does not support removal of instances");
}
AlterableContext alterableContext = AlterableContext.class.cast(context);
alterableContext.destroy(bean);
}
use of jakarta.enterprise.context.spi.Context in project mojarra by eclipse-ee4j.
the class CdiUtils method getBeanInstance.
/**
* Returns concrete (non-proxied) bean instance of given class in current context.
*
* @param <T> the generic bean type
* @param type the required bean type the instance must have
* @param create whether to auto-create bean if not exist
* @return a bean instance adhering to the required type
*/
public static <T> T getBeanInstance(Class<T> type, boolean create) {
BeanManager beanManager = Util.getCdiBeanManager(FacesContext.getCurrentInstance());
@SuppressWarnings("unchecked") Bean<T> bean = (Bean<T>) beanManager.resolve(beanManager.getBeans(type));
if (bean != null) {
Context context = beanManager.getContext(bean.getScope());
if (create) {
return context.get(bean, beanManager.createCreationalContext(bean));
} else {
return context.get(bean);
}
} else {
return null;
}
}
Aggregations