/** Returns the path to be used as the servlet name. */
  private Path getPagePath(String pathName) {
    Path rootDir = _webApp.getRootDirectory();
    String realPath = _webApp.getRealPath(pathName);
    Path path = rootDir.lookupNative(realPath);

    if (path.isFile() && path.canRead()) return path;

    java.net.URL url;
    ClassLoader loader = _webApp.getClassLoader();
    if (loader != null) {
      url = _webApp.getClassLoader().getResource(pathName);

      String name = url != null ? url.toString() : null;

      path = null;
      if (url != null && (name.endsWith(".jar") || name.endsWith(".zip")))
        path = JarPath.create(Vfs.lookup(url.toString())).lookup(pathName);
      else if (url != null) path = Vfs.lookup(url.toString());

      if (path != null && path.isFile() && path.canRead()) return path;
    }

    url = ClassLoader.getSystemResource(pathName);
    String name = url != null ? url.toString() : null;

    path = null;
    if (url != null && (name.endsWith(".jar") || name.endsWith(".zip")))
      path = JarPath.create(Vfs.lookup(url.toString())).lookup(pathName);
    else if (url != null) path = Vfs.lookup(url.toString());

    if (path != null && path.isFile() && path.canRead()) return path;
    else return null;
  }
  public Path getCauchoPath(String name) {
    String realPath = getRealPath(name);

    Path rootDirectory = getRootDirectory();
    Path path = rootDirectory.lookupNative(realPath);

    return path;
  }
  /** Returns the resource for a uripath as an input stream. */
  public InputStream getResourceAsStream(String uripath) {
    Path rootDirectory = getRootDirectory();
    Path path = rootDirectory.lookupNative(getRealPath(uripath));

    try {
      if (path.canRead()) return path.openRead();
      else {
        String resource = "META-INF/resources" + uripath;

        return getClassLoader().getResourceAsStream(resource);
      }
    } catch (IOException e) {
      log.log(Level.FINEST, e.toString(), e);

      return null;
    }
  }
  /**
   * Returns a resource for the given uri.
   *
   * <p>XXX: jdk 1.1.x doesn't appear to allow creation of private URL streams.
   */
  @Override
  public URL getResource(String name) throws java.net.MalformedURLException {
    if (!name.startsWith("/")) throw new java.net.MalformedURLException(name);

    String realPath = getRealPath(name);

    Path rootDirectory = getRootDirectory();
    Path path = rootDirectory.lookupNative(realPath);

    URL url = new URL("jndi:/server" + getContextPath() + name);

    if (path.exists() && name.startsWith("/resources/")) {
      return url;
    } else if (path.isFile()) {
      return url;
    } else if (getClassLoader().getResource("META-INF/resources/" + realPath) != null) {
      return url;
    } else if (path.exists()) {
      return new URL(path.getURL());
    }

    return null;
  }
  /**
   * Given a request and response, returns the compiled Page. For example, JspManager will return
   * the JspPage and XtpManager will return the XtpPage.
   *
   * @param req servlet request for generating the page.
   * @param res servlet response to any needed error messages.
   */
  private Page getSubPage(HttpServletRequest req, HttpServletResponse res) throws Exception {
    CauchoRequest cauchoRequest = null;

    initGetPage();

    /*
    if (! _webApp.isActive())
      throw new UnavailableException("JSP compilation unavailable during restart", 10);
    */

    if (req instanceof CauchoRequest) cauchoRequest = (CauchoRequest) req;

    String servletPath;

    if (cauchoRequest != null) servletPath = cauchoRequest.getPageServletPath();
    else servletPath = RequestAdapter.getPageServletPath(req);

    if (servletPath == null) servletPath = "/";

    String uri;

    if (cauchoRequest != null) uri = cauchoRequest.getPageURI();
    else uri = RequestAdapter.getPageURI(req);

    Path appDir = _webApp.getRootDirectory();

    String realPath;
    Path subcontext;

    ServletConfig config = null;

    String jspPath = (String) req.getAttribute("caucho.jsp.jsp-file");
    if (jspPath != null) {
      req.removeAttribute("caucho.jsp.jsp-file");

      subcontext = getPagePath(jspPath);

      return _manager.getPage(uri, jspPath, subcontext, config);
    }

    String pathInfo;

    if (cauchoRequest != null) pathInfo = cauchoRequest.getPagePathInfo();
    else pathInfo = RequestAdapter.getPagePathInfo(req);

    subcontext = getPagePath(servletPath);

    if (subcontext != null) return _manager.getPage(servletPath, subcontext);

    if (pathInfo == null) {
      realPath = _webApp.getRealPath(servletPath);
      subcontext = appDir.lookupNative(realPath);

      return _manager.getPage(servletPath, subcontext);
    }

    subcontext = getPagePath(servletPath + pathInfo);
    if (subcontext != null) return _manager.getPage(servletPath + pathInfo, subcontext);

    // If servlet path exists, can't use pathInfo to lookup servlet,
    // because /jsp/WEB-INF would be a security hole
    if (servletPath != null && !servletPath.equals("")) {
      // server/0035
      throw new FileNotFoundException(L.l("{0} was not found on this server.", uri));
      // return null;
    }

    subcontext = getPagePath(pathInfo);
    if (subcontext != null) return _manager.getPage(pathInfo, subcontext);

    subcontext = getPagePath(uri);
    if (subcontext == null)
      throw new FileNotFoundException(L.l("{0} was not found on this server.", uri));

    return _manager.getPage(uri, subcontext);
  }