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.repository.RemoteRepository;
031
032/**
033 * A request to read an artifact descriptor.
034 * 
035 * @see RepositorySystem#readArtifactDescriptor(RepositorySystemSession, ArtifactDescriptorRequest)
036 */
037public final class ArtifactDescriptorRequest
038{
039
040    private Artifact artifact;
041
042    private List<RemoteRepository> repositories = Collections.emptyList();
043
044    private String context = "";
045
046    private RequestTrace trace;
047
048    /**
049     * Creates an uninitialized request.
050     */
051    public ArtifactDescriptorRequest()
052    {
053        // enables default constructor
054    }
055
056    /**
057     * Creates a request with the specified properties.
058     * 
059     * @param artifact The artifact whose descriptor should be read, may be {@code null}.
060     * @param repositories The repositories to resolve the descriptor from, may be {@code null}.
061     * @param context The context in which this request is made, may be {@code null}.
062     */
063    public ArtifactDescriptorRequest( Artifact artifact, List<RemoteRepository> repositories, String context )
064    {
065        setArtifact( artifact );
066        setRepositories( repositories );
067        setRequestContext( context );
068    }
069
070    /**
071     * Gets the artifact whose descriptor shall be read.
072     * 
073     * @return The artifact or {@code null} if not set.
074     */
075    public Artifact getArtifact()
076    {
077        return artifact;
078    }
079
080    /**
081     * Sets the artifact whose descriptor shall be read. Eventually, a valid request must have an artifact set.
082     * 
083     * @param artifact The artifact, may be {@code null}.
084     * @return This request for chaining, never {@code null}.
085     */
086    public ArtifactDescriptorRequest setArtifact( Artifact artifact )
087    {
088        this.artifact = artifact;
089        return this;
090    }
091
092    /**
093     * Gets the repositories to resolve the descriptor from.
094     * 
095     * @return The repositories, never {@code null}.
096     */
097    public List<RemoteRepository> getRepositories()
098    {
099        return repositories;
100    }
101
102    /**
103     * Sets the repositories to resolve the descriptor from.
104     * 
105     * @param repositories The repositories, may be {@code null}.
106     * @return This request for chaining, never {@code null}.
107     */
108    public ArtifactDescriptorRequest setRepositories( List<RemoteRepository> repositories )
109    {
110        if ( repositories == null )
111        {
112            this.repositories = Collections.emptyList();
113        }
114        else
115        {
116            this.repositories = repositories;
117        }
118        return this;
119    }
120
121    /**
122     * Adds the specified repository for the resolution of the artifact descriptor.
123     * 
124     * @param repository The repository to add, may be {@code null}.
125     * @return This request for chaining, never {@code null}.
126     */
127    public ArtifactDescriptorRequest addRepository( RemoteRepository repository )
128    {
129        if ( repository != null )
130        {
131            if ( this.repositories.isEmpty() )
132            {
133                this.repositories = new ArrayList<RemoteRepository>();
134            }
135            this.repositories.add( repository );
136        }
137        return this;
138    }
139
140    /**
141     * Gets the context in which this request is made.
142     * 
143     * @return The context, never {@code null}.
144     */
145    public String getRequestContext()
146    {
147        return context;
148    }
149
150    /**
151     * Sets the context in which this request is made.
152     * 
153     * @param context The context, may be {@code null}.
154     * @return This request for chaining, never {@code null}.
155     */
156    public ArtifactDescriptorRequest setRequestContext( String context )
157    {
158        this.context = ( context != null ) ? context : "";
159        return this;
160    }
161
162    /**
163     * Gets the trace information that describes the higher level request/operation in which this request is issued.
164     * 
165     * @return The trace information about the higher level operation or {@code null} if none.
166     */
167    public RequestTrace getTrace()
168    {
169        return trace;
170    }
171
172    /**
173     * Sets the trace information that describes the higher level request/operation in which this request is issued.
174     * 
175     * @param trace The trace information about the higher level operation, may be {@code null}.
176     * @return This request for chaining, never {@code null}.
177     */
178    public ArtifactDescriptorRequest setTrace( RequestTrace trace )
179    {
180        this.trace = trace;
181        return this;
182    }
183
184    @Override
185    public String toString()
186    {
187        return getArtifact() + " < " + getRepositories();
188    }
189
190}