Skip to content

Commit 5b5803b

Browse files
committed
Fix ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD in MongoPersistenceProvider
Replace static field write from instance method with proper synchronization. Changed from writing to static boolean field from constructor to using synchronized block with volatile flag for thread-safe registration control.
1 parent 62d1f04 commit 5b5803b

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/main/java/de/rub/nds/crawler/persistence/MongoPersistenceProvider.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,18 @@ public class MongoPersistenceProvider implements IPersistenceProvider {
5050

5151
private static final String BULK_SCAN_COLLECTION_NAME = "bulkScans";
5252

53-
private static boolean isInitialized = false;
5453
private static final Set<JsonSerializer<?>> serializers = new HashSet<>();
5554
private static final Set<Module> modules = new HashSet<>();
55+
private static final Object registrationLock = new Object();
56+
private static volatile boolean registrationClosed = false;
5657

5758
public static void registerSerializer(JsonSerializer<?> serializer) {
58-
if (isInitialized) {
59-
throw new RuntimeException("Cannot register serializer after initialization");
59+
synchronized (registrationLock) {
60+
if (registrationClosed) {
61+
throw new RuntimeException("Cannot register serializer after initialization");
62+
}
63+
serializers.add(serializer);
6064
}
61-
serializers.add(serializer);
6265
}
6366

6467
public static void registerSerializer(JsonSerializer<?>... serializers) {
@@ -68,10 +71,12 @@ public static void registerSerializer(JsonSerializer<?>... serializers) {
6871
}
6972

7073
public static void registerModule(Module module) {
71-
if (isInitialized) {
72-
throw new RuntimeException("Cannot register module after initialization");
74+
synchronized (registrationLock) {
75+
if (registrationClosed) {
76+
throw new RuntimeException("Cannot register module after initialization");
77+
}
78+
modules.add(module);
7379
}
74-
modules.add(module);
7580
}
7681

7782
public static void registerModule(Module... modules) {
@@ -148,7 +153,9 @@ private static ObjectMapper createMapper() {
148153
* @param mongoDbDelegate Mongodb command line configuration parameters
149154
*/
150155
public MongoPersistenceProvider(MongoDbDelegate mongoDbDelegate) {
151-
isInitialized = true;
156+
synchronized (registrationLock) {
157+
registrationClosed = true;
158+
}
152159

153160
mapper = createMapper();
154161
mongoClient = createMongoClient(mongoDbDelegate);

0 commit comments

Comments
 (0)