Package org.apache.openejb.core

Examples of org.apache.openejb.core.WebContext


            } else {
                path += "/" + standardContext.getPath();
            }
        }

        WebContext webContext = cs.getWebContext(path);
        if (webContext == null) { // tomee-embedded deployment
            webContext = cs.getWebContext(standardContext.getPath().replaceFirst("/", ""));
            if (webContext == null) {
                webContext = cs.getWebContext(standardContext.getPath());
            }
        }

        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 (WebAppInfo webAppInfo : contextInfo.appInfo.webApps) {
                    if (webAppInfo.path != null && webAppInfo.path.equals(standardContext.getDocBase())) {
                        webContext = cs.getWebContext(webAppInfo.moduleId);
                        if (webContext != null) {
                            break;
                        }
                    }
                }
            }
        }
        Collection<String> ignoreNames = null;
        if (contextInfo != null) {
            ignoreNames = contextInfo.resourceNames;
        }

        if (webContext != null && webContext.getBindings() != null && root != null) {
            for (Map.Entry<String, Object> entry : webContext.getBindings().entrySet()) {
                try {
                    final String key = entry.getKey();
                    if (key.startsWith("global/")) { // will be done later
                        continue;
                    }

                    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 (NameNotFoundException nnfe) {
                            // no-op: let it be rebound or bound
                        }
                    }

                    Contexts.createSubcontexts(root, key);
                    root.rebind(key, value);
                } catch (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 (NamingException e) {
                // bind only global bindings
                if (webContext != null && webContext.getBindings() != null) {
                    for (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 (NamingException ignored) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

        // try to force some binding which probably failed earlier (see in TomcatWebappBuilder)
        try {
            Context comp = (Context) ContextBindings.getClassLoader().lookup("comp");
            TransactionManager transactionManager = SystemInstance.get().getComponent(TransactionManager.class);
            comp.rebind("TransactionManager", transactionManager);

            // bind TransactionSynchronizationRegistry
            TransactionSynchronizationRegistry synchronizationRegistry = SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class);
            comp.rebind("TransactionSynchronizationRegistry", synchronizationRegistry);

            comp.rebind("ORB", new SystemComponentReference(ORB.class));
            comp.rebind("HandleDelegate", new SystemComponentReference(HandleDelegate.class));

            if (webContext != null) {
                comp.rebind("BeanManager", webContext.getAppContext().getBeanManager());
            } else if (contextInfo != null) {
                comp.rebind("BeanManager", cs.getAppContext(contextInfo.appInfo.appId).getBeanManager());
            }
        } catch (Exception ignored) {
            ignored.printStackTrace();
View Full Code Here


            final Map<String, Object> bindings = new HashMap<String, Object>();
            bindings.putAll(appContext.getBindings());
            bindings.putAll(new JndiEncBuilder(webAppInfo.jndiEnc, injections, webAppInfo.moduleId, "Bean", null, webAppInfo.uniqueId, classLoader).buildBindings(JndiEncBuilder.JndiScope.comp));

            final WebContext webContext = new WebContext(appContext);
            webContext.setBindings(bindings);
            webContext.getBindings().putAll(new JndiEncBuilder(webAppInfo.jndiEnc, injections, webAppInfo.moduleId, "Bean", null, webAppInfo.uniqueId, classLoader).buildBindings(JndiEncBuilder.JndiScope.comp));
            webContext.setJndiEnc(WebInitialContext.create(bindings, appContext.getGlobalJndiContext()));
            webContext.setClassLoader(classLoader);
            webContext.setId(webAppInfo.moduleId);
            webContext.setContextRoot(webAppInfo.contextRoot);
            webContext.getInjections().addAll(injections);
            webContext.setInitialContext(new EmbeddedInitialContext(webContext.getJndiEnc(), webContext.getBindings()));

            appContext.getWebContexts().add(webContext);
            cs.addWebContext(webContext);

            if (!appInfo.webAppAlone) {
                final Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
                final List<BeanContext> beanContexts = assembler.initEjbs(classLoader, appInfo, appContext, injections, new ArrayList<BeanContext>(), webAppInfo.moduleId);
                appContext.getBeanContexts().addAll(beanContexts);
                new CdiBuilder().build(appInfo, appContext, appContext.getBeanContexts(), webContext);
                assembler.startEjbs(true, beanContexts);
            }

            final ServletContextEvent sce = new MockServletContextEvent();
            servletContextEvents.put(webAppInfo, sce);

            // listeners
            for (ListenerInfo listener : webAppInfo.listeners) {
                final Class<?> clazz = webContext.getClassLoader().loadClass(listener.classname);
                final Object instance = webContext.newInstance(clazz);
                if (ServletContextListener.class.isInstance(instance)) {
                    ((ServletContextListener) instance).contextInitialized(sce);
                }

                List<Object> list = listeners.get(webAppInfo);
                if (list == null) {
                    list = new ArrayList<Object>();
                    listeners.put(webAppInfo, list);
                }
                list.add(instance);
            }

            final DeployedWebObjects deployedWebObjects = new DeployedWebObjects();
            deployedWebObjects.webContext = webContext;
            servletDeploymentInfo.put(webAppInfo, deployedWebObjects);

            if (addServletMethod == null) { // can't manage filter/servlets
                continue;
            }

            // register filters
            for (FilterInfo info : webAppInfo.filters) {
                for (String mapping : info.mappings) {
                    final FilterConfig config = new SimpleFilterConfig(sce.getServletContext(), info.name, info.initParams);
                    try {
                        addFilterMethod.invoke(null, info.classname, webContext, mapping, config);
                        deployedWebObjects.filterMappings.add(mapping);
                    } catch (Exception e) {
                        LOGGER.warning(e.getMessage(), e);
                    }
                }
            }
            for (ClassListInfo info : webAppInfo.webAnnotatedClasses) {
                final String url = info.name;
                for (String filterPath : info.list) {
                    String classname = nameFromUrls(url, filterPath);

                    final Class<?> clazz = webContext.getClassLoader().loadClass(classname);
                    final WebFilter annotation = clazz.getAnnotation(WebFilter.class);
                    if (annotation != null) {
                        final Properties initParams = new Properties();
                        for (WebInitParam param : annotation.initParams()) {
                            initParams.put(param.name(), param.value());
                        }

                        final FilterConfig config = new SimpleFilterConfig(sce.getServletContext(), info.name, initParams);
                        for (String mapping: annotation.urlPatterns()) {
                            try {
                                addFilterMethod.invoke(null, classname, webContext, mapping, config);
                                deployedWebObjects.filterMappings.add(mapping);
                            } catch (Exception e) {
                                LOGGER.warning(e.getMessage(), e);
                            }
                        }
                    }
                }
            }

            // register servlets
            for (ServletInfo info : webAppInfo.servlets) {
                if ("true".equalsIgnoreCase(appInfo.properties.getProperty("openejb.jaxrs.on", "true"))) {
                    // skip jaxrs servlets
                    boolean skip = false;
                    for (ParamValueInfo pvi : info.initParams) {
                        if ("javax.ws.rs.Application".equals(pvi.name) || Application.class.getName().equals(pvi.name)) {
                            skip = true;
                        }
                    }

                    if (skip) {
                        continue;
                    }

                    if (info.servletClass == null) {
                        try {
                            if (Application.class.isAssignableFrom(classLoader.loadClass(info.servletName))) {
                                continue;
                            }
                        } catch (Exception e) {
                            // no-op
                        }
                    }
                } // else let the user manage itself a rest servlet etc...

                // deploy
                for (String mapping : info.mappings) {
                    try {
                        addServletMethod.invoke(null, info.servletClass, webContext, mapping);
                        deployedWebObjects.mappings.add(mapping);
                    } catch (Exception e) {
                        LOGGER.warning(e.getMessage(), e);
                    }
                }
            }
            for (ClassListInfo info : webAppInfo.webAnnotatedClasses) {
                final String url = info.name;
                for (String servletPath : info.list) {
                    String classname = nameFromUrls(url, servletPath);

                    final Class<?> clazz = webContext.getClassLoader().loadClass(classname);
                    final WebServlet annotation = clazz.getAnnotation(WebServlet.class);
                    if (annotation != null) {
                        for (String mapping: annotation.urlPatterns()) {
                            try {
                                addServletMethod.invoke(null, classname, webContext, mapping);
View Full Code Here

    private String virtualHost;
    private boolean enabled = true;
    private String wildcard = SystemInstance.get().getProperty("openejb.rest.wildcard", ".*"); // embedded = regex, tomee = servlet

    public void afterApplicationCreated(final AppInfo appInfo, final WebAppInfo webApp) {
        final WebContext webContext = containerSystem.getWebContext(webApp.moduleId);
        if (webContext == null) {
            return;
        }

        if (!deployedWebApps.add(webApp)) {
            return;
        }

        final Map<String, EJBRestServiceInfo> restEjbs = getRestEjbs(appInfo);

        final ClassLoader classLoader = getClassLoader(webContext.getClassLoader());
        final Collection<Injection> injections = webContext.getInjections();
        final WebBeansContext owbCtx;
        if (webContext.getWebbeansContext() != null) {
            owbCtx = webContext.getWebbeansContext();
        } else {
            owbCtx = webContext.getAppContext().getWebBeansContext();
        }

        Context context = webContext.getJndiEnc();
        if (context == null) { // usually true since it is set in org.apache.tomee.catalina.TomcatWebAppBuilder.afterStart() and lookup(comp) fails
            context = webContext.getAppContext().getAppJndiContext();
        }

        final Collection<Object> additionalProviders = new HashSet<Object>();
        if (useDiscoveredProviders()) {
            for (final String name : webApp.jaxRsProviders) {
                try {
                    additionalProviders.add(classLoader.loadClass(name));
                } catch (ClassNotFoundException e) {
                    LOGGER.warning("can't load '" + name + "'", e);
                }
            }
            additionalProviders.addAll(appProviders(appInfo, classLoader));
        }

        final ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(classLoader);

        Collection<IdPropertiesInfo> pojoConfigurations = null; // done lazily
        try {
            // The spec says:
            //
            // "The resources and providers that make up a JAX-RS application are configured via an application-supplied
            // subclass of Application. An implementation MAY provide alternate mechanisms for locating resource
            // classes and providers (e.g. runtime class scanning) but use of Application is the only portable means of
            //  configuration."
            //
            //  The choice here is to deploy using the Application if it exists or to use the scanned classes
            //  if there is no Application.
            //
            //  Like this providing an Application subclass user can totally control deployed services.

            boolean useApp = false;
            String appPrefix = webApp.contextRoot;
            for (final String app : webApp.restApplications) { // normally a unique one but we support more
                appPrefix = webApp.contextRoot; // if multiple application classes reinit it
                if (!appPrefix.endsWith("/")) {
                    appPrefix += "/";
                }

                final Application appInstance;
                final Class<?> appClazz;
                try {
                    appClazz = classLoader.loadClass(app);
                    appInstance = Application.class.cast(appClazz.newInstance());
                    if (owbCtx.getBeanManagerImpl().isInUse()) {
                        try {
                            webContext.inject(appInstance);
                        } catch (Exception e) {
                            // not important since not required by the spec
                        }
                    }
                } catch (Exception e) {
View Full Code Here

                return method.invoke(new InitialContext(), args);
            }
        });

        CoreContainerSystem containerSystem = new CoreContainerSystem(new IvmJndiFactory());
        WebContext webContext = new WebContext(application);
        webContext.setId(webApp.getId());
        webContext.setClassLoader(webModule.getClassLoader());
        webContext.getInjections().add(new Injection("SimpleEJBLocalBean", "simple", RestWithInjections.class));
        webContext.setJndiEnc(ctx);
        containerSystem.addWebContext(webContext);
        SystemInstance.get().setComponent(ContainerSystem.class, containerSystem);

        ServiceInfo serviceInfo = new ServiceInfo();
        serviceInfo.className = HttpServerFactory.class.getName();
View Full Code Here

                        }
                    }
                }

                // add WebDeploymentInfo to ContainerSystem
                final WebContext webContext = new WebContext(appContext);
                webContext.setJndiEnc(new InitialContext());
                webContext.setClassLoader(classLoader);
                webContext.setId(webAppInfo.moduleId);
                webContext.setContextRoot(webAppInfo.contextRoot);
                webContext.setBindings(bindings);
                webContext.getInjections().addAll(injections);
                appContext.getWebContexts().add(webContext);
                cs.addWebContext(webContext);

                if (!contextInfo.appInfo.webAppAlone) {
                    final List<BeanContext> beanContexts = assembler.initEjbs(classLoader, contextInfo.appInfo, appContext, injections, new ArrayList<BeanContext>(), webAppInfo.moduleId);
View Full Code Here

            // Context comp = (Context) root.lookup("comp");
            safeBind(root, "openejb", openejbContext);

            // add context to WebDeploymentInfo
            if (currentWebAppInfo != null) {
                final WebContext webContext = getContainerSystem().getWebContext(currentWebAppInfo.moduleId);
                if (webContext != null) {
                    webContext.setJndiEnc(comp);
                }

                try {
                    // Bean Validation
                    standardContext.getServletContext().setAttribute("javax.faces.validator.beanValidator.ValidatorFactory", openejbContext.lookup(Assembler.VALIDATOR_FACTORY_NAMING_CONTEXT.replaceFirst("openejb", "") + currentWebAppInfo.uniqueId));
View Full Code Here

        }
        return addressesByApplication.get(appId);
    }

    public void afterApplicationCreated(final AppInfo appInfo, final WebAppInfo webApp) {
        final WebContext webContext = containerSystem.getWebContext(webApp.moduleId);
        if (webContext == null)
            return;

        // if already deployed skip this webapp
        if (!deployedWebApps.add(webApp))
            return;

        final Map<String, PortInfo> ports = new TreeMap<String, PortInfo>();
        for (final PortInfo port : webApp.portInfos) {
            ports.put(port.serviceLink, port);
        }

        URL moduleBaseUrl = null;
        try {
            moduleBaseUrl = new File(webApp.path).toURI().toURL();
        } catch (MalformedURLException e) {
            logger.error("Invalid ejb jar location " + webApp.path, e);
        }

        Collection<IdPropertiesInfo> pojoConfiguration = null; // lazy init
        for (final ServletInfo servlet : webApp.servlets) {
            if (servlet.servletName == null) {
                continue;
            }

            final PortInfo portInfo = ports.get(servlet.servletName);
            if (portInfo == null) {
                continue;
            }

            final ClassLoader old = Thread.currentThread().getContextClassLoader();
            final ClassLoader classLoader = webContext.getClassLoader();
            Thread.currentThread().setContextClassLoader(classLoader);
            try {
                final Collection<Injection> injections = webContext.getInjections();
                final Context context = webContext.getJndiEnc();
                final Class target = classLoader.loadClass(servlet.servletClass);
                final Map<String, Object> bindings = webContext.getBindings();

                final PortData port = WsBuilder.toPortData(portInfo, injections, moduleBaseUrl, classLoader);

                pojoConfiguration = PojoUtil.findPojoConfig(pojoConfiguration, appInfo, webApp);
View Full Code Here

                return method.invoke(new InitialContext(), args);
            }
        });

        CoreContainerSystem containerSystem = new CoreContainerSystem(new IvmJndiFactory());
        WebContext webContext = new WebContext(application);
        webContext.setId(webApp.getId());
        webContext.setClassLoader(webModule.getClassLoader());
        webContext.getInjections().add(new Injection("SimpleEJBLocalBean", "simple", RestWithInjections.class));
        webContext.setJndiEnc(ctx);
        containerSystem.addWebContext(webContext);
        SystemInstance.get().setComponent(ContainerSystem.class, containerSystem);

        ServiceInfo serviceInfo = new ServiceInfo();
        serviceInfo.className = HttpServerFactory.class.getName();
View Full Code Here

        return addressesByApplication.get(appId);
    }


    public void afterApplicationCreated(AppInfo appInfo, WebAppInfo webApp) {
        WebContext webContext = containerSystem.getWebContext(webApp.moduleId);
        if (webContext == null) return;

        // if already deployed skip this webapp
        if (!deployedWebApps.add(webApp)) return;

        Map<String,PortInfo> ports = new TreeMap<String,PortInfo>();
        for (PortInfo port : webApp.portInfos) {
            ports.put(port.serviceLink, port);
        }

        URL moduleBaseUrl = null;
        try {
            moduleBaseUrl = new File(webApp.path).toURI().toURL();
        } catch (MalformedURLException e) {
            logger.error("Invalid ejb jar location " + webApp.path, e);
        }

        Collection<IdPropertiesInfo> pojoConfiguration = null; // lazy init
        for (ServletInfo servlet : webApp.servlets) {
            if (servlet.servletName == null) {
                continue;
            }

            PortInfo portInfo = ports.get(servlet.servletName);
            if (portInfo == null) {
                continue;
            }

            try {
                ClassLoader classLoader = webContext.getClassLoader();
                Collection<Injection> injections = webContext.getInjections();
                Context context = webContext.getJndiEnc();
                Class target = classLoader.loadClass(servlet.servletClass);
                final Map<String, Object> bindings = webContext.getBindings();

                PortData port = WsBuilder.toPortData(portInfo, injections, moduleBaseUrl, classLoader);

                pojoConfiguration = PojoUtil.findPojoConfig(pojoConfiguration, appInfo, webApp);
View Full Code Here

    private String wildcard = SystemInstance.get().getProperty("openejb.rest.wildcard", ".*");

    public void afterApplicationCreated(final AppInfo appInfo, final WebAppInfo webApp) {
        final Map<String, EJBRestServiceInfo> restEjbs = getRestEjbs(appInfo);

        final WebContext webContext = containerSystem.getWebContext(webApp.moduleId);
        if (webContext == null) {
            return;
        }

        if (!deployedWebApps.add(webApp)) {
            return;
        }

        final ClassLoader classLoader = getClassLoader(webContext.getClassLoader());
        final Collection<Injection> injections = webContext.getInjections();
        final WebBeansContext owbCtx;
        if (webContext.getWebbeansContext() != null) {
            owbCtx = webContext.getWebbeansContext();
        } else {
            owbCtx = webContext.getAppContext().getWebBeansContext();
        }

        Context context = webContext.getJndiEnc();
        if (context == null) { // usually true since it is set in org.apache.tomee.catalina.TomcatWebAppBuilder.afterStart() and lookup(comp) fails
            context = webContext.getAppContext().getAppJndiContext();
        }

        final Collection<Object> additionalProviders = new HashSet<Object>();
        if (useDiscoveredProviders()) {
            for (String name : webApp.jaxRsProviders) {
View Full Code Here

TOP

Related Classes of org.apache.openejb.core.WebContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.