private Handler doCreateHandler()

in runtime/runtime_impl_jetty9/src/main/java/com/google/apphosting/runtime/jetty9/AppVersionHandlerFactory.java [143:246]


  private Handler doCreateHandler(AppVersion appVersion) throws ServletException {
    try {
      File contextRoot = appVersion.getRootDirectory();

      final AppEngineWebAppContext context = contextFactory.createContext(appVersion, serverInfo);
      context.setServer(server);
      context.setDefaultsDescriptor(WEB_DEFAULTS_XML);
      context.setClassLoader(appVersion.getClassLoader());
      if (useJettyErrorPageHandler) {
        context.getErrorHandler().setShowStacks(false);
      } else {
        context.setErrorHandler(new NullErrorHandler());
      }
      File qswebxml = new File(contextRoot, "WEB-INF/quickstart-web.xml");
      if (qswebxml.exists()) {
        context.setConfigurationClasses(quickstartConfigurationClasses);
      } else {
        context.setConfigurationClasses(preconfigurationClasses);
      }

      // prevent jetty from trying to delete the temp dir
      context.setPersistTempDirectory(true);
      // ensure jetty does not unpack, probably not necessary because the unpacking
      // is done by AppEngineWebAppContext
      context.setExtractWAR(false);
      // ensure exception is thrown if context startup fails
      context.setThrowUnavailableOnStartupException(true);
      // for JSP 2.2

      try {
        // Use the App Class loader to try to initialize the JSP machinery.
        // Not an issue if it fails: it means the app does not contain the JSP jars in WEB-INF/lib.
        Class<?> klass = appVersion.getClassLoader().loadClass(TOMCAT_SIMPLE_INSTANCE_MANAGER);
        Object sim = klass.getConstructor().newInstance();
        context.getServletContext().setAttribute(TOMCAT_INSTANCE_MANAGER, sim);
        // Set JSP factory equivalent for:
        // JspFactory jspf = new JspFactoryImpl();
        klass = appVersion.getClassLoader().loadClass(TOMCAT_JSP_FACTORY);
        JspFactory jspf = (JspFactory) klass.getConstructor().newInstance();
        JspFactory.setDefaultFactory(jspf);
        Class.forName(
            "org.apache.jasper.compiler.JspRuntimeContext", true, appVersion.getClassLoader());
      } catch (Throwable t) {
        // No big deal, there are no JSPs in the App since the jsp libraries are not inside the
        // web app classloader.
      }

      SessionsConfig sessionsConfig = appVersion.getSessionsConfig();
      SessionManagerHandler.Config.Builder builder = SessionManagerHandler.Config.builder();
      if (sessionsConfig.getAsyncPersistenceQueueName() != null) {
        builder.setAsyncPersistenceQueueName(sessionsConfig.getAsyncPersistenceQueueName());
      }
      builder
          .setEnableSession(sessionsConfig.isEnabled())
          .setAsyncPersistence(sessionsConfig.isAsyncPersistence())
          .setServletContextHandler(context);

      SessionManagerHandler ignored = SessionManagerHandler.create(builder.build());
      // Pass the AppVersion on to any of our servlets (e.g. ResourceFileServlet).
      context.setAttribute(AppEngineConstants.APP_VERSION_CONTEXT_ATTR, appVersion);
      if (Boolean.getBoolean(AppEngineConstants.HTTP_CONNECTOR_MODE)) {
        context.addEventListener(
            new ContextHandler.ContextScopeListener() {
              @Override
              public void enterScope(
                  ContextHandler.Context context, Request request, Object reason) {
                if (request != null) {
                  ApiProxy.Environment environment =
                      (ApiProxy.Environment)
                          request.getAttribute(AppEngineConstants.ENVIRONMENT_ATTR);
                  if (environment != null) {
                    ApiProxy.setEnvironmentForCurrentThread(environment);
                  }
                }
              }

              @Override
              public void exitScope(ContextHandler.Context context, Request request) {
                ApiProxy.clearEnvironmentForCurrentThread();
              }
            });
      } else {
        context.start();
        // Check to see if servlet filter initialization failed.
        Throwable unavailableCause = context.getUnavailableException();
        if (unavailableCause != null) {
          if (unavailableCause instanceof ServletException) {
            throw (ServletException) unavailableCause;
          } else {
            UnavailableException unavailableException =
                new UnavailableException("Initialization failed.");
            unavailableException.initCause(unavailableCause);
            throw unavailableException;
          }
        }
      }

      return context;
    } catch (ServletException ex) {
      throw ex;
    } catch (Exception ex) {
      throw new ServletException(ex);
    }
  }