Ejemplo n.º 1
0
  public void makeTempDirectory(File parent, WebAppContext context, boolean deleteExisting)
      throws IOException {
    if (parent != null && parent.exists() && parent.canWrite() && parent.isDirectory()) {
      String temp = getCanonicalNameForWebAppTmpDir(context);
      File tmpDir = new File(parent, temp);

      if (deleteExisting && tmpDir.exists()) {
        if (!IO.delete(tmpDir)) {
          if (LOG.isDebugEnabled()) LOG.debug("Failed to delete temp dir " + tmpDir);
        }

        // If we can't delete the existing tmp dir, create a new one
        if (tmpDir.exists()) {
          String old = tmpDir.toString();
          tmpDir = File.createTempFile(temp + "_", "");
          if (tmpDir.exists()) IO.delete(tmpDir);
          LOG.warn("Can't reuse " + old + ", using " + tmpDir);
        }
      }

      if (!tmpDir.exists()) tmpDir.mkdir();

      // If the parent is not a work directory
      if (!isTempWorkDirectory(tmpDir)) {
        tmpDir.deleteOnExit();
      }

      if (LOG.isDebugEnabled()) LOG.debug("Set temp dir " + tmpDir);
      context.setTempDirectory(tmpDir);
    }
  }
Ejemplo n.º 2
0
 /**
  * @see
  *     org.eclipse.jetty.webapp.AbstractConfiguration#cloneConfigure(org.eclipse.jetty.webapp.WebAppContext,
  *     org.eclipse.jetty.webapp.WebAppContext)
  */
 @Override
 public void cloneConfigure(WebAppContext template, WebAppContext context) throws Exception {
   File tmpDir =
       File.createTempFile(
           WebInfConfiguration.getCanonicalNameForWebAppTmpDir(context),
           "",
           template.getTempDirectory().getParentFile());
   if (tmpDir.exists()) {
     IO.delete(tmpDir);
   }
   tmpDir.mkdir();
   tmpDir.deleteOnExit();
   context.setTempDirectory(tmpDir);
 }
Ejemplo n.º 3
0
  @Override
  public void deconfigure(WebAppContext context) throws Exception {
    // delete temp directory if we had to create it or if it isn't called work
    Boolean tmpdirConfigured = (Boolean) context.getAttribute(TEMPDIR_CONFIGURED);

    if (context.getTempDirectory() != null
        && (tmpdirConfigured == null || !tmpdirConfigured.booleanValue())
        && !isTempWorkDirectory(context.getTempDirectory())) {
      IO.delete(context.getTempDirectory());
      context.setTempDirectory(null);

      // clear out the context attributes for the tmp dir only if we had to
      // create the tmp dir
      context.setAttribute(TEMPDIR_CONFIGURED, null);
      context.setAttribute(WebAppContext.TEMPDIR, null);
    }

    // reset the base resource back to what it was before we did any unpacking of resources
    context.setBaseResource(_preUnpackBaseResource);
  }
Ejemplo n.º 4
0
  /**
   * Get a temporary directory in which to unpack the war etc etc. The algorithm for determining
   * this is to check these alternatives in the order shown:
   *
   * <p>A. Try to use an explicit directory specifically for this webapp:
   *
   * <ol>
   *   <li>Iff an explicit directory is set for this webapp, use it. Do NOT set delete on exit.
   *   <li>Iff javax.servlet.context.tempdir context attribute is set for this webapp && exists &&
   *       writeable, then use it. Do NOT set delete on exit.
   * </ol>
   *
   * <p>B. Create a directory based on global settings. The new directory will be called
   * "Jetty_"+host+"_"+port+"__"+context+"_"+virtualhost Work out where to create this directory:
   *
   * <ol>
   *   <li>Iff $(jetty.home)/work exists create the directory there. Do NOT set delete on exit. Do
   *       NOT delete contents if dir already exists.
   *   <li>Iff WEB-INF/work exists create the directory there. Do NOT set delete on exit. Do NOT
   *       delete contents if dir already exists.
   *   <li>Else create dir in $(java.io.tmpdir). Set delete on exit. Delete contents if dir already
   *       exists.
   * </ol>
   */
  public void resolveTempDirectory(WebAppContext context) {
    // If a tmp directory is already set, we're done
    File tmpDir = context.getTempDirectory();
    if (tmpDir != null && tmpDir.isDirectory() && tmpDir.canWrite()) {
      context.setAttribute(TEMPDIR_CONFIGURED, Boolean.TRUE);
      return; // Already have a suitable tmp dir configured
    }

    // No temp directory configured, try to establish one.
    // First we check the context specific, javax.servlet specified, temp directory attribute
    File servletTmpDir = asFile(context.getAttribute(WebAppContext.TEMPDIR));
    if (servletTmpDir != null && servletTmpDir.isDirectory() && servletTmpDir.canWrite()) {
      // Use as tmpDir
      tmpDir = servletTmpDir;
      // Ensure Attribute has File object
      context.setAttribute(WebAppContext.TEMPDIR, tmpDir);
      // Set as TempDir in context.
      context.setTempDirectory(tmpDir);
      return;
    }

    try {
      // Put the tmp dir in the work directory if we had one
      File work = new File(System.getProperty("jetty.home"), "work");
      if (work.exists() && work.canWrite() && work.isDirectory()) {
        makeTempDirectory(
            work, context, false); // make a tmp dir inside work, don't delete if it exists
      } else {
        File baseTemp = asFile(context.getAttribute(WebAppContext.BASETEMPDIR));
        if (baseTemp != null && baseTemp.isDirectory() && baseTemp.canWrite()) {
          // Use baseTemp directory (allow the funky Jetty_0_0_0_0.. subdirectory logic to kick in
          makeTempDirectory(baseTemp, context, false);
        } else {
          makeTempDirectory(
              new File(System.getProperty("java.io.tmpdir")),
              context,
              true); // make a tmpdir, delete if it already exists
        }
      }
    } catch (Exception e) {
      tmpDir = null;
      LOG.ignore(e);
    }

    // Third ... Something went wrong trying to make the tmp directory, just make
    // a jvm managed tmp directory
    if (context.getTempDirectory() == null) {
      try {
        // Last resort
        tmpDir = File.createTempFile("JettyContext", "");
        if (tmpDir.exists()) IO.delete(tmpDir);
        tmpDir.mkdir();
        tmpDir.deleteOnExit();
        context.setTempDirectory(tmpDir);
      } catch (IOException e) {
        tmpDir = null;
        throw new IllegalStateException(
            "Cannot create tmp dir in "
                + System.getProperty("java.io.tmpdir")
                + " for context "
                + context,
            e);
      }
    }
  }