Class ManifestFile

java.lang.Object
org.apache.juneau.commons.runtime.ManifestFile

public class ManifestFile extends Object
Lean accessor for the contents of a Jar manifest file.

Wraps a Manifest so callers can read main-attribute and per-section entries via an Optional-returning API without depending on the marshalling stack.

Example:

// Load the manifest of the jar containing this class. ManifestFile mf = new ManifestFile(MyClass.class); // Pull a main attribute. String version = mf.get("Bundle-Version").orElse("unknown"); // Pull a section attribute (new capability). String sectionAttr = mf.get("my-section", "some-key").orElse(null);

Notes:
  • This class is immutable and thread-safe once constructed.
  • Constructor Details

    • ManifestFile

      public ManifestFile(Class<?> c) throws IOException
      Loads the manifest of the jar containing the given class.

      If the class isn't packaged inside a Jar (e.g. running from an exploded classpath), the resulting instance has no main attributes and no sections.

      Parameters:
      c - The anchor class. Must not be null.
      Throws:
      IOException - If the manifest could not be read.
    • ManifestFile

      public ManifestFile(File f) throws IOException
      Loads a manifest from a file on disk.
      Parameters:
      f - The manifest file. Must not be null.
      Throws:
      IOException - If the manifest could not be read.
    • ManifestFile

      Loads a manifest from an input stream.

      The stream must end in a newline to pick up the last line.

      Parameters:
      in - The manifest stream. Must not be null.
      Throws:
      IOException - If the manifest could not be read.
    • ManifestFile

      public ManifestFile(Manifest mf)
      Wraps an existing Manifest.
      Parameters:
      mf - The manifest. Can be null, in which case the manifest is treated as having no attributes or sections.
    • ManifestFile

      public ManifestFile(Path path) throws IOException
      Loads a manifest from a path.
      Parameters:
      path - The manifest path. Must not be null.
      Throws:
      IOException - If the manifest could not be read.
    • ManifestFile

      public ManifestFile(Reader r) throws IOException
      Loads a manifest from a reader.

      The reader's contents must end in a newline to pick up the last line.

      Parameters:
      r - The manifest reader. Must not be null.
      Throws:
      IOException - If the manifest could not be read.
  • Method Details

    • get

      public Optional<String> get(String key)
      Returns the value of a main-attribute key.
      Parameters:
      key - The main-attribute key. Can be null, in which case an empty value is returned.
      Returns:
      The value, or empty if the key is unset.
    • get

      public Optional<String> get(String section, String key)
      Returns the value of a key inside the named section.
      Parameters:
      section - The section name. Can be null, in which case an empty value is returned.
      key - The attribute key. Can be null, in which case an empty value is returned.
      Returns:
      The value, or empty if the section or key is unset.
    • sections

      public Set<String> sections()
      Returns the names of all per-entry sections in the manifest.
      Returns:
      An unmodifiable set of section names.
    • asMap

      public Map<String,String> asMap()
      Returns the main attributes as an unmodifiable map.
      Returns:
      The main-attribute map.
    • asMap

      public Map<String,String> asMap(String section)
      Returns the attributes of the named section as an unmodifiable map.
      Parameters:
      section - The section name. Can be null, in which case an empty map is returned.
      Returns:
      The section's attribute map, or an empty map if the section is unknown.
    • toString

      public String toString()
      Overrides:
      toString in class Object