Exemple #1
0
  public void removeServletHolder(ServletHolder servletHolder) {

    synchronized (this.context) {
      ServletHandler handler = context.getServletHandler();

      /*
       * A list of all the servlets that don't implement the class
       * 'servlet', (i.e. They should be kept in the context
       */
      List<ServletHolder> servlets = new ArrayList<ServletHolder>();

      /*
       * The names all the servlets that we remove so we can drop the
       * mappings too
       */
      Set<String> names = new HashSet<String>();

      for (ServletHolder holder : handler.getServlets()) {
        /*
         * If it is the class we want to remove, then just keep track of
         * its name
         */
        if (servletHolder.equals(holder)) {
          names.add(holder.getName());
        } else /* We keep it */ {
          servlets.add(holder);
        }
      }

      List<ServletMapping> mappings = new ArrayList<ServletMapping>();

      for (ServletMapping mapping : handler.getServletMappings()) {
        /*
         * Only keep the mappings that didn't point to one of the
         * servlets we removed
         */
        if (!names.contains(mapping.getServletName())) {
          mappings.add(mapping);
        }
      }

      /* Set the new configuration for the mappings and the servlets */
      handler.setServletMappings(mappings.toArray(new ServletMapping[0]));
      handler.setServlets(servlets.toArray(new ServletHolder[0]));
    }
  }
  @Override
  public void delete(String path) {
    ServletMapping mapping = handler.getServletMapping(path);
    ServletMapping[] mappings = new ServletMapping[handler.getServletMappings().length - 1];
    ServletHolder[] servlets = new ServletHolder[handler.getServlets().length - 1];
    int mLength = 0, sLength = 0;

    for (ServletMapping m : handler.getServletMappings()) {
      if (!m.equals(mapping)) {
        mappings[mLength++] = m;
      }
    }

    for (ServletHolder s : handler.getServlets()) {
      if (!s.equals(handler.getServlet(mapping.getServletName()))) {
        servlets[sLength++] = s;
      }
    }

    handler.setServletMappings(mappings);
    handler.setServlets(servlets);
  }