Skip to content

Commit 098cdcc

Browse files
committed
Make ModelCopyManager threadsafe
Fixes #169 Signed-off-by: Gary O'Neall <gary@sourceauditor.com>
1 parent fb8fc86 commit 098cdcc

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

src/main/java/org/spdx/library/ModelCopyManager.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.spdx.library.model.SimpleUriValue;
3030
import org.spdx.library.model.TypedValue;
3131
import org.spdx.storage.IModelStore;
32+
import org.spdx.storage.IModelStore.IModelStoreLock;
3233
import org.spdx.storage.IModelStore.IdType;
3334

3435
/**
@@ -196,10 +197,17 @@ public void copy(IModelStore toStore, String toDocumentUri, String toId,
196197
*/
197198
private void copyIndividualProperty(IModelStore toStore, String toDocumentUri, String toId, IModelStore fromStore,
198199
String fromDocumentUri, String fromId, String propName, boolean excludeLicenseDetails) throws InvalidSPDXAnalysisException {
199-
if (fromStore.isCollectionProperty(fromDocumentUri, fromId, propName)) {
200-
throw new InvalidSPDXAnalysisException("Property "+propName+" is a collection type");
201-
}
202-
Optional<Object> result = fromStore.getValue(fromDocumentUri, fromId, propName);
200+
IModelStoreLock fromStoreLock = fromStore.enterCriticalSection(fromDocumentUri, false);
201+
//Note: we use a write lock since the RDF store may end up creating a property to check if it is a collection
202+
Optional<Object> result = Optional.empty();
203+
try {
204+
if (fromStore.isCollectionProperty(fromDocumentUri, fromId, propName)) {
205+
throw new InvalidSPDXAnalysisException("Property "+propName+" is a collection type");
206+
}
207+
result = fromStore.getValue(fromDocumentUri, fromId, propName);
208+
} finally {
209+
fromStoreLock.unlock();
210+
}
203211
if (result.isPresent()) {
204212
if (result.get() instanceof IndividualUriValue) {
205213
toStore.setValue(toDocumentUri, toId, propName, new SimpleUriValue((IndividualUriValue)result.get()));
@@ -232,10 +240,17 @@ private void copyIndividualProperty(IModelStore toStore, String toDocumentUri, S
232240
*/
233241
private void copyCollectionProperty(IModelStore toStore, String toDocumentUri, String toId, IModelStore fromStore,
234242
String fromDocumentUri, String fromId, String propName, boolean excludeLicenseDetails) throws InvalidSPDXAnalysisException {
235-
if (!fromStore.isCollectionProperty(fromDocumentUri, fromId, propName)) {
236-
throw new InvalidSPDXAnalysisException("Property "+propName+" is not a collection type");
237-
}
238-
Iterator<Object> fromListIter = fromStore.listValues(fromDocumentUri, fromId, propName);
243+
IModelStoreLock fromStoreLock = fromStore.enterCriticalSection(fromDocumentUri, false);
244+
//Note: we use a write lock since the RDF store may end up creating a property to check if it is a collection
245+
Iterator<Object> fromListIter = null;
246+
try {
247+
if (!fromStore.isCollectionProperty(fromDocumentUri, fromId, propName)) {
248+
throw new InvalidSPDXAnalysisException("Property "+propName+" is not a collection type");
249+
}
250+
fromListIter = fromStore.listValues(fromDocumentUri, fromId, propName);
251+
} finally {
252+
fromStoreLock.unlock();
253+
}
239254
while (fromListIter.hasNext()) {
240255
Object listItem = fromListIter.next();
241256
Object toStoreItem;

0 commit comments

Comments
 (0)