Package com.tnsai.hooks
Class HookDispatcher
java.lang.Object
com.tnsai.hooks.HookDispatcher
Runs the registered
Hook chain for an in-flight
HookEvent (issue #58).
Semantics
- Select hooks where
hook.eventType().isAssignableFrom(event.getClass()). - Filter by
Hook.matcher().matches(event). - Sort by
Hook.priority()ascending (lowest first). - Apply each in order. Each result:
HookResult.Allow— continue to next hookHookResult.Block— stop chain, return the BlockHookResult.Modify— replace the event; subsequent hooks see the modified event; return the Modify only if no further hook decided otherwise
- If no hook matched or all returned Allow, return
HookResult.Allow.
Isolation
EachHook.apply(E) call is wrapped in a timeout (default 5s,
configurable per dispatcher) + try/catch. Throws or timeouts are
reduced to HookResult.Allow or HookResult.Block
per HookFailurePolicy. A misbehaving hook never kills the
chain or propagates an exception to the caller.
Concurrency
Safe to calldispatch(HookEvent) concurrently. The
registration list is a CopyOnWriteArrayList so registration
from a hot path is safe but not particularly fast — configure at
startup, not on every call.- Since:
- 0.5.0 (issue #58)
-
Constructor Summary
ConstructorsConstructorDescriptionDefault: 5s timeout + fail-open (observability-first, not enforcement-first).HookDispatcher(Duration timeoutPerHook, HookFailurePolicy failurePolicy) -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Reset the registration list; primarily for tests.<E extends HookEvent>
HookResult<E> dispatch(E event) Run the hook chain foreventand return the final decision.<E extends HookEvent>
voidRegister a hook.intsize()Current hook count — for tests and monitoring.
-
Constructor Details
-
HookDispatcher
-
HookDispatcher
public HookDispatcher()Default: 5s timeout + fail-open (observability-first, not enforcement-first).
-
-
Method Details
-
register
Register a hook. Hooks take effect on the nextdispatchcall. Safe to call from any thread; not intended for hot-path use (CopyOnWriteArrayList snapshot on each add). -
size
public int size()Current hook count — for tests and monitoring. -
clear
public void clear()Reset the registration list; primarily for tests. -
dispatch
Run the hook chain foreventand return the final decision. Never throws (all hook exceptions reduce toHookResultper failure policy).
-