Skip to content

Commit

Permalink
Fixed race condition issue during TurtleLoader.loadTurtle
Browse files Browse the repository at this point in the history
  • Loading branch information
ysrbo authored and atextor committed Oct 11, 2023
1 parent 2599683 commit 022f844
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
public final class TurtleLoader {
private static final Logger LOG = LoggerFactory.getLogger( TurtleLoader.class );

private static volatile boolean isTurtleRegistered = false;

private TurtleLoader() {

}
Expand All @@ -62,7 +64,7 @@ public static Try<Model> loadTurtle( @Nullable final InputStream inputStream ) {
.collect( Collectors.joining( "\n" ) );

final Model streamModel = ModelFactory.createDefaultModel();
RDFParserRegistry.registerLangTriples( Lang.TURTLE, ReaderRIOTTurtle.factory );
registerTurtle();
try ( final InputStream turtleInputStream = new ByteArrayInputStream( modelContent.getBytes( StandardCharsets.UTF_8 ) ) ) {
streamModel.read( turtleInputStream, "", RDFLanguages.TURTLE.getName() );
return Try.success( streamModel );
Expand All @@ -79,6 +81,17 @@ public static Try<Model> loadTurtle( @Nullable final InputStream inputStream ) {
}
}

private static void registerTurtle() {
if( isTurtleRegistered )
return;
synchronized ( TurtleLoader.class ){
if( !isTurtleRegistered ){
RDFParserRegistry.registerLangTriples( Lang.TURTLE, ReaderRIOTTurtle.factory );
isTurtleRegistered = true;
}
}
}

/**
* Opens an URL and returns its InputStream, but does not throw a checked exception.
*
Expand Down

0 comments on commit 022f844

Please sign in to comment.