1 package org.eclipse.aether.resolution; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.util.ArrayList; 23 import java.util.Collections; 24 import java.util.List; 25 import static java.util.Objects.requireNonNull; 26 27 import org.eclipse.aether.RepositorySystem; 28 import org.eclipse.aether.artifact.Artifact; 29 import org.eclipse.aether.repository.ArtifactRepository; 30 import org.eclipse.aether.transfer.ArtifactNotFoundException; 31 32 /** 33 * The result of an artifact resolution request. 34 * 35 * @see RepositorySystem#resolveArtifacts(org.eclipse.aether.RepositorySystemSession, java.util.Collection) 36 * @see Artifact#getFile() 37 */ 38 public final class ArtifactResult 39 { 40 41 private final ArtifactRequest request; 42 43 private List<Exception> exceptions; 44 45 private Artifact artifact; 46 47 private ArtifactRepository repository; 48 49 /** 50 * Creates a new result for the specified request. 51 * 52 * @param request The resolution request, must not be {@code null}. 53 */ 54 public ArtifactResult( ArtifactRequest request ) 55 { 56 this.request = requireNonNull( request, "artifact request cannot be null" ); 57 exceptions = Collections.emptyList(); 58 } 59 60 /** 61 * Gets the resolution request that was made. 62 * 63 * @return The resolution request, never {@code null}. 64 */ 65 public ArtifactRequest getRequest() 66 { 67 return request; 68 } 69 70 /** 71 * Gets the resolved artifact (if any). Use {@link #getExceptions()} to query the errors that occurred while trying 72 * to resolve the artifact. 73 * 74 * @return The resolved artifact or {@code null} if the resolution failed. 75 */ 76 public Artifact getArtifact() 77 { 78 return artifact; 79 } 80 81 /** 82 * Sets the resolved artifact. 83 * 84 * @param artifact The resolved artifact, may be {@code null} if the resolution failed. 85 * @return This result for chaining, never {@code null}. 86 */ 87 public ArtifactResult setArtifact( Artifact artifact ) 88 { 89 this.artifact = artifact; 90 return this; 91 } 92 93 /** 94 * Gets the exceptions that occurred while resolving the artifact. Note that this list can be non-empty even if the 95 * artifact was successfully resolved, e.g. when one of the contacted remote repositories didn't contain the 96 * artifact but a later repository eventually contained it. 97 * 98 * @return The exceptions that occurred, never {@code null}. 99 * @see #isResolved() 100 */ 101 public List<Exception> getExceptions() 102 { 103 return exceptions; 104 } 105 106 /** 107 * Records the specified exception while resolving the artifact. 108 * 109 * @param exception The exception to record, may be {@code null}. 110 * @return This result for chaining, never {@code null}. 111 */ 112 public ArtifactResult addException( Exception exception ) 113 { 114 if ( exception != null ) 115 { 116 if ( exceptions.isEmpty() ) 117 { 118 exceptions = new ArrayList<>(); 119 } 120 exceptions.add( exception ); 121 } 122 return this; 123 } 124 125 /** 126 * Gets the repository from which the artifact was eventually resolved. Note that successive resolutions of the same 127 * artifact might yield different results if the employed local repository does not track the origin of an artifact. 128 * 129 * @return The repository from which the artifact was resolved or {@code null} if unknown. 130 */ 131 public ArtifactRepository getRepository() 132 { 133 return repository; 134 } 135 136 /** 137 * Sets the repository from which the artifact was resolved. 138 * 139 * @param repository The repository from which the artifact was resolved, may be {@code null}. 140 * @return This result for chaining, never {@code null}. 141 */ 142 public ArtifactResult setRepository( ArtifactRepository repository ) 143 { 144 this.repository = repository; 145 return this; 146 } 147 148 /** 149 * Indicates whether the requested artifact was resolved. Note that the artifact might have been successfully 150 * resolved despite {@link #getExceptions()} indicating transfer errors while trying to fetch the artifact from some 151 * of the specified remote repositories. 152 * 153 * @return {@code true} if the artifact was resolved, {@code false} otherwise. 154 * @see Artifact#getFile() 155 */ 156 public boolean isResolved() 157 { 158 return getArtifact() != null && getArtifact().getFile() != null; 159 } 160 161 /** 162 * Indicates whether the requested artifact is not present in any of the specified repositories. 163 * 164 * @return {@code true} if the artifact is not present in any repository, {@code false} otherwise. 165 */ 166 public boolean isMissing() 167 { 168 for ( Exception e : getExceptions() ) 169 { 170 if ( !( e instanceof ArtifactNotFoundException ) ) 171 { 172 return false; 173 } 174 } 175 return !isResolved(); 176 } 177 178 @Override 179 public String toString() 180 { 181 return getArtifact() + " < " + getRepository(); 182 } 183 184 }