001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.impl;
020
021import java.io.File;
022
023import org.eclipse.aether.RepositoryException;
024import org.eclipse.aether.repository.RemoteRepository;
025
026/**
027 * A request to check if an update of an artifact/metadata from a remote repository is needed.
028 *
029 * @param <T>
030 * @param <E>
031 * @see UpdateCheckManager
032 * @provisional This type is provisional and can be changed, moved or removed without prior notice.
033 */
034public final class UpdateCheck<T, E extends RepositoryException> {
035
036    private long localLastUpdated;
037
038    private T item;
039
040    private File file;
041
042    private boolean fileValid = true;
043
044    private String artifactPolicy;
045
046    private String metadataPolicy;
047
048    private RemoteRepository repository;
049
050    private RemoteRepository authoritativeRepository;
051
052    private boolean required;
053
054    private E exception;
055
056    /**
057     * Creates an uninitialized update check request.
058     */
059    public UpdateCheck() {}
060
061    /**
062     * Gets the last-modified timestamp of the corresponding item produced by a local installation. If non-zero, a
063     * remote update will be surpressed if the local item is up-to-date, even if the remote item has not been cached
064     * locally.
065     *
066     * @return The last-modified timestamp of the corresponding item produced by a local installation or {@code 0} to
067     *         ignore any local item.
068     */
069    public long getLocalLastUpdated() {
070        return localLastUpdated;
071    }
072
073    /**
074     * Sets the last-modified timestamp of the corresponding item produced by a local installation. If non-zero, a
075     * remote update will be surpressed if the local item is up-to-date, even if the remote item has not been cached
076     * locally.
077     *
078     * @param localLastUpdated The last-modified timestamp of the corresponding item produced by a local installation or
079     *            {@code 0} to ignore any local item.
080     * @return This object for chaining.
081     */
082    public UpdateCheck<T, E> setLocalLastUpdated(long localLastUpdated) {
083        this.localLastUpdated = localLastUpdated;
084        return this;
085    }
086
087    /**
088     * Gets the item of the check.
089     *
090     * @return The item of the check, never {@code null}.
091     */
092    public T getItem() {
093        return item;
094    }
095
096    /**
097     * Sets the item of the check.
098     *
099     * @param item The item of the check, must not be {@code null}.
100     * @return This object for chaining.
101     */
102    public UpdateCheck<T, E> setItem(T item) {
103        this.item = item;
104        return this;
105    }
106
107    /**
108     * Returns the local file of the item.
109     *
110     * @return The local file of the item.
111     */
112    public File getFile() {
113        return file;
114    }
115
116    /**
117     * Sets the local file of the item.
118     *
119     * @param file The file of the item, never {@code null} .
120     * @return This object for chaining.
121     */
122    public UpdateCheck<T, E> setFile(File file) {
123        this.file = file;
124        return this;
125    }
126
127    /**
128     * Indicates whether the local file given by {@link #getFile()}, if existent, should be considered valid or not. An
129     * invalid file is equivalent to a physically missing file.
130     *
131     * @return {@code true} if the file should be considered valid if existent, {@code false} if the file should be
132     *         treated as if it was missing.
133     */
134    public boolean isFileValid() {
135        return fileValid;
136    }
137
138    /**
139     * Controls whether the local file given by {@link #getFile()}, if existent, should be considered valid or not. An
140     * invalid file is equivalent to a physically missing file.
141     *
142     * @param fileValid {@code true} if the file should be considered valid if existent, {@code false} if the file
143     *            should be treated as if it was missing.
144     * @return This object for chaining.
145     */
146    public UpdateCheck<T, E> setFileValid(boolean fileValid) {
147        this.fileValid = fileValid;
148        return this;
149    }
150
151    /**
152     * Gets the policy to use for the artifact check.
153     *
154     * @return The policy to use for the artifact check.
155     * @see org.eclipse.aether.repository.RepositoryPolicy
156     * @since 2.0.0
157     */
158    public String getArtifactPolicy() {
159        return artifactPolicy;
160    }
161
162    /**
163     * Gets the policy to use for the metadata check.
164     *
165     * @return The policy to use for the metadata check.
166     * @see org.eclipse.aether.repository.RepositoryPolicy
167     * @since 2.0.0
168     */
169    public String getMetadataPolicy() {
170        return metadataPolicy;
171    }
172
173    /**
174     * Sets the artifact policy to use for the check.
175     *
176     * @param artifactPolicy The policy to use for the artifact check, may be {@code null}.
177     * @return This object for chaining.
178     * @see org.eclipse.aether.repository.RepositoryPolicy
179     * @since 2.0.0
180     */
181    public UpdateCheck<T, E> setArtifactPolicy(String artifactPolicy) {
182        this.artifactPolicy = artifactPolicy;
183        return this;
184    }
185
186    /**
187     * Sets the metadata policy to use for the check.
188     *
189     * @param metadataPolicy The policy to use for the metadata check, may be {@code null}.
190     * @return This object for chaining.
191     * @see org.eclipse.aether.repository.RepositoryPolicy
192     * @since 2.0.0
193     */
194    public UpdateCheck<T, E> setMetadataPolicy(String metadataPolicy) {
195        this.metadataPolicy = metadataPolicy;
196        return this;
197    }
198
199    /**
200     * Gets the repository from which a potential update/download will performed.
201     *
202     * @return The repository to use for the check.
203     */
204    public RemoteRepository getRepository() {
205        return repository;
206    }
207
208    /**
209     * Sets the repository from which a potential update/download will performed.
210     *
211     * @param repository The repository to use for the check, must not be {@code null}.
212     * @return This object for chaining.
213     */
214    public UpdateCheck<T, E> setRepository(RemoteRepository repository) {
215        this.repository = repository;
216        return this;
217    }
218
219    /**
220     * Gets the repository which ultimately hosts the metadata to update. This will be different from the repository
221     * given by {@link #getRepository()} in case the latter denotes a repository manager.
222     *
223     * @return The actual repository hosting the authoritative copy of the metadata to update, never {@code null} for a
224     *         metadata update check.
225     */
226    public RemoteRepository getAuthoritativeRepository() {
227        return authoritativeRepository != null ? authoritativeRepository : repository;
228    }
229
230    /**
231     * Sets the repository which ultimately hosts the metadata to update. This will be different from the repository
232     * given by {@link #getRepository()} in case the latter denotes a repository manager.
233     *
234     * @param authoritativeRepository The actual repository hosting the authoritative copy of the metadata to update,
235     *            must not be {@code null} for a metadata update check.
236     * @return This object for chaining.
237     */
238    public UpdateCheck<T, E> setAuthoritativeRepository(RemoteRepository authoritativeRepository) {
239        this.authoritativeRepository = authoritativeRepository;
240        return this;
241    }
242
243    /**
244     * Gets the result of a check, denoting whether the remote repository should be checked for updates.
245     *
246     * @return The result of a check.
247     */
248    public boolean isRequired() {
249        return required;
250    }
251
252    /**
253     * Sets the result of an update check.
254     *
255     * @param required The result of an update check. In case of {@code false} and the local file given by
256     *            {@link #getFile()} does actually not exist, {@link #setException(RepositoryException)} should be used
257     *            to provide the previous/cached failure that explains the absence of the file.
258     * @return This object for chaining.
259     */
260    public UpdateCheck<T, E> setRequired(boolean required) {
261        this.required = required;
262        return this;
263    }
264
265    /**
266     * Gets the exception that occurred during the update check.
267     *
268     * @return The occurred exception or {@code null} if the update check was successful.
269     */
270    public E getException() {
271        return exception;
272    }
273
274    /**
275     * Sets the exception for this update check.
276     *
277     * @param exception The exception for this update check, may be {@code null} if the check was successful.
278     * @return This object for chaining.
279     */
280    public UpdateCheck<T, E> setException(E exception) {
281        this.exception = exception;
282        return this;
283    }
284
285    @Override
286    public String toString() {
287        return getArtifactPolicy() + "/" + getMetadataPolicy() + ": " + getFile() + " < " + getRepository();
288    }
289}