Skip to content

Commit 77f3f78

Browse files
authored
Add functions to return maps of licenses (#290)
* Add functions to retun maps of licenses Fixes #237 * Use map for fetching listed licenses * Make SPDX spec 2 license map getters protected --------- Signed-off-by: Gary O'Neall <[email protected]>
1 parent 9d224cd commit 77f3f78

File tree

5 files changed

+192
-53
lines changed

5 files changed

+192
-53
lines changed

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

Lines changed: 133 additions & 31 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
*/
@@ -201,13 +204,7 @@ public boolean isSpdxListedExceptionId(String exceptionId) {
201204
* @throws InvalidSPDXAnalysisException on SPDX parsing error
202205
*/
203206
public SpdxListedLicense getListedLicenseByIdCompatV2(String licenseId) throws InvalidSPDXAnalysisException {
204-
try {
205-
return (SpdxListedLicense)SpdxModelFactoryCompatV2.getModelObjectV2(this.licenseStoreV2,
206-
SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX, licenseId,
207-
SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE, null, false);
208-
} catch (SpdxIdNotFoundException ex) {
209-
return null;
210-
}
207+
return getSpdxListedLicensesCompatV2().get(licenseId);
211208
}
212209

213210
/**
@@ -216,13 +213,7 @@ public SpdxListedLicense getListedLicenseByIdCompatV2(String licenseId) throws I
216213
* @throws InvalidSPDXAnalysisException on SPDX parsing error
217214
*/
218215
public org.spdx.library.model.v2.license.ListedLicenseException getListedExceptionByIdCompatV2(String exceptionId) throws InvalidSPDXAnalysisException {
219-
try {
220-
return (org.spdx.library.model.v2.license.ListedLicenseException)SpdxModelFactoryCompatV2.getModelObjectV2(
221-
this.licenseStoreV2, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX,
222-
exceptionId, SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE_EXCEPTION, null, false);
223-
} catch (SpdxIdNotFoundException ex) {
224-
return null;
225-
}
216+
return getSpdxListedLicenseExceptionsCompatV2().get(exceptionId);
226217
}
227218

228219
/**
@@ -231,22 +222,11 @@ public org.spdx.library.model.v2.license.ListedLicenseException getListedExcepti
231222
* @throws InvalidSPDXAnalysisException on SPDX parsing error
232223
*/
233224
public ListedLicense getListedLicenseById(String licenseId) throws InvalidSPDXAnalysisException {
234-
try {
235-
return new ListedLicense(this.licenseStoreV3, SpdxListedLicenseModelStore.licenseOrExceptionIdToObjectUri(licenseId), null,
236-
false, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX);
237-
} catch (SpdxIdNotFoundException ex) {
238-
return null;
239-
}
240-
225+
return getSpdxListedLicenses().get(licenseId);
241226
}
242227

243228
public ListedLicenseException getListedExceptionById(String exceptionId) throws InvalidSPDXAnalysisException {
244-
try {
245-
return new ListedLicenseException(this.licenseStoreV3, SpdxListedLicenseModelStore.licenseOrExceptionIdToObjectUri(exceptionId), null,
246-
false, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX);
247-
} catch (SpdxIdNotFoundException ex) {
248-
return null;
249-
}
229+
return getSpdxListedLicenseExceptions().get(exceptionId);
250230

251231
}
252232

@@ -261,6 +241,128 @@ public List<String> getSpdxListedLicenseIds() {
261241
listedLicenseModificationLock.readLock().unlock();
262242
}
263243
}
244+
245+
/**
246+
* @return a map of SPDX listed license IDs to the SPDX listed license
247+
* @throws InvalidSPDXAnalysisException on errors fetching the licenses
248+
*/
249+
public Map<String, ListedLicense> getSpdxListedLicenses() throws InvalidSPDXAnalysisException {
250+
listedLicenseModificationLock.readLock().lock();
251+
try {
252+
if (Objects.nonNull(this.spdxListedLicenseMap)) {
253+
return this.spdxListedLicenseMap;
254+
}
255+
} finally {
256+
listedLicenseModificationLock.readLock().unlock();
257+
}
258+
listedLicenseModificationLock.writeLock().lock();
259+
try {
260+
if (Objects.nonNull(this.spdxListedLicenseMap)) {
261+
return this.spdxListedLicenseMap;
262+
}
263+
Map<String, ListedLicense> allListedLicenses = new HashMap<>();
264+
for (String licenseId : this.baseModelStore.getSpdxListedLicenseIds()) {
265+
allListedLicenses.put(licenseId, new ListedLicense(this.licenseStoreV3, SpdxListedLicenseModelStore.licenseOrExceptionIdToObjectUri(licenseId), null,
266+
false, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX));
267+
}
268+
this.spdxListedLicenseMap = Collections.unmodifiableMap(allListedLicenses);
269+
return this.spdxListedLicenseMap;
270+
} finally {
271+
listedLicenseModificationLock.writeLock().unlock();
272+
}
273+
}
274+
275+
/**
276+
* @return a map of SPDX listed license exception IDs to the SPDX listed license exception
277+
* @throws InvalidSPDXAnalysisException on errors fetching the license exceptions
278+
*/
279+
public Map<String, ListedLicenseException> getSpdxListedLicenseExceptions() throws InvalidSPDXAnalysisException {
280+
listedLicenseModificationLock.readLock().lock();
281+
try {
282+
if (Objects.nonNull(this.spdxListedExceptionMap)) {
283+
return this.spdxListedExceptionMap;
284+
}
285+
} finally {
286+
listedLicenseModificationLock.readLock().unlock();
287+
}
288+
listedLicenseModificationLock.writeLock().lock();
289+
try {
290+
if (Objects.nonNull(this.spdxListedExceptionMap)) {
291+
return this.spdxListedExceptionMap;
292+
}
293+
Map<String, ListedLicenseException> allListedExceptions = new HashMap<>();
294+
for (String exceptionId : this.baseModelStore.getSpdxListedExceptionIds()) {
295+
allListedExceptions.put(exceptionId, new ListedLicenseException(this.licenseStoreV3, SpdxListedLicenseModelStore.licenseOrExceptionIdToObjectUri(exceptionId), null,
296+
false, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX));
297+
}
298+
this.spdxListedExceptionMap = Collections.unmodifiableMap(allListedExceptions);
299+
return this.spdxListedExceptionMap;
300+
} finally {
301+
listedLicenseModificationLock.writeLock().unlock();
302+
}
303+
}
304+
305+
/**
306+
* @return a map of SPDX listed license IDs to the SPDX Spec version 2 listed license
307+
* @throws InvalidSPDXAnalysisException on errors fetching the licenses
308+
*/
309+
protected Map<String, SpdxListedLicense> getSpdxListedLicensesCompatV2() throws InvalidSPDXAnalysisException {
310+
listedLicenseModificationLock.readLock().lock();
311+
try {
312+
if (Objects.nonNull(this.spdxListedLicenseMapCompatV2)) {
313+
return this.spdxListedLicenseMapCompatV2;
314+
}
315+
} finally {
316+
listedLicenseModificationLock.readLock().unlock();
317+
}
318+
listedLicenseModificationLock.writeLock().lock();
319+
try {
320+
if (Objects.nonNull(this.spdxListedLicenseMapCompatV2)) {
321+
return this.spdxListedLicenseMapCompatV2;
322+
}
323+
Map<String, SpdxListedLicense> allListedLicenses = new HashMap<>();
324+
for (String licenseId : this.baseModelStore.getSpdxListedLicenseIds()) {
325+
allListedLicenses.put(licenseId, (SpdxListedLicense)SpdxModelFactoryCompatV2.getModelObjectV2(this.licenseStoreV2,
326+
SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX, licenseId,
327+
SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE, null, false));
328+
}
329+
this.spdxListedLicenseMapCompatV2 = Collections.unmodifiableMap(allListedLicenses);
330+
return this.spdxListedLicenseMapCompatV2;
331+
} finally {
332+
listedLicenseModificationLock.writeLock().unlock();
333+
}
334+
}
335+
336+
/**
337+
* @return a map of SPDX listed license exception IDs to the SPDX listed license exception
338+
* @throws InvalidSPDXAnalysisException on errors fetching the license exceptions
339+
*/
340+
protected Map<String, org.spdx.library.model.v2.license.ListedLicenseException> getSpdxListedLicenseExceptionsCompatV2() throws InvalidSPDXAnalysisException {
341+
listedLicenseModificationLock.readLock().lock();
342+
try {
343+
if (Objects.nonNull(this.spdxListedExceptionMapCompatV2)) {
344+
return this.spdxListedExceptionMapCompatV2;
345+
}
346+
} finally {
347+
listedLicenseModificationLock.readLock().unlock();
348+
}
349+
listedLicenseModificationLock.writeLock().lock();
350+
try {
351+
if (Objects.nonNull(this.spdxListedExceptionMapCompatV2)) {
352+
return this.spdxListedExceptionMapCompatV2;
353+
}
354+
Map<String, org.spdx.library.model.v2.license.ListedLicenseException> allListedExceptions = new HashMap<>();
355+
for (String exceptionId : this.baseModelStore.getSpdxListedExceptionIds()) {
356+
allListedExceptions.put(exceptionId, (org.spdx.library.model.v2.license.ListedLicenseException)SpdxModelFactoryCompatV2.getModelObjectV2(
357+
this.licenseStoreV2, SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX,
358+
exceptionId, SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE_EXCEPTION, null, false));
359+
}
360+
this.spdxListedExceptionMapCompatV2 = Collections.unmodifiableMap(allListedExceptions);
361+
return this.spdxListedExceptionMapCompatV2;
362+
} finally {
363+
listedLicenseModificationLock.writeLock().unlock();
364+
}
365+
}
264366

265367
/**
266368
* @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: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.spdx.library;
2020

2121
import java.util.List;
22+
import java.util.Map;
2223
import java.util.Optional;
2324

2425
import org.spdx.core.InvalidSPDXAnalysisException;
@@ -30,6 +31,7 @@
3031
import org.spdx.storage.compatv2.CompatibleModelStoreWrapper;
3132

3233
import junit.framework.TestCase;
34+
import org.spdx.utility.compare.UnitTestHelper;
3335

3436

3537
/**
@@ -60,15 +62,15 @@ protected void tearDown() throws Exception {
6062
}
6163

6264
/**
63-
* Test method for {@link org.spdx.library.model.compat.v2.compat.v2.license.ListedLicenses#isSpdxListedLicenseId(java.lang.String)}.
65+
* Test method for {@link org.spdx.library.ListedLicenses#isSpdxListedLicenseId(java.lang.String)}.
6466
*/
6567
public void testIsSpdxListedLicenseID() {
6668
assertTrue(ListedLicenses.getListedLicenses().isSpdxListedLicenseId("Apache-2.0"));
6769
}
6870

6971
/**
70-
* Test method for {@link org.spdx.library.model.compat.v2.compat.v2.license.ListedLicenses#getListedLicenseById(java.lang.String)}.
71-
* @throws InvalidSPDXAnalysisException
72+
* Test method for {@link org.spdx.library.ListedLicenses#getListedLicenseById(java.lang.String)}.
73+
* @throws InvalidSPDXAnalysisException on error getting license IDs
7274
*/
7375
public void testGetListedLicenseById() throws InvalidSPDXAnalysisException {
7476
String id = "Apache-2.0";
@@ -95,7 +97,7 @@ public void testGetLicenseIbyIdLocal() throws InvalidSPDXAnalysisException {
9597
}
9698
}
9799
/**
98-
* Test method for {@link org.spdx.library.model.compat.v2.compat.v2.license.ListedLicenses#getSpdxListedLicenseIds()}.
100+
* Test method for {@link org.spdx.library.ListedLicenses#getSpdxListedLicenseIds()}.
99101
*/
100102
public void testGetSpdxListedLicenseIds() {
101103
List<String> result = ListedLicenses.getListedLicenses().getSpdxListedLicenseIds();
@@ -133,21 +135,21 @@ public void testGetExceptionByIdLocal() throws InvalidSPDXAnalysisException {
133135
}
134136
}
135137

136-
public void testGetExceptionIds() throws InvalidSPDXAnalysisException {
138+
public void testGetExceptionIds() {
137139
assertTrue(ListedLicenses.getListedLicenses().getSpdxListedExceptionIds().size() >= NUM_3_7_EXCEPTION);
138140
}
139141

140142
public void testListedLicenseIdCaseSensitive() {
141143
String expected = "Apache-2.0";
142144
String lower = expected.toLowerCase();
143-
assertEquals(expected, ListedLicenses.getListedLicenses().listedLicenseIdCaseSensitive(lower).get());
145+
assertEquals(expected, ListedLicenses.getListedLicenses().listedLicenseIdCaseSensitive(lower).orElse(null));
144146
assertFalse(ListedLicenses.getListedLicenses().listedLicenseIdCaseSensitive("NotaLicenseId").isPresent());
145147
}
146148

147149
public void testListedExceptionIdCaseSensitive() {
148150
String expected = "Classpath-exception-2.0";
149151
String lower = expected.toLowerCase();
150-
assertEquals(expected, ListedLicenses.getListedLicenses().listedExceptionIdCaseSensitive(lower).get());
152+
assertEquals(expected, ListedLicenses.getListedLicenses().listedExceptionIdCaseSensitive(lower).orElse(null));
151153
assertFalse(ListedLicenses.getListedLicenses().listedExceptionIdCaseSensitive("NotAnExceptionId").isPresent());
152154
}
153155

@@ -161,13 +163,50 @@ public void testGetLicenseIdProperty() throws InvalidSPDXAnalysisException {
161163
assertEquals(id, idProp.get());
162164
}
163165

164-
public void testGetExceptionIdProperty() throws InvalidSPDXAnalysisException {
165-
String id = "Classpath-exception-2.0";
166-
org.spdx.library.model.v2.license.ListedLicenseException ex = ListedLicenses.getListedLicenses().getListedExceptionByIdCompatV2(id);
167-
Optional<Object> idProp = ex.getModelStore().getValue(
168-
CompatibleModelStoreWrapper.documentUriIdToUri(ex.getDocumentUri(), id, false), SpdxConstantsCompatV2.PROP_LICENSE_EXCEPTION_ID);
169-
assertTrue(idProp.isPresent());
170-
assertTrue(idProp.get() instanceof String);
171-
assertEquals(id, idProp.get());
172-
}
166+
public void testGetExceptionIdProperty() throws InvalidSPDXAnalysisException {
167+
String id = "Classpath-exception-2.0";
168+
org.spdx.library.model.v2.license.ListedLicenseException ex = ListedLicenses.getListedLicenses().getListedExceptionByIdCompatV2(id);
169+
Optional<Object> idProp = ex.getModelStore().getValue(
170+
CompatibleModelStoreWrapper.documentUriIdToUri(ex.getDocumentUri(), id, false), SpdxConstantsCompatV2.PROP_LICENSE_EXCEPTION_ID);
171+
assertTrue(idProp.isPresent());
172+
assertTrue(idProp.get() instanceof String);
173+
assertEquals(id, idProp.get());
174+
}
175+
176+
public void testGetListedLicenses() throws InvalidSPDXAnalysisException {
177+
Map<String, ListedLicense> retval =ListedLicenses.getListedLicenses().getSpdxListedLicenses();
178+
List<String> ids = ListedLicenses.getListedLicenses().getSpdxListedLicenseIds();
179+
assertEquals(ids.size(), retval.size());
180+
for (String id:ids) {
181+
assertTrue(retval.get(id).getObjectUri().endsWith(id));
182+
}
183+
}
184+
185+
public void testGetListedLicensesCompatV2() throws InvalidSPDXAnalysisException {
186+
Map<String, SpdxListedLicense> retval =ListedLicenses.getListedLicenses().getSpdxListedLicensesCompatV2();
187+
List<String> ids = ListedLicenses.getListedLicenses().getSpdxListedLicenseIds();
188+
assertEquals(ids.size(), retval.size());
189+
for (String id:ids) {
190+
assertEquals(id, retval.get(id).getId());
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+
assertTrue(retval.get(id).getObjectUri().endsWith(id));
200+
}
201+
}
202+
203+
public void testGetListedLicenseExceptionsCompatV2() throws InvalidSPDXAnalysisException {
204+
Map<String, org.spdx.library.model.v2.license.ListedLicenseException> retval =
205+
ListedLicenses.getListedLicenses().getSpdxListedLicenseExceptionsCompatV2();
206+
List<String> ids = ListedLicenses.getListedLicenses().getSpdxListedExceptionIds();
207+
assertEquals(ids.size(), retval.size());
208+
for (String id:ids) {
209+
assertEquals(id, retval.get(id).getId());
210+
}
211+
}
173212
}

0 commit comments

Comments
 (0)