Skip to content

Commit b33561f

Browse files
authored
Refactor to remove ThreadLocal usage (#896)
* Refactor to remove threadlocal usage * Remove option for resetting the context as context reuse is explicit
1 parent 2971b79 commit b33561f

File tree

73 files changed

+534
-552
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+534
-552
lines changed

src/main/java/com/networknt/schema/AbstractJsonValidator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323

2424
public abstract class AbstractJsonValidator implements JsonValidator {
2525

26-
public Set<ValidationMessage> validate(JsonNode node) {
27-
return validate(node, node, PathType.LEGACY.getRoot());
26+
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node) {
27+
return validate(executionContext, node, node, PathType.LEGACY.getRoot());
2828
}
2929

3030
@Override
31-
public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) {
31+
public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) {
3232
Set<ValidationMessage> validationMessages = Collections.emptySet();
3333
if (shouldValidateSchema) {
34-
validationMessages = validate(node, rootNode, at);
34+
validationMessages = validate(executionContext, node, rootNode, at);
3535
}
3636
return validationMessages;
3737
}

src/main/java/com/networknt/schema/AdditionalPropertiesValidator.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public AdditionalPropertiesValidator(String schemaPath, JsonNode schemaNode, Jso
6464
parseErrorCode(getValidatorType().getErrorCodeKey());
6565
}
6666

67-
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
67+
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, String at) {
6868
debug(logger, node, rootNode, at);
69-
CollectorContext collectorContext = CollectorContext.getInstance();
69+
CollectorContext collectorContext = executionContext.getCollectorContext();
7070

7171
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
7272
if (!node.isObject()) {
@@ -102,9 +102,9 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
102102
if (additionalPropertiesSchema != null) {
103103
ValidatorState state = (ValidatorState) collectorContext.get(ValidatorState.VALIDATOR_STATE_KEY);
104104
if (state != null && state.isWalkEnabled()) {
105-
errors.addAll(additionalPropertiesSchema.walk(node.get(pname), rootNode, atPath(at, pname), state.isValidationEnabled()));
105+
errors.addAll(additionalPropertiesSchema.walk(executionContext, node.get(pname), rootNode, atPath(at, pname), state.isValidationEnabled()));
106106
} else {
107-
errors.addAll(additionalPropertiesSchema.validate(node.get(pname), rootNode, atPath(at, pname)));
107+
errors.addAll(additionalPropertiesSchema.validate(executionContext, node.get(pname), rootNode, atPath(at, pname)));
108108
}
109109
}
110110
}
@@ -114,9 +114,9 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
114114
}
115115

116116
@Override
117-
public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) {
117+
public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) {
118118
if (shouldValidateSchema) {
119-
return validate(node, rootNode, at);
119+
return validate(executionContext, node, rootNode, at);
120120
}
121121

122122
if (node == null || !node.isObject()) {
@@ -142,9 +142,9 @@ public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at,
142142
if (!allowedProperties.contains(pname) && !handledByPatternProperties) {
143143
if (allowAdditionalProperties) {
144144
if (additionalPropertiesSchema != null) {
145-
ValidatorState state = (ValidatorState) CollectorContext.getInstance().get(ValidatorState.VALIDATOR_STATE_KEY);
145+
ValidatorState state = (ValidatorState) executionContext.getCollectorContext().get(ValidatorState.VALIDATOR_STATE_KEY);
146146
if (state != null && state.isWalkEnabled()) {
147-
additionalPropertiesSchema.walk(node.get(pname), rootNode, atPath(at, pname), state.isValidationEnabled());
147+
additionalPropertiesSchema.walk(executionContext, node.get(pname), rootNode, atPath(at, pname), state.isValidationEnabled());
148148
}
149149
}
150150
}

src/main/java/com/networknt/schema/AllOfValidator.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public AllOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
4040
}
4141

4242
@Override
43-
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
43+
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, String at) {
4444
debug(logger, node, rootNode, at);
45-
CollectorContext collectorContext = CollectorContext.getInstance();
45+
CollectorContext collectorContext = executionContext.getCollectorContext();
4646

4747
// get the Validator state object storing validation data
4848
ValidatorState state = (ValidatorState) collectorContext.get(ValidatorState.VALIDATOR_STATE_KEY);
@@ -55,9 +55,9 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
5555
Scope parentScope = collectorContext.enterDynamicScope();
5656
try {
5757
if (!state.isWalkEnabled()) {
58-
localErrors = schema.validate(node, rootNode, at);
58+
localErrors = schema.validate(executionContext, node, rootNode, at);
5959
} else {
60-
localErrors = schema.walk(node, rootNode, at, true);
60+
localErrors = schema.walk(executionContext, node, rootNode, at, true);
6161
}
6262

6363
childSchemaErrors.addAll(localErrors);
@@ -105,13 +105,13 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
105105
}
106106

107107
@Override
108-
public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) {
108+
public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) {
109109
if (shouldValidateSchema) {
110-
return validate(node, rootNode, at);
110+
return validate(executionContext, node, rootNode, at);
111111
}
112112
for (JsonSchema schema : this.schemas) {
113113
// Walk through the schema
114-
schema.walk(node, rootNode, at, false);
114+
schema.walk(executionContext, node, rootNode, at, false);
115115
}
116116
return Collections.emptySet();
117117
}

src/main/java/com/networknt/schema/AnyOfValidator.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public AnyOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
4848
}
4949

5050
@Override
51-
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
51+
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, String at) {
5252
debug(logger, node, rootNode, at);
53-
CollectorContext collectorContext = CollectorContext.getInstance();
53+
CollectorContext collectorContext = executionContext.getCollectorContext();
5454

5555
// get the Validator state object storing validation data
5656
ValidatorState state = (ValidatorState) collectorContext.get(ValidatorState.VALIDATOR_STATE_KEY);
@@ -82,9 +82,9 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
8282
}
8383
}
8484
if (!state.isWalkEnabled()) {
85-
errors = schema.validate(node, rootNode, at);
85+
errors = schema.validate(executionContext, node, rootNode, at);
8686
} else {
87-
errors = schema.walk(node, rootNode, at, true);
87+
errors = schema.walk(executionContext, node, rootNode, at, true);
8888
}
8989

9090
// check if any validation errors have occurred
@@ -151,12 +151,12 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
151151
}
152152

153153
@Override
154-
public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) {
154+
public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) {
155155
if (shouldValidateSchema) {
156-
return validate(node, rootNode, at);
156+
return validate(executionContext, node, rootNode, at);
157157
}
158158
for (JsonSchema schema : this.schemas) {
159-
schema.walk(node, rootNode, at, false);
159+
schema.walk(executionContext, node, rootNode, at, false);
160160
}
161161
return new LinkedHashSet<>();
162162
}

src/main/java/com/networknt/schema/BaseJsonValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ protected JsonSchema fetchSubSchemaNode(ValidationContext validationContext) {
226226
}
227227

228228
@Override
229-
public Set<ValidationMessage> validate(JsonNode node) {
230-
return validate(node, node, atRoot());
229+
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node) {
230+
return validate(executionContext, node, node, atRoot());
231231
}
232232

233233
protected String getNodeFieldType() {

src/main/java/com/networknt/schema/CollectorContext.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@
3232
* implementations.
3333
*/
3434
public class CollectorContext {
35-
36-
// Using a namespace string as key in ThreadLocal so that it is unique in
37-
// ThreadLocal.
38-
static final String COLLECTOR_CONTEXT_THREAD_LOCAL_KEY = "com.networknt.schema.CollectorKey";
39-
40-
// Get an instance from thread info (which uses ThreadLocal).
41-
public static CollectorContext getInstance() {
42-
return (CollectorContext) ThreadInfo.get(COLLECTOR_CONTEXT_THREAD_LOCAL_KEY);
43-
}
44-
4535
/**
4636
* Map for holding the name and {@link Collector} or a simple Object.
4737
*/
@@ -212,16 +202,6 @@ public void combineWithCollector(String name, Object data) {
212202
}
213203
}
214204

215-
/**
216-
* Reset the context
217-
*/
218-
public void reset() {
219-
this.collectorMap = new HashMap<>();
220-
this.collectorLoadMap = new HashMap<>();
221-
this.dynamicScopes.clear();
222-
this.dynamicScopes.push(newTopScope());
223-
}
224-
225205
/**
226206
* Loads data from all collectors.
227207
*/

src/main/java/com/networknt/schema/ConstValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public ConstValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
3232
this.schemaNode = schemaNode;
3333
}
3434

35-
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
35+
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, String at) {
3636
debug(logger, node, rootNode, at);
3737

3838
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();

src/main/java/com/networknt/schema/ContainsValidator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,18 @@ public ContainsValidator(String schemaPath, JsonNode schemaNode, JsonSchema pare
6767
}
6868

6969
@Override
70-
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
70+
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, String at) {
7171
debug(logger, node, rootNode, at);
7272

7373
// ignores non-arrays
7474
if (null != this.schema && node.isArray()) {
75-
Collection<String> evaluatedItems = CollectorContext.getInstance().getEvaluatedItems();
75+
Collection<String> evaluatedItems = executionContext.getCollectorContext().getEvaluatedItems();
7676

7777
int actual = 0, i = 0;
7878
for (JsonNode n : node) {
7979
String path = atPath(at, i);
8080

81-
if (this.schema.validate(n, rootNode, path).isEmpty()) {
81+
if (this.schema.validate(executionContext, n, rootNode, path).isEmpty()) {
8282
++actual;
8383
evaluatedItems.add(path);
8484
}

src/main/java/com/networknt/schema/DependenciesValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public DependenciesValidator(String schemaPath, JsonNode schemaNode, JsonSchema
5151
parseErrorCode(getValidatorType().getErrorCodeKey());
5252
}
5353

54-
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
54+
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, String at) {
5555
debug(logger, node, rootNode, at);
5656

5757
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
@@ -68,7 +68,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
6868
}
6969
JsonSchema schema = schemaDeps.get(pname);
7070
if (schema != null) {
71-
errors.addAll(schema.validate(node, rootNode, at));
71+
errors.addAll(schema.validate(executionContext, node, rootNode, at));
7272
}
7373
}
7474

src/main/java/com/networknt/schema/DependentRequired.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public DependentRequired(String schemaPath, JsonNode schemaNode, JsonSchema pare
4545
parseErrorCode(getValidatorType().getErrorCodeKey());
4646
}
4747

48-
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
48+
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, String at) {
4949
debug(logger, node, rootNode, at);
5050

5151
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();

0 commit comments

Comments
 (0)