Class KiwiJars

java.lang.Object
org.kiwiproject.jar.KiwiJars

public final class KiwiJars extends Object
Utilities for working with Java JAR files.
  • Method Details

    • getPathComponents

      public static List<String> getPathComponents(Class<?> classInJar)
      Get the path components of the JAR file path that the given class lives in, or an empty list if the path components could not be obtained.
      Parameters:
      classInJar - the class which resides in the JAR you want to find
      Returns:
      a mutable list of path components if found, or an immutable empty list if not found or an exception is thrown attempting to get the path
    • getPath

      public static Optional<String> getPath(Class<?> classInJar)
      Get the path of the JAR file that the given class lives in, or an empty Optional if the path could not be obtained for any reason.
      Parameters:
      classInJar - the class which resides in the JAR you want to find
      Returns:
      optional containing the path if found, or an empty optional
    • getDirectoryPath

      public static Optional<String> getDirectoryPath(Class<?> classInJar)
      Get the directory path of the JAR file that the given class lives in, or an empty Optional if the path could not be obtained for any reason.
      Parameters:
      classInJar - the class which resides in the JAR you want to find
      Returns:
      optional containing the directory path if found, or an empty optional
    • readSingleValueFromJarManifest

      public static Optional<String> readSingleValueFromJarManifest(String manifestEntryName)
      Resolves a given entry name from the manifest file (if found) in the current class loader.
      Parameters:
      manifestEntryName - The name of the property to resolve
      Returns:
      an Optional containing the resolved value or Optional.empty() if not
    • readSingleValueFromJarManifest

      public static Optional<String> readSingleValueFromJarManifest(ClassLoader classLoader, String manifestEntryName)
      Resolves a given entry name from the manifest file (if found) from the given class loader.
      Parameters:
      classLoader - The class loader to find the manifest file to search
      manifestEntryName - The name of the property to resolve
      Returns:
      an Optional containing the resolved value or Optional.empty() if not
    • readSingleValueFromJarManifest

      public static Optional<String> readSingleValueFromJarManifest(ClassLoader classLoader, String manifestEntryName, Predicate<URL> manifestFilter)
      Resolves a given entry name from the manifest file (if found) from the given class loader.
      Parameters:
      classLoader - The class loader to find the manifest file to search
      manifestEntryName - The name of the property to resolve
      manifestFilter - An optional filter that can be used to filter down manifest files if there are more than one.
      Returns:
      an Optional containing the resolved value or Optional.empty() if not
      Implementation Note:
      If this code is called from a "fat-jar" with a single manifest file, then the filter predicate is unnecessary. The predicate filter is really only necessary if there are multiple jars loaded in the classpath all containing manifest files.
    • readValuesFromJarManifest

      public static Map<String,String> readValuesFromJarManifest(String... manifestEntryNames)
      Resolves all the given entry names from the manifest (if found) from the current class loader.
      Parameters:
      manifestEntryNames - an array of names to resolve from the manifest
      Returns:
      a Map<String,String> of resolved entries
    • readValuesFromJarManifest

      public static Map<String,String> readValuesFromJarManifest(ClassLoader classLoader, String... manifestEntryNames)
      Resolves all the given entry names from the manifest (if found) from the given class loader.
      Parameters:
      classLoader - the classloader to search for manifest files in
      manifestEntryNames - an array of names to resolve from the manifest
      Returns:
      a Map<String,String> of resolved entries
    • readValuesFromJarManifest

      public static Map<String,String> readValuesFromJarManifest(ClassLoader classLoader, Predicate<URL> manifestFilter, String... manifestEntryNames)
      Resolves all the given entry names from the manifest (if found) from the given class loader.
      Parameters:
      classLoader - the classloader to search for manifest files in
      manifestFilter - a predicate filter used to limit which jar files to search for a manifest file
      manifestEntryNames - an array of names to resolve from the manifest
      Returns:
      a Map<String,String> of resolved entries
      Implementation Note:
      If this code is called from a "fat-jar" with a single manifest file, then the filter predicate is unnecessary. The predicate filter is really only necessary if there are multiple jars loaded in the classpath all containing manifest files.