Skip to content

Commit bcd0381

Browse files
committed
Add functions to retun maps of licenses
Fixes #237
1 parent fceaa22 commit bcd0381

File tree

5 files changed

+195
-28
lines changed

5 files changed

+195
-28
lines changed

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

Lines changed: 131 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919

2020
import java.io.IOException;
2121
import java.io.InputStream;
22-
import java.util.List;
23-
import java.util.Objects;
24-
import java.util.Optional;
25-
import java.util.Properties;
22+
import java.util.*;
2623
import java.util.concurrent.locks.ReadWriteLock;
2724
import java.util.concurrent.locks.ReentrantReadWriteLock;
2825

@@ -33,6 +30,7 @@
3330
import org.spdx.library.model.v2.SpdxConstantsCompatV2;
3431
import org.spdx.library.model.v2.SpdxModelFactoryCompatV2;
3532
import org.spdx.library.model.v2.license.SpdxListedLicense;
33+
import org.spdx.library.model.v2.license.SpdxListedLicenseException;
3634
import org.spdx.library.model.v3_0_1.core.CreationInfo;
3735
import org.spdx.library.model.v3_0_1.expandedlicensing.ListedLicense;
3836
import org.spdx.library.model.v3_0_1.expandedlicensing.ListedLicenseException;
@@ -62,6 +60,11 @@ public class ListedLicenses {
6260
private SpdxV2ListedLicenseModelStore licenseStoreV2;
6361
private SpdxV3ListedLicenseModelStore licenseStoreV3;
6462
private static ListedLicenses listedLicenses = null;
63+
private Map<String, SpdxListedLicense> spdxListedLicenseMapCompatV2;
64+
private Map<String, ListedLicense> spdxListedLicenseMap;
65+
private Map<String, org.spdx.library.model.v2.license.ListedLicenseException> spdxListedExceptionMapCompatV2;
66+
private Map<String, ListedLicenseException> spdxListedExceptionMap;
67+
6568
/**
6669
* Lock for any modifications to the underlying licenseModelStore
6770
*/
@@ -202,8 +205,8 @@ public boolean isSpdxListedExceptionId(String exceptionId) {
202205
*/
203206
public SpdxListedLicense getListedLicenseByIdCompatV2(String licenseId) throws InvalidSPDXAnalysisException {
204207
try {
205-
return (SpdxListedLicense)SpdxModelFactoryCompatV2.getModelObjectV2(this.licenseStoreV2,
206-
SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX, licenseId,
208+
return (SpdxListedLicense)SpdxModelFactoryCompatV2.getModelObjectV2(this.licenseStoreV2,
209+
SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX, licenseId,
207210
SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE, null, false);
208211
} catch (SpdxIdNotFoundException ex) {
209212
return null;
@@ -261,6 +264,128 @@ public List<String> getSpdxListedLicenseIds() {
261264
listedLicenseModificationLock.readLock().unlock();
262265
}
263266
}
267+
268+
/**
269+
* @return a map of SPDX listed license IDs to the SPDX listed license
270+
* @throws InvalidSPDXAnalysisException on errors fetching the licenses
271+
*/
272+
public Map<String, ListedLicense> getSpdxListedLicenses() throws InvalidSPDXAnalysisException {
273+
listedLicenseModificationLock.readLock().lock();
274+
try {
275+
if (Objects.nonNull(this.spdxListedLicenseMap)) {
276+
return this.spdxListedLicenseMap;
277+
}
278+
} finally {
279+
listedLicenseModificationLock.readLock().unlock();
280+
}
281+
listedLicenseModificationLock.writeLock().lock();
282+
try {
283+
if (Objects.nonNull(this.spdxListedLicenseMap)) {
284+
return this.spdxListedLicenseMap;
285+
}
286+
Map<String, ListedLicense> allListedLicenses = new HashMap<>();
287+
for (String licenseId : this.baseModelStore.getSpdxListedLicenseIds()) {
288+
allListedLicenses.put(licenseId, new ListedLicense(this.licenseStoreV3, SpdxListedLicenseModelStore.licenseOrExceptionIdToObjectUri(licenseId), null,
289+
false, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX));
290+
}
291+
this.spdxListedLicenseMap = Collections.unmodifiableMap(allListedLicenses);
292+
return this.spdxListedLicenseMap;
293+
} finally {
294+
listedLicenseModificationLock.writeLock().unlock();
295+
}
296+
}
297+
298+
/**
299+
* @return a map of SPDX listed license exception IDs to the SPDX listed license exception
300+
* @throws InvalidSPDXAnalysisException on errors fetching the license exceptions
301+
*/
302+
public Map<String, ListedLicenseException> getSpdxListedLicenseExceptions() throws InvalidSPDXAnalysisException {
303+
listedLicenseModificationLock.readLock().lock();
304+
try {
305+
if (Objects.nonNull(this.spdxListedExceptionMap)) {
306+
return this.spdxListedExceptionMap;
307+
}
308+
} finally {
309+
listedLicenseModificationLock.readLock().unlock();
310+
}
311+
listedLicenseModificationLock.writeLock().lock();
312+
try {
313+
if (Objects.nonNull(this.spdxListedExceptionMap)) {
314+
return this.spdxListedExceptionMap;
315+
}
316+
Map<String, ListedLicenseException> allListedExceptions = new HashMap<>();
317+
for (String exceptionId : this.baseModelStore.getSpdxListedExceptionIds()) {
318+
allListedExceptions.put(exceptionId, new ListedLicenseException(this.licenseStoreV3, SpdxListedLicenseModelStore.licenseOrExceptionIdToObjectUri(exceptionId), null,
319+
false, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX));
320+
}
321+
this.spdxListedExceptionMap = Collections.unmodifiableMap(allListedExceptions);
322+
return this.spdxListedExceptionMap;
323+
} finally {
324+
listedLicenseModificationLock.writeLock().unlock();
325+
}
326+
}
327+
328+
/**
329+
* @return a map of SPDX listed license IDs to the SPDX Spec version 2 listed license
330+
* @throws InvalidSPDXAnalysisException on errors fetching the licenses
331+
*/
332+
public Map<String, SpdxListedLicense> getSpdxListedLicensesCompatV2() throws InvalidSPDXAnalysisException {
333+
listedLicenseModificationLock.readLock().lock();
334+
try {
335+
if (Objects.nonNull(this.spdxListedLicenseMapCompatV2)) {
336+
return this.spdxListedLicenseMapCompatV2;
337+
}
338+
} finally {
339+
listedLicenseModificationLock.readLock().unlock();
340+
}
341+
listedLicenseModificationLock.writeLock().lock();
342+
try {
343+
if (Objects.nonNull(this.spdxListedLicenseMapCompatV2)) {
344+
return this.spdxListedLicenseMapCompatV2;
345+
}
346+
Map<String, SpdxListedLicense> allListedLicenses = new HashMap<>();
347+
for (String licenseId : this.baseModelStore.getSpdxListedLicenseIds()) {
348+
allListedLicenses.put(licenseId, (SpdxListedLicense)SpdxModelFactoryCompatV2.getModelObjectV2(this.licenseStoreV2,
349+
SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX, licenseId,
350+
SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE, null, false));
351+
}
352+
this.spdxListedLicenseMapCompatV2 = Collections.unmodifiableMap(allListedLicenses);
353+
return this.spdxListedLicenseMapCompatV2;
354+
} finally {
355+
listedLicenseModificationLock.writeLock().unlock();
356+
}
357+
}
358+
359+
/**
360+
* @return a map of SPDX listed license exception IDs to the SPDX listed license exception
361+
* @throws InvalidSPDXAnalysisException on errors fetching the license exceptions
362+
*/
363+
public Map<String, org.spdx.library.model.v2.license.ListedLicenseException> getSpdxListedLicenseExceptionsCompatV2() throws InvalidSPDXAnalysisException {
364+
listedLicenseModificationLock.readLock().lock();
365+
try {
366+
if (Objects.nonNull(this.spdxListedExceptionMapCompatV2)) {
367+
return this.spdxListedExceptionMapCompatV2;
368+
}
369+
} finally {
370+
listedLicenseModificationLock.readLock().unlock();
371+
}
372+
listedLicenseModificationLock.writeLock().lock();
373+
try {
374+
if (Objects.nonNull(this.spdxListedExceptionMapCompatV2)) {
375+
return this.spdxListedExceptionMapCompatV2;
376+
}
377+
Map<String, org.spdx.library.model.v2.license.ListedLicenseException> allListedExceptions = new HashMap<>();
378+
for (String exceptionId : this.baseModelStore.getSpdxListedExceptionIds()) {
379+
allListedExceptions.put(exceptionId, (org.spdx.library.model.v2.license.ListedLicenseException)SpdxModelFactoryCompatV2.getModelObjectV2(
380+
this.licenseStoreV2, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX,
381+
exceptionId, SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE_EXCEPTION, null, false));
382+
}
383+
this.spdxListedExceptionMapCompatV2 = Collections.unmodifiableMap(allListedExceptions);
384+
return this.spdxListedExceptionMapCompatV2;
385+
} finally {
386+
listedLicenseModificationLock.writeLock().unlock();
387+
}
388+
}
264389

265390
/**
266391
* @return The version of the loaded license list in the form M.N, where M is the major release and N is the minor release.

src/main/java/org/spdx/storage/listedlicense/ExceptionJson.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ public boolean isPropertyValueAssignableTo(PropertyDescriptor propertyDescriptor
457457
* @param clazz class to test assignability
458458
* @return true if the list associated with the propertyDescriptor have a value added of type clazz
459459
*/
460-
public boolean isCollectionMembersAssignableTo(PropertyDescriptor propertyDescriptor, Class<?> clazz) {
460+
public static boolean isCollectionMembersAssignableTo(PropertyDescriptor propertyDescriptor, Class<?> clazz) {
461461
if (SpdxConstantsCompatV2.RDFS_PROP_SEE_ALSO.equals(propertyDescriptor) ||
462462
SpdxConstantsV3.PROP_SEE_ALSO.equals(propertyDescriptor)) {
463463
return String.class.isAssignableFrom(clazz);

src/main/java/org/spdx/storage/listedlicense/LicenseJson.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ public boolean isPropertyValueAssignableTo(PropertyDescriptor propertyDescriptor
510510

511511
}
512512

513-
public boolean isCollectionMembersAssignableTo(PropertyDescriptor propertyDescriptor, Class<?> clazz) throws InvalidSpdxPropertyException {
513+
public static boolean isCollectionMembersAssignableTo(PropertyDescriptor propertyDescriptor, Class<?> clazz) throws InvalidSpdxPropertyException {
514514
String propertyName = PROPERTY_DESCRIPTOR_TO_VALUE_NAME.get(propertyDescriptor);
515515
if (Objects.isNull(propertyName)) {
516516
throw new InvalidSpdxPropertyException("Invalid property for SPDX listed license:"+propertyDescriptor.getName());

src/main/java/org/spdx/storage/listedlicense/SpdxListedLicenseModelStore.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,11 +1098,9 @@ public boolean isCollectionMembersAssignableTo(String objectUri, PropertyDescrip
10981098
listedLicenseModificationLock.writeLock().unlock();
10991099
}
11001100
if (isLicenseId) {
1101-
LicenseJson license = fetchLicenseJson(id);
1102-
return license.isCollectionMembersAssignableTo(propertyDescriptor, clazz);
1101+
return LicenseJson.isCollectionMembersAssignableTo(propertyDescriptor, clazz);
11031102
} else if (isExceptionId) {
1104-
ExceptionJson exc = fetchExceptionJson(id);
1105-
return exc.isCollectionMembersAssignableTo(propertyDescriptor, clazz);
1103+
return ExceptionJson.isCollectionMembersAssignableTo(propertyDescriptor, clazz);
11061104
} else if (Objects.nonNull(crossRef)) {
11071105
return crossRef.isCollectionMembersAssignableTo(propertyDescriptor, clazz);
11081106
} else {

src/test/java/org/spdx/library/ListedLicensesTest.java

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.spdx.library;
22
import java.util.List;
3+
import java.util.Map;
34
import java.util.Optional;
45

56
import org.spdx.core.InvalidSPDXAnalysisException;
@@ -11,6 +12,7 @@
1112
import org.spdx.storage.compatv2.CompatibleModelStoreWrapper;
1213

1314
import junit.framework.TestCase;
15+
import org.spdx.utility.compare.UnitTestHelper;
1416

1517
/*
1618
* Copyright (c) 2019 Source Auditor Inc.
@@ -57,15 +59,15 @@ protected void tearDown() throws Exception {
5759
}
5860

5961
/**
60-
* Test method for {@link org.spdx.library.model.compat.v2.compat.v2.license.ListedLicenses#isSpdxListedLicenseId(java.lang.String)}.
62+
* Test method for {@link org.spdx.library.ListedLicenses#isSpdxListedLicenseId(java.lang.String)}.
6163
*/
6264
public void testIsSpdxListedLicenseID() {
6365
assertTrue(ListedLicenses.getListedLicenses().isSpdxListedLicenseId("Apache-2.0"));
6466
}
6567

6668
/**
67-
* Test method for {@link org.spdx.library.model.compat.v2.compat.v2.license.ListedLicenses#getListedLicenseById(java.lang.String)}.
68-
* @throws InvalidSPDXAnalysisException
69+
* Test method for {@link org.spdx.library.ListedLicenses#getListedLicenseById(java.lang.String)}.
70+
* @throws InvalidSPDXAnalysisException on error getting license IDs
6971
*/
7072
public void testGetListedLicenseById() throws InvalidSPDXAnalysisException {
7173
String id = "Apache-2.0";
@@ -92,7 +94,7 @@ public void testGetLicenseIbyIdLocal() throws InvalidSPDXAnalysisException {
9294
}
9395
}
9496
/**
95-
* Test method for {@link org.spdx.library.model.compat.v2.compat.v2.license.ListedLicenses#getSpdxListedLicenseIds()}.
97+
* Test method for {@link org.spdx.library.ListedLicenses#getSpdxListedLicenseIds()}.
9698
*/
9799
public void testGetSpdxListedLicenseIds() {
98100
List<String> result = ListedLicenses.getListedLicenses().getSpdxListedLicenseIds();
@@ -130,21 +132,21 @@ public void testGetExceptionByIdLocal() throws InvalidSPDXAnalysisException {
130132
}
131133
}
132134

133-
public void testGetExceptionIds() throws InvalidSPDXAnalysisException {
135+
public void testGetExceptionIds() {
134136
assertTrue(ListedLicenses.getListedLicenses().getSpdxListedExceptionIds().size() >= NUM_3_7_EXCEPTION);
135137
}
136138

137139
public void testListedLicenseIdCaseSensitive() {
138140
String expected = "Apache-2.0";
139141
String lower = expected.toLowerCase();
140-
assertEquals(expected, ListedLicenses.getListedLicenses().listedLicenseIdCaseSensitive(lower).get());
142+
assertEquals(expected, ListedLicenses.getListedLicenses().listedLicenseIdCaseSensitive(lower).orElse(null));
141143
assertFalse(ListedLicenses.getListedLicenses().listedLicenseIdCaseSensitive("NotaLicenseId").isPresent());
142144
}
143145

144146
public void testListedExceptionIdCaseSensitive() {
145147
String expected = "Classpath-exception-2.0";
146148
String lower = expected.toLowerCase();
147-
assertEquals(expected, ListedLicenses.getListedLicenses().listedExceptionIdCaseSensitive(lower).get());
149+
assertEquals(expected, ListedLicenses.getListedLicenses().listedExceptionIdCaseSensitive(lower).orElse(null));
148150
assertFalse(ListedLicenses.getListedLicenses().listedExceptionIdCaseSensitive("NotAnExceptionId").isPresent());
149151
}
150152

@@ -158,13 +160,55 @@ public void testGetLicenseIdProperty() throws InvalidSPDXAnalysisException {
158160
assertEquals(id, idProp.get());
159161
}
160162

161-
public void testGetExceptionIdProperty() throws InvalidSPDXAnalysisException {
162-
String id = "Classpath-exception-2.0";
163-
org.spdx.library.model.v2.license.ListedLicenseException ex = ListedLicenses.getListedLicenses().getListedExceptionByIdCompatV2(id);
164-
Optional<Object> idProp = ex.getModelStore().getValue(
165-
CompatibleModelStoreWrapper.documentUriIdToUri(ex.getDocumentUri(), id, false), SpdxConstantsCompatV2.PROP_LICENSE_EXCEPTION_ID);
166-
assertTrue(idProp.isPresent());
167-
assertTrue(idProp.get() instanceof String);
168-
assertEquals(id, idProp.get());
169-
}
163+
public void testGetExceptionIdProperty() throws InvalidSPDXAnalysisException {
164+
String id = "Classpath-exception-2.0";
165+
org.spdx.library.model.v2.license.ListedLicenseException ex = ListedLicenses.getListedLicenses().getListedExceptionByIdCompatV2(id);
166+
Optional<Object> idProp = ex.getModelStore().getValue(
167+
CompatibleModelStoreWrapper.documentUriIdToUri(ex.getDocumentUri(), id, false), SpdxConstantsCompatV2.PROP_LICENSE_EXCEPTION_ID);
168+
assertTrue(idProp.isPresent());
169+
assertTrue(idProp.get() instanceof String);
170+
assertEquals(id, idProp.get());
171+
}
172+
173+
public void testGetListedLicenses() throws InvalidSPDXAnalysisException {
174+
Map<String, ListedLicense> retval =ListedLicenses.getListedLicenses().getSpdxListedLicenses();
175+
List<String> ids = ListedLicenses.getListedLicenses().getSpdxListedLicenseIds();
176+
assertEquals(ids.size(), retval.size());
177+
for (String id:ids) {
178+
assertEquals(id, retval.get(id).getId());
179+
}
180+
}
181+
182+
public void testGetListedLicensesCompatV2() throws InvalidSPDXAnalysisException {
183+
if (UnitTestHelper.runSlowTests()) {
184+
//TODO: Once https://github.com/spdx/spdx-java-model-2_X/issues/13 is fixed, this conditional can be removed
185+
Map<String, SpdxListedLicense> retval =ListedLicenses.getListedLicenses().getSpdxListedLicensesCompatV2();
186+
List<String> ids = ListedLicenses.getListedLicenses().getSpdxListedLicenseIds();
187+
assertEquals(ids.size(), retval.size());
188+
for (String id:ids) {
189+
assertEquals(id, retval.get(id).getId());
190+
}
191+
}
192+
}
193+
194+
public void testGetListedLicenseExceptions() throws InvalidSPDXAnalysisException {
195+
Map<String, ListedLicenseException> retval =ListedLicenses.getListedLicenses().getSpdxListedLicenseExceptions();
196+
List<String> ids = ListedLicenses.getListedLicenses().getSpdxListedExceptionIds();
197+
assertEquals(ids.size(), retval.size());
198+
for (String id:ids) {
199+
assertEquals(id, retval.get(id).getId());
200+
}
201+
}
202+
203+
public void testGetListedLicenseExceptionsCompatV2() throws InvalidSPDXAnalysisException {
204+
if (UnitTestHelper.runSlowTests()) {
205+
Map<String, org.spdx.library.model.v2.license.ListedLicenseException> retval =
206+
ListedLicenses.getListedLicenses().getSpdxListedLicenseExceptionsCompatV2();
207+
List<String> ids = ListedLicenses.getListedLicenses().getSpdxListedExceptionIds();
208+
assertEquals(ids.size(), retval.size());
209+
for (String id:ids) {
210+
assertEquals(id, retval.get(id).getId());
211+
}
212+
}
213+
}
170214
}

0 commit comments

Comments
 (0)