Search in sources :

Example 11 with BeanManagerImpl

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

the class ValidatorBuilder method newInstance.

private static <T> T newInstance(final OpenEjbConfig config, final Class<T> clazz) throws Exception {
    final WebBeansContext webBeansContext = AppFinder.findAppContextOrWeb(Thread.currentThread().getContextClassLoader(), AppFinder.WebBeansContextTransformer.INSTANCE);
    if (webBeansContext == null) {
        return clazz.newInstance();
    }
    final BeanManagerImpl beanManager = webBeansContext.getBeanManagerImpl();
    if (!beanManager.isInUse()) {
        return clazz.newInstance();
    }
    final AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(clazz);
    final InjectionTarget<T> it = beanManager.createInjectionTarget(annotatedType);
    final CreationalContext<T> context = beanManager.createCreationalContext(null);
    final T instance = it.produce(context);
    it.inject(instance, context);
    it.postConstruct(instance);
    config.releasables.add(new Releasable<T>(context, it, instance));
    return instance;
}
Also used : WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl)

Example 12 with BeanManagerImpl

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

the class WebappBeanManager method mergeBeans.

private Set<Bean<?>> mergeBeans() {
    // override parent one with a "webapp" bean list
    final Set<Bean<?>> allBeans = new CopyOnWriteArraySet<>();
    final BeanManagerImpl parentBm = getParentBm();
    if (parentBm != null) {
        for (final Bean<?> bean : parentBm.getBeans()) {
            if (filter.accept(bean)) {
                allBeans.add(bean);
            }
        }
    }
    allBeans.addAll(super.getBeans());
    return allBeans;
}
Also used : BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) OwbBean(org.apache.webbeans.component.OwbBean) ExtensionBean(org.apache.webbeans.component.ExtensionBean) BuiltInOwbBean(org.apache.webbeans.component.BuiltInOwbBean) Bean(javax.enterprise.inject.spi.Bean)

Example 13 with BeanManagerImpl

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

the class WebappBeanManager method resolveObserverMethods.

@Override
public <T> Set<ObserverMethod<? super T>> resolveObserverMethods(final T event, final EventMetadataImpl metadata) {
    final Set<ObserverMethod<? super T>> set = new HashSet<>();
    set.addAll(getNotificationManager().resolveObservers(event, metadata, false));
    if (isEvent(event)) {
        final BeanManagerImpl parentBm = getParentBm();
        if (parentBm != null) {
            set.addAll(parentBm.getNotificationManager().resolveObservers(event, metadata, false));
        }
    }
    return set;
}
Also used : BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) HashSet(java.util.HashSet) ObserverMethod(javax.enterprise.inject.spi.ObserverMethod)

Example 14 with BeanManagerImpl

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

the class CdiPlugin method registerEEBeans.

@Override
public void registerEEBeans() {
    BeanManagerImpl beanManagerImpl = webBeansContext.getBeanManagerImpl();
    beanManagerImpl.addInternalBean(new org.apache.webbeans.ee.beans.ValidatorBean(webBeansContext));
    beanManagerImpl.addInternalBean(new org.apache.webbeans.ee.beans.ValidatorFactoryBean(webBeansContext));
    beanManagerImpl.addInternalBean(new org.apache.webbeans.ee.beans.UserTransactionBean(webBeansContext));
}
Also used : BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl)

Example 15 with BeanManagerImpl

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

the class ThreadSingletonServiceImpl method initialize.

@Override
public void initialize(final StartupObject startupObject) {
    if (lazyInit == null) {
        // done here cause Cdibuilder trigger this class loading and that's from Warmup so we can't init too early config
        synchronized (this) {
            if (lazyInit == null) {
                lazyInit = new Object();
                cachedApplicationScoped = "true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.cdi.applicationScope.cached", "true").trim());
                cachedRequestScoped = "true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.cdi.requestScope.cached", "true").trim());
                cachedSessionScoped = "true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.cdi.sessionScope.cached", "true").trim());
            }
        }
    }
    final AppContext appContext = startupObject.getAppContext();
    appContext.setCdiEnabled(hasBeans(startupObject.getAppInfo()));
    //initialize owb context, cf geronimo's OpenWebBeansGBean
    final Properties properties = new Properties();
    properties.setProperty(OpenWebBeansConfiguration.APPLICATION_IS_JSP, "true");
    properties.setProperty(OpenWebBeansConfiguration.USE_EJB_DISCOVERY, "true");
    //from CDI builder
    properties.setProperty(OpenWebBeansConfiguration.INTERCEPTOR_FORCE_NO_CHECKED_EXCEPTIONS, "false");
    properties.setProperty(SecurityService.class.getName(), ManagedSecurityService.class.getName());
    properties.setProperty(OpenWebBeansConfiguration.CONVERSATION_PERIODIC_DELAY, "1800000");
    properties.setProperty(OpenWebBeansConfiguration.APPLICATION_SUPPORTS_CONVERSATION, "true");
    properties.setProperty(OpenWebBeansConfiguration.IGNORED_INTERFACES, "org.apache.aries.proxy.weaving.WovenProxy");
    final boolean tomee = SystemInstance.get().getProperty("openejb.loader", "foo").startsWith("tomcat");
    final String defaultNormalScopeHandlerClass = NormalScopedBeanInterceptorHandler.class.getName();
    properties.setProperty("org.apache.webbeans.proxy.mapping.javax.enterprise.context.ApplicationScoped", cachedApplicationScoped ? ApplicationScopedBeanInterceptorHandler.class.getName() : defaultNormalScopeHandlerClass);
    properties.setProperty("org.apache.webbeans.proxy.mapping.javax.enterprise.context.RequestScoped", tomee && cachedRequestScoped ? RequestScopedBeanInterceptorHandler.class.getName() : defaultNormalScopeHandlerClass);
    properties.setProperty("org.apache.webbeans.proxy.mapping.javax.enterprise.context.SessionScoped", tomee && cachedSessionScoped ? SessionScopedBeanInterceptorHandler.class.getName() : defaultNormalScopeHandlerClass);
    properties.put(OpenWebBeansConfiguration.PRODUCER_INTERCEPTION_SUPPORT, SystemInstance.get().getProperty("openejb.cdi.producer.interception", "true"));
    properties.putAll(appContext.getProperties());
    // services needing WBC as constructor param
    properties.put(ContextsService.class.getName(), CdiAppContextsService.class.getName());
    properties.put(ResourceInjectionService.class.getName(), CdiResourceInjectionService.class.getName());
    properties.put(TransactionService.class.getName(), OpenEJBTransactionService.class.getName());
    // NOTE: ensure user can extend/override all the services = set it only if not present in properties, see WebBeansContext#getService()
    final Map<Class<?>, Object> services = new HashMap<>();
    services.put(AppContext.class, appContext);
    if (!properties.containsKey(ApplicationBoundaryService.class.getName())) {
        services.put(ApplicationBoundaryService.class, new DefaultApplicationBoundaryService());
    }
    if (!properties.containsKey(ScannerService.class.getName())) {
        services.put(ScannerService.class, new CdiScanner());
    }
    if (!properties.containsKey(JNDIService.class.getName())) {
        services.put(JNDIService.class, new OpenEJBJndiService());
    }
    if (!properties.containsKey(BeanArchiveService.class.getName())) {
        services.put(BeanArchiveService.class, new OpenEJBBeanInfoService());
    }
    if (!properties.containsKey(ELAdaptor.class.getName())) {
        try {
            services.put(ELAdaptor.class, new CustomELAdapter(appContext));
        } catch (final NoClassDefFoundError noClassDefFoundError) {
        // no-op: no javax.el
        }
    }
    if (!properties.containsKey(LoaderService.class.getName())) {
        final LoaderService loaderService = SystemInstance.get().getComponent(LoaderService.class);
        if (loaderService == null && !properties.containsKey(LoaderService.class.getName())) {
            services.put(LoaderService.class, new OptimizedLoaderService(appContext.getProperties()));
        } else if (loaderService != null) {
            services.put(LoaderService.class, loaderService);
        }
    }
    final ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    final ClassLoader cl;
    if (oldClassLoader != ThreadSingletonServiceImpl.class.getClassLoader() && ThreadSingletonServiceImpl.class.getClassLoader() != oldClassLoader.getParent()) {
        cl = new MultipleClassLoader(oldClassLoader, ThreadSingletonServiceImpl.class.getClassLoader());
    } else {
        cl = oldClassLoader;
    }
    Thread.currentThread().setContextClassLoader(cl);
    final WebBeansContext webBeansContext;
    Object old = null;
    try {
        if (startupObject.getWebContext() == null) {
            webBeansContext = new WebBeansContext(services, properties);
            appContext.set(WebBeansContext.class, webBeansContext);
        } else {
            webBeansContext = new WebappWebBeansContext(services, properties, appContext.getWebBeansContext());
            startupObject.getWebContext().setWebbeansContext(webBeansContext);
        }
        // we want the same reference as the ContextsService if that's our impl
        if (webBeansContext.getOpenWebBeansConfiguration().supportsConversation() && "org.apache.webbeans.jsf.DefaultConversationService".equals(webBeansContext.getOpenWebBeansConfiguration().getProperty(ConversationService.class.getName()))) {
            webBeansContext.registerService(ConversationService.class, ConversationService.class.cast(webBeansContext.getService(ContextsService.class)));
        }
        final BeanManagerImpl beanManagerImpl = webBeansContext.getBeanManagerImpl();
        beanManagerImpl.addContext(new TransactionContext());
        webBeansContext.getInterceptorsManager().addInterceptorBindingType(Transactional.class);
        SystemInstance.get().fireEvent(new WebBeansContextCreated(webBeansContext));
        old = contextEntered(webBeansContext);
        setConfiguration(webBeansContext.getOpenWebBeansConfiguration());
        try {
            webBeansContext.getService(ContainerLifecycle.class).startApplication(startupObject);
        } catch (final Exception e) {
            throw new DeploymentException("couldn't start owb context", e);
        }
    } finally {
        contextExited(old);
        Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
}
Also used : LoaderService(org.apache.webbeans.spi.LoaderService) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Properties(java.util.Properties) WebBeansContext(org.apache.webbeans.config.WebBeansContext) SecurityService(org.apache.webbeans.spi.SecurityService) MultipleClassLoader(org.apache.openejb.util.classloader.MultipleClassLoader) ContextsService(org.apache.webbeans.spi.ContextsService) TransactionService(org.apache.webbeans.spi.TransactionService) AppContext(org.apache.openejb.AppContext) ResourceInjectionService(org.apache.webbeans.spi.ResourceInjectionService) ConversationService(org.apache.webbeans.spi.ConversationService) DeploymentException(javax.enterprise.inject.spi.DeploymentException) ContainerLifecycle(org.apache.webbeans.spi.ContainerLifecycle) DefaultApplicationBoundaryService(org.apache.webbeans.corespi.se.DefaultApplicationBoundaryService) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) TransactionContext(org.apache.openejb.cdi.transactional.TransactionContext) DeploymentException(javax.enterprise.inject.spi.DeploymentException) MultipleClassLoader(org.apache.openejb.util.classloader.MultipleClassLoader)

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