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.resolution; 12 13 import org.eclipse.aether.RepositorySystem; 14 import org.eclipse.aether.RepositorySystemSession; 15 import org.eclipse.aether.metadata.Metadata; 16 import org.eclipse.aether.transfer.MetadataNotFoundException; 17 18 /** 19 * The result of a metadata resolution request. 20 * 21 * @see RepositorySystem#resolveMetadata(RepositorySystemSession, java.util.Collection) 22 */ 23 public final class MetadataResult 24 { 25 26 private final MetadataRequest request; 27 28 private Exception exception; 29 30 private boolean updated; 31 32 private Metadata metadata; 33 34 /** 35 * Creates a new result for the specified request. 36 * 37 * @param request The resolution request, must not be {@code null}. 38 */ 39 public MetadataResult( MetadataRequest request ) 40 { 41 if ( request == null ) 42 { 43 throw new IllegalArgumentException( "metadata request has not been specified" ); 44 } 45 this.request = request; 46 } 47 48 /** 49 * Gets the resolution request that was made. 50 * 51 * @return The resolution request, never {@code null}. 52 */ 53 public MetadataRequest getRequest() 54 { 55 return request; 56 } 57 58 /** 59 * Gets the resolved metadata (if any). 60 * 61 * @return The resolved metadata or {@code null} if the resolution failed. 62 */ 63 public Metadata getMetadata() 64 { 65 return metadata; 66 } 67 68 /** 69 * Sets the resolved metadata. 70 * 71 * @param metadata The resolved metadata, may be {@code null} if the resolution failed. 72 * @return This result for chaining, never {@code null}. 73 */ 74 public MetadataResult setMetadata( Metadata metadata ) 75 { 76 this.metadata = metadata; 77 return this; 78 } 79 80 /** 81 * Records the specified exception while resolving the metadata. 82 * 83 * @param exception The exception to record, may be {@code null}. 84 * @return This result for chaining, never {@code null}. 85 */ 86 public MetadataResult setException( Exception exception ) 87 { 88 this.exception = exception; 89 return this; 90 } 91 92 /** 93 * Gets the exception that occurred while resolving the metadata. 94 * 95 * @return The exception that occurred or {@code null} if none. 96 */ 97 public Exception getException() 98 { 99 return exception; 100 } 101 102 /** 103 * Sets the updated flag for the metadata. 104 * 105 * @param updated {@code true} if the metadata was actually fetched from the remote repository during the 106 * resolution, {@code false} if the metadata was resolved from a locally cached copy. 107 * @return This result for chaining, never {@code null}. 108 */ 109 public MetadataResult setUpdated( boolean updated ) 110 { 111 this.updated = updated; 112 return this; 113 } 114 115 /** 116 * Indicates whether the metadata was actually fetched from the remote repository or resolved from the local cache. 117 * If metadata has been locally cached during a previous resolution request and this local copy is still up-to-date 118 * according to the remote repository's update policy, no remote access is made. 119 * 120 * @return {@code true} if the metadata was actually fetched from the remote repository during the resolution, 121 * {@code false} if the metadata was resolved from a locally cached copy. 122 */ 123 public boolean isUpdated() 124 { 125 return updated; 126 } 127 128 /** 129 * Indicates whether the requested metadata was resolved. Note that the metadata might have been successfully 130 * resolved (from the local cache) despite {@link #getException()} indicating a transfer error while trying to 131 * refetch the metadata from the remote repository. 132 * 133 * @return {@code true} if the metadata was resolved, {@code false} otherwise. 134 * @see Metadata#getFile() 135 */ 136 public boolean isResolved() 137 { 138 return getMetadata() != null && getMetadata().getFile() != null; 139 } 140 141 /** 142 * Indicates whether the requested metadata is not present in the remote repository. 143 * 144 * @return {@code true} if the metadata is not present in the remote repository, {@code false} otherwise. 145 */ 146 public boolean isMissing() 147 { 148 return getException() instanceof MetadataNotFoundException; 149 } 150 151 @Override 152 public String toString() 153 { 154 return getMetadata() + ( isUpdated() ? " (updated)" : " (cached)" ); 155 } 156 157 }