org.apache.webbeans.spi.ContextsService Java Examples

The following examples show how to use org.apache.webbeans.spi.ContextsService. 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: OpenWebBeansContextControl.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
private void startSessionScope()
{
    ContextsService contextsService = getContextsService();

    Object mockSession = null;
    if (isServletApiAvailable())
    {
        mockSession = mockSessions.get();
        if (mockSession == null)
        {
            // we simply use the ThreadName as 'sessionId'
            mockSession = OwbHelper.getMockSession(Thread.currentThread().getName());
            mockSessions.set(mockSession);
        }
    }
    contextsService.startContext(SessionScoped.class, mockSession);
}
 
Example #2
Source File: ScopeRule.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {
        @Override
        public void evaluate() throws Throwable {
            final ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
            try {
                scopes.forEach(s -> contextsService.startContext(s, null));
                base.evaluate();
            } finally {
                Collections.reverse(scopes);
                scopes.forEach(s -> contextsService.endContext(s, null));
                Collections.reverse(scopes);
            }
        }
    };
}
 
Example #3
Source File: MeecrowaveSeContainerInitializer.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
@Override
protected SeContainer newContainer(final WebBeansContext context) {
    final Meecrowave meecrowave = new Meecrowave(builder);
    if (!services.containsKey(ContextsService.class.getName())) { // forced otherwise we mess up the env with owb-se
        context.registerService(ContextsService.class, new WebContextsService(context));
    }
    return new OWBContainer(context, meecrowave) {
        {
            meecrowave.bake();
        }

        @Override
        protected void doClose() {
            meecrowave.close();
        }
    };
}
 
Example #4
Source File: HAOpenWebBeansTestLifeCycle.java    From HotswapAgent with GNU General Public License v2.0 6 votes vote down vote up
public void beforeStopApplication(Object endObject)
{
    WebBeansContext webBeansContext = getWebBeansContext();
    ContextsService contextsService = webBeansContext.getContextsService();
    contextsService.endContext(Singleton.class, null);
    contextsService.endContext(ApplicationScoped.class, null);
    contextsService.endContext(RequestScoped.class, null);
    contextsService.endContext(SessionScoped.class, mockHttpSession);

    ELContextStore elStore = ELContextStore.getInstance(false);
    if (elStore == null)
    {
        return;
    }
    elStore.destroyELContextStore();
}
 
Example #5
Source File: StatefulConversationScopedTOMEE1138Test.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Before
public void startConversation() {
    final WebBeansContext webBeansContext = WebBeansContext.currentInstance();
    webBeansContext.registerService(ConversationService.class, new ConversationService() {
        @Override
        public String getConversationId() {
            return "conversation-test";
        }

        @Override
        public String generateConversationId() {
            return "cid_1";
        }
    });
    webBeansContext.getService(ContextsService.class).startContext(ConversationScoped.class, null);
}
 
Example #6
Source File: EnsureRequestScopeThreadLocalIsCleanUpTest.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Test
public void ensureRequestContextCanBeRestarted() throws Exception {
    final ApplicationComposers composers = new ApplicationComposers(EnsureRequestScopeThreadLocalIsCleanUpTest.class);
    composers.before(this);
    final CdiAppContextsService contextsService = CdiAppContextsService.class.cast(WebBeansContext.currentInstance().getService(ContextsService.class));
    final Context req1 = contextsService.getCurrentContext(RequestScoped.class);
    assertNotNull(req1);
    final Context session1 = contextsService.getCurrentContext(SessionScoped.class);
    assertNotNull(session1);
    contextsService.endContext(RequestScoped.class, null);
    contextsService.startContext(RequestScoped.class, null);
    final Context req2 = contextsService.getCurrentContext(RequestScoped.class);
    assertNotSame(req1, req2);
    final Context session2 = contextsService.getCurrentContext(SessionScoped.class);
    assertSame(session1, session2);
    composers.after();
    assertNull(contextsService.getCurrentContext(RequestScoped.class));
    assertNull(contextsService.getCurrentContext(SessionScoped.class));
}
 
Example #7
Source File: RequestScopedThreadContextListener.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Override
public void contextEntered(final ThreadContext oldContext, final ThreadContext newContext) {

    final BeanContext beanContext = newContext.getBeanContext();

    final WebBeansContext webBeansContext = beanContext.getModuleContext().getAppContext().getWebBeansContext();
    if (webBeansContext == null) {
        return;
    }

    final ContextsService contextsService = webBeansContext.getContextsService();

    final Context requestContext = CdiAppContextsService.class.cast(contextsService).getRequestContext(false);

    if (requestContext == null) {
        contextsService.startContext(RequestScoped.class, CdiAppContextsService.EJB_REQUEST_EVENT);
        newContext.set(DestroyContext.class, new DestroyContext(contextsService, newContext));
    }
}
 
Example #8
Source File: ScopeHelper.java    From tomee with Apache License 2.0 5 votes vote down vote up
public static void stopContexts(final ContextsService contextsService, final ServletContext servletContext, final HttpSession session) throws Exception {
    contextsService.endContext(SessionScoped.class, session);
    contextsService.endContext(RequestScoped.class, null);
    contextsService.endContext(ConversationScoped.class, null);
    if (CdiAppContextsService.class.isInstance(contextsService)) {
        CdiAppContextsService.class.cast(contextsService).removeThreadLocals();
    }
}
 
Example #9
Source File: OpenWebBeansContextControl.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
private void startApplicationScope()
{
    ContextsService contextsService = getContextsService();
    Object mockServletContext = null;
    if (isServletApiAvailable())
    {
        mockServletContext = OwbHelper.getMockServletContext();
    }
    contextsService.startContext(ApplicationScoped.class, mockServletContext);
}
 
Example #10
Source File: OpenWebBeansContextControl.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
private void startSingletonScope()
{
    ContextsService contextsService = getContextsService();
    Object mockServletContext = null;
    if (isServletApiAvailable())
    {
        mockServletContext = OwbHelper.getMockServletContext();
    }
    contextsService.startContext(Singleton.class, mockServletContext);
}
 
Example #11
Source File: OpenWebBeansContextControl.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
public void startContexts()
{
    ContextsService contextsService = getContextsService();

    startSingletonScope();
    startApplicationScope();
    startSessionScope();
    startRequestScope();
    startConversationScope();
}
 
Example #12
Source File: EnsureRequestScopeThreadLocalIsCleanUpTest.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Test
public void runAndCheckThreadLocal() throws Exception {
    final ApplicationComposers composers = new ApplicationComposers(EnsureRequestScopeThreadLocalIsCleanUpTest.class);
    composers.before(this);
    final CdiAppContextsService contextsService = CdiAppContextsService.class.cast(WebBeansContext.currentInstance().getService(ContextsService.class));
    assertNotNull(contextsService.getCurrentContext(RequestScoped.class));
    assertNotNull(contextsService.getCurrentContext(SessionScoped.class));
    composers.after();
    assertNull(contextsService.getCurrentContext(RequestScoped.class));
    assertNull(contextsService.getCurrentContext(SessionScoped.class));
}
 
Example #13
Source File: OpenWebBeansContextControl.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
private void stopSingletonScope()
{
    ContextsService contextsService = getContextsService();

    Object mockServletContext = null;
    if (isServletApiAvailable())
    {
        mockServletContext = OwbHelper.getMockServletContext();
    }
    contextsService.endContext(Singleton.class, mockServletContext);
}
 
Example #14
Source File: OpenWebBeansContextControl.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
private void stopApplicationScope()
{
    ContextsService contextsService = getContextsService();

    Object mockServletContext = null;
    if (isServletApiAvailable())
    {
        mockServletContext = OwbHelper.getMockServletContext();
    }
    contextsService.endContext(ApplicationScoped.class, mockServletContext);
}
 
Example #15
Source File: OpenWebBeansContextControl.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
private void stopSessionScope()
{
    ContextsService contextsService = getContextsService();

    Object mockSession = null;
    if (isServletApiAvailable())
    {
        mockSession = mockSessions.get();
        mockSessions.set(null);
        mockSessions.remove();
    }
    contextsService.endContext(SessionScoped.class, mockSession);
}
 
Example #16
Source File: EndWebBeansListener.java    From tomee with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor
 *
 * @param webBeansContext the OWB context
 */
public EndWebBeansListener(WebBeansContext webBeansContext) {
    this.webBeansContext = webBeansContext;
    if (webBeansContext != null) {
        this.contextsService = CdiAppContextsService.class.cast(webBeansContext.getService(ContextsService.class));
        this.cleanUpSession = Boolean.parseBoolean(webBeansContext.getOpenWebBeansConfiguration()
                .getProperty("tomee.session.remove-cdi-beans-on-passivate", "false"));
    } else {
        this.contextsService = null;
        this.cleanUpSession = false; // ignored anyway
    }
}
 
Example #17
Source File: WebBeansListenerHelper.java    From tomee with Apache License 2.0 5 votes vote down vote up
public static void ensureRequestScope(final ContextsService cs, final ServletRequestListener listener) {
    final Context reqCtx = cs.getCurrentContext(RequestScoped.class);
    if (reqCtx == null || !cs.getCurrentContext(RequestScoped.class).isActive()) {
        listener.requestInitialized(null);
        FAKE_REQUEST.set(true);
    }
}
 
Example #18
Source File: HAOpenWebBeansTestLifeCycle.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
public void beforeStartApplication(Object object)
{
    WebBeansContext webBeansContext = getWebBeansContext();
    ContextsService contextsService = webBeansContext.getContextsService();
    contextsService.startContext(Singleton.class, null);
    contextsService.startContext(ApplicationScoped.class, null);
}
 
Example #19
Source File: HAOpenWebBeansTestLifeCycle.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
public HAOpenWebBeansTestLifeCycle()
{
    super(new Properties()
    {{
        setProperty(ContextsService.class.getName(), WebContextsService.class.getName());
    }});
    this.logger = WebBeansLoggerFacade.getLogger(HAOpenWebBeansTestLifeCycle.class);
}
 
Example #20
Source File: ScopesExtension.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
@Override
public void afterEach(final ExtensionContext extensionContext) {
    ofNullable(extensionContext.getStore(NAMESPACE).get(Holder.class, Holder.class)).map(h -> h.scopes).ifPresent(scopes -> {
        final ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
        final List<Class<? extends Annotation>> list = new ArrayList<>(asList(scopes));
        Collections.reverse(list);
        list.forEach(s -> contextsService.endContext(s, null));
    });
}
 
Example #21
Source File: ScopesExtension.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeEach(final ExtensionContext extensionContext) {
    getScopes(extensionContext).ifPresent(scopes -> {
        final ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
        Stream.of(scopes).forEach(s -> contextsService.startContext(s, null));
        extensionContext.getStore(NAMESPACE).put(Holder.class, new Holder(scopes));
    });
}
 
Example #22
Source File: OpenWebBeansContextControl.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
private ContextsService getContextsService()
{
    WebBeansContext webBeansContext = WebBeansContext.currentInstance();
    return webBeansContext.getContextsService();
}
 
Example #23
Source File: OpenWebBeansContextControl.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
private void stopRequestScope()
{
    ContextsService contextsService = getContextsService();

    contextsService.endContext(RequestScoped.class, null);
}
 
Example #24
Source File: OpenWebBeansContextControl.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
private void startConversationScope()
{
    ContextsService contextsService = getContextsService();

    contextsService.startContext(ConversationScoped.class, null);
}
 
Example #25
Source File: OpenWebBeansContextControl.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
private void stopConversationScope()
{
    ContextsService contextsService = getContextsService();

    contextsService.endContext(ConversationScoped.class, null);
}
 
Example #26
Source File: OpenWebBeansContextControl.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
private void startRequestScope()
{
    ContextsService contextsService = getContextsService();

    contextsService.startContext(RequestScoped.class, null);
}
 
Example #27
Source File: StatefulConversationScopedTOMEE1138Test.java    From tomee with Apache License 2.0 4 votes vote down vote up
@After
public void stopConversation() {
    WebBeansContext.currentInstance().getService(ContextsService.class).endContext(ConversationScoped.class, null);
}
 
Example #28
Source File: JMS2AMQTest.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Test
public void cdi() throws InterruptedException {
    final String text = TEXT + "3";

    final AtomicReference<Throwable> error = new AtomicReference<>();
    final CountDownLatch ready = new CountDownLatch(1);
    final CountDownLatch over = new CountDownLatch(1);
    new Thread() {
        {
            setName(JMS2AMQTest.class.getName() + ".cdi#receiver");
        }

        @Override
        public void run() {
            final ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
            contextsService.startContext(RequestScoped.class, null); // spec defines it for request scope an transaction scope
            try {
                ready.countDown();
                assertEquals(text, context.createConsumer(destination3).receiveBody(String.class, TimeUnit.MINUTES.toMillis(1)));

                // ensure we dont do a NPE if there is nothing to read
                assertNull(context.createConsumer(destination3).receiveBody(String.class, 100));
            } catch (final Throwable t) {
                error.set(t);
            } finally {
                contextsService.endContext(RequestScoped.class, null);
                over.countDown();
            }
        }
    }.start();

    ready.await(1, TimeUnit.MINUTES);
    sleep(150); // just to ensure we called receive already

    // now send the message
    try (final JMSContext context = cf.createContext()) {
        context.createProducer().send(destination3, text);
    } catch (final JMSRuntimeException ex) {
        fail(ex.getMessage());
    }

    over.await(1, TimeUnit.MINUTES);

    // ensure we got the message and no exception
    final Throwable exception = error.get();
    if (exception != null) {
        exception.printStackTrace();
    }
    assertNull(exception == null ? "ok" : exception.getMessage(), exception);
}
 
Example #29
Source File: OpenEJBLifecycle.java    From tomee with Apache License 2.0 4 votes vote down vote up
/**
 * @return the contextsService
 */
public ContextsService getContextService() {
    return contextsService;
}
 
Example #30
Source File: RequestScopedThreadContextListener.java    From tomee with Apache License 2.0 4 votes vote down vote up
private DestroyContext(final ContextsService contextsService, final ThreadContext threadContext) {
    this.contextsService = contextsService;
    this.threadContext = threadContext;
}