runtime/runtime_impl_jetty12/src/main/java/com/google/apphosting/runtime/jetty/ee10/AppEngineWebAppContext.java [412:655]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  private static class TrimmedServlets {
    private final Map<String, ServletHolder> holders = new HashMap<>();
    private final List<ServletMapping> mappings = new ArrayList<>();

    TrimmedServlets(ServletHolder[] holders, ServletMapping[] mappings) {
      for (ServletHolder h : holders) {

        // Replace deprecated package names.
        String className = h.getClassName();
        if (className != null)
        {
          for (Map.Entry<String, String> entry : DEPRECATED_PACKAGE_NAMES.entrySet()) {
            if (className.startsWith(entry.getKey())) {
              h.setClassName(className.replace(entry.getKey(), entry.getValue()));
            }
          }
        }

        h.setAsyncSupported(APP_IS_ASYNC);
        this.holders.put(h.getName(), h);
      }
      this.mappings.addAll(Arrays.asList(mappings));
    }

    /**
     * Ensure the registration of a container provided servlet:
     *
     * <ul>
     *   <li>If any existing servlet registrations are for the passed servlet class, then their
     *       holder is updated with a new instance created on the containers classpath.
     *   <li>If a servlet registration for the passed servlet name does not exist, one is created to
     *       the passed servlet class.
     * </ul>
     *
     * @param name The servlet name
     * @param servlet The servlet class
     * @throws ReflectiveOperationException If a new instance of the servlet cannot be instantiated
     */
    void ensure(String name, Class<? extends Servlet> servlet) throws ReflectiveOperationException {
      // Instantiate any holders referencing this servlet (may be application instances)
      for (ServletHolder h : holders.values()) {
        if (servlet.getName().equals(h.getClassName())) {
          h.setServlet(servlet.getConstructor().newInstance());
          h.setAsyncSupported(APP_IS_ASYNC);
        }
      }

      // Look for (or instantiate) our named instance
      ServletHolder holder = holders.get(name);
      if (holder == null) {
        holder = new ServletHolder(servlet.getConstructor().newInstance());
        holder.setInitOrder(1);
        holder.setName(name);
        holder.setAsyncSupported(APP_IS_ASYNC);
        holders.put(name, holder);
      }
    }

    /**
     * Ensure the registration of a container provided servlet:
     *
     * <ul>
     *   <li>If any existing servlet registrations are for the passed servlet class, then their
     *       holder is updated with a new instance created on the containers classpath.
     *   <li>If a servlet registration for the passed servlet name does not exist, one is created to
     *       the passed servlet class.
     *   <li>If a servlet mapping for the passed servlet name and pathSpec does not exist, one is
     *       created.
     * </ul>
     *
     * @param name The servlet name
     * @param servlet The servlet class
     * @param pathSpec The servlet pathspec
     * @throws ReflectiveOperationException If a new instance of the servlet cannot be instantiated
     */
    void ensure(String name, Class<? extends Servlet> servlet, String pathSpec)
        throws ReflectiveOperationException {
      // Ensure Servlet
      ensure(name, servlet);

      // Ensure mapping
      if (pathSpec != null) {
        boolean mapped = false;
        for (ServletMapping mapping : mappings) {
          if (mapping.containsPathSpec(pathSpec)) {
            mapped = true;
            break;
          }
        }
        if (!mapped) {
          ServletMapping mapping = new ServletMapping();
          mapping.setServletName(name);
          mapping.setPathSpec(pathSpec);
          if (pathSpec.equals("/")) {
            mapping.setFromDefaultDescriptor(true);
          }
          mappings.add(mapping);
        }
      }
    }

    /**
     * Instantiate any registrations of a jetty provided servlet
     *
     * @throws ReflectiveOperationException If a new instance of the servlet cannot be instantiated
     */
    void instantiateJettyServlets() throws ReflectiveOperationException {
      for (ServletHolder h : holders.values()) {
        if (h.getClassName() != null && h.getClassName().startsWith(JETTY_PACKAGE)) {
          Class<? extends Servlet> servlet =
              ServletHolder.class
                  .getClassLoader()
                  .loadClass(h.getClassName())
                  .asSubclass(Servlet.class);
          h.setServlet(servlet.getConstructor().newInstance());
        }
      }
    }

    ServletHolder[] getHolders() {
      return holders.values().toArray(new ServletHolder[0]);
    }

    ServletMapping[] getMappings() {
      List<ServletMapping> trimmed = new ArrayList<>(mappings.size());
      for (ServletMapping m : mappings) {
        if (this.holders.containsKey(m.getServletName())) {
          trimmed.add(m);
        }
      }
      return trimmed.toArray(new ServletMapping[0]);
    }
  }

  private static class TrimmedFilters {
    private final Map<String, FilterHolder> holders = new HashMap<>();
    private final List<FilterMapping> mappings = new ArrayList<>();

    TrimmedFilters(FilterHolder[] holders, FilterMapping[] mappings) {
      for (FilterHolder h : holders) {

        // Replace deprecated package names.
        String className = h.getClassName();
        if (className != null)
        {
          for (Map.Entry<String, String> entry : DEPRECATED_PACKAGE_NAMES.entrySet()) {
            if (className.startsWith(entry.getKey())) {
              h.setClassName(className.replace(entry.getKey(), entry.getValue()));
            }
          }
        }

        h.setAsyncSupported(APP_IS_ASYNC);
        this.holders.put(h.getName(), h);
      }
      this.mappings.addAll(Arrays.asList(mappings));
    }

    /**
     * Ensure the registration of a container provided filter:
     *
     * <ul>
     *   <li>If any existing filter registrations are for the passed filter class, then their holder
     *       is updated with a new instance created on the containers classpath.
     *   <li>If a filter registration for the passed filter name does not exist, one is created to
     *       the passed filter class.
     *   <li>If a filter mapping for the passed filter name and pathSpec does not exist, one is
     *       created.
     * </ul>
     *
     * @param name The filter name
     * @param filter The filter class
     * @param pathSpec The servlet pathspec
     * @throws ReflectiveOperationException If a new instance of the servlet cannot be instantiated
     */
    void ensure(String name, Class<? extends Filter> filter, String pathSpec) throws Exception {

      // Instantiate any holders referencing this filter (may be application instances)
      for (FilterHolder h : holders.values()) {
        if (filter.getName().equals(h.getClassName())) {
          h.setFilter(filter.getConstructor().newInstance());
          h.setAsyncSupported(APP_IS_ASYNC);
        }
      }

      // Look for (or instantiate) our named instance
      FilterHolder holder = holders.get(name);
      if (holder == null) {
        holder = new FilterHolder(filter.getConstructor().newInstance());
        holder.setName(name);
        holders.put(name, holder);
        holder.setAsyncSupported(APP_IS_ASYNC);
      }

      // Ensure mapping
      boolean mapped = false;
      for (FilterMapping mapping : mappings) {

        for (String ps : mapping.getPathSpecs()) {
          if (pathSpec.equals(ps) && name.equals(mapping.getFilterName())) {
            mapped = true;
            break;
          }
        }
      }
      if (!mapped) {
        FilterMapping mapping = new FilterMapping();
        mapping.setFilterName(name);
        mapping.setPathSpec(pathSpec);
        mapping.setDispatches(FilterMapping.REQUEST);
        mappings.add(mapping);
      }
    }

    /**
     * Instantiate any registrations of a jetty provided filter
     *
     * @throws ReflectiveOperationException If a new instance of the filter cannot be instantiated
     */
    void instantiateJettyFilters() throws ReflectiveOperationException {
      for (FilterHolder h : holders.values()) {
        if (h.getClassName().startsWith(JETTY_PACKAGE)) {
          Class<? extends Filter> filter =
              ServletHolder.class
                  .getClassLoader()
                  .loadClass(h.getClassName())
                  .asSubclass(Filter.class);
          h.setFilter(filter.getConstructor().newInstance());
        }
      }
    }

    FilterHolder[] getHolders() {
      return holders.values().toArray(new FilterHolder[0]);
    }

    FilterMapping[] getMappings() {
      List<FilterMapping> trimmed = new ArrayList<>(mappings.size());
      for (FilterMapping m : mappings) {
        if (this.holders.containsKey(m.getFilterName())) {
          trimmed.add(m);
        }
      }
      return trimmed.toArray(new FilterMapping[0]);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



runtime/runtime_impl_jetty12/src/main/java/com/google/apphosting/runtime/jetty/ee8/AppEngineWebAppContext.java [414:657]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  private static class TrimmedServlets {
    private final Map<String, ServletHolder> holders = new HashMap<>();
    private final List<ServletMapping> mappings = new ArrayList<>();

    TrimmedServlets(ServletHolder[] holders, ServletMapping[] mappings) {
      for (ServletHolder h : holders) {

        // Replace deprecated package names.
        String className = h.getClassName();
        if (className != null)
        {
          for (Map.Entry<String, String> entry : DEPRECATED_PACKAGE_NAMES.entrySet()) {
            if (className.startsWith(entry.getKey())) {
              h.setClassName(className.replace(entry.getKey(), entry.getValue()));
            }
          }
        }

        h.setAsyncSupported(APP_IS_ASYNC);
        this.holders.put(h.getName(), h);
      }
      this.mappings.addAll(Arrays.asList(mappings));
    }

    /**
     * Ensure the registration of a container provided servlet:
     *
     * <ul>
     *   <li>If any existing servlet registrations are for the passed servlet class, then their
     *       holder is updated with a new instance created on the containers classpath.
     *   <li>If a servlet registration for the passed servlet name does not exist, one is created to
     *       the passed servlet class.
     * </ul>
     *
     * @param name The servlet name
     * @param servlet The servlet class
     * @throws ReflectiveOperationException If a new instance of the servlet cannot be instantiated
     */
    void ensure(String name, Class<? extends Servlet> servlet) throws ReflectiveOperationException {
      // Instantiate any holders referencing this servlet (may be application instances)
      for (ServletHolder h : holders.values()) {
        if (servlet.getName().equals(h.getClassName())) {
          h.setServlet(servlet.getConstructor().newInstance());
          h.setAsyncSupported(APP_IS_ASYNC);
        }
      }

      // Look for (or instantiate) our named instance
      ServletHolder holder = holders.get(name);
      if (holder == null) {
        holder = new ServletHolder(servlet.getConstructor().newInstance());
        holder.setInitOrder(1);
        holder.setName(name);
        holder.setAsyncSupported(APP_IS_ASYNC);
        holders.put(name, holder);
      }
    }

    /**
     * Ensure the registration of a container provided servlet:
     *
     * <ul>
     *   <li>If any existing servlet registrations are for the passed servlet class, then their
     *       holder is updated with a new instance created on the containers classpath.
     *   <li>If a servlet registration for the passed servlet name does not exist, one is created to
     *       the passed servlet class.
     *   <li>If a servlet mapping for the passed servlet name and pathSpec does not exist, one is
     *       created.
     * </ul>
     *
     * @param name The servlet name
     * @param servlet The servlet class
     * @param pathSpec The servlet pathspec
     * @throws ReflectiveOperationException If a new instance of the servlet cannot be instantiated
     */
    void ensure(String name, Class<? extends Servlet> servlet, String pathSpec)
        throws ReflectiveOperationException {
      // Ensure Servlet
      ensure(name, servlet);

      // Ensure mapping
      if (pathSpec != null) {
        boolean mapped = false;
        for (ServletMapping mapping : mappings) {
          if (mapping.containsPathSpec(pathSpec)) {
            mapped = true;
            break;
          }
        }
        if (!mapped) {
          ServletMapping mapping = new ServletMapping();
          mapping.setServletName(name);
          mapping.setPathSpec(pathSpec);
          if (pathSpec.equals("/")) {
            mapping.setFromDefaultDescriptor(true);
          }
          mappings.add(mapping);
        }
      }
    }

    /**
     * Instantiate any registrations of a jetty provided servlet
     *
     * @throws ReflectiveOperationException If a new instance of the servlet cannot be instantiated
     */
    void instantiateJettyServlets() throws ReflectiveOperationException {
      for (ServletHolder h : holders.values()) {
        if (h.getClassName() != null && h.getClassName().startsWith(JETTY_PACKAGE)) {
          Class<? extends Servlet> servlet =
              ServletHolder.class
                  .getClassLoader()
                  .loadClass(h.getClassName())
                  .asSubclass(Servlet.class);
          h.setServlet(servlet.getConstructor().newInstance());
        }
      }
    }

    ServletHolder[] getHolders() {
      return holders.values().toArray(new ServletHolder[0]);
    }

    ServletMapping[] getMappings() {
      List<ServletMapping> trimmed = new ArrayList<>(mappings.size());
      for (ServletMapping m : mappings) {
        if (this.holders.containsKey(m.getServletName())) {
          trimmed.add(m);
        }
      }
      return trimmed.toArray(new ServletMapping[0]);
    }
  }

  private static class TrimmedFilters {
    private final Map<String, FilterHolder> holders = new HashMap<>();
    private final List<FilterMapping> mappings = new ArrayList<>();

    TrimmedFilters(FilterHolder[] holders, FilterMapping[] mappings) {
      for (FilterHolder h : holders) {

        // Replace deprecated package names.
        String className = h.getClassName();
        if (className != null)
        {
          for (Map.Entry<String, String> entry : DEPRECATED_PACKAGE_NAMES.entrySet()) {
            if (className.startsWith(entry.getKey())) {
              h.setClassName(className.replace(entry.getKey(), entry.getValue()));
            }
          }
        }

        h.setAsyncSupported(APP_IS_ASYNC);
        this.holders.put(h.getName(), h);
      }
      this.mappings.addAll(Arrays.asList(mappings));
    }

    /**
     * Ensure the registration of a container provided filter:
     *
     * <ul>
     *   <li>If any existing filter registrations are for the passed filter class, then their holder
     *       is updated with a new instance created on the containers classpath.
     *   <li>If a filter registration for the passed filter name does not exist, one is created to
     *       the passed filter class.
     *   <li>If a filter mapping for the passed filter name and pathSpec does not exist, one is
     *       created.
     * </ul>
     *
     * @param name The filter name
     * @param filter The filter class
     * @param pathSpec The servlet pathspec
     * @throws ReflectiveOperationException If a new instance of the servlet cannot be instantiated
     */
    void ensure(String name, Class<? extends Filter> filter, String pathSpec) throws Exception {

      // Instantiate any holders referencing this filter (may be application instances)
      for (FilterHolder h : holders.values()) {
        if (filter.getName().equals(h.getClassName())) {
          h.setFilter(filter.getConstructor().newInstance());
          h.setAsyncSupported(APP_IS_ASYNC);
        }
      }

      // Look for (or instantiate) our named instance
      FilterHolder holder = holders.get(name);
      if (holder == null) {
        holder = new FilterHolder(filter.getConstructor().newInstance());
        holder.setName(name);
        holders.put(name, holder);
        holder.setAsyncSupported(APP_IS_ASYNC);
      }

      // Ensure mapping
      boolean mapped = false;
      for (FilterMapping mapping : mappings) {

        for (String ps : mapping.getPathSpecs()) {
          if (pathSpec.equals(ps) && name.equals(mapping.getFilterName())) {
            mapped = true;
            break;
          }
        }
      }
      if (!mapped) {
        FilterMapping mapping = new FilterMapping();
        mapping.setFilterName(name);
        mapping.setPathSpec(pathSpec);
        mapping.setDispatches(FilterMapping.REQUEST);
        mappings.add(mapping);
      }
    }

    /**
     * Instantiate any registrations of a jetty provided filter
     *
     * @throws ReflectiveOperationException If a new instance of the filter cannot be instantiated
     */
    void instantiateJettyFilters() throws ReflectiveOperationException {
      for (FilterHolder h : holders.values()) {
        if (h.getClassName().startsWith(JETTY_PACKAGE)) {
          Class<? extends Filter> filter =
              ServletHolder.class
                  .getClassLoader()
                  .loadClass(h.getClassName())
                  .asSubclass(Filter.class);
          h.setFilter(filter.getConstructor().newInstance());
        }
      }
    }

    FilterHolder[] getHolders() {
      return holders.values().toArray(new FilterHolder[0]);
    }

    FilterMapping[] getMappings() {
      List<FilterMapping> trimmed = new ArrayList<>(mappings.size());
      for (FilterMapping m : mappings) {
        if (this.holders.containsKey(m.getFilterName())) {
          trimmed.add(m);
        }
      }
      return trimmed.toArray(new FilterMapping[0]);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



