use of javax.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 javax.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 javax.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 javax.enterprise.context.spi.Context in project AngularBeans by bessemHmidi.
the class BeanLocator method lookup.
public Object lookup(String beanName, String sessionID) {
NGSessionScopeContext.setCurrentContext(sessionID);
Set<Bean<?>> beans = beanManager.getBeans(beanName);
Class beanClass = CommonUtils.beanNamesHolder.get(beanName);
if (beans.isEmpty()) {
beans = beanManager.getBeans(beanClass, new //
AnnotationLiteral<Any>() {
});
}
Bean bean = beanManager.resolve(beans);
Class scopeAnnotationClass = bean.getScope();
Context context;
if (scopeAnnotationClass.equals(RequestScoped.class)) {
context = beanManager.getContext(scopeAnnotationClass);
if (context == null)
return bean.create(beanManager.createCreationalContext(bean));
} else {
if (scopeAnnotationClass.equals(NGSessionScopeContext.class)) {
context = NGSessionScopeContext.getINSTANCE();
} else {
context = beanManager.getContext(scopeAnnotationClass);
}
}
CreationalContext creationalContext = beanManager.createCreationalContext(bean);
Object reference = context.get(bean, creationalContext);
return reference;
}
use of javax.enterprise.context.spi.Context in project kernel by exoplatform.
the class MX4JComponentAdapter method create.
private T create(ContextManager manager, boolean retryAllowed) {
Class<? extends Annotation> scope = getScope(true, false);
if (scope.equals(Unknown.class) || scope.equals(Singleton.class) || scope.equals(Dependent.class) || scope.equals(ApplicationScoped.class)) {
return create();
}
final Context ctx = manager.getContext(scope);
if (ctx == null) {
if (LOG.isTraceEnabled()) {
LOG.trace("The scope {} is unknown, thus we will create the component {} out of a scope context.", scope.getName(), getComponentImplementation().getName());
}
if (!retryAllowed)
throw new IllegalArgumentException("The scope and the default scope of the class " + getComponentImplementation().getName() + " are unknown");
try {
Class<? extends Annotation> defaultScope = ContainerUtil.getScope(getComponentImplementation(), true);
setScope(scope, defaultScope);
return create(manager, false);
} catch (DefinitionException e) {
throw new IllegalArgumentException("The scope of the class " + getComponentImplementation().getName() + " is unknown and we cannot get a clear default scope: " + e.getMessage());
}
}
NormalScope normalScope = scope.getAnnotation(NormalScope.class);
if (normalScope != null) {
// A proxy is expected
if (normalScope.passivating() && !Serializable.class.isAssignableFrom(getComponentImplementation())) {
throw new IllegalArgumentException("As the scope " + scope.getName() + " is a passivating scope, we expect only serializable objects and " + getComponentImplementation().getName() + " is not serializable.");
}
try {
lock.lock();
if (proxy != null)
return proxy;
T result = ContainerUtil.createProxy(getComponentImplementation(), new Provider<T>() {
public T get() {
return createInstance(ctx);
}
});
return proxy = result;
} finally {
lock.unlock();
}
}
return createInstance(ctx);
}
Aggregations