@SuppressWarnings("unchecked") public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) { Map<Contextual<?>, ContextualInstance<?>> ctx = currentContext.get(); if (ctx == null) { // Thread local not set - context is not active! throw new ContextNotActiveException(); } ContextualInstance<T> instance = (ContextualInstance<T>) ctx.get(contextual); if (instance == null && creationalContext != null) { // Bean instance does not exist - create one if we have CreationalContext instance = new ContextualInstance<T>(contextual.create(creationalContext), creationalContext, contextual); ctx.put(contextual, instance); } return instance != null ? instance.get() : null; }
@Override public SocialActivitiesEvent toSocial(Object object) { final ShowcaseSocialUserEvent event = (ShowcaseSocialUserEvent) object; SocialUser socialUser = null; try { socialUser = socialUserRepositoryAPI.findSocialUser(event.getUsername()); } catch (ContextNotActiveException e) { //clean repository socialUser = new SocialUser("system"); } final String desc = String.format("new social event (%d)", counter.incrementAndGet()); return new SocialActivitiesEvent(socialUser, SampleType.SAMPLE, new Date()) .withAdicionalInfo("edited") .withDescription(desc) .withLink(String.format("Main$%d.java", counter.get()), "file", SocialActivitiesEvent.LINK_TYPE.CUSTOM) .withParam("scheme", "http"); }
private static EntityManager lookupEntityManager(InjectionPoint ip, BeanManager bm) { final Class def = Default.class; @SuppressWarnings("unchecked") final Class<? extends Annotation> annotation = ip.getQualifiers() .stream() .filter(q -> q.annotationType() == DAO.class) .map(q -> ((DAO) q).value()) .findFirst() .orElse(def); if (bm.isQualifier(annotation)) { return lookupEntityManager(bm, annotation); } else { throw new ContextNotActiveException("no datasource qualifier nor stereotype presents in the " + "injection point " + ip); } }
@Override public <T> T get(Contextual<T> bean, CreationalContext<T> crco) { PortletSessionBeanHolder holder = PortletSessionBeanHolder.getBeanHolder(); if (holder == null) { throw new ContextNotActiveException("The portlet session context is not active."); } T inst = holder.getBean(bean); if (inst == null) { inst = bean.create(crco); holder.putBeanInstance(bean, crco, inst); } return inst; }
@Test public void produceFacesContext() { Resources tested = new Resources(); // case - faces context not initialized try { tested.produceFacesContext(); Assert.fail("ContextNotActiveException expected"); } catch (ContextNotActiveException e) { // OK } // case - facet context initialized try { FacesContext fcMock = Mockito.mock(FacesContext.class); FacesContextMock.setCurrentInstanceImpl(fcMock); Assert.assertEquals(fcMock, tested.produceFacesContext()); } finally { FacesContextMock.setCurrentInstanceImpl(null); } }
@Override public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) { checkArgumentNotNull(contextual); Map<Contextual<?>, ContextualInstance<?>> ctx = currentContext.get(); if (ctx == null) { throw new ContextNotActiveException(); } @SuppressWarnings("unchecked") ContextualInstance<T> instance = (ContextualInstance<T>) ctx.get(contextual); if (instance == null && creationalContext != null) { instance = new ContextualInstance<T>(contextual.create(creationalContext), creationalContext, contextual); ctx.put(contextual, instance); } return instance != null ? instance.get() : null; }
@Test public void testContextRegistered() { JoynrJeeMessageContext.getInstance().activate(); Context result = beanManager.getContext(JoynrJeeMessageScoped.class); assertNotNull(result); assertTrue(result instanceof JoynrJeeMessageContext); JoynrJeeMessageContext.getInstance().deactivate(); try { result = beanManager.getContext(JoynrJeeMessageScoped.class); fail("Shouldn't get it after deactivation."); } catch (ContextNotActiveException e) { logger.trace("Context not available after deactivation as expected."); } }
@Test public void produceFacesContext() { Resources tested = new Resources(); // case - faces context not initialized try { tested.produceFacesContext(); Assert.fail("ContextNotActiveException expected"); } catch (ContextNotActiveException e) { // OK } // case - faces context initialized try { FacesContext fcMock = Mockito.mock(FacesContext.class); FacesContextMock.setCurrentInstanceImpl(fcMock); Assert.assertEquals(fcMock, tested.produceFacesContext()); } finally { FacesContextMock.setCurrentInstanceImpl(null); } }
public <T> T get(Contextual<T> component) { Map<Contextual, TransactionBeanEntry> transactionBeanEntryMap = TransactionBeanStorage.getInstance().getActiveTransactionContext(); if (transactionBeanEntryMap == null) { TransactionBeanStorage.close(); throw new ContextNotActiveException("Not accessed within a transactional method - use @" + Transactional.class.getName()); } TransactionBeanEntry transactionBeanEntry = transactionBeanEntryMap.get(component); if (transactionBeanEntry != null) { return (T) transactionBeanEntry.getContextualInstance(); } return null; }
public <T> T get(Contextual<T> component, CreationalContext<T> creationalContext) { Map<Contextual, TransactionBeanEntry> transactionBeanEntryMap = TransactionBeanStorage.getInstance().getActiveTransactionContext(); if (transactionBeanEntryMap == null) { TransactionBeanStorage.close(); throw new ContextNotActiveException("Not accessed within a transactional method - use @" + Transactional.class.getName()); } TransactionBeanEntry transactionBeanEntry = transactionBeanEntryMap.get(component); if (transactionBeanEntry != null) { return (T) transactionBeanEntry.getContextualInstance(); } // if it doesn't yet exist, we need to create it now! T instance = component.create(creationalContext); transactionBeanEntry = new TransactionBeanEntry(component, instance, creationalContext); transactionBeanEntryMap.put(component, transactionBeanEntry); return instance; }
@Test public void entityManagerUsageWithoutTransaction() { try { //not available because there is no transactional method entityManager.getTransaction(); Assert.fail(ContextNotActiveException.class.getName() + " expected!"); } catch (ContextNotActiveException e) { //expected } Assert.assertEquals(false, TransactionBeanStorage.isOpen()); }
@Test public void invalidEntityManagerUsageAfterTransaction() { transactionalBean.executeInTransaction(); try { //not available because there is no transactional method entityManager.getTransaction(); Assert.fail(ContextNotActiveException.class.getName() + " expected!"); } catch (ContextNotActiveException e) { //expected } Assert.assertEquals(false, TransactionBeanStorage.isOpen()); }
/** * Checks if the context for the given scope annotation is active. * * @param scopeAnnotationClass The scope annotation (e.g. @RequestScoped.class) * @param beanManager The {@link BeanManager} * @return If the context is active. */ public static boolean isContextActive(Class<? extends Annotation> scopeAnnotationClass, BeanManager beanManager) { try { if (beanManager.getContext(scopeAnnotationClass) == null || !beanManager.getContext(scopeAnnotationClass).isActive()) { return false; } } catch (ContextNotActiveException e) { return false; } return true; }
@Test(expected = ContextNotActiveException.class) public void noWindowTest() { try { windowContext.activateWindow("w1"); implicitlyGroupedBean.setValue("x"); Assert.assertEquals("x", implicitlyGroupedBean.getValue()); this.windowContext.closeWindow("w1"); } catch (ContextNotActiveException e) { Assert.fail(); } implicitlyGroupedBean.getValue(); }
@Test(expected = ContextNotActiveException.class) public void noWindowTest() { try { windowContext.activateWindow("w1"); explicitlyGroupedBeanX.setValue("x1"); explicitlyGroupedBeanY.setValue("x2"); Assert.assertEquals("x1", explicitlyGroupedBeanX.getValue()); Assert.assertEquals("x2", explicitlyGroupedBeanY.getValue()); this.windowContext.closeWindow("w1"); } catch (ContextNotActiveException e) { Assert.fail(); } explicitlyGroupedBeanX.getValue(); }
@Override protected ContextualStorage getContextualStorage(Contextual<?> contextual, boolean createIfNotExist) { Session session = this.sessionHolder.get(); // return the storage for the Contextual. This can only be achieved if the Session was seen before as the storage // will not contain the if (session == null && !createIfNotExist) { session = getSession(contextual); } if (session == null) { throw new ContextNotActiveException("WebSocketContext: no WebSocket session set for the current Thread yet!"); } return this.holder.getContextualStorage(this.beanManager, session.getId(), createIfNotExist); }
@Override public void activate() { try { beanManager.getContext(delegate.getScope()); LOGGER.info("Command context already active"); } catch (ContextNotActiveException e) { // Only activate the context if not already active delegate.activate(); 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; } }
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 FlowableException("ScopedAssociation must carry exactly one annotation and it must be a @Scope annotation"); } try { beanManager.getContext(scopeAnnotation.annotationType()); return scopeType; } catch (ContextNotActiveException e) { LOGGER.trace("Context {} not active.", scopeAnnotation.annotationType()); } } throw new FlowableException("Could not determine an active context to associate the current process instance / task instance with."); }
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 ActivitiException("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 ActivitiException("Could not determine an active context to associate the current process instance / task instance with."); }
@Test public void testNotLoggedUserName() { doThrow(new ContextNotActiveException()).when(sessionInfo).getIdentity(); final String username = serverUsernameProvider.get(); verify(sessionInfo).getIdentity(); assertEquals("not-logged-user", username); }
@Test public void should_withoutTransaction_NOT_fail() throws Exception { try { ut.begin(); bean.withoutTransaction(); ut.commit(); } catch (ContextNotActiveException e) { fail(e.toString()); } }
@Test public void should_withoutTransaction_fail() { try { bean.withoutTransaction(); fail("No ContextNotActiveException"); } catch (ContextNotActiveException e) { } }
@Override public <T> T get(Contextual<T> bean) { PortletRequestScopedBeanHolder holder = PortletRequestScopedBeanHolder.getBeanHolder(); if (holder == null) { throw new ContextNotActiveException("The portlet request context is not active."); } return holder.getBean(bean); }
@Override public <T> T get(Contextual<T> bean, CreationalContext<T> crco) { PortletRequestScopedBeanHolder holder = PortletRequestScopedBeanHolder.getBeanHolder(); if (holder == null) { throw new ContextNotActiveException("The portlet request context is not active."); } // The bean holder will return an existing bean instance or create a new one // if no existing instance is available. T inst = holder.getBean(bean, crco); return inst; }
@Override public <T> T get(Contextual<T> bean) { PortletStateScopedBeanHolder holder = PortletStateScopedBeanHolder.getBeanHolder(); if (holder == null) { throw new ContextNotActiveException("The render state context is not active."); } return holder.getBean(bean); }
@Override public <T> T get(Contextual<T> bean, CreationalContext<T> crco) { PortletStateScopedBeanHolder holder = PortletStateScopedBeanHolder.getBeanHolder(); if (holder == null) { throw new ContextNotActiveException("The render state context is not active."); } // The bean hoder will return an existing bean instance or create a new one // if no existing instance is available. T inst = holder.getBean(bean, crco); return inst; }
@Override public <T> T get(Contextual<T> bean) { PortletSessionBeanHolder holder = PortletSessionBeanHolder.getBeanHolder(); if (holder == null) { throw new ContextNotActiveException("The portlet session context is not active."); } return holder.getBean(bean); }
private static void doReinjectBean(BeanManagerImpl beanManager, Class<?> beanClass, InjectionTargetBean<?> bean) { try { if (trackableSessionBasedScopes.contains(bean.getScope().getName())) { doReinjectSessionBasedBean(beanManager, beanClass, bean); } else { doReinjectBeanInstance(beanManager, beanClass, bean, beanManager.getContext(bean.getScope())); } } catch (ContextNotActiveException e) { LOGGER.info("No active contexts for bean '{}'", beanClass.getName()); } }
public boolean thereIsALoggedUserInScope() { try { if ( identity == null ) { return false; } checkIfThereIsAValidUserOnRequestScope(); } catch ( ContextNotActiveException c ) { return false; } return true; }
@Override public SocialActivitiesEvent toSocial( Object object ) { NewRepositoryEvent event = (NewRepositoryEvent) object; SocialUser socialUser = null; try { socialUser = socialUserRepositoryAPI.findSocialUser( loggedUser.getIdentifier() ); } catch(ContextNotActiveException e) { //clean repository socialUser = new SocialUser( "system" ); } String additionalInfo = "Created"; return new SocialActivitiesEvent( socialUser, ExtendedTypes.NEW_REPOSITORY_EVENT, new Date() ).withAdicionalInfo( additionalInfo ).withLink( event.getNewRepository().getAlias(), event.getNewRepository().getUri() ).withDescription( "" ); }
private String getIdentifier() { if (identity.isUnsatisfied()) { return "system"; } try { return identity.get().getIdentifier(); } catch (ContextNotActiveException e) { return "system"; } }
@Produces @RequestScoped @Named("facesContext") public FacesContext produceFacesContext() { FacesContext ctx = FacesContext.getCurrentInstance(); if (ctx == null) { throw new ContextNotActiveException("FacesContext is not active"); } return ctx; }
@SuppressWarnings("unchecked") @Override public <T> T get(Contextual<T> contextual) { Map<Contextual<?>, Object> store = contextualStore.get(); if (store == null) { throw new ContextNotActiveException(); } return (T) store.get(contextual); }
public boolean isActive() { try { return TransactionBeanStorage.isOpen() && TransactionBeanStorage.getInstance().getActiveTransactionContext() != null; } catch (ContextNotActiveException e) { return false; } }
private void processInitView(String viewId) { try { WindowMetaData windowMetaData = BeanProvider.getContextualReference(WindowMetaData.class); //view already initialized in this or any prev. request if (viewId.equals(windowMetaData.getInitializedViewId())) { return; } //override the view-id if we have a new view windowMetaData.setInitializedViewId(viewId); ViewConfigDescriptor viewDefinitionEntry = this.viewConfigResolver.getViewConfigDescriptor(viewId); if (viewDefinitionEntry == null) { return; } ViewControllerUtils.executeViewControllerCallback(viewDefinitionEntry, InitView.class); } catch (ContextNotActiveException e) { //TODO discuss how we handle it } }