001package org.eclipse.aether.resolution; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.ArrayList; 023import java.util.Collections; 024import java.util.List; 025import static java.util.Objects.requireNonNull; 026 027import org.eclipse.aether.RepositorySystem; 028import org.eclipse.aether.RepositorySystemSession; 029import org.eclipse.aether.artifact.Artifact; 030import org.eclipse.aether.repository.ArtifactRepository; 031import org.eclipse.aether.transfer.ArtifactNotFoundException; 032 033/** 034 * The result of an artifact resolution request. 035 * 036 * @see RepositorySystem#resolveArtifacts(RepositorySystemSession, java.util.Collection) 037 * @see Artifact#getFile() 038 */ 039public final class ArtifactResult 040{ 041 042 private final ArtifactRequest request; 043 044 private List<Exception> exceptions; 045 046 private Artifact artifact; 047 048 private ArtifactRepository repository; 049 050 /** 051 * Creates a new result for the specified request. 052 * 053 * @param request The resolution request, must not be {@code null}. 054 */ 055 public ArtifactResult( ArtifactRequest request ) 056 { 057 this.request = requireNonNull( request, "artifact request cannot be null" ); 058 exceptions = Collections.emptyList(); 059 } 060 061 /** 062 * Gets the resolution request that was made. 063 * 064 * @return The resolution request, never {@code null}. 065 */ 066 public ArtifactRequest getRequest() 067 { 068 return request; 069 } 070 071 /** 072 * Gets the resolved artifact (if any). Use {@link #getExceptions()} to query the errors that occurred while trying 073 * to resolve the artifact. 074 * 075 * @return The resolved artifact or {@code null} if the resolution failed. 076 */ 077 public Artifact getArtifact() 078 { 079 return artifact; 080 } 081 082 /** 083 * Sets the resolved artifact. 084 * 085 * @param artifact The resolved artifact, may be {@code null} if the resolution failed. 086 * @return This result for chaining, never {@code null}. 087 */ 088 public ArtifactResult setArtifact( Artifact artifact ) 089 { 090 this.artifact = artifact; 091 return this; 092 } 093 094 /** 095 * Gets the exceptions that occurred while resolving the artifact. Note that this list can be non-empty even if the 096 * artifact was successfully resolved, e.g. when one of the contacted remote repositories didn't contain the 097 * artifact but a later repository eventually contained it. 098 * 099 * @return The exceptions that occurred, never {@code null}. 100 * @see #isResolved() 101 */ 102 public List<Exception> getExceptions() 103 { 104 return exceptions; 105 } 106 107 /** 108 * Records the specified exception while resolving the artifact. 109 * 110 * @param exception The exception to record, may be {@code null}. 111 * @return This result for chaining, never {@code null}. 112 */ 113 public ArtifactResult addException( Exception exception ) 114 { 115 if ( exception != null ) 116 { 117 if ( exceptions.isEmpty() ) 118 { 119 exceptions = new ArrayList<Exception>(); 120 } 121 exceptions.add( exception ); 122 } 123 return this; 124 } 125 126 /** 127 * Gets the repository from which the artifact was eventually resolved. Note that successive resolutions of the same 128 * artifact might yield different results if the employed local repository does not track the origin of an artifact. 129 * 130 * @return The repository from which the artifact was resolved or {@code null} if unknown. 131 */ 132 public ArtifactRepository getRepository() 133 { 134 return repository; 135 } 136 137 /** 138 * Sets the repository from which the artifact was resolved. 139 * 140 * @param repository The repository from which the artifact was resolved, may be {@code null}. 141 * @return This result for chaining, never {@code null}. 142 */ 143 public ArtifactResult setRepository( ArtifactRepository repository ) 144 { 145 this.repository = repository; 146 return this; 147 } 148 149 /** 150 * Indicates whether the requested artifact was resolved. Note that the artifact might have been successfully 151 * resolved despite {@link #getExceptions()} indicating transfer errors while trying to fetch the artifact from some 152 * of the specified remote repositories. 153 * 154 * @return {@code true} if the artifact was resolved, {@code false} otherwise. 155 * @see Artifact#getFile() 156 */ 157 public boolean isResolved() 158 { 159 return getArtifact() != null && getArtifact().getFile() != null; 160 } 161 162 /** 163 * Indicates whether the requested artifact is not present in any of the specified repositories. 164 * 165 * @return {@code true} if the artifact is not present in any repository, {@code false} otherwise. 166 */ 167 public boolean isMissing() 168 { 169 for ( Exception e : getExceptions() ) 170 { 171 if ( !( e instanceof ArtifactNotFoundException ) ) 172 { 173 return false; 174 } 175 } 176 return !isResolved(); 177 } 178 179 @Override 180 public String toString() 181 { 182 return getArtifact() + " < " + getRepository(); 183 } 184 185}