1919
2020import java .io .IOException ;
2121import 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 .*;
2623import java .util .concurrent .locks .ReadWriteLock ;
2724import java .util .concurrent .locks .ReentrantReadWriteLock ;
2825
3330import org .spdx .library .model .v2 .SpdxConstantsCompatV2 ;
3431import org .spdx .library .model .v2 .SpdxModelFactoryCompatV2 ;
3532import org .spdx .library .model .v2 .license .SpdxListedLicense ;
33+ import org .spdx .library .model .v2 .license .SpdxListedLicenseException ;
3634import org .spdx .library .model .v3_0_1 .core .CreationInfo ;
3735import org .spdx .library .model .v3_0_1 .expandedlicensing .ListedLicense ;
3836import 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.
0 commit comments