Search in sources :

Example 1 with WebAppBuilder

use of org.apache.openejb.assembler.classic.WebAppBuilder in project tomee by apache.

the class OpenEJBDeployableContainer method quickDeploy.

private DeploymentInfo quickDeploy(final Archive<?> archive, final TestClass testClass, final Closeables cls) throws DeploymentException {
    final String name = archive.getName();
    DeploymentInfo info = DEPLOYMENT_INFO.get(name);
    if (info == null) {
        try {
            final AppModule module = OpenEJBArchiveProcessor.createModule(archive, testClass, cls);
            final AppInfo appInfo = configurationFactory.configureApplication(module);
            final WebAppBuilder webAppBuilder = SystemInstance.get().getComponent(WebAppBuilder.class);
            final boolean isEmbeddedWebAppBuilder = webAppBuilder != null && LightweightWebAppBuilder.class.isInstance(webAppBuilder);
            if (isEmbeddedWebAppBuilder) {
                // for now we keep the same classloader, open to discussion if we should recreate it, not sure it does worth it
                final LightweightWebAppBuilder lightweightWebAppBuilder = LightweightWebAppBuilder.class.cast(webAppBuilder);
                for (final WebModule w : module.getWebModules()) {
                    final String moduleId = w.getModuleId();
                    lightweightWebAppBuilder.setClassLoader(moduleId, w.getClassLoader());
                    cls.add(new Closeable() {

                        @Override
                        public void close() throws IOException {
                            lightweightWebAppBuilder.removeClassLoader(moduleId);
                        }
                    });
                }
            }
            final AppContext appCtx = assembler.createApplication(appInfo, module.getClassLoader());
            if (isEmbeddedWebAppBuilder && PROPERTIES.containsKey(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE) && !appCtx.getWebContexts().isEmpty()) {
                cls.add(new Closeable() {

                    @Override
                    public void close() throws IOException {
                        try {
                            final SessionManager sessionManager = SystemInstance.get().getComponent(SessionManager.class);
                            if (sessionManager != null) {
                                for (final WebContext web : appCtx.getWebContexts()) {
                                    sessionManager.destroy(web);
                                }
                            }
                        } catch (final Throwable e) {
                        // no-op
                        }
                    }
                });
            }
            final ServletContext appServletContext = new MockServletContext();
            final HttpSession appSession = new MockHttpSession();
            if (configuration.isStartDefaultScopes() && appCtx.getWebBeansContext() != null) {
                startContexts(appCtx.getWebBeansContext().getContextsService(), appServletContext, appSession);
            }
            info = new DeploymentInfo(appServletContext, appSession, appInfo, appCtx);
            if (configuration.isSingleDeploymentByArchiveName(name)) {
                DEPLOYMENT_INFO.putIfAbsent(name, info);
            }
        } catch (final Exception e) {
            throw new DeploymentException("can't deploy " + name, e);
        }
    }
    return info;
}
Also used : AppModule(org.apache.openejb.config.AppModule) WebContext(org.apache.openejb.core.WebContext) SessionManager(org.apache.openejb.server.httpd.session.SessionManager) HttpSession(javax.servlet.http.HttpSession) MockHttpSession(org.apache.webbeans.web.lifecycle.test.MockHttpSession) Closeable(java.io.Closeable) AppContext(org.apache.openejb.AppContext) WebModule(org.apache.openejb.config.WebModule) IOException(java.io.IOException) LightweightWebAppBuilder(org.apache.openejb.web.LightweightWebAppBuilder) WebAppBuilder(org.apache.openejb.assembler.classic.WebAppBuilder) MockServletContext(org.apache.webbeans.web.lifecycle.test.MockServletContext) NamingException(javax.naming.NamingException) LifecycleException(org.jboss.arquillian.container.spi.client.container.LifecycleException) IOException(java.io.IOException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) DeploymentException(org.jboss.arquillian.container.spi.client.container.DeploymentException) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) AppInfo(org.apache.openejb.assembler.classic.AppInfo) LightweightWebAppBuilder(org.apache.openejb.web.LightweightWebAppBuilder) MockServletContext(org.apache.webbeans.web.lifecycle.test.MockServletContext) ServletContext(javax.servlet.ServletContext) MockHttpSession(org.apache.webbeans.web.lifecycle.test.MockHttpSession) DeploymentException(org.jboss.arquillian.container.spi.client.container.DeploymentException)

Example 2 with WebAppBuilder

use of org.apache.openejb.assembler.classic.WebAppBuilder in project tomee by apache.

the class TomEEAnnotationProvider method getAnnotatedClasses.

@Override
public Map<Class<? extends Annotation>, Set<Class<?>>> getAnnotatedClasses(final ExternalContext ctx) {
    final ClassLoader cl = getClassLoader();
    final WebAppBuilder builder = SystemInstance.get().getComponent(WebAppBuilder.class);
    if (builder == null) {
        throw new IllegalStateException("WebAppBuilder not found in SystemInstance. " + "Ensure the following entry exists in the Tomcat server.xml file: <Listener className=\"org.apache.tomee.catalina.ServerListener\"/>");
    }
    final Map<Class<? extends Annotation>, Set<Class<?>>> map = new HashMap<Class<? extends Annotation>, Set<Class<?>>>();
    final Map<ClassLoader, Map<String, Set<String>>> jsfClasses = builder.getJsfClasses();
    if (jsfClasses == null) {
        throw new IllegalStateException("JsfClasses not found in WebAppBuilder");
    }
    final Map<String, Set<String>> scanned = jsfClasses.get(cl);
    if (scanned == null) {
        return Collections.emptyMap();
    }
    for (final Map.Entry<String, Set<String>> entry : scanned.entrySet()) {
        final Class<? extends Annotation> annotation;
        try {
            annotation = (Class<? extends Annotation>) cl.loadClass(entry.getKey());
        } catch (final ClassNotFoundException e) {
            continue;
        }
        final Set<String> list = entry.getValue();
        final Set<Class<?>> annotated = new HashSet<Class<?>>(list.size());
        for (final String name : list) {
            try {
                annotated.add(cl.loadClass(name));
            } catch (final ClassNotFoundException ignored) {
                LOGGER.warning("class '" + name + "' was found but can't be loaded as a JSF class");
            }
        }
        map.put(annotation, annotated);
    }
    return map;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) WebAppBuilder(org.apache.openejb.assembler.classic.WebAppBuilder) Annotation(java.lang.annotation.Annotation) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 3 with WebAppBuilder

use of org.apache.openejb.assembler.classic.WebAppBuilder in project tomee by apache.

the class LightweightWebAppBuilderListenerExtractor method findByTypeForContext.

public static <T> Collection<T> findByTypeForContext(final String context, final Class<T> type) {
    final WebAppBuilder builder = SystemInstance.get().getComponent(WebAppBuilder.class);
    if (!LightweightWebAppBuilder.class.isInstance(builder)) {
        return Collections.emptyList();
    }
    for (final AppContext app : SystemInstance.get().getComponent(ContainerSystem.class).getAppContexts()) {
        for (final WebContext web : app.getWebContexts()) {
            if (web.getContextRoot().replace("/", "").equals(context.replace("/", ""))) {
                final Collection<Object> potentials = LightweightWebAppBuilder.class.cast(builder).listenersFor(web.getContextRoot());
                if (potentials == null) {
                    return Collections.emptyList();
                }
                final Collection<T> filtered = new ArrayList<>(potentials.size());
                for (final Object o : potentials) {
                    if (type.isInstance(o)) {
                        filtered.add(type.cast(o));
                    }
                }
                return filtered;
            }
        }
    }
    return Collections.emptyList();
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) WebContext(org.apache.openejb.core.WebContext) LightweightWebAppBuilder(org.apache.openejb.web.LightweightWebAppBuilder) AppContext(org.apache.openejb.AppContext) ArrayList(java.util.ArrayList) LightweightWebAppBuilder(org.apache.openejb.web.LightweightWebAppBuilder) WebAppBuilder(org.apache.openejb.assembler.classic.WebAppBuilder)

Example 4 with WebAppBuilder

use of org.apache.openejb.assembler.classic.WebAppBuilder in project tomee by apache.

the class OpenEJBContextConfig method getJsfClasses.

private Set<Class<?>> getJsfClasses(final Context context) {
    final WebAppBuilder builder = SystemInstance.get().getComponent(WebAppBuilder.class);
    final ClassLoader cl = context.getLoader().getClassLoader();
    final Map<String, Set<String>> scanned = builder.getJsfClasses().get(cl);
    if (scanned == null || scanned.isEmpty()) {
        return null;
    }
    final Set<Class<?>> classes = new HashSet<>();
    for (final Set<String> entry : scanned.values()) {
        for (final String name : entry) {
            try {
                classes.add(cl.loadClass(name));
            } catch (final ClassNotFoundException ignored) {
                logger.warning("class '" + name + "' was found but can't be loaded as a JSF class");
            }
        }
    }
    return classes;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) JavaClass(org.apache.tomcat.util.bcel.classfile.JavaClass) TomcatWebAppBuilder(org.apache.tomee.catalina.TomcatWebAppBuilder) WebAppBuilder(org.apache.openejb.assembler.classic.WebAppBuilder) HashSet(java.util.HashSet)

Aggregations

WebAppBuilder (org.apache.openejb.assembler.classic.WebAppBuilder)4 HashSet (java.util.HashSet)2 Set (java.util.Set)2 AppContext (org.apache.openejb.AppContext)2 WebContext (org.apache.openejb.core.WebContext)2 LightweightWebAppBuilder (org.apache.openejb.web.LightweightWebAppBuilder)2 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NamingException (javax.naming.NamingException)1 ServletContext (javax.servlet.ServletContext)1 HttpSession (javax.servlet.http.HttpSession)1 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)1 AppInfo (org.apache.openejb.assembler.classic.AppInfo)1 WebAppInfo (org.apache.openejb.assembler.classic.WebAppInfo)1 AppModule (org.apache.openejb.config.AppModule)1 WebModule (org.apache.openejb.config.WebModule)1