Skip to content

Commit 8af5e69

Browse files
jansupolpa314159
authored andcommitted
Prevent race condition in entity filtering (eclipse-ee4j#4204)
Signed-off-by: Jan Supol <jan.supol@oracle.com>
1 parent 72a300e commit 8af5e69

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

ext/entity-filtering/src/main/java/org/glassfish/jersey/message/filtering/EntityGraphProviderImpl.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -82,4 +82,10 @@ public ObjectGraph createObjectGraph(final Class<?> entityClass, final Set<Strin
8282
? new EmptyObjectGraph(entityClass)
8383
: new ObjectGraphImpl(classToGraph, entityGraph, filteringScopes);
8484
}
85+
86+
@Override
87+
public EntityGraph putIfAbsent(Class<?> entityClass, EntityGraph entityGraph, boolean forWriter) {
88+
final ConcurrentMap<Class<?>, EntityGraph> classToGraph = forWriter ? writerClassToGraph : readerClassToGraph;
89+
return classToGraph.putIfAbsent(entityClass, entityGraph);
90+
}
8591
}

ext/entity-filtering/src/main/java/org/glassfish/jersey/message/filtering/EntityInspectorImpl.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -50,7 +50,6 @@
5050
*/
5151
@Singleton
5252
final class EntityInspectorImpl implements EntityInspector {
53-
5453
private final List<EntityProcessor> entityProcessors;
5554

5655
@Inject
@@ -71,21 +70,26 @@ public EntityInspectorImpl(final InjectionManager injectionManager) {
7170
@Override
7271
public void inspect(final Class<?> entityClass, final boolean forWriter) {
7372
if (!graphProvider.containsEntityGraph(entityClass, forWriter)) {
74-
final EntityGraph graph = graphProvider.getOrCreateEntityGraph(entityClass, forWriter);
73+
final EntityGraph graph = new EntityGraphImpl(entityClass);
7574
final Set<Class<?>> inspect = new HashSet<>();
7675

7776
// Class.
7877
if (!inspectEntityClass(entityClass, graph, forWriter)) {
7978
// Properties.
80-
final Map<String, Method> unmatchedAccessors = inspectEntityProperties(entityClass, graph, inspect, forWriter);
79+
final Map<String, Method> unmatchedAccessors =
80+
inspectEntityProperties(entityClass, graph, inspect, forWriter);
8181

8282
// Setters/Getters without fields.
8383
inspectStandaloneAccessors(unmatchedAccessors, graph, forWriter);
8484

85+
graphProvider.putIfAbsent(entityClass, graph, forWriter);
86+
8587
// Inspect new classes.
8688
for (final Class<?> clazz : inspect) {
8789
inspect(clazz, forWriter);
8890
}
91+
} else {
92+
graphProvider.putIfAbsent(entityClass, graph, forWriter);
8993
}
9094
}
9195
}

ext/entity-filtering/src/main/java/org/glassfish/jersey/message/filtering/spi/EntityGraphProvider.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -66,4 +66,15 @@ public interface EntityGraphProvider {
6666
* @return an entity-filtering object graph instance.
6767
*/
6868
public ObjectGraph createObjectGraph(final Class<?> entityClass, final Set<String> filteringScopes, final boolean forWriter);
69+
70+
/**
71+
* Store an {@code EntityGraph} for a given entity if not already stored
72+
* @param entityClass entity class which the object graph should be created for.
73+
* @param entityGraph an entity graph to be stored
74+
* @param forWriter flag determining whether the graph should be created for writer/reader.
75+
* @return the previous value associated with the {@code entityClass, forWriter} key,
76+
* or {@code null} if there was no previous mapping for the key. This behavior is inherited from
77+
* {@link java.util.Map#putIfAbsent(Object, Object)}.
78+
*/
79+
public EntityGraph putIfAbsent(final Class<?> entityClass, EntityGraph entityGraph, final boolean forWriter);
6980
}

0 commit comments

Comments
 (0)