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,22 @@ public RemoteChecksumCalculator(
3741 this .pluginBuildingRequest = pluginBuildingRequest ;
3842 }
3943
44+ private String getCacheKey (Artifact artifact ) {
45+ String classifier = artifact .getClassifier ();
46+ if (classifier == null ) {
47+ classifier = "" ;
48+ }
49+ return artifact .getGroupId () + ":" + artifact .getArtifactId () + ":" + artifact .getVersion () + ":" + classifier
50+ + ":" + artifact .getType ();
51+ }
52+
4053 private Optional <String > calculateChecksumInternal (Artifact artifact , ProjectBuildingRequest buildingRequest ) {
54+ String cacheKey = getCacheKey (artifact ) + ":" + checksumAlgorithm ;
55+ String cached = checksumCache .get (cacheKey );
56+ if (cached != null ) {
57+ return cached .isEmpty () ? Optional .empty () : Optional .of (cached );
58+ }
59+
4160 try {
4261 String groupId = artifact .getGroupId ().replace ("." , "/" );
4362 String artifactId = artifact .getArtifactId ();
@@ -73,7 +92,9 @@ private Optional<String> calculateChecksumInternal(Artifact artifact, ProjectBui
7392 client .send (checksumRequest , HttpResponse .BodyHandlers .ofString ());
7493
7594 if (checksumResponse .statusCode () >= 200 && checksumResponse .statusCode () < 300 ) {
76- return Optional .of (checksumResponse .body ().strip ());
95+ String checksum = checksumResponse .body ().strip ();
96+ checksumCache .put (cacheKey , checksum );
97+ return Optional .of (checksum );
7798 }
7899
79100 if (checksumResponse .statusCode () == 404 ) {
@@ -133,6 +154,7 @@ private Optional<String> calculateChecksumInternal(Artifact artifact, ProjectBui
133154 String checksum = baseEncoding
134155 .encode (messageDigest .digest (artifactResponse .body ()))
135156 .toLowerCase (Locale .ROOT );
157+ checksumCache .put (cacheKey , checksum );
136158 return Optional .of (checksum );
137159 }
138160 }
@@ -141,16 +163,24 @@ private Optional<String> calculateChecksumInternal(Artifact artifact, ProjectBui
141163 .warn (String .format (
142164 "Artifact checksum `%s.%s` not found among remote repositories." ,
143165 artifact , checksumAlgorithm ));
166+ checksumCache .put (cacheKey , "" );
144167 return Optional .empty ();
145168 } catch (Exception e ) {
146169 PluginLogManager .getLog ()
147170 .warn (String .format ("Could not resolve artifact: %s" , artifact .getArtifactId ()), e );
171+ checksumCache .put (cacheKey , "" );
148172 return Optional .empty ();
149173 }
150174 }
151175
152176 private Optional <RepositoryInformation > getResolvedFieldInternal (
153177 Artifact artifact , ProjectBuildingRequest buildingRequest ) {
178+ String cacheKey = getCacheKey (artifact );
179+ RepositoryInformation cached = resolvedCache .get (cacheKey );
180+ if (cached != null ) {
181+ return cached .equals (RepositoryInformation .Unresolved ()) ? Optional .empty () : Optional .of (cached );
182+ }
183+
154184 try {
155185 String groupId = artifact .getGroupId ().replace ("." , "/" );
156186 String artifactId = artifact .getArtifactId ();
@@ -184,16 +214,20 @@ private Optional<RepositoryInformation> getResolvedFieldInternal(
184214 HttpResponse <Void > response = client .send (request , HttpResponse .BodyHandlers .discarding ());
185215
186216 if (response .statusCode () >= 200 && response .statusCode () < 300 ) {
187- return Optional .of (
188- new RepositoryInformation (ResolvedUrl .of (url ), RepositoryId .of (repository .getId ())));
217+ RepositoryInformation result =
218+ new RepositoryInformation (ResolvedUrl .of (url ), RepositoryId .of (repository .getId ()));
219+ resolvedCache .put (cacheKey , result );
220+ return Optional .of (result );
189221 }
190222 }
191223
192224 PluginLogManager .getLog ().warn (String .format ("Artifact resolved url `%s` not found." , artifact ));
225+ resolvedCache .put (cacheKey , RepositoryInformation .Unresolved ());
193226 return Optional .empty ();
194227 } catch (Exception e ) {
195228 PluginLogManager .getLog ()
196229 .warn (String .format ("Could not resolve url for artifact: %s" , artifact .getArtifactId ()), e );
230+ resolvedCache .put (cacheKey , RepositoryInformation .Unresolved ());
197231 return Optional .empty ();
198232 }
199233 }
0 commit comments