Example #1
0
  public void registerELResolverAndListenerWithJsp(ServletContext context, boolean reloaded) {

    if (webConfig.isSet(WebContextInitParameter.ExpressionFactory) || !isJspTwoOne(context)) {

      // first try to load a factory defined in web.xml
      if (!installExpressionFactory(
          context, webConfig.getOptionValue(WebContextInitParameter.ExpressionFactory))) {

        throw new ConfigurationException(
            MessageUtils.getExceptionMessageString(
                MessageUtils.INCORRECT_JSP_VERSION_ID,
                WebContextInitParameter.ExpressionFactory.getDefaultValue(),
                WebContextInitParameter.ExpressionFactory.getQualifiedName()));
      }

    } else {

      // JSP 2.1 specific check
      if (JspFactory.getDefaultFactory().getJspApplicationContext(context) == null) {
        return;
      }

      // register an empty resolver for now. It will be populated after the
      // first request is serviced.
      FacesCompositeELResolver compositeELResolverForJsp =
          new ChainTypeCompositeELResolver(FacesCompositeELResolver.ELResolverChainType.JSP);
      ApplicationAssociate associate = ApplicationAssociate.getInstance(context);
      if (associate != null) {
        associate.setFacesELResolverForJsp(compositeELResolverForJsp);
      }

      // get JspApplicationContext.
      JspApplicationContext jspAppContext =
          JspFactory.getDefaultFactory().getJspApplicationContext(context);

      // cache the ExpressionFactory instance in ApplicationAssociate
      if (associate != null) {
        associate.setExpressionFactory(jspAppContext.getExpressionFactory());
      }

      // register compositeELResolver with JSP
      try {
        jspAppContext.addELResolver(compositeELResolverForJsp);
      } catch (IllegalStateException e) {
        ApplicationFactory factory =
            (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
        Application app = factory.getApplication();
        if (app.getProjectStage() != ProjectStage.UnitTest && !reloaded) {
          throw e;
        }
      }

      // register JSF ELContextListenerImpl with Jsp
      ELContextListenerImpl elContextListener = new ELContextListenerImpl();
      jspAppContext.addELContextListener(elContextListener);
    }
  }
Example #2
0
  @Override
  public void contextInitialized(ServletContextEvent sce) {

    ClassLoader classLoader = Reflections.getClassLoader();
    ServletContext context = sce.getServletContext();

    URLScanner scanner = createUrlScanner(classLoader, context);
    if (scanner != null) {
      context.setAttribute(URLScanner.class.getName(), scanner);
    }

    ServletDeployment deployment = createServletDeployment(context, bootstrap);
    try {
      deployment
          .getWebAppBeanDeploymentArchive()
          .getServices()
          .add(ResourceInjectionServices.class, new ServletResourceInjectionServices() {});
    } catch (NoClassDefFoundError e) {
      // Support GAE
      log.warn("@Resource injection not available in simple beans");
    }

    bootstrap.startContainer(Environments.SERVLET, deployment).startInitialization();
    WeldManager manager = bootstrap.getManager(deployment.getWebAppBeanDeploymentArchive());

    ContainerContext cc = new ContainerContext(sce, manager);
    StringBuilder dump = new StringBuilder();
    Container container = findContainer(cc, dump);
    if (container == null) {
      log.info(
          "No supported servlet container detected, CDI injection will NOT be available in Servlets, Filters or Listeners");
      log.debug("Exception dump from Container lookup: {}", dump);
    } else {
      container.initialize(cc);
      this.container = container;
    }

    // Push the manager into the servlet context so we can access in JSF
    context.setAttribute(BEAN_MANAGER_ATTRIBUTE_NAME, manager);

    if (JspFactory.getDefaultFactory() != null) {
      JspApplicationContext jspApplicationContext =
          JspFactory.getDefaultFactory().getJspApplicationContext(context);

      // Register the ELResolver with JSP
      jspApplicationContext.addELResolver(manager.getELResolver());

      // Register ELContextListener with JSP
      jspApplicationContext.addELContextListener(
          Reflections.<ELContextListener>newInstance("org.jboss.weld.el.WeldELContextListener"));

      // 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()));
    }

    bootstrap.deployBeans().validateBeans().endInitialization();
    super.contextInitialized(sce);
  }