View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.resolution;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.eclipse.aether.RepositorySystem;
26  import org.eclipse.aether.RequestTrace;
27  import org.eclipse.aether.artifact.Artifact;
28  import org.eclipse.aether.graph.DependencyNode;
29  import org.eclipse.aether.repository.RemoteRepository;
30  
31  /**
32   * A request to resolve an artifact.
33   *
34   * @see RepositorySystem#resolveArtifacts(org.eclipse.aether.RepositorySystemSession, java.util.Collection)
35   * @see Artifact#getFile()
36   */
37  public final class ArtifactRequest {
38  
39      private Artifact artifact;
40  
41      private DependencyNode node;
42  
43      private List<RemoteRepository> repositories = Collections.emptyList();
44  
45      private String context = "";
46  
47      private RequestTrace trace;
48  
49      /**
50       * Creates an uninitialized request.
51       */
52      public ArtifactRequest() {
53          // enables default constructor
54      }
55  
56      /**
57       * Creates a request with the specified properties.
58       *
59       * @param artifact The artifact to resolve, may be {@code null}.
60       * @param repositories The repositories to resolve the artifact from, may be {@code null}.
61       * @param context The context in which this request is made, may be {@code null}.
62       */
63      public ArtifactRequest(Artifact artifact, List<RemoteRepository> repositories, String context) {
64          setArtifact(artifact);
65          setRepositories(repositories);
66          setRequestContext(context);
67      }
68  
69      /**
70       * Creates a request from the specified dependency node.
71       *
72       * @param node The dependency node to resolve, may be {@code null}.
73       */
74      public ArtifactRequest(DependencyNode node) {
75          setDependencyNode(node);
76          setRepositories(node.getRepositories());
77          setRequestContext(node.getRequestContext());
78      }
79  
80      /**
81       * Gets the artifact to resolve.
82       *
83       * @return The artifact to resolve or {@code null}.
84       */
85      public Artifact getArtifact() {
86          return artifact;
87      }
88  
89      /**
90       * Sets the artifact to resolve.
91       *
92       * @param artifact The artifact to resolve, may be {@code null}.
93       * @return This request for chaining, never {@code null}.
94       */
95      public ArtifactRequest setArtifact(Artifact artifact) {
96          this.artifact = artifact;
97          return this;
98      }
99  
100     /**
101      * Gets the dependency node (if any) for which to resolve the artifact.
102      *
103      * @return The dependency node to resolve or {@code null} if unknown.
104      */
105     public DependencyNode getDependencyNode() {
106         return node;
107     }
108 
109     /**
110      * Sets the dependency node to resolve.
111      *
112      * @param node The dependency node to resolve, may be {@code null}.
113      * @return This request for chaining, never {@code null}.
114      */
115     public ArtifactRequest setDependencyNode(DependencyNode node) {
116         this.node = node;
117         if (node != null) {
118             setArtifact(node.getDependency().getArtifact());
119         }
120         return this;
121     }
122 
123     /**
124      * Gets the repositories to resolve the artifact from.
125      *
126      * @return The repositories, never {@code null}.
127      */
128     public List<RemoteRepository> getRepositories() {
129         return repositories;
130     }
131 
132     /**
133      * Sets the repositories to resolve the artifact from.
134      *
135      * @param repositories The repositories, may be {@code null}.
136      * @return This request for chaining, never {@code null}.
137      */
138     public ArtifactRequest setRepositories(List<RemoteRepository> repositories) {
139         if (repositories == null) {
140             this.repositories = Collections.emptyList();
141         } else {
142             this.repositories = repositories;
143         }
144         return this;
145     }
146 
147     /**
148      * Adds the specified repository for the resolution.
149      *
150      * @param repository The repository to add, may be {@code null}.
151      * @return This request for chaining, never {@code null}.
152      */
153     public ArtifactRequest addRepository(RemoteRepository repository) {
154         if (repository != null) {
155             if (this.repositories.isEmpty()) {
156                 this.repositories = new ArrayList<>();
157             }
158             this.repositories.add(repository);
159         }
160         return this;
161     }
162 
163     /**
164      * Gets the context in which this request is made.
165      *
166      * @return The context, never {@code null}.
167      */
168     public String getRequestContext() {
169         return context;
170     }
171 
172     /**
173      * Sets the context in which this request is made.
174      *
175      * @param context The context, may be {@code null}.
176      * @return This request for chaining, never {@code null}.
177      */
178     public ArtifactRequest setRequestContext(String context) {
179         this.context = (context != null) ? context : "";
180         return this;
181     }
182 
183     /**
184      * Gets the trace information that describes the higher level request/operation in which this request is issued.
185      *
186      * @return The trace information about the higher level operation or {@code null} if none.
187      */
188     public RequestTrace getTrace() {
189         return trace;
190     }
191 
192     /**
193      * Sets the trace information that describes the higher level request/operation in which this request is issued.
194      *
195      * @param trace The trace information about the higher level operation, may be {@code null}.
196      * @return This request for chaining, never {@code null}.
197      */
198     public ArtifactRequest setTrace(RequestTrace trace) {
199         this.trace = trace;
200         return this;
201     }
202 
203     @Override
204     public String toString() {
205         return getArtifact() + " < " + getRepositories();
206     }
207 }