View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.impl;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.Collections;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.concurrent.atomic.AtomicBoolean;
34  
35  import org.eclipse.aether.RepositoryEvent;
36  import org.eclipse.aether.RepositoryEvent.EventType;
37  import org.eclipse.aether.RepositorySystemSession;
38  import org.eclipse.aether.RequestTrace;
39  import org.eclipse.aether.SyncContext;
40  import org.eclipse.aether.artifact.Artifact;
41  import org.eclipse.aether.artifact.ArtifactProperties;
42  import org.eclipse.aether.impl.ArtifactResolver;
43  import org.eclipse.aether.impl.OfflineController;
44  import org.eclipse.aether.impl.RemoteRepositoryFilterManager;
45  import org.eclipse.aether.impl.RemoteRepositoryManager;
46  import org.eclipse.aether.impl.RepositoryConnectorProvider;
47  import org.eclipse.aether.impl.RepositoryEventDispatcher;
48  import org.eclipse.aether.impl.UpdateCheck;
49  import org.eclipse.aether.impl.UpdateCheckManager;
50  import org.eclipse.aether.impl.VersionResolver;
51  import org.eclipse.aether.repository.ArtifactRepository;
52  import org.eclipse.aether.repository.LocalArtifactRegistration;
53  import org.eclipse.aether.repository.LocalArtifactRequest;
54  import org.eclipse.aether.repository.LocalArtifactResult;
55  import org.eclipse.aether.repository.LocalRepository;
56  import org.eclipse.aether.repository.LocalRepositoryManager;
57  import org.eclipse.aether.repository.RemoteRepository;
58  import org.eclipse.aether.repository.RepositoryPolicy;
59  import org.eclipse.aether.repository.WorkspaceReader;
60  import org.eclipse.aether.resolution.ArtifactRequest;
61  import org.eclipse.aether.resolution.ArtifactResolutionException;
62  import org.eclipse.aether.resolution.ArtifactResult;
63  import org.eclipse.aether.resolution.ResolutionErrorPolicy;
64  import org.eclipse.aether.resolution.VersionRequest;
65  import org.eclipse.aether.resolution.VersionResolutionException;
66  import org.eclipse.aether.resolution.VersionResult;
67  import org.eclipse.aether.spi.connector.ArtifactDownload;
68  import org.eclipse.aether.spi.connector.RepositoryConnector;
69  import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilter;
70  import org.eclipse.aether.spi.io.FileProcessor;
71  import org.eclipse.aether.spi.locator.Service;
72  import org.eclipse.aether.spi.locator.ServiceLocator;
73  import org.eclipse.aether.spi.resolution.ArtifactResolverPostProcessor;
74  import org.eclipse.aether.spi.synccontext.SyncContextFactory;
75  import org.eclipse.aether.transfer.ArtifactFilteredOutException;
76  import org.eclipse.aether.transfer.ArtifactNotFoundException;
77  import org.eclipse.aether.transfer.ArtifactTransferException;
78  import org.eclipse.aether.transfer.NoRepositoryConnectorException;
79  import org.eclipse.aether.transfer.RepositoryOfflineException;
80  import org.eclipse.aether.util.ConfigUtils;
81  import org.slf4j.Logger;
82  import org.slf4j.LoggerFactory;
83  
84  import static java.util.Objects.requireNonNull;
85  
86  /**
87   *
88   */
89  @Singleton
90  @Named
91  public class DefaultArtifactResolver implements ArtifactResolver, Service {
92  
93      /**
94       * Configuration to enable "snapshot normalization", downloaded snapshots from remote with timestamped file names
95       * will have file names converted back to baseVersion. Default: {@code true}.
96       */
97      private static final String CONFIG_PROP_SNAPSHOT_NORMALIZATION = "aether.artifactResolver.snapshotNormalization";
98  
99      /**
100      * Configuration to enable "interoperability" with Simple LRM, but this breaks RRF feature, hence this configuration
101      * is IGNORED when RRF is used, and is warmly recommended to leave it disabled even if no RRF is being used.
102      * Default: {@code false}.
103      */
104     private static final String CONFIG_PROP_SIMPLE_LRM_INTEROP = "aether.artifactResolver.simpleLrmInterop";
105 
106     private static final Logger LOGGER = LoggerFactory.getLogger(DefaultArtifactResolver.class);
107 
108     private FileProcessor fileProcessor;
109 
110     private RepositoryEventDispatcher repositoryEventDispatcher;
111 
112     private VersionResolver versionResolver;
113 
114     private UpdateCheckManager updateCheckManager;
115 
116     private RepositoryConnectorProvider repositoryConnectorProvider;
117 
118     private RemoteRepositoryManager remoteRepositoryManager;
119 
120     private SyncContextFactory syncContextFactory;
121 
122     private OfflineController offlineController;
123 
124     private Map<String, ArtifactResolverPostProcessor> artifactResolverPostProcessors;
125 
126     private RemoteRepositoryFilterManager remoteRepositoryFilterManager;
127 
128     @Deprecated
129     public DefaultArtifactResolver() {
130         // enables default constructor
131     }
132 
133     @SuppressWarnings("checkstyle:parameternumber")
134     @Inject
135     public DefaultArtifactResolver(
136             FileProcessor fileProcessor,
137             RepositoryEventDispatcher repositoryEventDispatcher,
138             VersionResolver versionResolver,
139             UpdateCheckManager updateCheckManager,
140             RepositoryConnectorProvider repositoryConnectorProvider,
141             RemoteRepositoryManager remoteRepositoryManager,
142             SyncContextFactory syncContextFactory,
143             OfflineController offlineController,
144             Map<String, ArtifactResolverPostProcessor> artifactResolverPostProcessors,
145             RemoteRepositoryFilterManager remoteRepositoryFilterManager) {
146         setFileProcessor(fileProcessor);
147         setRepositoryEventDispatcher(repositoryEventDispatcher);
148         setVersionResolver(versionResolver);
149         setUpdateCheckManager(updateCheckManager);
150         setRepositoryConnectorProvider(repositoryConnectorProvider);
151         setRemoteRepositoryManager(remoteRepositoryManager);
152         setSyncContextFactory(syncContextFactory);
153         setOfflineController(offlineController);
154         setArtifactResolverPostProcessors(artifactResolverPostProcessors);
155         setRemoteRepositoryFilterManager(remoteRepositoryFilterManager);
156     }
157 
158     public void initService(ServiceLocator locator) {
159         setFileProcessor(locator.getService(FileProcessor.class));
160         setRepositoryEventDispatcher(locator.getService(RepositoryEventDispatcher.class));
161         setVersionResolver(locator.getService(VersionResolver.class));
162         setUpdateCheckManager(locator.getService(UpdateCheckManager.class));
163         setRepositoryConnectorProvider(locator.getService(RepositoryConnectorProvider.class));
164         setRemoteRepositoryManager(locator.getService(RemoteRepositoryManager.class));
165         setSyncContextFactory(locator.getService(SyncContextFactory.class));
166         setOfflineController(locator.getService(OfflineController.class));
167         setArtifactResolverPostProcessors(Collections.emptyMap());
168         setRemoteRepositoryFilterManager(locator.getService(RemoteRepositoryFilterManager.class));
169     }
170 
171     /**
172      * @deprecated not used any more since MRESOLVER-36 move to slf4j, added back in MRESOLVER-64 for compatibility
173      */
174     @Deprecated
175     public DefaultArtifactResolver setLoggerFactory(org.eclipse.aether.spi.log.LoggerFactory loggerFactory) {
176         // this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() );
177         return this;
178     }
179 
180     public DefaultArtifactResolver setFileProcessor(FileProcessor fileProcessor) {
181         this.fileProcessor = requireNonNull(fileProcessor, "file processor cannot be null");
182         return this;
183     }
184 
185     public DefaultArtifactResolver setRepositoryEventDispatcher(RepositoryEventDispatcher repositoryEventDispatcher) {
186         this.repositoryEventDispatcher =
187                 requireNonNull(repositoryEventDispatcher, "repository event dispatcher cannot be null");
188         return this;
189     }
190 
191     public DefaultArtifactResolver setVersionResolver(VersionResolver versionResolver) {
192         this.versionResolver = requireNonNull(versionResolver, "version resolver cannot be null");
193         return this;
194     }
195 
196     public DefaultArtifactResolver setUpdateCheckManager(UpdateCheckManager updateCheckManager) {
197         this.updateCheckManager = requireNonNull(updateCheckManager, "update check manager cannot be null");
198         return this;
199     }
200 
201     public DefaultArtifactResolver setRepositoryConnectorProvider(
202             RepositoryConnectorProvider repositoryConnectorProvider) {
203         this.repositoryConnectorProvider =
204                 requireNonNull(repositoryConnectorProvider, "repository connector provider cannot be null");
205         return this;
206     }
207 
208     public DefaultArtifactResolver setRemoteRepositoryManager(RemoteRepositoryManager remoteRepositoryManager) {
209         this.remoteRepositoryManager =
210                 requireNonNull(remoteRepositoryManager, "remote repository provider cannot be null");
211         return this;
212     }
213 
214     public DefaultArtifactResolver setSyncContextFactory(SyncContextFactory syncContextFactory) {
215         this.syncContextFactory = requireNonNull(syncContextFactory, "sync context factory cannot be null");
216         return this;
217     }
218 
219     public DefaultArtifactResolver setOfflineController(OfflineController offlineController) {
220         this.offlineController = requireNonNull(offlineController, "offline controller cannot be null");
221         return this;
222     }
223 
224     public DefaultArtifactResolver setArtifactResolverPostProcessors(
225             Map<String, ArtifactResolverPostProcessor> artifactResolverPostProcessors) {
226         this.artifactResolverPostProcessors =
227                 requireNonNull(artifactResolverPostProcessors, "artifact resolver post-processors cannot be null");
228         return this;
229     }
230 
231     public DefaultArtifactResolver setRemoteRepositoryFilterManager(
232             RemoteRepositoryFilterManager remoteRepositoryFilterManager) {
233         this.remoteRepositoryFilterManager =
234                 requireNonNull(remoteRepositoryFilterManager, "remote repository filter manager cannot be null");
235         return this;
236     }
237 
238     public ArtifactResult resolveArtifact(RepositorySystemSession session, ArtifactRequest request)
239             throws ArtifactResolutionException {
240         requireNonNull(session, "session cannot be null");
241         requireNonNull(request, "request cannot be null");
242 
243         return resolveArtifacts(session, Collections.singleton(request)).get(0);
244     }
245 
246     public List<ArtifactResult> resolveArtifacts(
247             RepositorySystemSession session, Collection<? extends ArtifactRequest> requests)
248             throws ArtifactResolutionException {
249         requireNonNull(session, "session cannot be null");
250         requireNonNull(requests, "requests cannot be null");
251         try (SyncContext shared = syncContextFactory.newInstance(session, true);
252                 SyncContext exclusive = syncContextFactory.newInstance(session, false)) {
253             Collection<Artifact> artifacts = new ArrayList<>(requests.size());
254             for (ArtifactRequest request : requests) {
255                 if (request.getArtifact().getProperty(ArtifactProperties.LOCAL_PATH, null) != null) {
256                     continue;
257                 }
258                 artifacts.add(request.getArtifact());
259             }
260 
261             return resolve(shared, exclusive, artifacts, session, requests);
262         }
263     }
264 
265     @SuppressWarnings("checkstyle:methodlength")
266     private List<ArtifactResult> resolve(
267             SyncContext shared,
268             SyncContext exclusive,
269             Collection<Artifact> subjects,
270             RepositorySystemSession session,
271             Collection<? extends ArtifactRequest> requests)
272             throws ArtifactResolutionException {
273         SyncContext current = shared;
274         try {
275             while (true) {
276                 current.acquire(subjects, null);
277 
278                 boolean failures = false;
279                 final List<ArtifactResult> results = new ArrayList<>(requests.size());
280                 final boolean simpleLrmInterop = ConfigUtils.getBoolean(session, false, CONFIG_PROP_SIMPLE_LRM_INTEROP);
281                 final LocalRepositoryManager lrm = session.getLocalRepositoryManager();
282                 final WorkspaceReader workspace = session.getWorkspaceReader();
283                 final List<ResolutionGroup> groups = new ArrayList<>();
284                 // filter != null: means "filtering applied", if null no filtering applied (behave as before)
285                 final RemoteRepositoryFilter filter = remoteRepositoryFilterManager.getRemoteRepositoryFilter(session);
286 
287                 for (ArtifactRequest request : requests) {
288                     RequestTrace trace = RequestTrace.newChild(request.getTrace(), request);
289 
290                     ArtifactResult result = new ArtifactResult(request);
291                     results.add(result);
292 
293                     Artifact artifact = request.getArtifact();
294 
295                     if (current == shared) {
296                         artifactResolving(session, trace, artifact);
297                     }
298 
299                     String localPath = artifact.getProperty(ArtifactProperties.LOCAL_PATH, null);
300                     if (localPath != null) {
301                         // unhosted artifact, just validate file
302                         File file = new File(localPath);
303                         if (!file.isFile()) {
304                             failures = true;
305                             result.addException(new ArtifactNotFoundException(artifact, null));
306                         } else {
307                             artifact = artifact.setFile(file);
308                             result.setArtifact(artifact);
309                             artifactResolved(session, trace, artifact, null, result.getExceptions());
310                         }
311                         continue;
312                     }
313 
314                     List<RemoteRepository> remoteRepositories = request.getRepositories();
315                     List<RemoteRepository> filteredRemoteRepositories = new ArrayList<>(remoteRepositories);
316                     if (filter != null) {
317                         for (RemoteRepository repository : remoteRepositories) {
318                             RemoteRepositoryFilter.Result filterResult = filter.acceptArtifact(repository, artifact);
319                             if (!filterResult.isAccepted()) {
320                                 result.addException(new ArtifactFilteredOutException(
321                                         artifact, repository, filterResult.reasoning()));
322                                 filteredRemoteRepositories.remove(repository);
323                             }
324                         }
325                     }
326 
327                     VersionResult versionResult;
328                     try {
329                         VersionRequest versionRequest =
330                                 new VersionRequest(artifact, filteredRemoteRepositories, request.getRequestContext());
331                         versionRequest.setTrace(trace);
332                         versionResult = versionResolver.resolveVersion(session, versionRequest);
333                     } catch (VersionResolutionException e) {
334                         result.addException(e);
335                         continue;
336                     }
337 
338                     artifact = artifact.setVersion(versionResult.getVersion());
339 
340                     if (versionResult.getRepository() != null) {
341                         if (versionResult.getRepository() instanceof RemoteRepository) {
342                             filteredRemoteRepositories =
343                                     Collections.singletonList((RemoteRepository) versionResult.getRepository());
344                         } else {
345                             filteredRemoteRepositories = Collections.emptyList();
346                         }
347                     }
348 
349                     if (workspace != null) {
350                         File file = workspace.findArtifact(artifact);
351                         if (file != null) {
352                             artifact = artifact.setFile(file);
353                             result.setArtifact(artifact);
354                             result.setRepository(workspace.getRepository());
355                             artifactResolved(session, trace, artifact, result.getRepository(), null);
356                             continue;
357                         }
358                     }
359 
360                     LocalArtifactResult local = lrm.find(
361                             session,
362                             new LocalArtifactRequest(
363                                     artifact, filteredRemoteRepositories, request.getRequestContext()));
364                     result.setLocalArtifactResult(local);
365                     boolean found = (filter != null && local.isAvailable()) || isLocallyInstalled(local, versionResult);
366                     // with filtering it is availability that drives logic
367                     // without filtering it is simply presence of file that drives the logic
368                     // "interop" logic with simple LRM leads to RRF breakage: hence is ignored when filtering in effect
369                     if (found) {
370                         if (local.getRepository() != null) {
371                             result.setRepository(local.getRepository());
372                         } else {
373                             result.setRepository(lrm.getRepository());
374                         }
375 
376                         try {
377                             artifact = artifact.setFile(getFile(session, artifact, local.getFile()));
378                             result.setArtifact(artifact);
379                             artifactResolved(session, trace, artifact, result.getRepository(), null);
380                         } catch (ArtifactTransferException e) {
381                             result.addException(e);
382                         }
383                         if (filter == null && simpleLrmInterop && !local.isAvailable()) {
384                             /*
385                              * NOTE: Interop with simple local repository: An artifact installed by a simple local repo
386                              * manager will not show up in the repository tracking file of the enhanced local repository.
387                              * If however the maven-metadata-local.xml tells us the artifact was installed locally, we
388                              * sync the repository tracking file.
389                              */
390                             lrm.add(session, new LocalArtifactRegistration(artifact));
391                         }
392 
393                         continue;
394                     }
395 
396                     if (local.getFile() != null) {
397                         LOGGER.info(
398                                 "Artifact {} is present in the local repository, but cached from a remote repository ID that is unavailable in current build context, verifying that is downloadable from {}",
399                                 artifact,
400                                 remoteRepositories);
401                     }
402 
403                     LOGGER.debug("Resolving artifact {} from {}", artifact, remoteRepositories);
404                     AtomicBoolean resolved = new AtomicBoolean(false);
405                     Iterator<ResolutionGroup> groupIt = groups.iterator();
406                     for (RemoteRepository repo : filteredRemoteRepositories) {
407                         if (!repo.getPolicy(artifact.isSnapshot()).isEnabled()) {
408                             continue;
409                         }
410 
411                         try {
412                             Utils.checkOffline(session, offlineController, repo);
413                         } catch (RepositoryOfflineException e) {
414                             Exception exception = new ArtifactNotFoundException(
415                                     artifact,
416                                     repo,
417                                     "Cannot access " + repo.getId() + " ("
418                                             + repo.getUrl() + ") in offline mode and the artifact " + artifact
419                                             + " has not been downloaded from it before.",
420                                     e);
421                             result.addException(exception);
422                             continue;
423                         }
424 
425                         ResolutionGroup group = null;
426                         while (groupIt.hasNext()) {
427                             ResolutionGroup t = groupIt.next();
428                             if (t.matches(repo)) {
429                                 group = t;
430                                 break;
431                             }
432                         }
433                         if (group == null) {
434                             group = new ResolutionGroup(repo);
435                             groups.add(group);
436                             groupIt = Collections.emptyIterator();
437                         }
438                         group.items.add(new ResolutionItem(trace, artifact, resolved, result, local, repo));
439                     }
440                 }
441 
442                 if (!groups.isEmpty() && current == shared) {
443                     current.close();
444                     current = exclusive;
445                     continue;
446                 }
447 
448                 for (ResolutionGroup group : groups) {
449                     performDownloads(session, group);
450                 }
451 
452                 for (ArtifactResolverPostProcessor artifactResolverPostProcessor :
453                         artifactResolverPostProcessors.values()) {
454                     artifactResolverPostProcessor.postProcess(session, results);
455                 }
456 
457                 for (ArtifactResult result : results) {
458                     ArtifactRequest request = result.getRequest();
459 
460                     Artifact artifact = result.getArtifact();
461                     if (artifact == null || artifact.getFile() == null) {
462                         failures = true;
463                         if (result.getExceptions().isEmpty()) {
464                             Exception exception = new ArtifactNotFoundException(request.getArtifact(), null);
465                             result.addException(exception);
466                         }
467                         RequestTrace trace = RequestTrace.newChild(request.getTrace(), request);
468                         artifactResolved(session, trace, request.getArtifact(), null, result.getExceptions());
469                     }
470                 }
471 
472                 if (failures) {
473                     throw new ArtifactResolutionException(results);
474                 }
475 
476                 return results;
477             }
478         } finally {
479             current.close();
480         }
481     }
482 
483     private boolean isLocallyInstalled(LocalArtifactResult lar, VersionResult vr) {
484         if (lar.isAvailable()) {
485             return true;
486         }
487         if (lar.getFile() != null) {
488             if (vr.getRepository() instanceof LocalRepository) {
489                 // resolution of (snapshot) version found locally installed artifact
490                 return true;
491             } else if (vr.getRepository() == null
492                     && lar.getRequest().getRepositories().isEmpty()) {
493                 // resolution of version range found locally installed artifact
494                 return true;
495             }
496         }
497         return false;
498     }
499 
500     private File getFile(RepositorySystemSession session, Artifact artifact, File file)
501             throws ArtifactTransferException {
502         if (artifact.isSnapshot()
503                 && !artifact.getVersion().equals(artifact.getBaseVersion())
504                 && ConfigUtils.getBoolean(session, true, CONFIG_PROP_SNAPSHOT_NORMALIZATION)) {
505             String name = file.getName().replace(artifact.getVersion(), artifact.getBaseVersion());
506             File dst = new File(file.getParent(), name);
507 
508             boolean copy = dst.length() != file.length() || dst.lastModified() != file.lastModified();
509             if (copy) {
510                 try {
511                     fileProcessor.copy(file, dst);
512                     dst.setLastModified(file.lastModified());
513                 } catch (IOException e) {
514                     throw new ArtifactTransferException(artifact, null, e);
515                 }
516             }
517 
518             file = dst;
519         }
520 
521         return file;
522     }
523 
524     private void performDownloads(RepositorySystemSession session, ResolutionGroup group) {
525         List<ArtifactDownload> downloads = gatherDownloads(session, group);
526         if (downloads.isEmpty()) {
527             return;
528         }
529 
530         for (ArtifactDownload download : downloads) {
531             artifactDownloading(session, download.getTrace(), download.getArtifact(), group.repository);
532         }
533 
534         try {
535             try (RepositoryConnector connector =
536                     repositoryConnectorProvider.newRepositoryConnector(session, group.repository)) {
537                 connector.get(downloads, null);
538             }
539         } catch (NoRepositoryConnectorException e) {
540             for (ArtifactDownload download : downloads) {
541                 download.setException(new ArtifactTransferException(download.getArtifact(), group.repository, e));
542             }
543         }
544 
545         evaluateDownloads(session, group);
546     }
547 
548     private List<ArtifactDownload> gatherDownloads(RepositorySystemSession session, ResolutionGroup group) {
549         LocalRepositoryManager lrm = session.getLocalRepositoryManager();
550         List<ArtifactDownload> downloads = new ArrayList<>();
551 
552         for (ResolutionItem item : group.items) {
553             Artifact artifact = item.artifact;
554 
555             if (item.resolved.get()) {
556                 // resolved in previous resolution group
557                 continue;
558             }
559 
560             ArtifactDownload download = new ArtifactDownload();
561             download.setArtifact(artifact);
562             download.setRequestContext(item.request.getRequestContext());
563             download.setListener(SafeTransferListener.wrap(session));
564             download.setTrace(item.trace);
565             if (item.local.getFile() != null) {
566                 download.setFile(item.local.getFile());
567                 download.setExistenceCheck(true);
568             } else {
569                 String path =
570                         lrm.getPathForRemoteArtifact(artifact, group.repository, item.request.getRequestContext());
571                 download.setFile(new File(lrm.getRepository().getBasedir(), path));
572             }
573 
574             boolean snapshot = artifact.isSnapshot();
575             RepositoryPolicy policy = remoteRepositoryManager.getPolicy(session, group.repository, !snapshot, snapshot);
576 
577             int errorPolicy = Utils.getPolicy(session, artifact, group.repository);
578             if ((errorPolicy & ResolutionErrorPolicy.CACHE_ALL) != 0) {
579                 UpdateCheck<Artifact, ArtifactTransferException> check = new UpdateCheck<>();
580                 check.setItem(artifact);
581                 check.setFile(download.getFile());
582                 check.setFileValid(false);
583                 check.setRepository(group.repository);
584                 check.setPolicy(policy.getUpdatePolicy());
585                 item.updateCheck = check;
586                 updateCheckManager.checkArtifact(session, check);
587                 if (!check.isRequired()) {
588                     item.result.addException(check.getException());
589                     continue;
590                 }
591             }
592 
593             download.setChecksumPolicy(policy.getChecksumPolicy());
594             download.setRepositories(item.repository.getMirroredRepositories());
595             downloads.add(download);
596             item.download = download;
597         }
598 
599         return downloads;
600     }
601 
602     private void evaluateDownloads(RepositorySystemSession session, ResolutionGroup group) {
603         LocalRepositoryManager lrm = session.getLocalRepositoryManager();
604 
605         for (ResolutionItem item : group.items) {
606             ArtifactDownload download = item.download;
607             if (download == null) {
608                 continue;
609             }
610 
611             Artifact artifact = download.getArtifact();
612             if (download.getException() == null) {
613                 item.resolved.set(true);
614                 item.result.setRepository(group.repository);
615                 try {
616                     artifact = artifact.setFile(getFile(session, artifact, download.getFile()));
617                     item.result.setArtifact(artifact);
618 
619                     lrm.add(
620                             session,
621                             new LocalArtifactRegistration(artifact, group.repository, download.getSupportedContexts()));
622                 } catch (ArtifactTransferException e) {
623                     download.setException(e);
624                     item.result.addException(e);
625                 }
626             } else {
627                 item.result.addException(download.getException());
628             }
629 
630             /*
631              * NOTE: Touch after registration with local repo to ensure concurrent resolution is not rejected with
632              * "already updated" via session data when actual update to local repo is still pending.
633              */
634             if (item.updateCheck != null) {
635                 item.updateCheck.setException(download.getException());
636                 updateCheckManager.touchArtifact(session, item.updateCheck);
637             }
638 
639             artifactDownloaded(session, download.getTrace(), artifact, group.repository, download.getException());
640             if (download.getException() == null) {
641                 artifactResolved(session, download.getTrace(), artifact, group.repository, null);
642             }
643         }
644     }
645 
646     private void artifactResolving(RepositorySystemSession session, RequestTrace trace, Artifact artifact) {
647         RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_RESOLVING);
648         event.setTrace(trace);
649         event.setArtifact(artifact);
650 
651         repositoryEventDispatcher.dispatch(event.build());
652     }
653 
654     private void artifactResolved(
655             RepositorySystemSession session,
656             RequestTrace trace,
657             Artifact artifact,
658             ArtifactRepository repository,
659             List<Exception> exceptions) {
660         RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_RESOLVED);
661         event.setTrace(trace);
662         event.setArtifact(artifact);
663         event.setRepository(repository);
664         event.setExceptions(exceptions);
665         if (artifact != null) {
666             event.setFile(artifact.getFile());
667         }
668 
669         repositoryEventDispatcher.dispatch(event.build());
670     }
671 
672     private void artifactDownloading(
673             RepositorySystemSession session, RequestTrace trace, Artifact artifact, RemoteRepository repository) {
674         RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_DOWNLOADING);
675         event.setTrace(trace);
676         event.setArtifact(artifact);
677         event.setRepository(repository);
678 
679         repositoryEventDispatcher.dispatch(event.build());
680     }
681 
682     private void artifactDownloaded(
683             RepositorySystemSession session,
684             RequestTrace trace,
685             Artifact artifact,
686             RemoteRepository repository,
687             Exception exception) {
688         RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_DOWNLOADED);
689         event.setTrace(trace);
690         event.setArtifact(artifact);
691         event.setRepository(repository);
692         event.setException(exception);
693         if (artifact != null) {
694             event.setFile(artifact.getFile());
695         }
696 
697         repositoryEventDispatcher.dispatch(event.build());
698     }
699 
700     static class ResolutionGroup {
701 
702         final RemoteRepository repository;
703 
704         final List<ResolutionItem> items = new ArrayList<>();
705 
706         ResolutionGroup(RemoteRepository repository) {
707             this.repository = repository;
708         }
709 
710         boolean matches(RemoteRepository repo) {
711             return repository.getUrl().equals(repo.getUrl())
712                     && repository.getContentType().equals(repo.getContentType())
713                     && repository.isRepositoryManager() == repo.isRepositoryManager();
714         }
715     }
716 
717     static class ResolutionItem {
718 
719         final RequestTrace trace;
720 
721         final ArtifactRequest request;
722 
723         final ArtifactResult result;
724 
725         final LocalArtifactResult local;
726 
727         final RemoteRepository repository;
728 
729         final Artifact artifact;
730 
731         final AtomicBoolean resolved;
732 
733         ArtifactDownload download;
734 
735         UpdateCheck<Artifact, ArtifactTransferException> updateCheck;
736 
737         ResolutionItem(
738                 RequestTrace trace,
739                 Artifact artifact,
740                 AtomicBoolean resolved,
741                 ArtifactResult result,
742                 LocalArtifactResult local,
743                 RemoteRepository repository) {
744             this.trace = trace;
745             this.artifact = artifact;
746             this.resolved = resolved;
747             this.result = result;
748             this.request = result.getRequest();
749             this.local = local;
750             this.repository = repository;
751         }
752     }
753 }