Java Code Examples for org.apache.webbeans.config.WebBeansContext#currentInstance()

The following examples show how to use org.apache.webbeans.config.WebBeansContext#currentInstance() . 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: CustomELAdapter.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Override
public ELResolver getOwbELResolver() {
    WebBeansContext old = null;
    boolean exit = false;
    try { // just some safety around this but should be very very rare
        WebBeansContext.currentInstance();
    } catch (final IllegalStateException ise) {
        old = ThreadSingletonServiceImpl.enter(appContext.getWebBeansContext());
        exit = true;
    }
    try {
        return new WebBeansELResolver();
    } finally {
        if (exit) {
            ThreadSingletonServiceImpl.exit(old);
        }
    }
}
 
Example 2
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 3
Source File: CdiEventRealm.java    From tomee with Apache License 2.0 5 votes vote down vote up
private BeanManager beanManager() {
    final WebBeansContext webBeansContext = WebBeansContext.currentInstance();
    if (webBeansContext == null) {
        return null; // too early to have a cdi bean
    }
    return webBeansContext.getBeanManagerImpl();
}
 
Example 4
Source File: CdiPasswordCipher.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public String decrypt(final char[] encryptedPassword) {
    final String string = new String(encryptedPassword);
    final BeanManagerImpl mgr;
    try {
        final WebBeansContext wbc = WebBeansContext.currentInstance();
        mgr = wbc.getBeanManagerImpl();
        if (!mgr.isInUse()) { // not yet the time to use CDI, container is not started
            // would be cool to log a warning here but would pollute the logs with false positives
            return "cipher:cdi:" + string;
        }
    } catch (final IllegalStateException ise) { // no cdi
        return "cipher:cdi:" + string;
    }

    final int split = string.indexOf(':');
    final String delegate = string.substring(0, split);
    final String pwdStr = string.substring(split + 1, string.length());
    final char[] pwd = pwdStr.toCharArray();

    try {
        final Class<?> beanType = Thread.currentThread().getContextClassLoader().loadClass(delegate);
        final Bean<?> bean = mgr.resolve(mgr.getBeans(beanType));
        if (bean == null) {
            throw new IllegalArgumentException("No bean for " + delegate);
        }

        final CreationalContext<?> cc = mgr.createCreationalContext(null);
        try {
            return PasswordCipher.class.cast(mgr.getReference(bean, PasswordCipher.class, cc)).decrypt(pwd);
        } finally {
            if (!mgr.isNormalScope(bean.getScope())) {
                cc.release();
            }
        }
    } catch (final ClassNotFoundException e) {
        throw new IllegalArgumentException("Can't find " + delegate, e);
    }
}
 
Example 5
Source File: OpenEJBLifecycle.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public T get() {
    if (webBeansContext == null) {
        webBeansContext = WebBeansContext.currentInstance();
    }
    return (T) SystemInstance.get().getComponent(type);
}
 
Example 6
Source File: EarEjbButNoCdiTest.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Test
public void check() {
    assertEquals("1", b1.val());
    try {
        WebBeansContext.currentInstance();
        fail();
    } catch (final IllegalStateException ise) {
        // ok
    }
}
 
Example 7
Source File: WebbeansContextInEmbeddedModeTest.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Test
public void checkWebbeansContext() {
    final WebBeansContext ctx1 = WebBeansContext.currentInstance();
    final List<AppContext> appCtxs = SystemInstance.get().getComponent(ContainerSystem.class).getAppContexts();
    assertEquals(1, appCtxs.size());
    final WebBeansContext ctx2 = appCtxs.iterator().next().getWebBeansContext();
    assertSame(ctx1, ctx2);
}
 
Example 8
Source File: ExtraJCacheExtensionTest.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void startContainer()
{
    final WebBeansContext webBeansContext = WebBeansContext.currentInstance();
    lifecycle = webBeansContext.getService(ContainerLifecycle.class);
    lifecycle.startApplication(null);
    bm = webBeansContext.getBeanManagerImpl();
}
 
Example 9
Source File: OWBBeanProvider.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
public OWBBeanProvider()
{
    final WebBeansContext webBeansContext = WebBeansContext.currentInstance();
    final ContainerLifecycle lifecycle = webBeansContext.getService(ContainerLifecycle.class);
    lifecycle.startApplication(null);
    Runtime.getRuntime().addShutdownHook(new Thread()
    {
        @Override
        public void run()
        {
            lifecycle.stopApplication(null);
        }
    });
    bm = webBeansContext.getBeanManagerImpl();
}
 
Example 10
Source File: OpenEJBHttpRegistry.java    From tomee with Apache License 2.0 4 votes vote down vote up
public void onMessage(HttpRequest request, HttpResponse response) throws Exception {
    final Thread thread = Thread.currentThread();
    final ClassLoader oldCl = thread.getContextClassLoader();

    WebBeansContext wbc = null;
    try {
        if (request instanceof HttpRequestImpl) {
            final HttpRequestImpl httpRequest = HttpRequestImpl.class.cast(request);
            final WebContext web = findWebContext(request.getURI() == null ? request.getContextPath() : request.getURI().getPath());
            if (web != null) {
                httpRequest.setApplication(web);

                if (web.getClassLoader() != null) {
                    thread.setContextClassLoader(web.getClassLoader());
                } else if (web.getAppContext().getClassLoader() != null) {
                    thread.setContextClassLoader(web.getAppContext().getClassLoader());
                }

                final String ctx = (web.getContextRoot().startsWith("/") ? "" : "/") + web.getContextRoot();
                httpRequest.initPathFromContext(ctx);
                wbc = web.getWebbeansContext() != null ? web.getWebbeansContext() : web.getAppContext().getWebBeansContext();
            } else {
                thread.setContextClassLoader(classLoader);

                if (SystemInstance.isInitialized()) { // avoid to rely on default if we didnt init it and then create lazily a context
                    try { // surely an issue or something just tolerated for fake webapps
                        wbc = WebBeansContext.currentInstance();
                    } catch (final IllegalStateException ise) {
                        // no-op
                    }
                }
            }
            if (wbc != null) {
                httpRequest.setAttribute("openejb_owb_context", wbc);
                initCdi(wbc, httpRequest).init();
            }
        }

        delegate.onMessage(request, response);
    } finally {
        if (wbc != null) {
            HttpRequestImpl.class.cast(request).destroy();
        }

        thread.setContextClassLoader(oldCl);
    }
}
 
Example 11
Source File: SingleApplicationComposerRunner.java    From tomee with Apache License 2.0 4 votes vote down vote up
private static void composerInject(final Object target) throws IllegalAccessException {
    WebBeansContext wbc = null;
    try {
        wbc = WebBeansContext.currentInstance();
    } catch (final IllegalStateException ise) {
        // no-op
    }
    if (wbc != null) {
        OWBInjector.inject(wbc.getBeanManagerImpl(), target, null);
    }

    final Object app = APP.get();
    final Class<?> aClass = target.getClass();
    for (final Field f : aClass.getDeclaredFields()) {
        if (f.isAnnotationPresent(RandomPort.class)) {
            for (final Field field : app.getClass().getDeclaredFields()) {
                if (field.getType() ==  f.getType()) {
                    if (!field.isAccessible()) {
                        field.setAccessible(true);
                    }
                    if (!f.isAccessible()) {
                        f.setAccessible(true);
                    }

                    final Object value = field.get(app);
                    f.set(target, value);
                    break;
                }
            }
        } else if (f.isAnnotationPresent(Application.class)) {
            if (!f.isAccessible()) {
                f.setAccessible(true);
            }
            f.set(target, app);
        }
    }
    final Class<?> superclass = aClass.getSuperclass();
    if (superclass != Object.class) {
        composerInject(superclass);
    }
}
 
Example 12
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();
}