11package io .openaev .database .audit ;
22
33import com .fasterxml .jackson .annotation .JsonProperty ;
4+ import java .util .Collections ;
45import java .util .LinkedHashMap ;
56import java .util .Map ;
67import org .springframework .web .context .request .RequestAttributes ;
@@ -24,17 +25,6 @@ public final class AuditLogContext {
2425 private static final String REQUEST_ATTR_CLEANUP_REGISTERED = "openaev.audit.cleanupRegistered" ;
2526 private static final String REQUEST_ATTR_ENABLED = "openaev.audit.enabled" ;
2627
27- private static final ThreadLocal <Map <String , Map <String , Object >>> BEFORE_SNAPSHOTS_TL =
28- ThreadLocal .withInitial (LinkedHashMap ::new );
29-
30- private static final ThreadLocal <Map <String , EntitySnapshot >> SNAPSHOTS_TL =
31- ThreadLocal .withInitial (LinkedHashMap ::new );
32-
33- private static final ThreadLocal <Boolean > CLEANUP_REGISTERED_TL =
34- ThreadLocal .withInitial (() -> false );
35-
36- private static final ThreadLocal <Boolean > ENABLED_TL = ThreadLocal .withInitial (() -> true );
37-
3828 private AuditLogContext () {}
3929
4030 // -- Before snapshot --
@@ -48,9 +38,7 @@ public static void setEnabled(boolean enabled) {
4838 if (hasRequestContext ()) {
4939 requestAttributes ()
5040 .setAttribute (REQUEST_ATTR_ENABLED , enabled , RequestAttributes .SCOPE_REQUEST );
51- return ;
5241 }
53- ENABLED_TL .set (enabled );
5442 }
5543
5644 /** Returns {@code true} if audit logging is enabled for the current request/thread. */
@@ -60,7 +48,7 @@ public static boolean isEnabled() {
6048 requestAttributes ().getAttribute (REQUEST_ATTR_ENABLED , RequestAttributes .SCOPE_REQUEST );
6149 return val == null || Boolean .TRUE .equals (val );
6250 }
63- return Boolean . TRUE . equals ( ENABLED_TL . get ()) ;
51+ return true ;
6452 }
6553
6654 public static void storeBefore (String entityId , Map <String , Object > snapshot ) {
@@ -92,17 +80,15 @@ public static boolean isCleanupRegistered() {
9280 .getAttribute (REQUEST_ATTR_CLEANUP_REGISTERED , RequestAttributes .SCOPE_REQUEST );
9381 return Boolean .TRUE .equals (val );
9482 }
95- return Boolean . TRUE . equals ( CLEANUP_REGISTERED_TL . get ()) ;
83+ return true ;
9684 }
9785
9886 public static void markCleanupRegistered () {
9987 if (hasRequestContext ()) {
10088 requestAttributes ()
10189 .setAttribute (
10290 REQUEST_ATTR_CLEANUP_REGISTERED , Boolean .TRUE , RequestAttributes .SCOPE_REQUEST );
103- return ;
10491 }
105- CLEANUP_REGISTERED_TL .set (true );
10692 }
10793
10894 public static void clear () {
@@ -112,11 +98,6 @@ public static void clear() {
11298 attrs .removeAttribute (REQUEST_ATTR_ENTITY_SNAPSHOTS , RequestAttributes .SCOPE_REQUEST );
11399 attrs .removeAttribute (REQUEST_ATTR_CLEANUP_REGISTERED , RequestAttributes .SCOPE_REQUEST );
114100 attrs .removeAttribute (REQUEST_ATTR_ENABLED , RequestAttributes .SCOPE_REQUEST );
115- } else {
116- BEFORE_SNAPSHOTS_TL .remove ();
117- SNAPSHOTS_TL .remove ();
118- CLEANUP_REGISTERED_TL .remove ();
119- ENABLED_TL .remove ();
120101 }
121102 }
122103
@@ -132,10 +113,6 @@ public static void clearAfterTransactionCompletion() {
132113 attrs .removeAttribute (REQUEST_ATTR_BEFORE_SNAPSHOTS , RequestAttributes .SCOPE_REQUEST );
133114 attrs .removeAttribute (REQUEST_ATTR_CLEANUP_REGISTERED , RequestAttributes .SCOPE_REQUEST );
134115 }
135-
136- BEFORE_SNAPSHOTS_TL .remove ();
137- SNAPSHOTS_TL .remove ();
138- CLEANUP_REGISTERED_TL .remove ();
139116 }
140117
141118 // -- Request/thread storage helpers --
@@ -153,7 +130,7 @@ private static Map<String, Map<String, Object>> beforeSnapshots() {
153130 attrs .setAttribute (REQUEST_ATTR_BEFORE_SNAPSHOTS , created , RequestAttributes .SCOPE_REQUEST );
154131 return created ;
155132 }
156- return BEFORE_SNAPSHOTS_TL . get ();
133+ return Collections . emptyMap ();
157134 }
158135
159136 @ SuppressWarnings ("unchecked" )
@@ -169,10 +146,10 @@ private static Map<String, EntitySnapshot> snapshots() {
169146 attrs .setAttribute (REQUEST_ATTR_ENTITY_SNAPSHOTS , created , RequestAttributes .SCOPE_REQUEST );
170147 return created ;
171148 }
172- return SNAPSHOTS_TL . get ();
149+ return Collections . emptyMap ();
173150 }
174151
175- private static boolean hasRequestContext () {
152+ public static boolean hasRequestContext () {
176153 return RequestContextHolder .getRequestAttributes () != null ;
177154 }
178155
0 commit comments