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.collection;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024
025import org.eclipse.aether.RepositorySystem;
026import org.eclipse.aether.RequestTrace;
027import org.eclipse.aether.artifact.Artifact;
028import org.eclipse.aether.graph.Dependency;
029import org.eclipse.aether.repository.RemoteRepository;
030import org.eclipse.aether.scope.ResolutionScope;
031
032/**
033 * A request to collect the transitive dependencies and to build a dependency graph from them. There are three ways to
034 * create a dependency graph. First, only the root dependency can be given. Second, a root dependency and direct
035 * dependencies can be specified in which case the specified direct dependencies are merged with the direct dependencies
036 * retrieved from the artifact descriptor of the root dependency. And last, only direct dependencies can be specified in
037 * which case the root node of the resulting graph has no associated dependency.
038 *
039 * @see RepositorySystem#collectDependencies(org.eclipse.aether.RepositorySystemSession, CollectRequest)
040 */
041public final class CollectRequest {
042    private ResolutionScope resolutionScope;
043
044    private Artifact rootArtifact;
045
046    private Dependency root;
047
048    private List<Dependency> dependencies = Collections.emptyList();
049
050    private List<Dependency> managedDependencies = Collections.emptyList();
051
052    private List<RemoteRepository> repositories = Collections.emptyList();
053
054    private String context = "";
055
056    private RequestTrace trace;
057
058    /**
059     * Creates an uninitialized request.
060     */
061    public CollectRequest() {
062        // enables default constructor
063    }
064
065    /**
066     * Creates a request with the specified properties.
067     *
068     * @param root The root dependency whose transitive dependencies should be collected, may be {@code null}.
069     * @param repositories The repositories to use for the collection, may be {@code null}.
070     */
071    public CollectRequest(Dependency root, List<RemoteRepository> repositories) {
072        setRoot(root);
073        setRepositories(repositories);
074    }
075
076    /**
077     * Creates a new request with the specified properties.
078     *
079     * @param root The root dependency whose transitive dependencies should be collected, may be {@code null}.
080     * @param dependencies The direct dependencies to merge with the direct dependencies from the root dependency's
081     *            artifact descriptor.
082     * @param repositories The repositories to use for the collection, may be {@code null}.
083     */
084    public CollectRequest(Dependency root, List<Dependency> dependencies, List<RemoteRepository> repositories) {
085        setRoot(root);
086        setDependencies(dependencies);
087        setRepositories(repositories);
088    }
089
090    /**
091     * Creates a new request with the specified properties.
092     *
093     * @param dependencies The direct dependencies of some imaginary root, may be {@code null}.
094     * @param managedDependencies The dependency management information to apply to the transitive dependencies, may be
095     *            {@code null}.
096     * @param repositories The repositories to use for the collection, may be {@code null}.
097     */
098    public CollectRequest(
099            List<Dependency> dependencies, List<Dependency> managedDependencies, List<RemoteRepository> repositories) {
100        setDependencies(dependencies);
101        setManagedDependencies(managedDependencies);
102        setRepositories(repositories);
103    }
104
105    /**
106     * Gets the wanted resolution scope. If set, the {@link org.eclipse.aether.scope.ScopeManager} has to be set on
107     * session as well, otherwise it is considered user error: misconfigured collect request.
108     *
109     * @since 2.0.0
110     * @return The wanted resolution scope.
111     */
112    public ResolutionScope getResolutionScope() {
113        return resolutionScope;
114    }
115
116    /**
117     * Sets the wanted resolution scope. Usable only if {@link org.eclipse.aether.scope.ScopeManager} is used.
118     *
119     * @since 2.0.0
120     * @param resolutionScope The wanted resolution scope, may be {@code null} to "drive by yourself".
121     */
122    public void setResolutionScope(ResolutionScope resolutionScope) {
123        this.resolutionScope = resolutionScope;
124    }
125
126    /**
127     * Gets the root artifact for the dependency graph.
128     *
129     * @return The root artifact for the dependency graph or {@code null} if none.
130     */
131    public Artifact getRootArtifact() {
132        return rootArtifact;
133    }
134
135    /**
136     * Sets the root artifact for the dependency graph. This must not be confused with {@link #setRoot(Dependency)}: The
137     * root <em>dependency</em>, like any other specified dependency, will be subject to dependency
138     * collection/resolution, i.e. should have an artifact descriptor and a corresponding artifact file. The root
139     * <em>artifact</em> on the other hand is only used as a label for the root node of the graph in case no root
140     * dependency was specified. As such, the configured root artifact is ignored if {@link #getRoot()} does not return
141     * {@code null}.
142     *
143     * @param rootArtifact The root artifact for the dependency graph, may be {@code null}.
144     * @return This request for chaining, never {@code null}.
145     */
146    public CollectRequest setRootArtifact(Artifact rootArtifact) {
147        this.rootArtifact = rootArtifact;
148        return this;
149    }
150
151    /**
152     * Gets the root dependency of the graph.
153     *
154     * @return The root dependency of the graph or {@code null} if none.
155     */
156    public Dependency getRoot() {
157        return root;
158    }
159
160    /**
161     * Sets the root dependency of the graph.
162     *
163     * @param root The root dependency of the graph, may be {@code null}.
164     * @return This request for chaining, never {@code null}.
165     */
166    public CollectRequest setRoot(Dependency root) {
167        this.root = root;
168        return this;
169    }
170
171    /**
172     * Gets the direct dependencies.
173     *
174     * @return The direct dependencies, never {@code null}.
175     */
176    public List<Dependency> getDependencies() {
177        return dependencies;
178    }
179
180    /**
181     * Sets the direct dependencies. If both a root dependency and direct dependencies are given in the request, the
182     * direct dependencies from the request will be merged with the direct dependencies from the root dependency's
183     * artifact descriptor, giving higher priority to the dependencies from the request.
184     *
185     * @param dependencies The direct dependencies, may be {@code null}.
186     * @return This request for chaining, never {@code null}.
187     */
188    public CollectRequest setDependencies(List<Dependency> dependencies) {
189        if (dependencies == null) {
190            this.dependencies = Collections.emptyList();
191        } else {
192            this.dependencies = dependencies;
193        }
194        return this;
195    }
196
197    /**
198     * Adds the specified direct dependency.
199     *
200     * @param dependency The dependency to add, may be {@code null}.
201     * @return This request for chaining, never {@code null}.
202     */
203    public CollectRequest addDependency(Dependency dependency) {
204        if (dependency != null) {
205            if (this.dependencies.isEmpty()) {
206                this.dependencies = new ArrayList<>();
207            }
208            this.dependencies.add(dependency);
209        }
210        return this;
211    }
212
213    /**
214     * Gets the dependency management to apply to transitive dependencies.
215     *
216     * @return The dependency management to apply to transitive dependencies, never {@code null}.
217     */
218    public List<Dependency> getManagedDependencies() {
219        return managedDependencies;
220    }
221
222    /**
223     * Sets the dependency management to apply to transitive dependencies. To clarify, this management does not apply to
224     * the direct dependencies of the root node.
225     *
226     * @param managedDependencies The dependency management, may be {@code null}.
227     * @return This request for chaining, never {@code null}.
228     */
229    public CollectRequest setManagedDependencies(List<Dependency> managedDependencies) {
230        if (managedDependencies == null) {
231            this.managedDependencies = Collections.emptyList();
232        } else {
233            this.managedDependencies = managedDependencies;
234        }
235        return this;
236    }
237
238    /**
239     * Adds the specified managed dependency.
240     *
241     * @param managedDependency The managed dependency to add, may be {@code null}.
242     * @return This request for chaining, never {@code null}.
243     */
244    public CollectRequest addManagedDependency(Dependency managedDependency) {
245        if (managedDependency != null) {
246            if (this.managedDependencies.isEmpty()) {
247                this.managedDependencies = new ArrayList<>();
248            }
249            this.managedDependencies.add(managedDependency);
250        }
251        return this;
252    }
253
254    /**
255     * Gets the repositories to use for the collection.
256     *
257     * @return The repositories to use for the collection, never {@code null}.
258     */
259    public List<RemoteRepository> getRepositories() {
260        return repositories;
261    }
262
263    /**
264     * Sets the repositories to use for the collection.
265     *
266     * @param repositories The repositories to use for the collection, may be {@code null}.
267     * @return This request for chaining, never {@code null}.
268     */
269    public CollectRequest setRepositories(List<RemoteRepository> repositories) {
270        if (repositories == null) {
271            this.repositories = Collections.emptyList();
272        } else {
273            this.repositories = repositories;
274        }
275        return this;
276    }
277
278    /**
279     * Adds the specified repository for collection.
280     *
281     * @param repository The repository to collect dependency information from, may be {@code null}.
282     * @return This request for chaining, never {@code null}.
283     */
284    public CollectRequest addRepository(RemoteRepository repository) {
285        if (repository != null) {
286            if (this.repositories.isEmpty()) {
287                this.repositories = new ArrayList<>();
288            }
289            this.repositories.add(repository);
290        }
291        return this;
292    }
293
294    /**
295     * Gets the context in which this request is made.
296     *
297     * @return The context, never {@code null}.
298     */
299    public String getRequestContext() {
300        return context;
301    }
302
303    /**
304     * Sets the context in which this request is made.
305     *
306     * @param context The context, may be {@code null}.
307     * @return This request for chaining, never {@code null}.
308     */
309    public CollectRequest setRequestContext(String context) {
310        this.context = (context != null) ? context : "";
311        return this;
312    }
313
314    /**
315     * Gets the trace information that describes the higher level request/operation in which this request is issued.
316     *
317     * @return The trace information about the higher level operation or {@code null} if none.
318     */
319    public RequestTrace getTrace() {
320        return trace;
321    }
322
323    /**
324     * Sets the trace information that describes the higher level request/operation in which this request is issued.
325     *
326     * @param trace The trace information about the higher level operation, may be {@code null}.
327     * @return This request for chaining, never {@code null}.
328     */
329    public CollectRequest setTrace(RequestTrace trace) {
330        this.trace = trace;
331        return this;
332    }
333
334    @Override
335    public String toString() {
336        return getRoot() + " -> " + getDependencies() + " < " + getRepositories();
337    }
338}