Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-2795: Fix race condition in datatype registration #2796

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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