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

Initialize the default model store in the SpdxModelFactory init method #285

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@
<dependency>
<groupId>org.spdx</groupId>
<artifactId>spdx-java-core</artifactId>
<version>1.0.0-RC1</version>
<version>1.0.0-RC2</version>
</dependency>
<dependency>
<groupId>org.spdx</groupId>
<artifactId>spdx-java-model-3_0</artifactId>
<version>1.0.0-RC1</version>
<version>1.0.0-RC2</version>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/spdx/library/ListedLicenses.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private void initializeLicenseModelStore() {
try {
baseModelStore = new SpdxListedLicenseLocalStore();
} catch(InvalidSPDXAnalysisException ex) {
logger.error("Error loading cached SPDX licenses");
logger.error("Error loading cached SPDX licenses", ex);
throw new RuntimeException("Unexpected error loading SPDX Listed Licenses");
}
}
Expand Down
58 changes: 50 additions & 8 deletions src/main/java/org/spdx/library/SpdxModelFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@

import javax.annotation.Nullable;

import org.spdx.core.CoreModelObject;
import org.spdx.core.IModelCopyManager;
import org.spdx.core.InvalidSPDXAnalysisException;
import org.spdx.core.ModelRegistry;
import org.spdx.core.ModelRegistryException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spdx.core.*;
import org.spdx.library.model.v2.SpdxModelInfoV2_X;
import org.spdx.library.model.v3_0_1.SpdxModelInfoV3_0;
import org.spdx.storage.IModelStore;
import org.spdx.storage.listedlicense.SpdxListedLicenseModelStore;
import org.spdx.storage.simple.InMemSpdxStore;

/**
* Main entrypoint for the SPDX Java Library
Expand All @@ -52,7 +52,9 @@
*/
@SuppressWarnings("unused")
public class SpdxModelFactory {


static final Logger logger = LoggerFactory.getLogger(SpdxModelFactory.class.getName());

static {
// register the supported spec version models
ModelRegistry.getModelRegistry().registerModel(new SpdxModelInfoV2_X());
Expand All @@ -61,6 +63,10 @@ public class SpdxModelFactory {

public static final String IMPLEMENTATION_VERSION = "2.0.0";

static final String DEFAULT_DOCUMENT_URI = "https://default/spdx/document";

private static final Object INIT_LOCK = new Object();

/**
* Static class private constructor
*/
Expand Down Expand Up @@ -107,12 +113,48 @@ public static String getLatestSpecVersion() {
}

/**
* This static method is a convenience to load this class and initialize the supported model versions.
* This static method is a convenience to load this class and initialize the supported model versions and
* initialize the DefaultModelStore with default values
* <p>
* It should be called before using any other functionality from the library
*/
public static void init() {
// doesn't do anything
synchronized (INIT_LOCK) {
if (!DefaultModelStore.isInitialized()) {
DefaultModelStore.initialize(new InMemSpdxStore(), DEFAULT_DOCUMENT_URI, new ModelCopyManager());
}
}
}

/**
* This static method is a convenience to load this class and initialize the supported model versions and
* initialize the DefaultModelStore with the parameter values
* <p>
* It should be called before using any other functionality from the library
* @param modelStore Model store to use as a default
* @param defaultDocumentUri Document URI to use as a default
* @param defaultCopyManager Copy manager to use as a default
*/
public static void init(IModelStore modelStore, String defaultDocumentUri,
IModelCopyManager defaultCopyManager) {
Objects.requireNonNull(modelStore, "Model store can not be null");
Objects.requireNonNull(defaultDocumentUri, "Document URI can not be null");
Objects.requireNonNull(defaultCopyManager, "Copy manager can not be null");
synchronized (INIT_LOCK) {
if (!DefaultModelStore.isInitialized()) {
DefaultModelStore.initialize(modelStore, defaultDocumentUri, defaultCopyManager);
} else {
try {
if (!(Objects.equals(modelStore, DefaultModelStore.getDefaultModelStore()) &&
Objects.equals(defaultDocumentUri, DefaultModelStore.getDefaultDocumentUri()) &&
Objects.equals(defaultCopyManager, DefaultModelStore.getDefaultCopyManager()))) {
logger.warn("Ignoring second call to initialize the model store");
}
} catch (DefaultStoreNotInitialized e) {
logger.error("Unexpected store not initialized during init", e);
}
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ public NamespaceMap convertAndStore(org.spdx.library.model.v2.ExternalDocumentRe
NamespaceMap toNamespaceMap = (NamespaceMap)SpdxModelClassFactoryV3.getModelObject(toModelStore,
toObjectUri, SpdxConstantsV3.CORE_NAMESPACE_MAP, copyManager, true, defaultUriPrefix);
toNamespaceMap.setPrefix(externalDocRef.getId());
toNamespaceMap.setNamespace(externalDocRef.getSpdxDocumentNamespace());
toNamespaceMap.setNamespace(externalDocRef.getSpdxDocumentNamespace() + "#");
return toNamespaceMap;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ private void loadIds() throws InvalidSPDXAnalysisException {
ExceptionJsonTOC exceptionToc = gson.fromJson(tocJsonStr.toString(), ExceptionJsonTOC.class);
exceptionIds = exceptionToc.getExceptionIds();
} catch (MalformedURLException e) {
logger.error("License TOC URL invalid", e);
throw new SpdxListedLicenseException("License TOC URL invalid", e) ;
} catch (IOException e) {
logger.error("I/O error reading license TOC", e);
throw new SpdxListedLicenseException("I/O error reading license TOC", e);
} finally {
if (reader != null) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/spdx/storage/simple/InMemSpdxStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,10 @@ public void leaveCriticalSection(IModelStoreLock lock) {
}

@Override
public Optional<String> getCaseSensisitiveId(String nameSpace, String caseInsensisitiveId) {
public Optional<String> getCaseSensitiveId(String nameSpace, String caseInsensitiveId) {
Objects.requireNonNull(nameSpace, "Namespace can not be null");
Objects.requireNonNull(caseInsensisitiveId, "CaseInsensisitiveId can not be null");
String objectUri = nameSpace + "#" + caseInsensisitiveId;
Objects.requireNonNull(caseInsensitiveId, "CaseInsensitiveId can not be null");
String objectUri = nameSpace + "#" + caseInsensitiveId;
StoredTypedItem item = typedValueMap.get(objectUri.toLowerCase());
if (Objects.isNull(item)) {
return Optional.empty();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/spdx/storage/simple/StoredTypedItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public void setValue(PropertyDescriptor propertyDescriptor, Object value) throws
!String.class.isAssignableFrom(value.getClass()) &&
!Boolean.class.isAssignableFrom(value.getClass()) &&
!Integer.class.isAssignableFrom(value.getClass()) &&
!Double.class.isAssignableFrom(value.getClass()) &&
!Float.class.isAssignableFrom((value.getClass())) &&
!TypedValue.class.isAssignableFrom(value.getClass()) &&
!(value instanceof IndividualUriValue)) {
throw new SpdxInvalidTypeException(value.getClass() +" is not a supported class to be stored.");
Expand Down