Exemple #1
0
  /** Compile (if needed) and load a tag file */
  public Class<?> loadTagFile() throws JasperException {

    try {
      if (ctxt.isRemoved()) {
        throw new FileNotFoundException(jspUri);
      }
      if (options.getDevelopment() || firstTime) {
        synchronized (this) {
          firstTime = false;
          ctxt.compile();
        }
      } else {
        if (compileException != null) {
          throw compileException;
        }
      }

      if (reload) {
        tagHandlerClass = ctxt.load();
        reload = false;
      }
    } catch (FileNotFoundException ex) {
      throw new JasperException(ex);
    }

    return tagHandlerClass;
  }
  /**
   * Create a JspRuntimeContext for a web application context.
   *
   * <p>Loads in any previously generated dependencies from file.
   *
   * @param context ServletContext for web application
   */
  public JspRuntimeContext(ServletContext context, Options options) {

    this.context = context;
    this.options = options;

    // Get the parent class loader
    parentClassLoader = (URLClassLoader) Thread.currentThread().getContextClassLoader();
    if (parentClassLoader == null) {
      parentClassLoader = (URLClassLoader) this.getClass().getClassLoader();
    }

    if (log.isDebugEnabled()) {
      if (parentClassLoader != null) {
        log.debug(
            Localizer.getMessage(
                "jsp.message.parent_class_loader_is", parentClassLoader.toString()));
      } else {
        log.debug(Localizer.getMessage("jsp.message.parent_class_loader_is", "<none>"));
      }
    }

    initClassPath();

    if (context instanceof org.apache.jasper.servlet.JspCServletContext) {
      return;
    }

    if (Constants.IS_SECURITY_ENABLED) {
      initSecurity();
    }

    // If this web application context is running from a
    // directory, start the background compilation thread
    String appBase = context.getRealPath("/");
    if (!options.getDevelopment() && appBase != null && options.getCheckInterval() > 0) {
      lastCheck = System.currentTimeMillis();
    }
  }
Exemple #3
0
  public void service(HttpServletRequest request, HttpServletResponse response, boolean precompile)
      throws ServletException, IOException, FileNotFoundException {

    Servlet servlet;

    try {

      if (ctxt.isRemoved()) {
        throw new FileNotFoundException(jspUri);
      }

      if ((available > 0L) && (available < Long.MAX_VALUE)) {
        if (available > System.currentTimeMillis()) {
          response.setDateHeader("Retry-After", available);
          response.sendError(
              HttpServletResponse.SC_SERVICE_UNAVAILABLE,
              Localizer.getMessage("jsp.error.unavailable"));
          return;
        }

        // Wait period has expired. Reset.
        available = 0;
      }

      /*
       * (1) Compile
       */
      if (options.getDevelopment() || firstTime) {
        synchronized (this) {
          firstTime = false;

          // The following sets reload to true, if necessary
          ctxt.compile();
        }
      } else {
        if (compileException != null) {
          // Throw cached compilation exception
          throw compileException;
        }
      }

      /*
       * (2) (Re)load servlet class file
       */
      servlet = getServlet();

      // If a page is to be precompiled only, return.
      if (precompile) {
        return;
      }

    } catch (ServletException ex) {
      if (options.getDevelopment()) {
        throw handleJspException(ex);
      }
      throw ex;
    } catch (FileNotFoundException fnfe) {
      // File has been removed. Let caller handle this.
      throw fnfe;
    } catch (IOException ex) {
      if (options.getDevelopment()) {
        throw handleJspException(ex);
      }
      throw ex;
    } catch (IllegalStateException ex) {
      if (options.getDevelopment()) {
        throw handleJspException(ex);
      }
      throw ex;
    } catch (Exception ex) {
      if (options.getDevelopment()) {
        throw handleJspException(ex);
      }
      throw new JasperException(ex);
    }

    try {

      /*
       * (3) Handle limitation of number of loaded Jsps
       */
      if (unloadAllowed) {
        synchronized (this) {
          if (unloadByCount) {
            if (unloadHandle == null) {
              unloadHandle = ctxt.getRuntimeContext().push(this);
            } else if (lastUsageTime < ctxt.getRuntimeContext().getLastJspQueueUpdate()) {
              ctxt.getRuntimeContext().makeYoungest(unloadHandle);
              lastUsageTime = System.currentTimeMillis();
            }
          } else {
            if (lastUsageTime < ctxt.getRuntimeContext().getLastJspQueueUpdate()) {
              lastUsageTime = System.currentTimeMillis();
            }
          }
        }
      }
      /*
       * (4) Service request
       */
      if (servlet instanceof SingleThreadModel) {
        // sync on the wrapper so that the freshness
        // of the page is determined right before servicing
        synchronized (this) {
          servlet.service(request, response);
        }
      } else {
        servlet.service(request, response);
      }
    } catch (UnavailableException ex) {
      String includeRequestUri =
          (String) request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI);
      if (includeRequestUri != null) {
        // This file was included. Throw an exception as
        // a response.sendError() will be ignored by the
        // servlet engine.
        throw ex;
      }

      int unavailableSeconds = ex.getUnavailableSeconds();
      if (unavailableSeconds <= 0) {
        unavailableSeconds = 60; // Arbitrary default
      }
      available = System.currentTimeMillis() + (unavailableSeconds * 1000L);
      response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, ex.getMessage());
    } catch (ServletException ex) {
      if (options.getDevelopment()) {
        throw handleJspException(ex);
      }
      throw ex;
    } catch (IOException ex) {
      if (options.getDevelopment()) {
        throw handleJspException(ex);
      }
      throw ex;
    } catch (IllegalStateException ex) {
      if (options.getDevelopment()) {
        throw handleJspException(ex);
      }
      throw ex;
    } catch (Exception ex) {
      if (options.getDevelopment()) {
        throw handleJspException(ex);
      }
      throw new JasperException(ex);
    }
  }
  public void service(HttpServletRequest request, HttpServletResponse response, boolean precompile)
      throws ServletException, IOException, FileNotFoundException {
    try {

      if (ctxt.isRemoved()) {
        throw new FileNotFoundException(jspUri);
      }

      if ((available > 0L) && (available < Long.MAX_VALUE)) {
        response.setDateHeader("Retry-After", available);
        response.sendError(
            HttpServletResponse.SC_SERVICE_UNAVAILABLE,
            Localizer.getMessage("jsp.error.unavailable"));
      }

      if (options.getDevelopment() || firstTime) {
        synchronized (this) {
          ctxt.compile();
        }
      }

      if (reload) {
        getServlet();
      }

      // If a page is to only to be precompiled return.
      if (precompile) {
        return;
      }

      if (theServlet instanceof SingleThreadModel) {
        // sync on the wrapper so that the freshness
        // of the page is determined right before servicing
        synchronized (this) {
          theServlet.service(request, response);
        }
      } else {
        theServlet.service(request, response);
      }

    } catch (UnavailableException ex) {
      String includeRequestUri = (String) request.getAttribute("javax.servlet.include.request_uri");
      if (includeRequestUri != null) {
        // This file was included. Throw an exception as
        // a response.sendError() will be ignored by the
        // servlet engine.
        throw ex;
      } else {
        int unavailableSeconds = ex.getUnavailableSeconds();
        if (unavailableSeconds <= 0) {
          unavailableSeconds = 60; // Arbitrary default
        }
        available = System.currentTimeMillis() + (unavailableSeconds * 1000L);
        response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, ex.getMessage());
      }
    } catch (FileNotFoundException ex) {
      String includeRequestUri = (String) request.getAttribute("javax.servlet.include.request_uri");
      if (includeRequestUri != null) {
        // This file was included. Throw an exception as
        // a response.sendError() will be ignored by the
        // servlet engine.
        throw new ServletException(ex);
      } else {
        try {
          response.sendError(HttpServletResponse.SC_NOT_FOUND, ex.getMessage());
        } catch (IllegalStateException ise) {
          log.error(Localizer.getMessage("jsp.error.file.not.found", ex.getMessage()), ex);
        }
      }
    } catch (ServletException ex) {
      throw ex;
    } catch (IOException ex) {
      throw ex;
    } catch (IllegalStateException ex) {
      throw ex;
    } catch (Exception ex) {
      throw new JasperException(ex);
    }
  }