Search in sources :

Example 1 with ManagedExecutorServiceImpl

use of org.apache.openejb.threads.impl.ManagedExecutorServiceImpl in project tomee by apache.

the class Assembler method ensureWebBeansContext.

private void ensureWebBeansContext(final AppContext appContext) {
    WebBeansContext webBeansContext = appContext.get(WebBeansContext.class);
    if (webBeansContext == null) {
        webBeansContext = appContext.getWebBeansContext();
    } else {
        if (null == appContext.getWebBeansContext()) {
            appContext.setWebBeansContext(webBeansContext);
        }
        return;
    }
    if (webBeansContext == null) {
        final Map<Class<?>, Object> services = new HashMap<>();
        services.put(Executor.class, new ManagedExecutorServiceImpl(ForkJoinPool.commonPool()));
        services.put(JNDIService.class, new OpenEJBJndiService());
        services.put(AppContext.class, appContext);
        services.put(ScannerService.class, new CdiScanner());
        services.put(BeanArchiveService.class, new OpenEJBBeanInfoService());
        services.put(ELAdaptor.class, new CustomELAdapter(appContext));
        services.put(LoaderService.class, new OptimizedLoaderService(appContext.getProperties()));
        final Properties properties = new Properties();
        properties.setProperty(org.apache.webbeans.spi.SecurityService.class.getName(), ManagedSecurityService.class.getName());
        properties.setProperty(ContextsService.class.getName(), CdiAppContextsService.class.getName());
        properties.setProperty(ResourceInjectionService.class.getName(), CdiResourceInjectionService.class.getName());
        properties.setProperty(TransactionService.class.getName(), OpenEJBTransactionService.class.getName());
        // like in ClassDefiner. We need to explicitly set the Proxy service
        if (ClassDefiner.isClassLoaderDefineClass()) {
            properties.setProperty(DefiningClassService.class.getName(), ClassDefiner.class.getName());
        }
        webBeansContext = new WebBeansContext(services, properties);
        appContext.setCdiEnabled(false);
        appContext.set(WebBeansContext.class, webBeansContext);
        appContext.setWebBeansContext(webBeansContext);
    }
}
Also used : ManagedExecutorServiceImpl(org.apache.openejb.threads.impl.ManagedExecutorServiceImpl) ContextsService(org.apache.webbeans.spi.ContextsService) CdiAppContextsService(org.apache.openejb.cdi.CdiAppContextsService) OpenEJBJndiService(org.apache.openejb.cdi.OpenEJBJndiService) TransactionService(org.apache.webbeans.spi.TransactionService) OpenEJBTransactionService(org.apache.openejb.cdi.OpenEJBTransactionService) HashMap(java.util.HashMap) CdiAppContextsService(org.apache.openejb.cdi.CdiAppContextsService) ManagedSecurityService(org.apache.openejb.cdi.ManagedSecurityService) OpenEJBTransactionService(org.apache.openejb.cdi.OpenEJBTransactionService) SuperProperties(org.apache.openejb.util.SuperProperties) Properties(java.util.Properties) CdiResourceInjectionService(org.apache.openejb.cdi.CdiResourceInjectionService) ResourceInjectionService(org.apache.webbeans.spi.ResourceInjectionService) WebBeansContext(org.apache.webbeans.config.WebBeansContext) CustomELAdapter(org.apache.openejb.cdi.CustomELAdapter) OptimizedLoaderService(org.apache.openejb.cdi.OptimizedLoaderService) DefiningClassService(org.apache.webbeans.spi.DefiningClassService) ManagedSecurityService(org.apache.openejb.cdi.ManagedSecurityService) SecurityService(org.apache.openejb.spi.SecurityService) CdiScanner(org.apache.openejb.cdi.CdiScanner) ClassDefiner(org.apache.openejb.util.proxy.ClassDefiner) OpenEJBBeanInfoService(org.apache.openejb.cdi.OpenEJBBeanInfoService) CdiResourceInjectionService(org.apache.openejb.cdi.CdiResourceInjectionService)

Example 2 with ManagedExecutorServiceImpl

use of org.apache.openejb.threads.impl.ManagedExecutorServiceImpl 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.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());
    properties.put("org.apache.webbeans.component.PrincipalBean.proxy", "false");
    // like in ClassDefiner. We need to explicitly set the Proxy service
    if (ClassDefiner.isClassLoaderDefineClass()) {
        properties.setProperty(DefiningClassService.class.getName(), ClassDefiner.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(Executor.class.getName())) {
        services.put(Executor.class, new Executor() {

            // lazy to create threads only for apps requiring it
            private final AtomicReference<Executor> delegate = new AtomicReference<>();

            @Override
            public void execute(final Runnable command) {
                Executor executor = delegate.get();
                if (executor == null) {
                    synchronized (this) {
                        final Executor alreadyUpdated = delegate.get();
                        if (alreadyUpdated == null) {
                            executor = new ManagedExecutorServiceImpl(new ExecutorBuilder().size(3).threadFactory(new ManagedThreadFactoryImpl(appContext.getId() + "-cdi-fireasync-")).prefix("CDIAsyncPool").build(appContext.getOptions()));
                            delegate.compareAndSet(null, executor);
                        } else {
                            executor = alreadyUpdated;
                        }
                    }
                }
                executor.execute(command);
            }

            @Override
            public String toString() {
                return "CDIAsyncEventExecutor(app=" + appContext.getId() + ")";
            }
        });
    }
    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 : ManagedExecutorServiceImpl(org.apache.openejb.threads.impl.ManagedExecutorServiceImpl) LoaderService(org.apache.webbeans.spi.LoaderService) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Properties(java.util.Properties) ExecutorBuilder(org.apache.openejb.util.ExecutorBuilder) Executor(java.util.concurrent.Executor) WebBeansContext(org.apache.webbeans.config.WebBeansContext) SecurityService(org.apache.webbeans.spi.SecurityService) MultipleClassLoader(org.apache.openejb.util.classloader.MultipleClassLoader) ClassDefiner(org.apache.openejb.util.proxy.ClassDefiner) ContextsService(org.apache.webbeans.spi.ContextsService) ManagedThreadFactoryImpl(org.apache.openejb.threads.impl.ManagedThreadFactoryImpl) TransactionService(org.apache.webbeans.spi.TransactionService) AppContext(org.apache.openejb.AppContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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) DefiningClassService(org.apache.webbeans.spi.DefiningClassService) 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

HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 ManagedExecutorServiceImpl (org.apache.openejb.threads.impl.ManagedExecutorServiceImpl)2 ClassDefiner (org.apache.openejb.util.proxy.ClassDefiner)2 WebBeansContext (org.apache.webbeans.config.WebBeansContext)2 ContextsService (org.apache.webbeans.spi.ContextsService)2 DefiningClassService (org.apache.webbeans.spi.DefiningClassService)2 ResourceInjectionService (org.apache.webbeans.spi.ResourceInjectionService)2 TransactionService (org.apache.webbeans.spi.TransactionService)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Executor (java.util.concurrent.Executor)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 DeploymentException (javax.enterprise.inject.spi.DeploymentException)1 AppContext (org.apache.openejb.AppContext)1 CdiAppContextsService (org.apache.openejb.cdi.CdiAppContextsService)1 CdiResourceInjectionService (org.apache.openejb.cdi.CdiResourceInjectionService)1 CdiScanner (org.apache.openejb.cdi.CdiScanner)1 CustomELAdapter (org.apache.openejb.cdi.CustomELAdapter)1 ManagedSecurityService (org.apache.openejb.cdi.ManagedSecurityService)1 OpenEJBBeanInfoService (org.apache.openejb.cdi.OpenEJBBeanInfoService)1