1010import java .net .http .HttpResponse ;
1111import java .security .MessageDigest ;
1212import java .util .Locale ;
13+ import java .util .Map ;
1314import java .util .Optional ;
15+ import java .util .concurrent .ConcurrentHashMap ;
1416import org .apache .maven .artifact .Artifact ;
1517import org .apache .maven .artifact .repository .ArtifactRepository ;
1618import org .apache .maven .project .ProjectBuildingRequest ;
@@ -19,6 +21,8 @@ public class RemoteChecksumCalculator extends AbstractChecksumCalculator {
1921
2022 private final ProjectBuildingRequest artifactBuildingRequest ;
2123 private final ProjectBuildingRequest pluginBuildingRequest ;
24+ private final Map <String , String > checksumCache = new ConcurrentHashMap <>();
25+ private final Map <String , RepositoryInformation > resolvedCache = new ConcurrentHashMap <>();
2226
2327 public RemoteChecksumCalculator (
2428 String checksumAlgorithm ,
@@ -37,7 +41,18 @@ public RemoteChecksumCalculator(
3741 this .pluginBuildingRequest = pluginBuildingRequest ;
3842 }
3943
44+ private String getCacheKey (Artifact artifact ) {
45+ return artifact .getGroupId () + ":" + artifact .getArtifactId () + ":" + artifact .getVersion () + ":"
46+ + artifact .getClassifier () + ":" + artifact .getType ();
47+ }
48+
4049 private Optional <String > calculateChecksumInternal (Artifact artifact , ProjectBuildingRequest buildingRequest ) {
50+ String cacheKey = getCacheKey (artifact ) + ":" + checksumAlgorithm ;
51+ String cached = checksumCache .get (cacheKey );
52+ if (cached != null ) {
53+ return cached .isEmpty () ? Optional .empty () : Optional .of (cached );
54+ }
55+
4156 try {
4257 String groupId = artifact .getGroupId ().replace ("." , "/" );
4358 String artifactId = artifact .getArtifactId ();
@@ -73,7 +88,9 @@ private Optional<String> calculateChecksumInternal(Artifact artifact, ProjectBui
7388 client .send (checksumRequest , HttpResponse .BodyHandlers .ofString ());
7489
7590 if (checksumResponse .statusCode () >= 200 && checksumResponse .statusCode () < 300 ) {
76- return Optional .of (checksumResponse .body ().strip ());
91+ String checksum = checksumResponse .body ().strip ();
92+ checksumCache .put (cacheKey , checksum );
93+ return Optional .of (checksum );
7794 }
7895
7996 if (checksumResponse .statusCode () == 404 ) {
@@ -133,6 +150,7 @@ private Optional<String> calculateChecksumInternal(Artifact artifact, ProjectBui
133150 String checksum = baseEncoding
134151 .encode (messageDigest .digest (artifactResponse .body ()))
135152 .toLowerCase (Locale .ROOT );
153+ checksumCache .put (cacheKey , checksum );
136154 return Optional .of (checksum );
137155 }
138156 }
@@ -141,16 +159,24 @@ private Optional<String> calculateChecksumInternal(Artifact artifact, ProjectBui
141159 .warn (String .format (
142160 "Artifact checksum `%s.%s` not found among remote repositories." ,
143161 artifact , checksumAlgorithm ));
162+ checksumCache .put (cacheKey , "" );
144163 return Optional .empty ();
145164 } catch (Exception e ) {
146165 PluginLogManager .getLog ()
147166 .warn (String .format ("Could not resolve artifact: %s" , artifact .getArtifactId ()), e );
167+ checksumCache .put (cacheKey , "" );
148168 return Optional .empty ();
149169 }
150170 }
151171
152172 private Optional <RepositoryInformation > getResolvedFieldInternal (
153173 Artifact artifact , ProjectBuildingRequest buildingRequest ) {
174+ String cacheKey = getCacheKey (artifact );
175+ RepositoryInformation cached = resolvedCache .get (cacheKey );
176+ if (cached != null ) {
177+ return cached .equals (RepositoryInformation .Unresolved ()) ? Optional .empty () : Optional .of (cached );
178+ }
179+
154180 try {
155181 String groupId = artifact .getGroupId ().replace ("." , "/" );
156182 String artifactId = artifact .getArtifactId ();
@@ -184,16 +210,20 @@ private Optional<RepositoryInformation> getResolvedFieldInternal(
184210 HttpResponse <Void > response = client .send (request , HttpResponse .BodyHandlers .discarding ());
185211
186212 if (response .statusCode () >= 200 && response .statusCode () < 300 ) {
187- return Optional .of (
188- new RepositoryInformation (ResolvedUrl .of (url ), RepositoryId .of (repository .getId ())));
213+ RepositoryInformation result =
214+ new RepositoryInformation (ResolvedUrl .of (url ), RepositoryId .of (repository .getId ()));
215+ resolvedCache .put (cacheKey , result );
216+ return Optional .of (result );
189217 }
190218 }
191219
192220 PluginLogManager .getLog ().warn (String .format ("Artifact resolved url `%s` not found." , artifact ));
221+ resolvedCache .put (cacheKey , RepositoryInformation .Unresolved ());
193222 return Optional .empty ();
194223 } catch (Exception e ) {
195224 PluginLogManager .getLog ()
196225 .warn (String .format ("Could not resolve url for artifact: %s" , artifact .getArtifactId ()), e );
226+ resolvedCache .put (cacheKey , RepositoryInformation .Unresolved ());
197227 return Optional .empty ();
198228 }
199229 }
0 commit comments