Java 类javax.enterprise.context.ContextNotActiveException 实例源码

项目:weld-junit    文件:ContextImpl.java   
@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;
}
项目:command-context-example    文件:CommandContextImpl.java   
@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;
}
项目:wildfly-swarm    文件:DeploymentContextImpl.java   
@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;
}
项目:appformer    文件:ShowcaseSocialUserEventAdapter.java   
@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");
}
项目:javaee-samples    文件:DaoProducer.java   
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);
    }
}
项目:portals-pluto    文件:PortletSessionScopedContext.java   
@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;
}
项目:dcp-api    文件:ResourcesTest.java   
@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);
    }

}
项目:trimou    文件:RenderingContext.java   
@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;
}
项目:joynr    文件:JoynrJeeMessageContextTest.java   
@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.");
    }
}
项目:searchisko    文件:ResourcesTest.java   
@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);
    }

}
项目:deltaspike    文件:TransactionContext.java   
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;
}
项目:deltaspike    文件:TransactionContext.java   
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;
}
项目:deltaspike    文件:DefaultTransactionScopedEntityManagerInjectionTest.java   
@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());
}
项目:deltaspike    文件:DefaultTransactionScopedEntityManagerInjectionTest.java   
@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());
}
项目:deltaspike    文件:ContextUtils.java   
/**
 * 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;
}
项目:deltaspike    文件:ImplicitlyGroupedConversationsTest.java   
@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();
}
项目:deltaspike    文件:ExplicitlyGroupedConversationsTest.java   
@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();
}
项目:cito    文件:WebSocketContext.java   
@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);
}
项目:command-context-example    文件:CommandContextImpl.java   
@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;
    }
}
项目:wildfly-swarm    文件:DeploymentContextImpl.java   
@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;
    }
}
项目:flowable-engine    文件:DefaultContextAssociationManager.java   
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.");
}
项目:SecureBPMN    文件:DefaultBusinessProcessAssociationManager.java   
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.");
}
项目:appformer    文件:ServerUsernameProviderTest.java   
@Test
public void testNotLoggedUserName() {
    doThrow(new ContextNotActiveException()).when(sessionInfo).getIdentity();

    final String username = serverUsernameProvider.get();

    verify(sessionInfo).getIdentity();

    assertEquals("not-logged-user",
                 username);
}
项目:JavaIncrementalParser    文件:MyTransactionalBeanWithUserTransactionTest.java   
@Test
public void should_withoutTransaction_NOT_fail() throws Exception {
    try {
        ut.begin();
        bean.withoutTransaction();
        ut.commit();
    } catch (ContextNotActiveException e) {
        fail(e.toString());
    }
}
项目:JavaIncrementalParser    文件:MyTransactionalBeanTest.java   
@Test
public void should_withoutTransaction_fail() {
    try {
        bean.withoutTransaction();
        fail("No ContextNotActiveException");
    } catch (ContextNotActiveException e) {
    }
}
项目:portals-pluto    文件:PortletRequestScopedContext.java   
@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);
}
项目:portals-pluto    文件:PortletRequestScopedContext.java   
@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;
}
项目:portals-pluto    文件:PortletStateScopedContext.java   
@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);
}
项目:portals-pluto    文件:PortletStateScopedContext.java   
@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;
}
项目:portals-pluto    文件:PortletSessionScopedContext.java   
@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);
}
项目:FiWare-Template-Handler    文件:DefaultBusinessProcessAssociationManager.java   
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.");
}
项目:HotswapAgent    文件:BeanClassRefreshAgent.java   
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());
    }
}
项目:kie-wb-common    文件:UserCDIContextHelper.java   
public boolean thereIsALoggedUserInScope() {
    try {
        if ( identity == null ) {
            return false;
        }
        checkIfThereIsAValidUserOnRequestScope();
    } catch ( ContextNotActiveException c ) {
        return false;
    }
    return true;
}
项目:kie-wb-common    文件:NewRepositoryEventAdapter.java   
@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( "" );
}
项目:kie-wb-common    文件:BuildHelper.java   
private String getIdentifier() {
    if (identity.isUnsatisfied()) {
        return "system";
    }
    try {
        return identity.get().getIdentifier();
    } catch (ContextNotActiveException e) {
        return "system";
    }
}
项目:dcp-api    文件:Resources.java   
@Produces
@RequestScoped
@Named("facesContext")
public FacesContext produceFacesContext() {
    FacesContext ctx = FacesContext.getCurrentInstance();
    if (ctx == null) {
        throw new ContextNotActiveException("FacesContext is not active");
    }
    return ctx;
}
项目:joynr    文件:JoynrJeeMessageContext.java   
@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);
}
项目:searchisko    文件:Resources.java   
@Produces
@RequestScoped
@Named("facesContext")
public FacesContext produceFacesContext() {
    FacesContext ctx = FacesContext.getCurrentInstance();
    if (ctx == null) {
        throw new ContextNotActiveException("FacesContext is not active");
    }
    return ctx;
}
项目:deltaspike    文件:TransactionContext.java   
public boolean isActive()
{
    try
    {
        return TransactionBeanStorage.isOpen() &&
               TransactionBeanStorage.getInstance().getActiveTransactionContext() != null;
    }
    catch (ContextNotActiveException e)
    {
        return false;
    }
}
项目:deltaspike    文件:DeltaSpikePhaseListener.java   
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
    }
}