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.artifact.Artifact; 029import org.eclipse.aether.repository.ArtifactRepository; 030import org.eclipse.aether.transfer.ArtifactNotFoundException; 031 032/** 033 * The result of an artifact resolution request. 034 * 035 * @see RepositorySystem#resolveArtifacts(org.eclipse.aether.RepositorySystemSession, java.util.Collection) 036 * @see Artifact#getFile() 037 */ 038public final class ArtifactResult 039{ 040 041 private final ArtifactRequest request; 042 043 private List<Exception> exceptions; 044 045 private Artifact artifact; 046 047 private ArtifactRepository repository; 048 049 /** 050 * Creates a new result for the specified request. 051 * 052 * @param request The resolution request, must not be {@code null}. 053 */ 054 public ArtifactResult( ArtifactRequest request ) 055 { 056 this.request = requireNonNull( request, "artifact request cannot be null" ); 057 exceptions = Collections.emptyList(); 058 } 059 060 /** 061 * Gets the resolution request that was made. 062 * 063 * @return The resolution request, never {@code null}. 064 */ 065 public ArtifactRequest getRequest() 066 { 067 return request; 068 } 069 070 /** 071 * Gets the resolved artifact (if any). Use {@link #getExceptions()} to query the errors that occurred while trying 072 * to resolve the artifact. 073 * 074 * @return The resolved artifact or {@code null} if the resolution failed. 075 */ 076 public Artifact getArtifact() 077 { 078 return artifact; 079 } 080 081 /** 082 * Sets the resolved artifact. 083 * 084 * @param artifact The resolved artifact, may be {@code null} if the resolution failed. 085 * @return This result for chaining, never {@code null}. 086 */ 087 public ArtifactResult setArtifact( Artifact artifact ) 088 { 089 this.artifact = artifact; 090 return this; 091 } 092 093 /** 094 * Gets the exceptions that occurred while resolving the artifact. Note that this list can be non-empty even if the 095 * artifact was successfully resolved, e.g. when one of the contacted remote repositories didn't contain the 096 * artifact but a later repository eventually contained it. 097 * 098 * @return The exceptions that occurred, never {@code null}. 099 * @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}