1 package org.eclipse.aether.resolution;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.eclipse.aether.RepositorySystem;
23 import org.eclipse.aether.RepositorySystemSession;
24 import org.eclipse.aether.RequestTrace;
25 import org.eclipse.aether.metadata.Metadata;
26 import org.eclipse.aether.repository.RemoteRepository;
27
28 /**
29 * A request to resolve metadata from either a remote repository or the local repository.
30 *
31 * @see RepositorySystem#resolveMetadata(RepositorySystemSession, java.util.Collection)
32 * @see Metadata#getFile()
33 */
34 public final class MetadataRequest
35 {
36
37 private Metadata metadata;
38
39 private RemoteRepository repository;
40
41 private String context = "";
42
43 private boolean deleteLocalCopyIfMissing;
44
45 private boolean favorLocalRepository;
46
47 private RequestTrace trace;
48
49 /**
50 * Creates an uninitialized request.
51 */
52 public MetadataRequest()
53 {
54 // enables default constructor
55 }
56
57 /**
58 * Creates a request to resolve the specified metadata from the local repository.
59 *
60 * @param metadata The metadata to resolve, may be {@code null}.
61 */
62 public MetadataRequest( Metadata metadata )
63 {
64 setMetadata( metadata );
65 }
66
67 /**
68 * Creates a request with the specified properties.
69 *
70 * @param metadata The metadata to resolve, may be {@code null}.
71 * @param repository The repository to resolve the metadata from, may be {@code null} to resolve from the local
72 * repository.
73 * @param context The context in which this request is made, may be {@code null}.
74 */
75 public MetadataRequest( Metadata metadata, RemoteRepository repository, String context )
76 {
77 setMetadata( metadata );
78 setRepository( repository );
79 setRequestContext( context );
80 }
81
82 /**
83 * Gets the metadata to resolve.
84 *
85 * @return The metadata or {@code null} if not set.
86 */
87 public Metadata getMetadata()
88 {
89 return metadata;
90 }
91
92 /**
93 * Sets the metadata to resolve.
94 *
95 * @param metadata The metadata, may be {@code null}.
96 * @return This request for chaining, never {@code null}.
97 */
98 public MetadataRequest setMetadata( Metadata metadata )
99 {
100 this.metadata = metadata;
101 return this;
102 }
103
104 /**
105 * Gets the repository from which the metadata should be resolved.
106 *
107 * @return The repository or {@code null} to resolve from the local repository.
108 */
109 public RemoteRepository getRepository()
110 {
111 return repository;
112 }
113
114 /**
115 * Sets the repository from which the metadata should be resolved.
116 *
117 * @param repository The repository, may be {@code null} to resolve from the local repository.
118 * @return This request for chaining, never {@code null}.
119 */
120 public MetadataRequest setRepository( RemoteRepository repository )
121 {
122 this.repository = repository;
123 return this;
124 }
125
126 /**
127 * Gets the context in which this request is made.
128 *
129 * @return The context, never {@code null}.
130 */
131 public String getRequestContext()
132 {
133 return context;
134 }
135
136 /**
137 * Sets the context in which this request is made.
138 *
139 * @param context The context, may be {@code null}.
140 * @return This request for chaining, never {@code null}.
141 */
142 public MetadataRequest setRequestContext( String context )
143 {
144 this.context = ( context != null ) ? context : "";
145 return this;
146 }
147
148 /**
149 * Indicates whether the locally cached copy of the metadata should be removed if the corresponding file does not
150 * exist (any more) in the remote repository.
151 *
152 * @return {@code true} if locally cached metadata should be deleted if no corresponding remote file exists,
153 * {@code false} to keep the local copy.
154 */
155 public boolean isDeleteLocalCopyIfMissing()
156 {
157 return deleteLocalCopyIfMissing;
158 }
159
160 /**
161 * Controls whether the locally cached copy of the metadata should be removed if the corresponding file does not
162 * exist (any more) in the remote repository.
163 *
164 * @param deleteLocalCopyIfMissing {@code true} if locally cached metadata should be deleted if no corresponding
165 * remote file exists, {@code false} to keep the local copy.
166 * @return This request for chaining, never {@code null}.
167 */
168 public MetadataRequest setDeleteLocalCopyIfMissing( boolean deleteLocalCopyIfMissing )
169 {
170 this.deleteLocalCopyIfMissing = deleteLocalCopyIfMissing;
171 return this;
172 }
173
174 /**
175 * Indicates whether the metadata resolution should be suppressed if the corresponding metadata of the local
176 * repository is up-to-date according to the update policy of the remote repository. In this case, the metadata
177 * resolution will even be suppressed if no local copy of the remote metadata exists yet.
178 *
179 * @return {@code true} to suppress resolution of remote metadata if the corresponding metadata of the local
180 * repository is up-to-date, {@code false} to resolve the remote metadata normally according to the update
181 * policy.
182 */
183 public boolean isFavorLocalRepository()
184 {
185 return favorLocalRepository;
186 }
187
188 /**
189 * Controls resolution of remote metadata when already corresponding metadata of the local repository exists. In
190 * cases where the local repository's metadata is sufficient and going to be preferred, resolution of the remote
191 * metadata can be suppressed to avoid unnecessary network access.
192 *
193 * @param favorLocalRepository {@code true} to suppress resolution of remote metadata if the corresponding metadata
194 * of the local repository is up-to-date, {@code false} to resolve the remote metadata normally according
195 * to the update policy.
196 * @return This request for chaining, never {@code null}.
197 */
198 public MetadataRequest setFavorLocalRepository( boolean favorLocalRepository )
199 {
200 this.favorLocalRepository = favorLocalRepository;
201 return this;
202 }
203
204 /**
205 * Gets the trace information that describes the higher level request/operation in which this request is issued.
206 *
207 * @return The trace information about the higher level operation or {@code null} if none.
208 */
209 public RequestTrace getTrace()
210 {
211 return trace;
212 }
213
214 /**
215 * Sets the trace information that describes the higher level request/operation in which this request is issued.
216 *
217 * @param trace The trace information about the higher level operation, may be {@code null}.
218 * @return This request for chaining, never {@code null}.
219 */
220 public MetadataRequest setTrace( RequestTrace trace )
221 {
222 this.trace = trace;
223 return this;
224 }
225
226 @Override
227 public String toString()
228 {
229 return getMetadata() + " < " + getRepository();
230 }
231
232 }