Search in sources :

Example 21 with WebContext

use of org.apache.openejb.core.WebContext 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 22 with WebContext

use of org.apache.openejb.core.WebContext in project tomee by apache.

the class CxfRsHttpListener method fireServerCreated.

private void fireServerCreated(final ClassLoader oldLoader) {
    final Object ctx = AppFinder.findAppContextOrWeb(oldLoader, new AppFinder.Transformer<Object>() {

        @Override
        public Object from(final AppContext appCtx) {
            return appCtx;
        }

        @Override
        public Object from(final WebContext webCtx) {
            return webCtx;
        }
    });
    final AppContext appCtx = AppContext.class.isInstance(ctx) ? AppContext.class.cast(ctx) : WebContext.class.cast(ctx).getAppContext();
    WebContext webContext = appCtx == ctx ? null : WebContext.class.cast(ctx);
    if (webContext == null && appCtx.getWebContexts().size() == 1 && appCtx.getWebContexts().get(0).getClassLoader() == oldLoader) {
        webContext = appCtx.getWebContexts().get(0);
    }
    SystemInstance.get().fireEvent(new ServerCreated(server, appCtx, webContext, this.context));
}
Also used : WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext) ServerCreated(org.apache.openejb.server.cxf.rs.event.ServerCreated) AppFinder(org.apache.openejb.util.AppFinder)

Example 23 with WebContext

use of org.apache.openejb.core.WebContext in project tomee by apache.

the class TomcatJndiBuilder method mergeJava.

public static void mergeJava(final StandardContext standardContext) {
    final ContainerSystem cs = SystemInstance.get().getComponent(ContainerSystem.class);
    final String name = standardContext.getNamingContextListener().getName();
    final Object namingToken = standardContext.getNamingToken();
    ContextAccessController.setWritable(name, namingToken);
    Context root = null;
    try {
        root = ContextBindings.getClassLoader();
    } catch (final NamingException ignored) {
    // shouldn't occur
    // no-op
    }
    // classical deployment - needed because can be overriden through META-INF/context.xml
    final String hostname = org.apache.tomee.catalina.Contexts.getHostname(standardContext);
    String path = standardContext.findParameter(TomcatWebAppBuilder.OPENEJB_WEBAPP_MODULE_ID);
    if (path == null) {
        // standardContext not created by OpenEJB
        path = hostname;
        if (standardContext.getPath().startsWith("/")) {
            path += standardContext.getPath();
        } else {
            path += "/" + standardContext.getPath();
        }
    }
    WebContext webContext = cs.getWebContextByHost(path, hostname);
    if (webContext == null) {
        // tomee-embedded deployment
        webContext = cs.getWebContextByHost(standardContext.getPath().replaceFirst("/", ""), hostname);
        if (webContext == null) {
            webContext = cs.getWebContextByHost(standardContext.getPath(), hostname);
        }
    }
    final TomcatWebAppBuilder builder = (TomcatWebAppBuilder) SystemInstance.get().getComponent(WebAppBuilder.class);
    TomcatWebAppBuilder.ContextInfo contextInfo = null;
    if (builder != null) {
        contextInfo = builder.getContextInfo(standardContext);
        if (webContext == null && contextInfo != null && contextInfo.appInfo != null) {
            // can happen if deployed from apps/
            for (final WebAppInfo webAppInfo : contextInfo.appInfo.webApps) {
                if (webAppInfo.path != null && webAppInfo.path.replace(File.separatorChar, '/').equals(standardContext.getDocBase())) {
                    webContext = cs.getWebContextByHost(webAppInfo.moduleId, hostname);
                    if (webContext != null) {
                        break;
                    }
                }
            }
        }
    }
    Collection<String> ignoreNames = null;
    if (contextInfo != null) {
        ignoreNames = contextInfo.resourceNames;
    }
    if (webContext != null && webContext.getBindings() != null && root != null) {
        for (final Map.Entry<String, Object> entry : webContext.getBindings().entrySet()) {
            try {
                final String key = entry.getKey();
                if (key.startsWith("global/")) {
                    // will be done later
                    continue;
                }
                final Object value = normalize(entry.getValue());
                if (ignoreNames.contains(removeCompEnv(key))) {
                    // keep tomcat resources
                    try {
                        // tomcat can get the reference but the bound value
                        // can come from OpenEJB (ejb-jar.xml for instance)
                        // so check the lookup can be resolved before skipping it
                        root.lookup(key);
                        continue;
                    } catch (final NameNotFoundException nnfe) {
                    // no-op: let it be rebound or bound
                    }
                }
                Contexts.createSubcontexts(root, key);
                root.rebind(key, value);
            } catch (final NamingException e) {
                e.printStackTrace();
            }
        }
    }
    // merge global: we bind our global to be able to get a real global context and not a local one (bindigns)
    if (root != null) {
        try {
            root.bind("global", SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext().lookup("global"));
        } catch (final NamingException e) {
            // bind only global bindings
            if (webContext != null && webContext.getBindings() != null) {
                for (final Map.Entry<String, Object> entry : webContext.getBindings().entrySet()) {
                    try {
                        final String key = entry.getKey();
                        if (!key.startsWith("global/")) {
                            continue;
                        }
                        final Object value = normalize(entry.getValue());
                        Contexts.createSubcontexts(root, key);
                        root.rebind(key, value);
                    } catch (final NamingException ignored) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    // try to force some binding which probably failed earlier (see in TomcatWebappBuilder)
    try {
        final Context comp = (Context) ContextBindings.getClassLoader().lookup("comp");
        final TransactionManager transactionManager = SystemInstance.get().getComponent(TransactionManager.class);
        comp.rebind("TransactionManager", transactionManager);
        // bind TransactionSynchronizationRegistry
        final TransactionSynchronizationRegistry synchronizationRegistry = SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class);
        comp.rebind("TransactionSynchronizationRegistry", synchronizationRegistry);
        try {
            comp.rebind("ORB", new SystemComponentReference(TomcatJndiBuilder.class.getClassLoader().loadClass("org.omg.CORBA.ORB")));
        } catch (final NoClassDefFoundError | ClassNotFoundException ncdfe) {
        // no-op
        }
        comp.rebind("HandleDelegate", new SystemComponentReference(HandleDelegate.class));
        if (webContext != null && webContext.getWebbeansContext() != null) {
            comp.rebind("BeanManager", new InjectableBeanManager(webContext.getWebbeansContext().getBeanManagerImpl()));
        } else if (contextInfo != null) {
            final WebBeansContext webBeansContext = cs.getAppContext(contextInfo.appInfo.appId).getWebBeansContext();
            if (webBeansContext != null) {
                // can be null if cdi is inhibited
                comp.rebind("BeanManager", new InjectableBeanManager(webBeansContext.getBeanManagerImpl()));
            }
        }
    } catch (final Exception ignored) {
        ignored.printStackTrace();
    // no-op
    }
    // merge comp/env in app if available (some users are doing it, JBoss habit?)
    try {
        final Context app = (Context) ContextBindings.getClassLoader().lookup("app");
        final Context ctx = (Context) ContextBindings.getClassLoader().lookup("comp/env");
        final List<Binding> bindings = Collections.list(ctx.listBindings("app"));
        for (final Binding binding : bindings) {
            try {
                app.bind(binding.getName(), binding.getObject());
            } catch (final NamingException ne) {
            // we don't want to rebind
            // no-op
            }
        }
    } catch (final Exception ne) {
    // no-op
    }
    ContextAccessController.setReadOnly(name);
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) WebContext(org.apache.openejb.core.WebContext) InjectableBeanManager(org.apache.webbeans.container.InjectableBeanManager) WebAppBuilder(org.apache.openejb.assembler.classic.WebAppBuilder) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) WebBeansContext(org.apache.webbeans.config.WebBeansContext) NamingException(javax.naming.NamingException) WebContext(org.apache.openejb.core.WebContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) StandardContext(org.apache.catalina.core.StandardContext) Context(javax.naming.Context) Binding(javax.naming.Binding) NameNotFoundException(javax.naming.NameNotFoundException) SystemComponentReference(org.apache.openejb.core.ivm.naming.SystemComponentReference) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) NameNotFoundException(javax.naming.NameNotFoundException) MalformedURLException(java.net.MalformedURLException) HandleDelegate(javax.ejb.spi.HandleDelegate) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) Map(java.util.Map)

Example 24 with WebContext

use of org.apache.openejb.core.WebContext in project tomee by apache.

the class ReloadingLoaderTest method initContext.

@Before
public void initContext() throws LifecycleException {
    final OpenEjbConfiguration configuration = new OpenEjbConfiguration();
    configuration.facilities = new FacilitiesInfo();
    final CoreContainerSystem containerSystem = new CoreContainerSystem(new IvmJndiFactory());
    SystemInstance.get().setComponent(OpenEjbConfiguration.class, configuration);
    SystemInstance.get().setComponent(ContainerSystem.class, containerSystem);
    SystemInstance.get().setComponent(WebAppEnricher.class, new WebAppEnricher() {

        @Override
        public URL[] enrichment(final ClassLoader webappClassLaoder) {
            return new URL[0];
        }
    });
    parentInstance = new AtomicReference<>(ParentClassLoaderFinder.Helper.get());
    loader = new TomEEWebappClassLoader(parentInstance.get()) {

        @Override
        public ClassLoader getInternalParent() {
            return parentInstance.get();
        }

        @Override
        protected void clearReferences() {
        // no-op: this test should be reworked to support it but in real life a loader is not stopped/started
        }
    };
    loader.init();
    final StandardRoot resources = new StandardRoot();
    loader.setResources(resources);
    resources.setContext(new StandardContext() {

        @Override
        public String getDocBase() {
            final File file = new File("target/foo");
            file.mkdirs();
            return file.getAbsolutePath();
        }

        @Override
        public String getMBeanKeyProperties() {
            return "foo";
        }

        {
        }
    });
    resources.start();
    loader.start();
    info = new AppInfo();
    info.appId = "test";
    context = new AppContext(info.appId, SystemInstance.get(), loader, new IvmContext(), new IvmContext(), true);
    containerSystem.addAppContext(context);
    final WebContext webDeployment = new WebContext(context);
    webDeployment.setId(context.getId());
    webDeployment.setClassLoader(loader);
    containerSystem.addWebContext(webDeployment);
}
Also used : IvmContext(org.apache.openejb.core.ivm.naming.IvmContext) WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext) StandardRoot(org.apache.catalina.webresources.StandardRoot) IvmJndiFactory(org.apache.openejb.core.ivm.naming.IvmJndiFactory) TomEEWebappClassLoader(org.apache.tomee.catalina.TomEEWebappClassLoader) CoreContainerSystem(org.apache.openejb.core.CoreContainerSystem) WebAppEnricher(org.apache.openejb.classloader.WebAppEnricher) OpenEjbConfiguration(org.apache.openejb.assembler.classic.OpenEjbConfiguration) AppInfo(org.apache.openejb.assembler.classic.AppInfo) FacilitiesInfo(org.apache.openejb.assembler.classic.FacilitiesInfo) StandardContext(org.apache.catalina.core.StandardContext) TomEEWebappClassLoader(org.apache.tomee.catalina.TomEEWebappClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File) Before(org.junit.Before)

Example 25 with WebContext

use of org.apache.openejb.core.WebContext in project tomee by apache.

the class FailingJspTest method run.

@Test
public void run() throws MalformedURLException {
    // this test passes just cause we skip container tags
    final Collection<Object> tracked1;
    final WebContext ctx;
    try (final Container container = new Container(new Configuration().http(NetworkUtil.getNextAvailablePort()).property("openejb.container.additional.exclude", "org.apache.tomee.embedded.").property("openejb.additional.include", "tomee-").user("tomee", "tomeepwd").loginConfig(new LoginConfigBuilder().basic()).securityConstaint(new SecurityConstaintBuilder().addAuthRole("**").authConstraint(true).addCollection("api", "/api/resource2/"))).deployPathsAsWebapp(JarLocation.jarLocation(FailingJspTest.class)).inject(this)) {
        ctx = SystemInstance.get().getComponent(ContainerSystem.class).getWebContextByHost("", "localhost");
        tracked1 = getTrackedContexts(ctx);
        for (int i = 0; i < 5; i++) {
            try {
                IO.slurp(new URL("http://localhost:" + container.getConfiguration().getHttpPort() + "/fail.jsp"));
            } catch (final IOException e) {
            // no-op
            }
        }
        final Collection<Object> tracked2 = getTrackedContexts(ctx);
        // bug in org.apache.jasper.servlet.JspServletWrapper.destroy()
        tracked2.removeAll(tracked1);
        assertEquals(String.valueOf(tracked2), 1, tracked2.size());
    }
    final Collection<Object> tracked2 = getTrackedContexts(ctx);
    // bug in org.apache.jasper.servlet.JspServletWrapper.destroy()
    tracked2.removeAll(tracked1);
    assertEquals(String.valueOf(tracked2), 0, tracked2.size());
}
Also used : WebContext(org.apache.openejb.core.WebContext) IOException(java.io.IOException) URL(java.net.URL) Test(org.junit.Test)

Aggregations

WebContext (org.apache.openejb.core.WebContext)25 AppContext (org.apache.openejb.AppContext)17 BeanContext (org.apache.openejb.BeanContext)11 ContainerSystem (org.apache.openejb.spi.ContainerSystem)11 WebBeansContext (org.apache.webbeans.config.WebBeansContext)9 NamingException (javax.naming.NamingException)8 IOException (java.io.IOException)7 Context (javax.naming.Context)7 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)7 ArrayList (java.util.ArrayList)6 ServletContext (javax.servlet.ServletContext)6 OpenEJBException (org.apache.openejb.OpenEJBException)6 MalformedURLException (java.net.MalformedURLException)5 NameNotFoundException (javax.naming.NameNotFoundException)5 Injection (org.apache.openejb.Injection)5 WebAppInfo (org.apache.openejb.assembler.classic.WebAppInfo)5 InitialContext (javax.naming.InitialContext)4 ThreadContext (org.apache.openejb.core.ThreadContext)4 URL (java.net.URL)3 HashSet (java.util.HashSet)3