protected Class< ? extends ScopedAssociation> getBroadestActiveContext() { for (Class< ? extends ScopedAssociation> scopeType : getAvailableScopedAssociationClasses()) { Annotation scopeAnnotation = scopeType.getAnnotations().length > 0 ? scopeType.getAnnotations()[0] : null; if (scopeAnnotation == null || !beanManager.isScope(scopeAnnotation.annotationType())) { throw new ProcessEngineException("ScopedAssociation must carry exactly one annotation and it must be a @Scope annotation"); } try { beanManager.getContext(scopeAnnotation.annotationType()); return scopeType; } catch (ContextNotActiveException e) { log.finest("Context " + scopeAnnotation.annotationType() + " not active."); } } throw new ProcessEngineException("Could not determine an active context to associate the current process instance / task instance with."); }
@Inject BeanManager beanManager; //inject bean manager into the class void someMethod() { Set<Bean<E>> possibleBeans = beanManager.getBeans("aBeanName"); Bean<E> bean = (Bean<E>) beanManager.resolve(possibleBeans); E instance = beanManager.getContext(bean.getScope()).get(bean, beanManager.createCreationalContext(bean)); }
/** * @see Beans#isActive(Class) */ public static <S extends Annotation> boolean isActive(BeanManager beanManager, Class<S> scope) { try { return beanManager.getContext(scope).isActive(); } catch (Exception ignore) { logger.log(FINEST, "Ignoring thrown exception; given scope is very unlikely active anyway.", ignore); return false; } }
/** * @see Beans#isActive(Class) */ public static <S extends Annotation> boolean isActive(BeanManager beanManager, Class<S> scope) { try { return beanManager.getContext(scope).isActive(); } catch (Exception ignore) { logger.log(FINEST, "Ignoring thrown exception; given scope is very unlikely active anyway.", ignore); return false; } }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (!jsr299Manager.getContext(ApplicationScoped.class).isActive()) { throw new ServletException("Application context is not active"); } else { chain.doFilter(request, response); } }
@Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (!jsr299Manager.getContext(SessionScoped.class).isActive()) { throw new ServletException("Session is not active"); } else { super.service(req, resp); } }
@PostConstruct public void construct() { try { dependentContextActiveDuringPostConstruct = beanManager.getContext(Dependent.class).isActive(); } catch (ContextNotActiveException e) { dependentContextActiveDuringPostConstruct = false; } }
public void testConversationContextIsActive() { try { result = beanManager.getContext(ConversationScoped.class).isActive(); } catch (ContextNotActiveException expected) { result = false; } }
@Override public void activate(Archive archive, String asName, boolean implicit) { try { beanManager.getContext(delegate.getScope()); LOGGER.info("Command context already active"); } catch (ContextNotActiveException e) { // Only activate the context if not already active delegate.activate(archive, asName, implicit); isActivator = true; } }
/** * Returns true if given scope is active in current context. */ public static <S extends Annotation> boolean isScopeActive(Class<S> scope) { BeanManager beanManager = Util.getCdiBeanManager(FacesContext.getCurrentInstance()); try { Context context = beanManager.getContext(scope); return context.isActive(); } catch (ContextNotActiveException ignore) { return false; } }
/** * Returns true if given scope is active in current context. */ public static <S extends Annotation> boolean isScopeActive(Class<S> scope) { BeanManager beanManager = Util.getCdiBeanManager(FacesContext.getCurrentInstance()); try { Context context = beanManager.getContext(scope); return context.isActive(); } catch (ContextNotActiveException ignore) { return false; } }
@Test @SpecAssertion(section = AFTER_BEAN_DISCOVERY, id = "f") public void testAddContext() { Context context = getCurrentManager().getContext(SuperScoped.class); assertNotNull(context); assertTrue(context.isActive()); assertTrue(context instanceof SuperContext); }
@Override public void activate(Archive archive, String asName, boolean implicit) { try { beanManager.getContext(delegate.getScope()); LOGGER.info("Command context already active"); } catch (ContextNotActiveException e) { // Only activate the context if not already active delegate.activate(archive, asName, implicit); isActivator = true; } }
@Override public void activate(Archive archive, String asName, boolean implicit) { try { beanManager.getContext(delegate.getScope()); LOGGER.info("Command context already active"); } catch (ContextNotActiveException e) { // Only activate the context if not already active delegate.activate(archive, asName, implicit); isActivator = true; } }
public static CDIExtension getCdiExtensionInstance(BeanManager beanManager) { Bean<?> bean = getBean(beanManager, CDIExtension.class); if (bean == null) { return null; } return ( CDIExtension ) beanManager.getContext( ApplicationScoped.class ).get( bean ); }
@Override public void processEvent(SystemEvent event) throws AbortProcessingException { if (event instanceof PreDestroyViewMapEvent) { final Context context = Beans.getBeanManager().getContext(ViewScoped.class); if (context instanceof FacesViewContextImpl) { ((FacesViewContextImpl) context).clearView(); } } }
public static CDIExtension getCdiExtensionInstance(BeanManager beanManager) { Bean<?> bean = getBean(beanManager, CDIExtension.class); if (bean == null) { return null; } return ( CDIExtension ) beanManager.getContext( ApplicationScoped.class ).get( bean ); }
public static <T> T getInstance(BeanManager beanManager, Bean<T> bean, boolean create) { Context context = beanManager.getContext(bean.getScope()); if (create) { return context.get(bean, beanManager.createCreationalContext(bean)); } else { return context.get(bean); } }
@Test(groups = { "contexts" }, expectedExceptions = { ContextNotActiveException.class }) @SpecAssertion(section = "6.2", id = "m") public void testInvokingGetOnInactiveContextFails() { Context sessionContext = getCurrentManager().getContext(SessionScoped.class); assert sessionContext.isActive(); setContextInactive(sessionContext); Contextual<MySessionBean> mySessionBean = getBeans(MySessionBean.class).iterator().next(); sessionContext.get(mySessionBean); }
@Test(dataProvider = ARQUILLIAN_DATA_PROVIDER) @SpecAssertion(section = CONTEXT, id = "t") public void testNothingHappensIfNoInstanceToDestroy(ApplicationScopedComponent application) { Bean<?> bean = getUniqueBean(ApplicationScopedComponent.class); AlterableContext context = (AlterableContext) getCurrentManager().getContext(bean.getScope()); AbstractComponent.reset(); application.setValue("value"); context.destroy(bean); assertTrue(AbstractComponent.isDestroyed()); context.destroy(bean); // make sure subsequent calls do not raise exception context.destroy(bean); }