View Javadoc
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 java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.eclipse.aether.RepositorySystem;
27  import org.eclipse.aether.RequestTrace;
28  import org.eclipse.aether.artifact.Artifact;
29  import org.eclipse.aether.graph.DependencyNode;
30  import org.eclipse.aether.repository.RemoteRepository;
31  
32  /**
33   * A request to resolve an artifact.
34   * 
35   * @see RepositorySystem#resolveArtifacts(org.eclipse.aether.RepositorySystemSession, java.util.Collection)
36   * @see Artifact#getFile()
37   */
38  public final class ArtifactRequest
39  {
40  
41      private Artifact artifact;
42  
43      private DependencyNode node;
44  
45      private List<RemoteRepository> repositories = Collections.emptyList();
46  
47      private String context = "";
48  
49      private RequestTrace trace;
50  
51      /**
52       * Creates an uninitialized request.
53       */
54      public ArtifactRequest()
55      {
56          // enables default constructor
57      }
58  
59      /**
60       * Creates a request with the specified properties.
61       * 
62       * @param artifact The artifact to resolve, may be {@code null}.
63       * @param repositories The repositories to resolve the artifact from, may be {@code null}.
64       * @param context The context in which this request is made, may be {@code null}.
65       */
66      public ArtifactRequest( Artifact artifact, List<RemoteRepository> repositories, String context )
67      {
68          setArtifact( artifact );
69          setRepositories( repositories );
70          setRequestContext( context );
71      }
72  
73      /**
74       * Creates a request from the specified dependency node.
75       * 
76       * @param node The dependency node to resolve, may be {@code null}.
77       */
78      public ArtifactRequest( DependencyNode node )
79      {
80          setDependencyNode( node );
81          setRepositories( node.getRepositories() );
82          setRequestContext( node.getRequestContext() );
83      }
84  
85      /**
86       * Gets the artifact to resolve.
87       * 
88       * @return The artifact to resolve or {@code null}.
89       */
90      public Artifact getArtifact()
91      {
92          return artifact;
93      }
94  
95      /**
96       * Sets the artifact to resolve.
97       * 
98       * @param artifact The artifact to resolve, may be {@code null}.
99       * @return This request for chaining, never {@code null}.
100      */
101     public ArtifactRequest setArtifact( Artifact artifact )
102     {
103         this.artifact = artifact;
104         return this;
105     }
106 
107     /**
108      * Gets the dependency node (if any) for which to resolve the artifact.
109      * 
110      * @return The dependency node to resolve or {@code null} if unknown.
111      */
112     public DependencyNode getDependencyNode()
113     {
114         return node;
115     }
116 
117     /**
118      * Sets the dependency node to resolve.
119      * 
120      * @param node The dependency node to resolve, may be {@code null}.
121      * @return This request for chaining, never {@code null}.
122      */
123     public ArtifactRequest setDependencyNode( DependencyNode node )
124     {
125         this.node = node;
126         if ( node != null )
127         {
128             setArtifact( node.getDependency().getArtifact() );
129         }
130         return this;
131     }
132 
133     /**
134      * Gets the repositories to resolve the artifact from.
135      * 
136      * @return The repositories, never {@code null}.
137      */
138     public List<RemoteRepository> getRepositories()
139     {
140         return repositories;
141     }
142 
143     /**
144      * Sets the repositories to resolve the artifact from.
145      * 
146      * @param repositories The repositories, may be {@code null}.
147      * @return This request for chaining, never {@code null}.
148      */
149     public ArtifactRequest setRepositories( List<RemoteRepository> repositories )
150     {
151         if ( repositories == null )
152         {
153             this.repositories = Collections.emptyList();
154         }
155         else
156         {
157             this.repositories = repositories;
158         }
159         return this;
160     }
161 
162     /**
163      * Adds the specified repository for the resolution.
164      * 
165      * @param repository The repository to add, may be {@code null}.
166      * @return This request for chaining, never {@code null}.
167      */
168     public ArtifactRequest addRepository( RemoteRepository repository )
169     {
170         if ( repository != null )
171         {
172             if ( this.repositories.isEmpty() )
173             {
174                 this.repositories = new ArrayList<>();
175             }
176             this.repositories.add( repository );
177         }
178         return this;
179     }
180 
181     /**
182      * Gets the context in which this request is made.
183      * 
184      * @return The context, never {@code null}.
185      */
186     public String getRequestContext()
187     {
188         return context;
189     }
190 
191     /**
192      * Sets the context in which this request is made.
193      * 
194      * @param context The context, may be {@code null}.
195      * @return This request for chaining, never {@code null}.
196      */
197     public ArtifactRequest setRequestContext( String context )
198     {
199         this.context = ( context != null ) ? context : "";
200         return this;
201     }
202 
203     /**
204      * Gets the trace information that describes the higher level request/operation in which this request is issued.
205      * 
206      * @return The trace information about the higher level operation or {@code null} if none.
207      */
208     public RequestTrace getTrace()
209     {
210         return trace;
211     }
212 
213     /**
214      * Sets the trace information that describes the higher level request/operation in which this request is issued.
215      * 
216      * @param trace The trace information about the higher level operation, may be {@code null}.
217      * @return This request for chaining, never {@code null}.
218      */
219     public ArtifactRequest setTrace( RequestTrace trace )
220     {
221         this.trace = trace;
222         return this;
223     }
224 
225     @Override
226     public String toString()
227     {
228         return getArtifact() + " < " + getRepositories();
229     }
230 
231 }