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