From 7b8db2e221ff94206ee0b75003045e57171ba4c9 Mon Sep 17 00:00:00 2001 From: Gary O'Neall Date: Mon, 11 Mar 2024 14:36:52 -0700 Subject: [PATCH] Make the Mojo threadsafe Fixes #161 Signed-off-by: Gary O'Neall --- .../java/org/spdx/maven/CreateSpdxMojo.java | 4 ++-- .../maven/utils/MavenToSpdxLicenseMapper.java | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/spdx/maven/CreateSpdxMojo.java b/src/main/java/org/spdx/maven/CreateSpdxMojo.java index 7b5bde3..afda541 100644 --- a/src/main/java/org/spdx/maven/CreateSpdxMojo.java +++ b/src/main/java/org/spdx/maven/CreateSpdxMojo.java @@ -28,7 +28,6 @@ import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.plugins.annotations.ResolutionScope; import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectHelper; @@ -98,7 +97,8 @@ */ @Mojo( name = "createSPDX", defaultPhase = LifecyclePhase.VERIFY, - requiresOnline = true ) + requiresOnline = true, + threadSafe = true ) public class CreateSpdxMojo extends AbstractMojo { public static final String INCLUDE_ALL = "**/*"; diff --git a/src/main/java/org/spdx/maven/utils/MavenToSpdxLicenseMapper.java b/src/main/java/org/spdx/maven/utils/MavenToSpdxLicenseMapper.java index 391c90d..6847044 100644 --- a/src/main/java/org/spdx/maven/utils/MavenToSpdxLicenseMapper.java +++ b/src/main/java/org/spdx/maven/utils/MavenToSpdxLicenseMapper.java @@ -61,7 +61,8 @@ public class MavenToSpdxLicenseMapper private static final String LISTED_LICENSE_JSON_URL = SPDX_LICENSE_URL_PREFIX + "licenses.json"; private static final String LISTED_LICENSE_JSON_PATH = "resources/licenses.json"; - static MavenToSpdxLicenseMapper instance; + static volatile MavenToSpdxLicenseMapper instance; + private static Object instanceMutex = new Object(); private Map urlStringToSpdxLicenseId; private MavenToSpdxLicenseMapper() throws LicenseMapperException @@ -101,11 +102,19 @@ private MavenToSpdxLicenseMapper() throws LicenseMapperException public static MavenToSpdxLicenseMapper getInstance() throws LicenseMapperException { - if ( instance == null ) + MavenToSpdxLicenseMapper result = instance; + if ( result == null ) { - instance = new MavenToSpdxLicenseMapper(); + synchronized ( instanceMutex ) + { + result = instance; + if ( result == null ) + { + instance = result = new MavenToSpdxLicenseMapper(); + } + } } - return instance; + return result; } /**