Skip to content

Commit

Permalink
apacheGH-2795: Fix race condition in datatype registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Ostrzyciel authored and arne-bdt committed Oct 27, 2024
1 parent 0d8060c commit 08209ae
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions jena-core/src/main/java/org/apache/jena/datatypes/TypeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,16 @@ public RDFDatatype getSafeTypeByName(final String uri) {
// Plain literal
return null;
}
RDFDatatype dtype = uriToDT.get(uri);
if (dtype == null) {
return uriToDT.computeIfAbsent(uri, u -> {
// Unknown datatype
if (JenaParameters.enableSilentAcceptanceOfUnknownDatatypes) {
dtype = new BaseDatatype(uri);
registerDatatype(dtype);
// No need to update classToDT because BaseDatatype.getJavaClass is always null
return new BaseDatatype(u);
} else {
throw new DatatypeFormatException(
"Attempted to created typed literal using an unknown datatype - " + uri);
}
}
return dtype;
});
}

/**
Expand Down Expand Up @@ -183,6 +181,9 @@ public RDFDatatype getTypeByClass(final Class<?> clazz) {

/**
* Register a new datatype
* This will overwrite any existing registration for this datatype IRI.
* Comparisons of literals with different datatype instances will fail, so be careful
* if you are using this outside of initialization code.
*/
public void registerDatatype(final RDFDatatype type) {
uriToDT.put(type.getURI(), type);
Expand All @@ -194,6 +195,10 @@ public void registerDatatype(final RDFDatatype type) {

/**
* Remove a datatype registration.
* <p>
* WARNING: This method may cause unexpected behavior if the datatype is still in use.
* If you unregister a datatype that is still used somewhere on the heap, literal comparisons with
* that datatype will fail.
*/
public void unregisterDatatype(final RDFDatatype type) {
uriToDT.remove(type.getURI());
Expand Down

0 comments on commit 08209ae

Please sign in to comment.