001package org.eclipse.aether.resolution;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025
026import org.eclipse.aether.RepositorySystem;
027import org.eclipse.aether.RepositorySystemSession;
028import org.eclipse.aether.RequestTrace;
029import org.eclipse.aether.artifact.Artifact;
030import org.eclipse.aether.graph.DependencyNode;
031import org.eclipse.aether.repository.RemoteRepository;
032
033/**
034 * A request to resolve an artifact.
035 * 
036 * @see RepositorySystem#resolveArtifacts(RepositorySystemSession, java.util.Collection)
037 * @see Artifact#getFile()
038 */
039public final class ArtifactRequest
040{
041
042    private Artifact artifact;
043
044    private DependencyNode node;
045
046    private List<RemoteRepository> repositories = Collections.emptyList();
047
048    private String context = "";
049
050    private RequestTrace trace;
051
052    /**
053     * Creates an uninitialized request.
054     */
055    public ArtifactRequest()
056    {
057        // enables default constructor
058    }
059
060    /**
061     * Creates a request with the specified properties.
062     * 
063     * @param artifact The artifact to resolve, may be {@code null}.
064     * @param repositories The repositories to resolve the artifact from, may be {@code null}.
065     * @param context The context in which this request is made, may be {@code null}.
066     */
067    public ArtifactRequest( Artifact artifact, List<RemoteRepository> repositories, String context )
068    {
069        setArtifact( artifact );
070        setRepositories( repositories );
071        setRequestContext( context );
072    }
073
074    /**
075     * Creates a request from the specified dependency node.
076     * 
077     * @param node The dependency node to resolve, may be {@code null}.
078     */
079    public ArtifactRequest( DependencyNode node )
080    {
081        setDependencyNode( node );
082        setRepositories( node.getRepositories() );
083        setRequestContext( node.getRequestContext() );
084    }
085
086    /**
087     * Gets the artifact to resolve.
088     * 
089     * @return The artifact to resolve or {@code null}.
090     */
091    public Artifact getArtifact()
092    {
093        return artifact;
094    }
095
096    /**
097     * Sets the artifact to resolve.
098     * 
099     * @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}