Search in sources :

Example 16 with BeanManagerImpl

use of org.apache.webbeans.container.BeanManagerImpl in project tomee by apache.

the class CDILoginModule method initialize.

@Override
public void initialize(final Subject subject, final CallbackHandler callbackHandler, final Map<String, ?> sharedState, final Map<String, ?> options) {
    final WebBeansContext webBeansContext = WebBeansContext.currentInstance();
    final BeanManagerImpl bm = webBeansContext.getBeanManagerImpl();
    if (!bm.isInUse()) {
        throw new OpenEJBRuntimeException("CDI not activated");
    }
    String delegate = String.valueOf(options.get("delegate"));
    if ("null".equals(delegate)) {
        final String app = findAppName(webBeansContext);
        delegate = String.valueOf(options.get(app));
        if ("null".equals(delegate)) {
            throw new OpenEJBRuntimeException("Please specify a delegate class");
        }
    }
    final Class<?> clazz;
    try {
        clazz = Thread.currentThread().getContextClassLoader().loadClass(delegate);
    } catch (final ClassNotFoundException e) {
        throw new OpenEJBRuntimeException(e.getMessage(), e);
    }
    cc = bm.createCreationalContext(null);
    final String cdiName = String.valueOf(options.get("cdiName"));
    if ("true".equals(String.valueOf(options.get("loginModuleAsCdiBean")))) {
        final Set<Bean<?>> beans;
        if ("null".equals(cdiName)) {
            beans = bm.getBeans(clazz);
        } else {
            beans = bm.getBeans(cdiName);
        }
        loginModule = LoginModule.class.cast(bm.getReference(bm.resolve(beans), clazz, cc));
    } else {
        try {
            loginModule = LoginModule.class.cast(clazz.newInstance());
            OWBInjector.inject(bm, loginModule, cc);
        } catch (final Exception e) {
            throw new OpenEJBRuntimeException("Can't inject into delegate class " + loginModule, e);
        }
    }
    loginModule.initialize(subject, callbackHandler, sharedState, options);
}
Also used : OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) LoginModule(javax.security.auth.spi.LoginModule) LoginException(javax.security.auth.login.LoginException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) Bean(javax.enterprise.inject.spi.Bean)

Example 17 with BeanManagerImpl

use of org.apache.webbeans.container.BeanManagerImpl in project tomee by apache.

the class OpenEJBEnricher method resolve.

public static Object[] resolve(final AppContext appContext, final TestClass ignored, final Method method) {
    // suppose all is a CDI bean...
    final Object[] values = new Object[method.getParameterTypes().length];
    if (appContext == null) {
        return values;
    }
    final List<BeanManager> beanManagers = new ArrayList<>();
    final BeanManager bm = findBeanManager(appContext);
    if (bm != null) {
        // then add web bean manager first, TODO: selection of the webapp containing the test?
        for (final WebContext web : appContext.getWebContexts()) {
            final WebBeansContext webBeansContext = web.getWebBeansContext();
            if (webBeansContext == null) {
                continue;
            }
            final BeanManagerImpl webAppBm = webBeansContext.getBeanManagerImpl();
            if (bm != webAppBm) {
                beanManagers.add(webAppBm);
            }
        }
        beanManagers.add(bm);
    }
    if (beanManagers.isEmpty()) {
        return values;
    }
    final Class<?>[] parameterTypes = method.getParameterTypes();
    for (int i = 0; i < parameterTypes.length; i++) {
        Exception ex = null;
        for (final BeanManager beanManager : beanManagers) {
            try {
                values[i] = getParamInstance(beanManager, i, method);
                break;
            } catch (final Exception e) {
                ex = e;
            }
        }
        if (ex != null) {
            LOGGER.info(ex.getMessage());
        }
    }
    return values;
}
Also used : WebContext(org.apache.openejb.core.WebContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) ArrayList(java.util.ArrayList) TestClass(org.jboss.arquillian.test.spi.TestClass) BeanManager(javax.enterprise.inject.spi.BeanManager) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) OpenEJBException(org.apache.openejb.OpenEJBException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException)

Aggregations

BeanManagerImpl (org.apache.webbeans.container.BeanManagerImpl)17 WebBeansContext (org.apache.webbeans.config.WebBeansContext)8 Bean (javax.enterprise.inject.spi.Bean)7 ArrayList (java.util.ArrayList)4 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)4 IOException (java.io.IOException)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 OwbBean (org.apache.webbeans.component.OwbBean)3 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 DefinitionException (javax.enterprise.inject.spi.DefinitionException)2 DeploymentException (javax.enterprise.inject.spi.DeploymentException)2 ObserverMethod (javax.enterprise.inject.spi.ObserverMethod)2 ServletException (javax.servlet.ServletException)2 BusException (org.apache.cxf.BusException)2 EndpointException (org.apache.cxf.endpoint.EndpointException)2 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)2 ResponseConstraintViolationException (org.apache.cxf.validation.ResponseConstraintViolationException)2 BeanContext (org.apache.openejb.BeanContext)2 InjectionProcessor (org.apache.openejb.InjectionProcessor)2