예제 #1
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);
  }
예제 #2
0
 @Override
 public void postConfigure(WebAppContext context) throws Exception {
   context.setAttribute(FRAGMENT_RESOURCES, null);
 }
예제 #3
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);
      }
    }
  }
예제 #4
0
  /** Resolve all servlet/filter/listener metadata from all sources: descriptors and annotations. */
  public void resolve(WebAppContext context) throws Exception {
    LOG.debug("metadata resolve {}", context);

    // Ensure origins is fresh
    _origins.clear();

    // Set the ordered lib attribute
    if (_ordering != null) {
      List<String> orderedLibs = new ArrayList<String>();
      for (Resource webInfJar : _orderedWebInfJars) {
        // get just the name of the jar file
        String fullname = webInfJar.getName();
        int i = fullname.indexOf(".jar");
        int j = fullname.lastIndexOf("/", i);
        orderedLibs.add(fullname.substring(j + 1, i + 4));
      }
      context.setAttribute(ServletContext.ORDERED_LIBS, orderedLibs);
    }

    // set the webxml version
    if (_webXmlRoot != null) {
      context.getServletContext().setEffectiveMajorVersion(_webXmlRoot.getMajorVersion());
      context.getServletContext().setEffectiveMinorVersion(_webXmlRoot.getMinorVersion());
    }

    for (DescriptorProcessor p : _descriptorProcessors) {
      p.process(context, getWebDefault());
      p.process(context, getWebXml());
      for (WebDescriptor wd : getOverrideWebs()) {
        LOG.debug("process {} {}", context, wd);
        p.process(context, wd);
      }
    }

    // get an apply the annotations that are not associated with a fragment (and hence for
    // which no ordering applies
    List<DiscoveredAnnotation> nonFragAnnotations = _annotations.get(NON_FRAG_RESOURCE);
    if (nonFragAnnotations != null) {
      for (DiscoveredAnnotation a : nonFragAnnotations) {
        LOG.debug("apply {}", a);
        a.apply();
      }
    }

    // apply the annotations that are associated with a fragment, according to the
    // established ordering
    List<Resource> resources = getOrderedWebInfJars();
    for (Resource r : resources) {
      FragmentDescriptor fd = _webFragmentResourceMap.get(r);
      if (fd != null) {
        for (DescriptorProcessor p : _descriptorProcessors) {
          LOG.debug("process {} {}", context, fd);
          p.process(context, fd);
        }
      }

      List<DiscoveredAnnotation> fragAnnotations = _annotations.get(r);
      if (fragAnnotations != null) {
        for (DiscoveredAnnotation a : fragAnnotations) {
          LOG.debug("apply {}", a);
          a.apply();
        }
      }
    }
  }