|
19 | 19 |
|
20 | 20 | import java.io.IOException; |
21 | 21 | 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.*; |
26 | 23 | import java.util.concurrent.locks.ReadWriteLock; |
27 | 24 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
28 | 25 |
|
|
33 | 30 | import org.spdx.library.model.v2.SpdxConstantsCompatV2; |
34 | 31 | import org.spdx.library.model.v2.SpdxModelFactoryCompatV2; |
35 | 32 | import org.spdx.library.model.v2.license.SpdxListedLicense; |
| 33 | +import org.spdx.library.model.v2.license.SpdxListedLicenseException; |
36 | 34 | import org.spdx.library.model.v3_0_1.core.CreationInfo; |
37 | 35 | import org.spdx.library.model.v3_0_1.expandedlicensing.ListedLicense; |
38 | 36 | import org.spdx.library.model.v3_0_1.expandedlicensing.ListedLicenseException; |
@@ -62,6 +60,11 @@ public class ListedLicenses { |
62 | 60 | private SpdxV2ListedLicenseModelStore licenseStoreV2; |
63 | 61 | private SpdxV3ListedLicenseModelStore licenseStoreV3; |
64 | 62 | 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 | + |
65 | 68 | /** |
66 | 69 | * Lock for any modifications to the underlying licenseModelStore |
67 | 70 | */ |
@@ -202,8 +205,8 @@ public boolean isSpdxListedExceptionId(String exceptionId) { |
202 | 205 | */ |
203 | 206 | public SpdxListedLicense getListedLicenseByIdCompatV2(String licenseId) throws InvalidSPDXAnalysisException { |
204 | 207 | 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, |
207 | 210 | SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE, null, false); |
208 | 211 | } catch (SpdxIdNotFoundException ex) { |
209 | 212 | return null; |
@@ -261,6 +264,128 @@ public List<String> getSpdxListedLicenseIds() { |
261 | 264 | listedLicenseModificationLock.readLock().unlock(); |
262 | 265 | } |
263 | 266 | } |
| 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 | + } |
264 | 389 |
|
265 | 390 | /** |
266 | 391 | * @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