Class ScanRunContext

java.lang.Object
org.egothor.methodatlas.ScanRunContext

public final class ScanRunContext extends Object
Thread-local holder for the current ScanRun, the JUL-friendly equivalent of SLF4J's MDC.

MethodAtlasApp.run(String[], java.io.PrintWriter) constructs a ScanRun once at the top of every invocation and calls set(ScanRun) so that any code executed on the same thread for the duration of the run can read the correlation id through current(). A custom java.util.logging.Formatter reads the context and prepends the run id to every log record (introduced in Item 20 of the architecture remediation plan).

Thread safety

Each thread sees its own value. The MethodAtlas CLI runs single-threaded scans by default; AI provider calls are sequential. When concurrent threads do appear (the ServiceLoader per-plugin isolation), callers that want the run id on those threads must propagate it explicitly.

Cleanup

Always pair set(ScanRun) with a clear() in a finally block — without it, the thread-local reference outlives the CLI invocation in container deployments that pool threads. The standard MethodAtlas CLI exits the JVM at end-of-run, so cleanup matters mainly when MethodAtlas is invoked programmatically.

Since:
1.0.0
  • Method Details

    • set

      public static void set(ScanRun run)
      Sets the current scan run for the calling thread.
      Parameters:
      run - the run identifier; must not be null
    • current

      public static Optional<ScanRun> current()
      Returns the current scan run for the calling thread, or Optional.empty() when no run is currently set (the standard case before MethodAtlasApp.main(String[]) runs or after clear()).
      Returns:
      optional carrying the current run
    • clear

      public static void clear()
      Removes the current scan run from the calling thread. Always called in a finally block paired with set(ScanRun) so that the thread-local reference does not outlive the CLI invocation.