Java Code Examples for javax.enterprise.context.spi.Context#get()
The following examples show how to use
javax.enterprise.context.spi.Context#get() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VertxProducer.java From quarkus with Apache License 2.0 | 6 votes |
/** * Undeploy verticles backed by contextual instances of {@link ApplicationScoped} beans before the application context is * destroyed. Otherwise Vertx may attempt to stop the verticles after the CDI container is shut down. * * @param event * @param beanManager */ void undeployVerticles(@Observes @BeforeDestroyed(ApplicationScoped.class) Object event, BeanManager beanManager, io.vertx.mutiny.core.Vertx mutiny) { // Only beans with the AbstractVerticle in the set of bean types are considered - we need a deployment id Set<Bean<?>> beans = beanManager.getBeans(AbstractVerticle.class, Any.Literal.INSTANCE); Context applicationContext = beanManager.getContext(ApplicationScoped.class); for (Bean<?> bean : beans) { if (ApplicationScoped.class.equals(bean.getScope())) { // Only beans with @ApplicationScoped are considered Object instance = applicationContext.get(bean); if (instance != null) { // Only existing instances are considered try { AbstractVerticle verticle = (AbstractVerticle) instance; mutiny.undeploy(verticle.deploymentID()).await().indefinitely(); LOGGER.debugf("Undeployed verticle: %s", instance.getClass()); } catch (Exception e) { // In theory, a user can undeploy the verticle manually LOGGER.debugf("Unable to undeploy verticle %s: %s", instance.getClass(), e.toString()); } } } } }
Example 2
Source File: ContextualReloadHelper.java From HotswapAgent with GNU General Public License v2.0 | 6 votes |
/** * Will re-inject any managed beans in the target. Will not call any other life-cycle methods * * @param ctx * @param managedBean */ @SuppressWarnings("unchecked") static void reinitialize(Context ctx, Contextual<Object> contextual) { try { ManagedBean<Object> managedBean = ManagedBean.class.cast(contextual); LOGGER.debug("Re-Initializing bean '{}' in context '{}'", managedBean, ctx); Object get = ctx.get(managedBean); if (get != null) { LOGGER.debug("Bean injection points are reinitialized '{}'", managedBean); CreationalContextImpl<Object> creationalContext = managedBean.getWebBeansContext().getCreationalContextFactory().getCreationalContext(managedBean); managedBean.getProducer().inject(get, creationalContext); } } catch (Exception e) { LOGGER.error("Error reinitializing bean '{}' in context '{}'", e, contextual, ctx); } }
Example 3
Source File: BeanLocator.java From AngularBeans with GNU Lesser General Public License v3.0 | 5 votes |
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); // if(reference==null && scopeAnnotationClass.equals(RequestScoped.class)){ // reference= bean.create(beanManager.createCreationalContext(bean)); // } return reference; }
Example 4
Source File: BeanClassRefreshAgent.java From HotswapAgent with GNU General Public License v2.0 | 5 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) private static void doReinjectBeanInstance(BeanManagerImpl beanManager, InjectionTargetBean bean, Context context) { Object instance = context.get(bean); if (instance != null) { instance = unwrapInstance(beanManager, instance); bean.getProducer().inject(instance, beanManager.createCreationalContext(bean)); LOGGER.info("Bean '{}' injection points was reinjected.", bean.getBeanClass().getName()); } }
Example 5
Source File: ContextualReloadHelper.java From HotswapAgent with GNU General Public License v2.0 | 5 votes |
/** * Will re-inject any managed beans in the target. Will not call any other life-cycle methods * * @param ctx * @param managedBean */ public static void reinitialize(Context ctx, Contextual<Object> contextual) { try { ManagedBean<Object> managedBean = ManagedBean.class.cast(contextual); LOGGER.debug("Re-Initializing........ {},: {}", managedBean, ctx); Object get = ctx.get(managedBean); if (get != null) { LOGGER.debug("Bean injection points are reinitialized '{}'", managedBean); managedBean.getProducer().inject(get, managedBean.getBeanManager().createCreationalContext(managedBean)); } } catch (Exception e) { LOGGER.error("Error reinitializing bean {},: {}", e, contextual, ctx); } }
Example 6
Source File: BeanReloadExecutor.java From HotswapAgent with GNU General Public License v2.0 | 5 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) private static void doReinjectBeanInstance(BeanManagerImpl beanManager, AbstractClassBean bean, Context context) { Object instance = context.get(bean); if (instance != null) { doCallInject(beanManager, bean, instance); } }
Example 7
Source File: RepositoryRefreshAgent.java From HotswapAgent with GNU General Public License v2.0 | 4 votes |
private static <T> T getInstance(BeanManager beanManager, Bean<T> bean) { Context context = beanManager.getContext(bean.getScope()); return context.get(bean); }
Example 8
Source File: CdiPlugin.java From tomee with Apache License 2.0 | 4 votes |
@Override public Object getSessionBeanProxy(final Bean<?> inBean, final Class<?> interfce, final CreationalContext<?> creationalContext) { Object instance = cacheProxies.get(inBean); if (instance != null) { return instance; } synchronized (inBean) { // singleton for the app so safe to sync on it instance = cacheProxies.get(inBean); if (instance != null) { return instance; } final Class<? extends Annotation> scopeClass = inBean.getScope(); final CdiEjbBean<Object> cdiEjbBean = (CdiEjbBean<Object>) inBean; final CreationalContext<Object> cc = (CreationalContext<Object>) creationalContext; if (scopeClass == null || Dependent.class == scopeClass) { // no need to add any layer, null = @New return cdiEjbBean.createEjb(cc); } // only stateful normally final InstanceBean<Object> bean = new InstanceBean<>(cdiEjbBean); if (webBeansContext.getBeanManagerImpl().isNormalScope(scopeClass)) { final BeanContext beanContext = cdiEjbBean.getBeanContext(); final Provider provider = webBeansContext.getNormalScopeProxyFactory().getInstanceProvider(beanContext.getClassLoader(), cdiEjbBean); if (!beanContext.isLocalbean()) { final List<Class> interfaces = new ArrayList<>(); final InterfaceType type = beanContext.getInterfaceType(interfce); if (type != null) { interfaces.addAll(beanContext.getInterfaces(type)); } else { // can happen when looked up from impl instead of API in OWB -> default to business local interfaces.addAll(beanContext.getInterfaces(InterfaceType.BUSINESS_LOCAL)); } interfaces.add(Serializable.class); interfaces.add(IntraVmProxy.class); if (BeanType.STATEFUL.equals(beanContext.getComponentType()) || BeanType.MANAGED.equals(beanContext.getComponentType())) { interfaces.add(BeanContext.Removable.class); } try { instance = ProxyManager.newProxyInstance(interfaces.toArray(new Class<?>[interfaces.size()]), new InvocationHandler() { @Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { try { return method.invoke(provider.get(), args); } catch (final InvocationTargetException ite) { throw ite.getCause(); } } }); } catch (final IllegalAccessException e) { throw new OpenEJBRuntimeException(e); } } else { final NormalScopeProxyFactory normalScopeProxyFactory = webBeansContext.getNormalScopeProxyFactory(); final Class<?> proxyClass = normalScopeProxyFactory.createProxyClass(beanContext.getClassLoader(), beanContext.getBeanClass()); instance = normalScopeProxyFactory.createProxyInstance(proxyClass, provider); } cacheProxies.put(inBean, instance); } else { final Context context = webBeansContext.getBeanManagerImpl().getContext(scopeClass); instance = context.get(bean, cc); } bean.setOwbProxy(instance); return instance; } }
Example 9
Source File: ApplicationScopedTest.java From tomee with Apache License 2.0 | 2 votes |
private <T extends Message> T createAndMutate(final Context context, final Class<T> beanType) { final Bean<T> bean = (Bean<T>) beanManager.getBeans(beanType).iterator().next(); // We haven't created anything yet, so the instance should not exist in the context assertNull(context.get(bean)); final CreationalContext<T> cc1 = beanManager.createCreationalContext(bean); // This should create the instance and put it in the context final T instance = context.get(bean, cc1); // Assert the instance is now in the context and can be generically retrieved assertNotNull(context.get(bean)); final String prefix = beanType.getSimpleName(); // Mutate the instance... instance.setMessage(prefix + ": hello application"); // Now check the reference in the context assertEquals(prefix + ": hello application", context.get(bean, cc1).getMessage()); // Attempt to create a second instance (should not work) final CreationalContext<T> cc2 = beanManager.createCreationalContext(bean); // We should still have the same mutated instance as before assertEquals(prefix + ": hello application", context.get(bean, cc2).getMessage()); // Mutate the instance one more time instance.setMessage(prefix + ": hello again application"); // And double check that we still just have the single instance in the context assertEquals(prefix + ": hello again application", context.get(bean).getMessage()); assertEquals(prefix + ": hello again application", context.get(bean, null).getMessage()); assertEquals(prefix + ": hello again application", context.get(bean, cc1).getMessage()); assertEquals(prefix + ": hello again application", context.get(bean, cc2).getMessage()); return instance; }