Skip to content

Commit f01d6a5

Browse files
committed
Initialize default model store in init method
Fixes #284 # Conflicts: # pom.xml
1 parent 6234bda commit f01d6a5

1 file changed

Lines changed: 50 additions & 8 deletions

File tree

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

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@
2525

2626
import javax.annotation.Nullable;
2727

28-
import org.spdx.core.CoreModelObject;
29-
import org.spdx.core.IModelCopyManager;
30-
import org.spdx.core.InvalidSPDXAnalysisException;
31-
import org.spdx.core.ModelRegistry;
32-
import org.spdx.core.ModelRegistryException;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
30+
import org.spdx.core.*;
3331
import org.spdx.library.model.v2.SpdxModelInfoV2_X;
3432
import org.spdx.library.model.v3_0_1.SpdxModelInfoV3_0;
3533
import org.spdx.storage.IModelStore;
34+
import org.spdx.storage.simple.InMemSpdxStore;
3635

3736
/**
3837
* Main entrypoint for the SPDX Java Library
@@ -52,6 +51,8 @@
5251
*/
5352
@SuppressWarnings("unused")
5453
public class SpdxModelFactory {
54+
55+
static final Logger logger = LoggerFactory.getLogger(SpdxModelFactory.class.getName());
5556

5657
static {
5758
// register the supported spec version models
@@ -61,6 +62,10 @@ public class SpdxModelFactory {
6162

6263
public static final String IMPLEMENTATION_VERSION = "2.0.0";
6364

65+
static final String DEFAULT_DOCUMENT_URI = "https://default/spdx/document";
66+
67+
private static final Object INIT_LOCK = new Object();
68+
6469
/**
6570
* Static class private constructor
6671
*/
@@ -105,15 +110,52 @@ public static String getLatestSpecVersion() {
105110
return "";
106111
}
107112
}
108-
113+
109114
/**
110-
* This static method is a convenience to load this class and initialize the supported model versions.
115+
* This static method is a convenience to load this class and initialize the supported model versions and
116+
* initialize the DefaultModelStore with default values
111117
* <p>
112118
* It should be called before using any other functionality from the library
113119
*/
114120
public static void init() {
115-
// doesn't do anything
121+
synchronized (INIT_LOCK) {
122+
if (!DefaultModelStore.isInitialized()) {
123+
DefaultModelStore.initialize(new InMemSpdxStore(), DEFAULT_DOCUMENT_URI, new ModelCopyManager());
124+
}
125+
}
116126
}
127+
128+
/**
129+
* This static method is a convenience to load this class and initialize the supported model versions and
130+
* initialize the DefaultModelStore with the parameter values
131+
* <p>
132+
* It should be called before using any other functionality from the library
133+
* @param modelStore Model store to use as a default
134+
* @param defaultDocumentUri Document URI to use as a default
135+
* @param defaultCopyManager Copy manager to use as a default
136+
*/
137+
public static void init(IModelStore modelStore, String defaultDocumentUri,
138+
IModelCopyManager defaultCopyManager) {
139+
Objects.requireNonNull(modelStore, "Model store can not be null");
140+
Objects.requireNonNull(defaultDocumentUri, "Document URI can not be null");
141+
Objects.requireNonNull(defaultCopyManager, "Copy manager can not be null");
142+
synchronized (INIT_LOCK) {
143+
if (!DefaultModelStore.isInitialized()) {
144+
DefaultModelStore.initialize(modelStore, defaultDocumentUri, defaultCopyManager);
145+
} else {
146+
try {
147+
if (!(Objects.equals(modelStore, DefaultModelStore.getDefaultModelStore()) &&
148+
Objects.equals(defaultDocumentUri, DefaultModelStore.getDefaultDocumentUri()) &&
149+
Objects.equals(defaultCopyManager, DefaultModelStore.getDefaultCopyManager()))) {
150+
logger.warn("Ignoring second call to initialize the model store");
151+
}
152+
} catch (DefaultStoreNotInitialized e) {
153+
logger.error("Unexpected store not initialized during init", e);
154+
}
155+
}
156+
}
157+
}
158+
117159

118160
/**
119161
* If the object exists in the model store, it will be "inflated" back to the Java object.

0 commit comments

Comments
 (0)