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; 025 026import org.eclipse.aether.RepositorySystem; 027import org.eclipse.aether.RepositorySystemSession; 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(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 if ( request == null ) 057 { 058 throw new IllegalArgumentException( "resolution request has not been specified" ); 059 } 060 this.request = request; 061 exceptions = Collections.emptyList(); 062 } 063 064 /** 065 * Gets the resolution request that was made. 066 * 067 * @return The resolution request, never {@code null}. 068 */ 069 public ArtifactRequest getRequest() 070 { 071 return request; 072 } 073 074 /** 075 * Gets the resolved artifact (if any). Use {@link #getExceptions()} to query the errors that occurred while trying 076 * to resolve the artifact. 077 * 078 * @return The resolved artifact or {@code null} if the resolution failed. 079 */ 080 public Artifact getArtifact() 081 { 082 return artifact; 083 } 084 085 /** 086 * Sets the resolved artifact. 087 * 088 * @param artifact The resolved artifact, may be {@code null} if the resolution failed. 089 * @return This result for chaining, never {@code null}. 090 */ 091 public ArtifactResult setArtifact( Artifact artifact ) 092 { 093 this.artifact = artifact; 094 return this; 095 } 096 097 /** 098 * Gets the exceptions that occurred while resolving the artifact. Note that this list can be non-empty even if the 099 * artifact was successfully resolved, e.g. when one of the contacted remote repositories didn't contain the 100 * artifact but a later repository eventually contained it. 101 * 102 * @return The exceptions that occurred, never {@code null}. 103 * @see #isResolved() 104 */ 105 public List<Exception> getExceptions() 106 { 107 return exceptions; 108 } 109 110 /** 111 * Records the specified exception while resolving the artifact. 112 * 113 * @param exception The exception to record, may be {@code null}. 114 * @return This result for chaining, never {@code null}. 115 */ 116 public ArtifactResult addException( Exception exception ) 117 { 118 if ( exception != null ) 119 { 120 if ( exceptions.isEmpty() ) 121 { 122 exceptions = new ArrayList<Exception>(); 123 } 124 exceptions.add( exception ); 125 } 126 return this; 127 } 128 129 /** 130 * Gets the repository from which the artifact was eventually resolved. Note that successive resolutions of the same 131 * artifact might yield different results if the employed local repository does not track the origin of an artifact. 132 * 133 * @return The repository from which the artifact was resolved or {@code null} if unknown. 134 */ 135 public ArtifactRepository getRepository() 136 { 137 return repository; 138 } 139 140 /** 141 * Sets the repository from which the artifact was resolved. 142 * 143 * @param repository The repository from which the artifact was resolved, may be {@code null}. 144 * @return This result for chaining, never {@code null}. 145 */ 146 public ArtifactResult setRepository( ArtifactRepository repository ) 147 { 148 this.repository = repository; 149 return this; 150 } 151 152 /** 153 * Indicates whether the requested artifact was resolved. Note that the artifact might have been successfully 154 * resolved despite {@link #getExceptions()} indicating transfer errors while trying to fetch the artifact from some 155 * of the specified remote repositories. 156 * 157 * @return {@code true} if the artifact was resolved, {@code false} otherwise. 158 * @see Artifact#getFile() 159 */ 160 public boolean isResolved() 161 { 162 return getArtifact() != null && getArtifact().getFile() != null; 163 } 164 165 /** 166 * Indicates whether the requested artifact is not present in any of the specified repositories. 167 * 168 * @return {@code true} if the artifact is not present in any repository, {@code false} otherwise. 169 */ 170 public boolean isMissing() 171 { 172 for ( Exception e : getExceptions() ) 173 { 174 if ( !( e instanceof ArtifactNotFoundException ) ) 175 { 176 return false; 177 } 178 } 179 return !isResolved(); 180 } 181 182 @Override 183 public String toString() 184 { 185 return getArtifact() + " < " + getRepository(); 186 } 187 188}