Search in sources :

Example 1 with JspApplicationContext

use of javax.servlet.jsp.JspApplicationContext in project freemarker by apache.

the class _FreeMarkerPageContext21 method getELContext.

@Override
public ELContext getELContext() {
    if (elContext == null) {
        JspApplicationContext jspctx = JspFactory.getDefaultFactory().getJspApplicationContext(getServletContext());
        if (jspctx instanceof FreeMarkerJspApplicationContext) {
            elContext = ((FreeMarkerJspApplicationContext) jspctx).createNewELContext(this);
            elContext.putContext(JspContext.class, this);
        } else {
            throw new UnsupportedOperationException("Can not create an ELContext using a foreign JspApplicationContext (of class " + ClassUtil.getShortClassNameOfObject(jspctx) + ").\n" + "Hint: The cause of this is often that you are trying to use JSTL tags/functions in FTL. " + "In that case, know that that's not really suppored, and you are supposed to use FTL " + "constrcuts instead, like #list instead of JSTL's forEach, etc.");
        }
    }
    return elContext;
}
Also used : JspApplicationContext(javax.servlet.jsp.JspApplicationContext)

Example 2 with JspApplicationContext

use of javax.servlet.jsp.JspApplicationContext in project wildfly by wildfly.

the class JspInitializationListener method contextInitialized.

@Override
public void contextInitialized(final ServletContextEvent sce) {
    // if the servlet version is 3.1 or higher, setup a ELResolver which allows usage of static fields java.lang.*
    final ServletContext servletContext = sce.getServletContext();
    final JspApplicationContext jspApplicationContext = JspFactory.getDefaultFactory().getJspApplicationContext(servletContext);
    boolean disableImportedClassELResolver = Boolean.parseBoolean(WildFlySecurityManager.getSystemPropertiesPrivileged().getProperty(DISABLE_IMPORTED_CLASS_EL_RESOLVER_PROPERTY));
    if (!disableImportedClassELResolver && (servletContext.getEffectiveMajorVersion() > 3 || (servletContext.getEffectiveMajorVersion() == 3 && servletContext.getEffectiveMinorVersion() >= 1))) {
        jspApplicationContext.addELResolver(new ImportedClassELResolver());
    }
    // setup a wrapped JspApplicationContext if there are any EL expression factory wrappers for this servlet context
    final List<ExpressionFactoryWrapper> expressionFactoryWrappers = (List<ExpressionFactoryWrapper>) sce.getServletContext().getAttribute(CONTEXT_KEY);
    if (expressionFactoryWrappers != null && !expressionFactoryWrappers.isEmpty()) {
        final JspApplicationContextWrapper jspApplicationContextWrapper = new JspApplicationContextWrapper(JspApplicationContextImpl.getInstance(servletContext), expressionFactoryWrappers, sce.getServletContext());
        sce.getServletContext().setAttribute(JspApplicationContextImpl.class.getName(), jspApplicationContextWrapper);
    }
}
Also used : ImportedClassELResolver(org.wildfly.extension.undertow.ImportedClassELResolver) JspApplicationContext(javax.servlet.jsp.JspApplicationContext) JspApplicationContextImpl(org.apache.jasper.runtime.JspApplicationContextImpl) ExpressionFactoryWrapper(org.jboss.as.web.common.ExpressionFactoryWrapper) ServletContext(javax.servlet.ServletContext) List(java.util.List)

Example 3 with JspApplicationContext

use of javax.servlet.jsp.JspApplicationContext in project tomee by apache.

the class OpenEJBLifecycle method setJspELFactory.

/**
 * On Tomcat we need to sometimes force a class load to get our hands on the JspFactory
 */
private static void setJspELFactory(ServletContext startupObject, ELResolver resolver) {
    JspFactory factory = JspFactory.getDefaultFactory();
    if (factory == null) {
        try {
            try {
                Class.forName("org.apache.jasper.servlet.JasperInitializer");
            } catch (final Throwable th) {
                Class.forName("org.apache.jasper.compiler.JspRuntimeContext");
            }
            factory = JspFactory.getDefaultFactory();
        } catch (Exception e) {
        // ignore
        }
    }
    if (factory != null) {
        JspApplicationContext applicationCtx = factory.getJspApplicationContext(startupObject);
        applicationCtx.addELResolver(resolver);
    } else {
        logger.debug("Default JSPFactroy instance has not found. Skipping OWB JSP handling");
    }
}
Also used : JspApplicationContext(javax.servlet.jsp.JspApplicationContext) JspFactory(javax.servlet.jsp.JspFactory) ObjectStreamException(java.io.ObjectStreamException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException)

Example 4 with JspApplicationContext

use of javax.servlet.jsp.JspApplicationContext in project freemarker by apache.

the class FreeMarkerJspFactory21 method getJspApplicationContext.

@Override
public JspApplicationContext getJspApplicationContext(ServletContext ctx) {
    JspApplicationContext jspctx = (JspApplicationContext) ctx.getAttribute(JSPCTX_KEY);
    if (jspctx == null) {
        synchronized (ctx) {
            jspctx = (JspApplicationContext) ctx.getAttribute(JSPCTX_KEY);
            if (jspctx == null) {
                jspctx = new FreeMarkerJspApplicationContext();
                ctx.setAttribute(JSPCTX_KEY, jspctx);
            }
        }
    }
    return jspctx;
}
Also used : JspApplicationContext(javax.servlet.jsp.JspApplicationContext)

Example 5 with JspApplicationContext

use of javax.servlet.jsp.JspApplicationContext in project core by weld.

the class WeldServletLifecycle method initialize.

/**
 * @param context
 * @return <code>true</code> if initialized properly, <code>false</code> otherwise
 */
boolean initialize(ServletContext context) {
    isDevModeEnabled = Boolean.valueOf(context.getInitParameter(CONTEXT_PARAM_DEV_MODE));
    WeldManager manager = (WeldManager) context.getAttribute(BEAN_MANAGER_ATTRIBUTE_NAME);
    if (manager != null) {
        isBootstrapNeeded = false;
        String contextId = BeanManagerProxy.unwrap(manager).getContextId();
        context.setInitParameter(org.jboss.weld.Container.CONTEXT_ID_KEY, contextId);
    } else {
        Object container = context.getAttribute(Listener.CONTAINER_ATTRIBUTE_NAME);
        if (container instanceof ContainerInstanceFactory) {
            ContainerInstanceFactory factory = (ContainerInstanceFactory) container;
            // start the container
            ContainerInstance containerInstance = factory.initialize();
            container = containerInstance;
            // we are in charge of shutdown also
            this.shutdownAction = () -> containerInstance.shutdown();
        }
        if (container instanceof ContainerInstance) {
            // the container instance was either passed to us directly or was created in the block above
            ContainerInstance containerInstance = (ContainerInstance) container;
            manager = BeanManagerProxy.unwrap(containerInstance.getBeanManager());
            context.setInitParameter(org.jboss.weld.Container.CONTEXT_ID_KEY, containerInstance.getId());
            isBootstrapNeeded = false;
        }
    }
    final CDI11Bootstrap bootstrap = new WeldBootstrap();
    if (isBootstrapNeeded) {
        final CDI11Deployment deployment = createDeployment(context, bootstrap);
        deployment.getServices().add(ExternalConfiguration.class, new ExternalConfigurationBuilder().add(BEAN_IDENTIFIER_INDEX_OPTIMIZATION.get(), Boolean.FALSE.toString()).build());
        if (deployment.getBeanDeploymentArchives().isEmpty()) {
            // Skip initialization - there is no bean archive in the deployment
            CommonLogger.LOG.initSkippedNoBeanArchiveFound();
            return false;
        }
        ResourceInjectionServices resourceInjectionServices = new ServletResourceInjectionServices() {
        };
        try {
            for (BeanDeploymentArchive archive : deployment.getBeanDeploymentArchives()) {
                archive.getServices().add(ResourceInjectionServices.class, resourceInjectionServices);
            }
        } catch (NoClassDefFoundError e) {
            // Support GAE
            WeldServletLogger.LOG.resourceInjectionNotAvailable();
        }
        String id = context.getInitParameter(org.jboss.weld.Container.CONTEXT_ID_KEY);
        if (id != null) {
            bootstrap.startContainer(id, Environments.SERVLET, deployment);
        } else {
            bootstrap.startContainer(Environments.SERVLET, deployment);
        }
        bootstrap.startInitialization();
        /*
             * Determine the BeanManager used for example for EL resolution - this should work fine as all bean archives share the same classloader. The only
             * difference this can make is per-BDA (CDI 1.0 style) enablement of alternatives, interceptors and decorators. Nothing we can do about that.
             *
             * First try to find the bean archive for WEB-INF/classes. If not found, take the first one available.
             */
        for (BeanDeploymentArchive bda : deployment.getBeanDeploymentArchives()) {
            if (bda.getId().contains(ManagerObjectFactory.WEB_INF_CLASSES_FILE_PATH) || bda.getId().contains(ManagerObjectFactory.WEB_INF_CLASSES)) {
                manager = bootstrap.getManager(bda);
                break;
            }
        }
        if (manager == null) {
            manager = bootstrap.getManager(deployment.getBeanDeploymentArchives().iterator().next());
        }
        // Push the manager into the servlet context so we can access in JSF
        context.setAttribute(BEAN_MANAGER_ATTRIBUTE_NAME, manager);
    }
    ContainerContext containerContext = new ContainerContext(context, manager);
    StringBuilder dump = new StringBuilder();
    Container container = findContainer(containerContext, dump);
    if (container == null) {
        WeldServletLogger.LOG.noSupportedServletContainerDetected();
        WeldServletLogger.LOG.debugv("Exception dump from Container lookup: {0}", dump);
    } else {
        container.initialize(containerContext);
        this.container = container;
    }
    if (Reflections.isClassLoadable(WeldClassLoaderResourceLoader.INSTANCE, JSP_FACTORY_CLASS_NAME) && JspFactory.getDefaultFactory() != null) {
        JspApplicationContext jspApplicationContext = JspFactory.getDefaultFactory().getJspApplicationContext(context);
        // Register the ELResolver with JSP
        jspApplicationContext.addELResolver(manager.getELResolver());
        // Register ELContextListener with JSP
        try {
            jspApplicationContext.addELContextListener(new WeldELContextListener());
        } catch (Exception e) {
            throw WeldServletLogger.LOG.errorLoadingWeldELContextListener(e);
        }
        // Push the wrapped expression factory into the servlet context so that Tomcat or Jetty can hook it in using a container code
        context.setAttribute(EXPRESSION_FACTORY_NAME, manager.wrapExpressionFactory(jspApplicationContext.getExpressionFactory()));
    }
    if (isBootstrapNeeded) {
        bootstrap.deployBeans().validateBeans().endInitialization();
        if (isDevModeEnabled) {
            FilterRegistration.Dynamic filterDynamic = context.addFilter("Weld Probe Filter", DevelopmentMode.PROBE_FILTER_CLASS_NAME);
            filterDynamic.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE), true, "/*");
        }
        this.shutdownAction = () -> bootstrap.shutdown();
    }
    return true;
}
Also used : ServletResourceInjectionServices(org.jboss.weld.environment.servlet.services.ServletResourceInjectionServices) ResourceInjectionServices(org.jboss.weld.injection.spi.ResourceInjectionServices) JspApplicationContext(javax.servlet.jsp.JspApplicationContext) CDI11Bootstrap(org.jboss.weld.bootstrap.api.CDI11Bootstrap) WeldBootstrap(org.jboss.weld.bootstrap.WeldBootstrap) ExternalConfigurationBuilder(org.jboss.weld.configuration.spi.helpers.ExternalConfigurationBuilder) CDI11Deployment(org.jboss.weld.bootstrap.spi.CDI11Deployment) WeldManager(org.jboss.weld.manager.api.WeldManager) ContainerInstance(org.jboss.weld.environment.ContainerInstance) UndertowContainer(org.jboss.weld.environment.undertow.UndertowContainer) JettyContainer(org.jboss.weld.environment.jetty.JettyContainer) TomcatContainer(org.jboss.weld.environment.tomcat.TomcatContainer) GwtDevHostedModeContainer(org.jboss.weld.environment.gwtdev.GwtDevHostedModeContainer) ContainerInstanceFactory(org.jboss.weld.environment.ContainerInstanceFactory) WeldBeanDeploymentArchive(org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) WeldELContextListener(org.jboss.weld.module.web.el.WeldELContextListener) ServletResourceInjectionServices(org.jboss.weld.environment.servlet.services.ServletResourceInjectionServices) FilterRegistration(javax.servlet.FilterRegistration)

Aggregations

JspApplicationContext (javax.servlet.jsp.JspApplicationContext)8 JspApplicationContextImpl (org.apache.jasper.runtime.JspApplicationContextImpl)2 WeldELContextListener (org.jboss.weld.module.web.el.WeldELContextListener)2 ObjectStreamException (java.io.ObjectStreamException)1 List (java.util.List)1 BeanManager (javax.enterprise.inject.spi.BeanManager)1 FilterRegistration (javax.servlet.FilterRegistration)1 ServletContext (javax.servlet.ServletContext)1 JspFactory (javax.servlet.jsp.JspFactory)1 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)1 ExpressionFactoryWrapper (org.jboss.as.web.common.ExpressionFactoryWrapper)1 WeldBootstrap (org.jboss.weld.bootstrap.WeldBootstrap)1 CDI11Bootstrap (org.jboss.weld.bootstrap.api.CDI11Bootstrap)1 BeanDeploymentArchive (org.jboss.weld.bootstrap.spi.BeanDeploymentArchive)1 CDI11Deployment (org.jboss.weld.bootstrap.spi.CDI11Deployment)1 ExternalConfigurationBuilder (org.jboss.weld.configuration.spi.helpers.ExternalConfigurationBuilder)1 ContainerInstance (org.jboss.weld.environment.ContainerInstance)1 ContainerInstanceFactory (org.jboss.weld.environment.ContainerInstanceFactory)1 WeldBeanDeploymentArchive (org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive)1 GwtDevHostedModeContainer (org.jboss.weld.environment.gwtdev.GwtDevHostedModeContainer)1