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 7 * 8 * Contributors: 9 * Sonatype, Inc. - initial API and implementation 10 *******************************************************************************/ 11 package org.eclipse.aether.resolution; 12 13 import java.util.ArrayList; 14 import java.util.Collections; 15 import java.util.List; 16 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; 22 23 /** 24 * The result of an artifact resolution request. 25 * 26 * @see RepositorySystem#resolveArtifacts(RepositorySystemSession, java.util.Collection) 27 * @see Artifact#getFile() 28 */ 29 public final class ArtifactResult 30 { 31 32 private final ArtifactRequest request; 33 34 private List<Exception> exceptions; 35 36 private Artifact artifact; 37 38 private ArtifactRepository repository; 39 40 /** 41 * Creates a new result for the specified request. 42 * 43 * @param request The resolution request, must not be {@code null}. 44 */ 45 public ArtifactResult( ArtifactRequest request ) 46 { 47 if ( request == null ) 48 { 49 throw new IllegalArgumentException( "resolution request has not been specified" ); 50 } 51 this.request = request; 52 exceptions = Collections.emptyList(); 53 } 54 55 /** 56 * Gets the resolution request that was made. 57 * 58 * @return The resolution request, never {@code null}. 59 */ 60 public ArtifactRequest getRequest() 61 { 62 return request; 63 } 64 65 /** 66 * Gets the resolved artifact (if any). Use {@link #getExceptions()} to query the errors that occurred while trying 67 * to resolve the artifact. 68 * 69 * @return The resolved artifact or {@code null} if the resolution failed. 70 */ 71 public Artifact getArtifact() 72 { 73 return artifact; 74 } 75 76 /** 77 * Sets the resolved artifact. 78 * 79 * @param artifact The resolved artifact, may be {@code null} if the resolution failed. 80 * @return This result for chaining, never {@code null}. 81 */ 82 public ArtifactResult setArtifact( Artifact artifact ) 83 { 84 this.artifact = artifact; 85 return this; 86 } 87 88 /** 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. 92 * 93 * @return The exceptions that occurred, never {@code null}. 94 * @see #isResolved() 95 */ 96 public List<Exception> getExceptions() 97 { 98 return exceptions; 99 } 100 101 /** 102 * Records the specified exception while resolving the artifact. 103 * 104 * @param exception The exception to record, may be {@code null}. 105 * @return This result for chaining, never {@code null}. 106 */ 107 public ArtifactResult addException( Exception exception ) 108 { 109 if ( exception != null ) 110 { 111 if ( exceptions.isEmpty() ) 112 { 113 exceptions = new ArrayList<Exception>(); 114 } 115 exceptions.add( exception ); 116 } 117 return this; 118 } 119 120 /** 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. 123 * 124 * @return The repository from which the artifact was resolved or {@code null} if unknown. 125 */ 126 public ArtifactRepository getRepository() 127 { 128 return repository; 129 } 130 131 /** 132 * Sets the repository from which the artifact was resolved. 133 * 134 * @param repository The repository from which the artifact was resolved, may be {@code null}. 135 * @return This result for chaining, never {@code null}. 136 */ 137 public ArtifactResult setRepository( ArtifactRepository repository ) 138 { 139 this.repository = repository; 140 return this; 141 } 142 143 /** 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. 147 * 148 * @return {@code true} if the artifact was resolved, {@code false} otherwise. 149 * @see Artifact#getFile() 150 */ 151 public boolean isResolved() 152 { 153 return getArtifact() != null && getArtifact().getFile() != null; 154 } 155 156 /** 157 * Indicates whether the requested artifact is not present in any of the specified repositories. 158 * 159 * @return {@code true} if the artifact is not present in any repository, {@code false} otherwise. 160 */ 161 public boolean isMissing() 162 { 163 for ( Exception e : getExceptions() ) 164 { 165 if ( !( e instanceof ArtifactNotFoundException ) ) 166 { 167 return false; 168 } 169 } 170 return !isResolved(); 171 } 172 173 @Override 174 public String toString() 175 { 176 return getArtifact() + " < " + getRepository(); 177 } 178 179 }