1 /******************************************************************************* 2 * Copyright (c) 2010, 2011 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.repository; 12 13 import java.io.File; 14 15 import org.eclipse.aether.RepositorySystemSession; 16 17 /** 18 * A result from the local repository about the existence of an artifact. 19 * 20 * @see LocalRepositoryManager#find(RepositorySystemSession, LocalArtifactRequest) 21 */ 22 public final class LocalArtifactResult 23 { 24 25 private final LocalArtifactRequest request; 26 27 private File file; 28 29 private boolean available; 30 31 private RemoteRepository repository; 32 33 /** 34 * Creates a new result for the specified request. 35 * 36 * @param request The local artifact request, must not be {@code null}. 37 */ 38 public LocalArtifactResult( LocalArtifactRequest request ) 39 { 40 if ( request == null ) 41 { 42 throw new IllegalArgumentException( "local artifact request has not been specified" ); 43 } 44 this.request = request; 45 } 46 47 /** 48 * Gets the request corresponding to this result. 49 * 50 * @return The corresponding request, never {@code null}. 51 */ 52 public LocalArtifactRequest getRequest() 53 { 54 return request; 55 } 56 57 /** 58 * Gets the file to the requested artifact. Note that this file must not be used unless {@link #isAvailable()} 59 * returns {@code true}. An artifact file can be found but considered unavailable if the artifact was cached from a 60 * remote repository that is not part of the list of remote repositories used for the query. 61 * 62 * @return The file to the requested artifact or {@code null} if the artifact does not exist locally. 63 */ 64 public File getFile() 65 { 66 return file; 67 } 68 69 /** 70 * Sets the file to requested artifact. 71 * 72 * @param file The artifact file, may be {@code null}. 73 * @return This result for chaining, never {@code null}. 74 */ 75 public LocalArtifactResult setFile( File file ) 76 { 77 this.file = file; 78 return this; 79 } 80 81 /** 82 * Indicates whether the requested artifact is available for use. As a minimum, the file needs to be physically 83 * existent in the local repository to be available. Additionally, a local repository manager can consider the list 84 * of supplied remote repositories to determine whether the artifact is logically available and mark an artifact 85 * unavailable (despite its physical existence) if it is not known to be hosted by any of the provided repositories. 86 * 87 * @return {@code true} if the artifact is available, {@code false} otherwise. 88 * @see LocalArtifactRequest#getRepositories() 89 */ 90 public boolean isAvailable() 91 { 92 return available; 93 } 94 95 /** 96 * Sets whether the artifact is available. 97 * 98 * @param available {@code true} if the artifact is available, {@code false} otherwise. 99 * @return This result for chaining, never {@code null}. 100 */ 101 public LocalArtifactResult setAvailable( boolean available ) 102 { 103 this.available = available; 104 return this; 105 } 106 107 /** 108 * Gets the (first) remote repository from which the artifact was cached (if any). 109 * 110 * @return The remote repository from which the artifact was originally retrieved or {@code null} if unknown or if 111 * the artifact has been locally installed. 112 * @see LocalArtifactRequest#getRepositories() 113 */ 114 public RemoteRepository getRepository() 115 { 116 return repository; 117 } 118 119 /** 120 * Sets the (first) remote repository from which the artifact was cached. 121 * 122 * @param repository The remote repository from which the artifact was originally retrieved, may be {@code null} if 123 * unknown or if the artifact has been locally installed. 124 * @return This result for chaining, never {@code null}. 125 */ 126 public LocalArtifactResult setRepository( RemoteRepository repository ) 127 { 128 this.repository = repository; 129 return this; 130 } 131 132 @Override 133 public String toString() 134 { 135 return getFile() + " (" + ( isAvailable() ? "available" : "unavailable" ) + ")"; 136 } 137 138 }