1 /*******************************************************************************
2 * Copyright (c) 2010, 2012 Sonatype, Inc.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Sonatype, Inc. - initial API and implementation
10 *******************************************************************************/
11 package org
.eclipse
.aether
.resolution
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collections
;
15 import java
.util
.List
;
17 import org
.eclipse
.aether
.RepositorySystem
;
18 import org
.eclipse
.aether
.RepositorySystemSession
;
19 import org
.eclipse
.aether
.artifact
.Artifact
;
20 import org
.eclipse
.aether
.repository
.ArtifactRepository
;
21 import org
.eclipse
.aether
.transfer
.ArtifactNotFoundException
;
24 * The result of an artifact resolution request.
26 * @see RepositorySystem#resolveArtifacts(RepositorySystemSession, java.util.Collection)
27 * @see Artifact#getFile()
29 public final class ArtifactResult
32 private final ArtifactRequest request
;
34 private List
<Exception
> exceptions
;
36 private Artifact artifact
;
38 private ArtifactRepository repository
;
41 * Creates a new result for the specified request.
43 * @param request The resolution request, must not be {@code null}.
45 public ArtifactResult( ArtifactRequest request
)
47 if ( request
== null )
49 throw new IllegalArgumentException( "resolution request has not been specified" );
51 this.request
= request
;
52 exceptions
= Collections
.emptyList();
56 * Gets the resolution request that was made.
58 * @return The resolution request, never {@code null}.
60 public ArtifactRequest
getRequest()
66 * Gets the resolved artifact (if any). Use {@link #getExceptions()} to query the errors that occurred while trying
67 * to resolve the artifact.
69 * @return The resolved artifact or {@code null} if the resolution failed.
71 public Artifact
getArtifact()
77 * Sets the resolved artifact.
79 * @param artifact The resolved artifact, may be {@code null} if the resolution failed.
80 * @return This result for chaining, never {@code null}.
82 public ArtifactResult
setArtifact( Artifact artifact
)
84 this.artifact
= artifact
;
89 * Gets the exceptions that occurred while resolving the artifact. Note that this list can be non-empty even if the
90 * artifact was successfully resolved, e.g. when one of the contacted remote repositories didn't contain the
91 * artifact but a later repository eventually contained it.
93 * @return The exceptions that occurred, never {@code null}.
96 public List
<Exception
> getExceptions()
102 * Records the specified exception while resolving the artifact.
104 * @param exception The exception to record, may be {@code null}.
105 * @return This result for chaining, never {@code null}.
107 public ArtifactResult
addException( Exception exception
)
109 if ( exception
!= null )
111 if ( exceptions
.isEmpty() )
113 exceptions
= new ArrayList
<Exception
>();
115 exceptions
.add( exception
);
121 * Gets the repository from which the artifact was eventually resolved. Note that successive resolutions of the same
122 * artifact might yield different results if the employed local repository does not track the origin of an artifact.
124 * @return The repository from which the artifact was resolved or {@code null} if unknown.
126 public ArtifactRepository
getRepository()
132 * Sets the repository from which the artifact was resolved.
134 * @param repository The repository from which the artifact was resolved, may be {@code null}.
135 * @return This result for chaining, never {@code null}.
137 public ArtifactResult
setRepository( ArtifactRepository repository
)
139 this.repository
= repository
;
144 * Indicates whether the requested artifact was resolved. Note that the artifact might have been successfully
145 * resolved despite {@link #getExceptions()} indicating transfer errors while trying to fetch the artifact from some
146 * of the specified remote repositories.
148 * @return {@code true} if the artifact was resolved, {@code false} otherwise.
149 * @see Artifact#getFile()
151 public boolean isResolved()
153 return getArtifact() != null && getArtifact().getFile() != null;
157 * Indicates whether the requested artifact is not present in any of the specified repositories.
159 * @return {@code true} if the artifact is not present in any repository, {@code false} otherwise.
161 public boolean isMissing()
163 for ( Exception e
: getExceptions() )
165 if ( !( e
instanceof ArtifactNotFoundException
) )
170 return !isResolved();
174 public String
toString()
176 return getArtifact() + " < " + getRepository();