Search in sources :

Example 26 with AppContext

use of org.apache.openejb.AppContext in project tomee by apache.

the class ValidatorUtil method proxy.

// proxy because depending on when injection/threadcontext is set
// it is better to do it lazily
// this is mainly done for tests since the first lookup will work in TomEE
private static <T> T proxy(final Class<T> t, final String jndi) {
    return t.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { t }, new InvocationHandler() {

        @Override
        public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
            if (Object.class.equals(method.getDeclaringClass())) {
                return method.invoke(this);
            }
            final ThreadContext ctx = ThreadContext.getThreadContext();
            if (ctx != null) {
                return method.invoke(ctx.getBeanContext().getJndiContext().lookup(jndi), args);
            }
            // try to find from current ClassLoader
            // can lead to find the bad validator regarding module separation
            // but since it shares the same classloader
            // it will probably share the same config
            // so the behavior will be the same
            // + this code should rarely be used
            final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
            if (tccl == null) {
                return null;
            }
            final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
            Object value = null;
            for (final AppContext appContext : containerSystem.getAppContexts()) {
                final ClassLoader appContextClassLoader = appContext.getClassLoader();
                if (tccl.equals(appContextClassLoader) || appContextClassLoader.equals(tccl)) {
                    final Collection<String> tested = new ArrayList<String>();
                    for (final BeanContext bean : appContext.getBeanContexts()) {
                        if (BeanContext.Comp.class.equals(bean.getBeanClass())) {
                            final String uniqueId = bean.getModuleContext().getUniqueId();
                            if (tested.contains(uniqueId)) {
                                continue;
                            }
                            tested.add(uniqueId);
                            try {
                                value = containerSystem.getJNDIContext().lookup((jndi.endsWith("Factory") ? Assembler.VALIDATOR_FACTORY_NAMING_CONTEXT : Assembler.VALIDATOR_NAMING_CONTEXT) + uniqueId);
                                break;
                            } catch (final NameNotFoundException nnfe) {
                            // no-op
                            }
                        }
                    }
                    if (ClassLoader.getSystemClassLoader() != appContextClassLoader) {
                        break;
                    }
                // else we surely have a single AppContext so let's try WebContext
                }
                for (final WebContext web : appContext.getWebContexts()) {
                    final ClassLoader webClassLoader = web.getClassLoader();
                    if (webClassLoader.equals(tccl) || tccl.equals(webClassLoader)) {
                        value = web.getJndiEnc().lookup(jndi);
                        break;
                    }
                }
                if (value != null) {
                    break;
                }
            }
            if (value != null) {
                return method.invoke(value, args);
            }
            return null;
        }

        @Override
        public String toString() {
            return "Proxy::" + t.getName();
        }
    }));
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) WebContext(org.apache.openejb.core.WebContext) NameNotFoundException(javax.naming.NameNotFoundException) AppContext(org.apache.openejb.AppContext) ThreadContext(org.apache.openejb.core.ThreadContext) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) BeanContext(org.apache.openejb.BeanContext)

Example 27 with AppContext

use of org.apache.openejb.AppContext 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)

Example 28 with AppContext

use of org.apache.openejb.AppContext in project tomee by apache.

the class OWBContextThreadListener method contextEntered.

@Override
public void contextEntered(final ThreadContext oldContext, final ThreadContext newContext) {
    final BeanContext beanContext = newContext.getBeanContext();
    if (beanContext == null) {
        // OWBContextHolder will be null so calling contextExited will throw a NPE
        return;
    }
    final ModuleContext moduleContext = beanContext.getModuleContext();
    //TODO its not clear what the scope for one of these context should be: ejb, module, or app
    //For now, go with the attachment of the BeanManager to AppContext
    final AppContext appContext = moduleContext.getAppContext();
    final WebBeansContext owbContext = appContext.getWebBeansContext();
    final Object oldOWBContext;
    if (owbContext != null) {
        oldOWBContext = singletonService.contextEntered(owbContext);
    } else {
        oldOWBContext = null;
    }
    final OWBContextHolder holder = new OWBContextHolder(oldOWBContext);
    newContext.set(OWBContextHolder.class, holder);
}
Also used : BeanContext(org.apache.openejb.BeanContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) AppContext(org.apache.openejb.AppContext) ModuleContext(org.apache.openejb.ModuleContext)

Example 29 with AppContext

use of org.apache.openejb.AppContext in project tomee by apache.

the class javaURLContextFactory method getContext.

public static Context getContext() {
    final ThreadContext callContext = ThreadContext.getThreadContext();
    if (callContext == null) {
        final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
        final ClassLoader current = Thread.currentThread().getContextClassLoader();
        final Context globalContext = containerSystem.getJNDIContext();
        if (current == null) {
            return globalContext;
        }
        for (final AppContext appContext : containerSystem.getAppContexts()) {
            for (final WebContext web : appContext.getWebContexts()) {
                // more specific first
                if (current.equals(web.getClassLoader())) {
                    return new ContextHandler(web.getJndiEnc());
                }
            }
            if (current.equals(appContext.getClassLoader())) {
                return new ContextHandler(appContext.getAppJndiContext());
            }
        }
        return globalContext;
    }
    final BeanContext di = callContext.getBeanContext();
    if (di != null) {
        return di.getJndiEnc();
    } else {
        final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
        return containerSystem.getJNDIContext();
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) BeanContext(org.apache.openejb.BeanContext) WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext) Context(javax.naming.Context) ThreadContext(org.apache.openejb.core.ThreadContext) ContextHandler(org.apache.openejb.core.ivm.ContextHandler) BeanContext(org.apache.openejb.BeanContext) WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext) ThreadContext(org.apache.openejb.core.ThreadContext)

Example 30 with AppContext

use of org.apache.openejb.AppContext in project tomee by apache.

the class ApplicationComposers method stopApplication.

public void stopApplication() throws NamingException {
    if (appContext != null && appContext.getWebBeansContext() != null) {
        final ContextsService contextsService = appContext.getWebBeansContext().getContextsService();
        // No need to stop the ConversationContext manually as it gets stored inside the SessionContext as Bean
        contextsService.endContext(SessionScoped.class, session);
        contextsService.endContext(RequestScoped.class, null);
    }
    if (appInfo != null) {
        try {
            assembler.destroyApplication(appInfo.path);
        } catch (final Exception e) {
        // no-op
        }
    }
    final ContainerSystem component = SystemInstance.get().getComponent(ContainerSystem.class);
    if (null != component) {
        final Context context = component.getJNDIContext();
        for (final String entry : globalJndiEntries) {
            context.unbind(entry);
        }
    }
    globalJndiEntries.clear();
    if (mockCdiContexts() && appContext != null && appContext.getWebBeansContext() != null) {
        try {
            ScopeHelper.stopContexts(appContext.getWebBeansContext().getContextsService(), servletContext, session);
        } catch (final Exception e) {
        // no-op
        }
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) WebContext(org.apache.openejb.core.WebContext) InitialContext(javax.naming.InitialContext) MockServletContext(org.apache.webbeans.web.lifecycle.test.MockServletContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) ThreadContext(org.apache.openejb.core.ThreadContext) AppContext(org.apache.openejb.AppContext) ContextsService(org.apache.webbeans.spi.ContextsService) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) MalformedURLException(java.net.MalformedURLException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException)

Aggregations

AppContext (org.apache.openejb.AppContext)44 BeanContext (org.apache.openejb.BeanContext)23 WebContext (org.apache.openejb.core.WebContext)17 ContainerSystem (org.apache.openejb.spi.ContainerSystem)16 ModuleContext (org.apache.openejb.ModuleContext)11 Context (javax.naming.Context)9 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)9 Assembler (org.apache.openejb.assembler.classic.Assembler)9 ArrayList (java.util.ArrayList)8 WebBeansContext (org.apache.webbeans.config.WebBeansContext)8 InitialContext (javax.naming.InitialContext)7 NamingException (javax.naming.NamingException)7 ServletContext (javax.servlet.ServletContext)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 Properties (java.util.Properties)6 WebAppInfo (org.apache.openejb.assembler.classic.WebAppInfo)6 OpenEJBException (org.apache.openejb.OpenEJBException)5 AppInfo (org.apache.openejb.assembler.classic.AppInfo)5 ThreadContext (org.apache.openejb.core.ThreadContext)5