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.RequestTrace;
028import org.eclipse.aether.artifact.Artifact;
029import org.eclipse.aether.graph.DependencyNode;
030import org.eclipse.aether.repository.RemoteRepository;
031
032/**
033 * A request to resolve an artifact.
034 * 
035 * @see RepositorySystem#resolveArtifacts(org.eclipse.aether.RepositorySystemSession, java.util.Collection)
036 * @see Artifact#getFile()
037 */
038public final class ArtifactRequest
039{
040
041    private Artifact artifact;
042
043    private DependencyNode node;
044
045    private List<RemoteRepository> repositories = Collections.emptyList();
046
047    private String context = "";
048
049    private RequestTrace trace;
050
051    /**
052     * Creates an uninitialized request.
053     */
054    public ArtifactRequest()
055    {
056        // enables default constructor
057    }
058
059    /**
060     * Creates a request with the specified properties.
061     * 
062     * @param artifact The artifact to resolve, may be {@code null}.
063     * @param repositories The repositories to resolve the artifact from, may be {@code null}.
064     * @param context The context in which this request is made, may be {@code null}.
065     */
066    public ArtifactRequest( Artifact artifact, List<RemoteRepository> repositories, String context )
067    {
068        setArtifact( artifact );
069        setRepositories( repositories );
070        setRequestContext( context );
071    }
072
073    /**
074     * Creates a request from the specified dependency node.
075     * 
076     * @param node The dependency node to resolve, may be {@code null}.
077     */
078    public ArtifactRequest( DependencyNode node )
079    {
080        setDependencyNode( node );
081        setRepositories( node.getRepositories() );
082        setRequestContext( node.getRequestContext() );
083    }
084
085    /**
086     * Gets the artifact to resolve.
087     * 
088     * @return The artifact to resolve or {@code null}.
089     */
090    public Artifact getArtifact()
091    {
092        return artifact;
093    }
094
095    /**
096     * Sets the artifact to resolve.
097     * 
098     * @param artifact The artifact to resolve, may be {@code null}.
099     * @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}