From 84c9e4bc780a714281b324c8e5b152dacd956442 Mon Sep 17 00:00:00 2001 From: Tim Whittington Date: Fri, 12 May 2023 12:17:04 +1200 Subject: [PATCH 1/3] Use singleton holder for SpdxLicenseList cache. Use singleton holder pattern instead of double checked locking. --- .../mojo/license/spdx/SpdxLicenseList.java | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/codehaus/mojo/license/spdx/SpdxLicenseList.java b/src/main/java/org/codehaus/mojo/license/spdx/SpdxLicenseList.java index d450057f9..e0504d454 100644 --- a/src/main/java/org/codehaus/mojo/license/spdx/SpdxLicenseList.java +++ b/src/main/java/org/codehaus/mojo/license/spdx/SpdxLicenseList.java @@ -42,23 +42,15 @@ */ public class SpdxLicenseList { - private static volatile SpdxLicenseList latest; - - private static final Object LOCK = new Object(); + private static final class LatestHolder + { + private LatestHolder() {} + private static final SpdxLicenseList latest = SpdxLicenseListData.createList(); + } public static SpdxLicenseList getLatest() { - if ( latest == null ) - { - synchronized ( LOCK ) - { - if ( latest == null ) - { - latest = SpdxLicenseListData.createList(); - } - } - } - return latest; + return LatestHolder.latest; } private final String licenseListVersion; From 62ae37b0a4709d4a1c6c3b11a13be0fc885badce Mon Sep 17 00:00:00 2001 From: Tim Whittington Date: Fri, 12 May 2023 12:18:20 +1200 Subject: [PATCH 2/3] Make artifact cache threadsafe. Use singleton holder pattern for lazy initialisation of static cache, and make the cache concurrency safe as it can be read and updated simultaneously in multi module builds. --- .../mojo/license/api/DefaultThirdPartyHelper.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/codehaus/mojo/license/api/DefaultThirdPartyHelper.java b/src/main/java/org/codehaus/mojo/license/api/DefaultThirdPartyHelper.java index a0c5ec70b..bb60301ef 100644 --- a/src/main/java/org/codehaus/mojo/license/api/DefaultThirdPartyHelper.java +++ b/src/main/java/org/codehaus/mojo/license/api/DefaultThirdPartyHelper.java @@ -44,6 +44,7 @@ import java.util.SortedMap; import java.util.SortedSet; import java.util.TreeMap; +import java.util.concurrent.ConcurrentSkipListMap; import org.eclipse.aether.repository.RemoteRepository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -101,7 +102,12 @@ public class DefaultThirdPartyHelper /** * Cache of dependencies (as maven project) loaded. */ - private static SortedMap artifactCache; + private static final class ArtifactCacheHolder + { + private ArtifactCacheHolder() {} + + private static final SortedMap artifactCache = new ConcurrentSkipListMap<>(); + } /** * Constructor of the helper. @@ -136,11 +142,7 @@ public DefaultThirdPartyHelper( MavenProject project, String encoding, boolean v */ public SortedMap getArtifactCache() { - if ( artifactCache == null ) - { - artifactCache = new TreeMap<>(); - } - return artifactCache; + return ArtifactCacheHolder.artifactCache; } /** From 8dfc370e1529885096f39eb077cbf03e13d937dc Mon Sep 17 00:00:00 2001 From: Tim Whittington Date: Fri, 12 May 2023 12:19:06 +1200 Subject: [PATCH 3/3] Mark all mojos threadsafe. --- src/main/java/org/codehaus/mojo/license/AddThirdPartyMojo.java | 2 +- .../codehaus/mojo/license/AggregatorThirdPartyReportMojo.java | 3 ++- .../java/org/codehaus/mojo/license/CommentStyleListMojo.java | 2 +- .../java/org/codehaus/mojo/license/DownloadLicensesMojo.java | 2 +- src/main/java/org/codehaus/mojo/license/LicenseListMojo.java | 2 +- .../codehaus/mojo/license/LicensesXmlInsertVersionsMojo.java | 2 +- .../java/org/codehaus/mojo/license/ThirdPartyReportMojo.java | 2 +- .../org/codehaus/mojo/license/UpdateProjectLicenseMojo.java | 2 +- 8 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/codehaus/mojo/license/AddThirdPartyMojo.java b/src/main/java/org/codehaus/mojo/license/AddThirdPartyMojo.java index 1e5bec0d5..091bc3068 100644 --- a/src/main/java/org/codehaus/mojo/license/AddThirdPartyMojo.java +++ b/src/main/java/org/codehaus/mojo/license/AddThirdPartyMojo.java @@ -67,7 +67,7 @@ */ // CHECKSTYLE_ON: LineLength @Mojo( name = "add-third-party", requiresDependencyResolution = ResolutionScope.TEST, - defaultPhase = LifecyclePhase.GENERATE_RESOURCES ) + defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true ) public class AddThirdPartyMojo extends AbstractAddThirdPartyMojo implements MavenProjectDependenciesConfigurator { private static final Logger LOG = LoggerFactory.getLogger( AddThirdPartyMojo.class ); diff --git a/src/main/java/org/codehaus/mojo/license/AggregatorThirdPartyReportMojo.java b/src/main/java/org/codehaus/mojo/license/AggregatorThirdPartyReportMojo.java index dbbfa1223..208538043 100644 --- a/src/main/java/org/codehaus/mojo/license/AggregatorThirdPartyReportMojo.java +++ b/src/main/java/org/codehaus/mojo/license/AggregatorThirdPartyReportMojo.java @@ -49,7 +49,8 @@ * @author Tony Chemit - dev@tchemit.fr * @since 1.10 */ -@Mojo( name = "aggregate-third-party-report", aggregator = true, requiresDependencyResolution = ResolutionScope.TEST ) +@Mojo( name = "aggregate-third-party-report", aggregator = true, + requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true ) public class AggregatorThirdPartyReportMojo extends AbstractThirdPartyReportMojo { diff --git a/src/main/java/org/codehaus/mojo/license/CommentStyleListMojo.java b/src/main/java/org/codehaus/mojo/license/CommentStyleListMojo.java index 1d9414104..7c21f8369 100644 --- a/src/main/java/org/codehaus/mojo/license/CommentStyleListMojo.java +++ b/src/main/java/org/codehaus/mojo/license/CommentStyleListMojo.java @@ -43,7 +43,7 @@ * @author tchemit dev@tchemit.fr * @since 1.0 */ -@Mojo( name = "comment-style-list", requiresProject = false, requiresDirectInvocation = true ) +@Mojo( name = "comment-style-list", requiresProject = false, requiresDirectInvocation = true, threadSafe = true ) public class CommentStyleListMojo extends AbstractLicenseMojo { diff --git a/src/main/java/org/codehaus/mojo/license/DownloadLicensesMojo.java b/src/main/java/org/codehaus/mojo/license/DownloadLicensesMojo.java index b0a6bebed..20929fde9 100644 --- a/src/main/java/org/codehaus/mojo/license/DownloadLicensesMojo.java +++ b/src/main/java/org/codehaus/mojo/license/DownloadLicensesMojo.java @@ -53,7 +53,7 @@ * @since 1.0 */ @Mojo( name = "download-licenses", requiresDependencyResolution = ResolutionScope.TEST, - defaultPhase = LifecyclePhase.PACKAGE ) + defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true ) public class DownloadLicensesMojo extends AbstractDownloadLicensesMojo { diff --git a/src/main/java/org/codehaus/mojo/license/LicenseListMojo.java b/src/main/java/org/codehaus/mojo/license/LicenseListMojo.java index f5e2d6ab6..2d5c28359 100644 --- a/src/main/java/org/codehaus/mojo/license/LicenseListMojo.java +++ b/src/main/java/org/codehaus/mojo/license/LicenseListMojo.java @@ -42,7 +42,7 @@ * @author tchemit dev@tchemit.fr * @since 1.0 */ -@Mojo( name = "license-list", requiresProject = false, requiresDirectInvocation = true ) +@Mojo( name = "license-list", requiresProject = false, requiresDirectInvocation = true, threadSafe = true ) public class LicenseListMojo extends AbstractLicenseMojo { diff --git a/src/main/java/org/codehaus/mojo/license/LicensesXmlInsertVersionsMojo.java b/src/main/java/org/codehaus/mojo/license/LicensesXmlInsertVersionsMojo.java index fddcb50e9..7798b2ea5 100644 --- a/src/main/java/org/codehaus/mojo/license/LicensesXmlInsertVersionsMojo.java +++ b/src/main/java/org/codehaus/mojo/license/LicensesXmlInsertVersionsMojo.java @@ -52,7 +52,7 @@ * @since 1.19 */ @Mojo( name = "licenses-xml-insert-versions", requiresDependencyResolution = ResolutionScope.TEST, - defaultPhase = LifecyclePhase.PACKAGE ) + defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true ) public class LicensesXmlInsertVersionsMojo extends AbstractLicensesXmlMojo { diff --git a/src/main/java/org/codehaus/mojo/license/ThirdPartyReportMojo.java b/src/main/java/org/codehaus/mojo/license/ThirdPartyReportMojo.java index 81f2cd4da..f4ad15d1f 100644 --- a/src/main/java/org/codehaus/mojo/license/ThirdPartyReportMojo.java +++ b/src/main/java/org/codehaus/mojo/license/ThirdPartyReportMojo.java @@ -41,7 +41,7 @@ * @author tchemit dev@tchemit.fr * @since 1.1 */ -@Mojo( name = "third-party-report", requiresDependencyResolution = ResolutionScope.TEST ) +@Mojo( name = "third-party-report", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true ) public class ThirdPartyReportMojo extends AbstractThirdPartyReportMojo { diff --git a/src/main/java/org/codehaus/mojo/license/UpdateProjectLicenseMojo.java b/src/main/java/org/codehaus/mojo/license/UpdateProjectLicenseMojo.java index 0390e20e2..ce162200d 100644 --- a/src/main/java/org/codehaus/mojo/license/UpdateProjectLicenseMojo.java +++ b/src/main/java/org/codehaus/mojo/license/UpdateProjectLicenseMojo.java @@ -43,7 +43,7 @@ * @author tchemit dev@tchemit.fr * @since 1.0 */ -@Mojo( name = "update-project-license", defaultPhase = LifecyclePhase.GENERATE_RESOURCES ) +@Mojo( name = "update-project-license", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true ) public class UpdateProjectLicenseMojo extends AbstractLicenseNameMojo {