001package org.apache.maven.artifact.repository;
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.List;
024
025import org.apache.maven.execution.MavenSession;
026import org.apache.maven.project.MavenProject;
027
028/**
029 * Collects basic settings to access the repository system.
030 * 
031 * @author Benjamin Bentmann
032 */
033public class DefaultRepositoryRequest
034    implements RepositoryRequest
035{
036
037    private boolean offline;
038
039    private boolean forceUpdate;
040
041    private ArtifactRepository localRepository;
042
043    private List<ArtifactRepository> remoteRepositories;
044
045    /**
046     * Creates an empty repository request.
047     */
048    public DefaultRepositoryRequest()
049    {
050        // enables no-arg constructor
051    }
052
053    /**
054     * Creates a shallow copy of the specified repository request.
055     * 
056     * @param repositoryRequest The repository request to copy from, must not be {@code null}.
057     */
058    public DefaultRepositoryRequest( RepositoryRequest repositoryRequest )
059    {
060        setLocalRepository( repositoryRequest.getLocalRepository() );
061        setRemoteRepositories( repositoryRequest.getRemoteRepositories() );
062        setOffline( repositoryRequest.isOffline() );
063        setForceUpdate( repositoryRequest.isForceUpdate() );
064    }
065
066    public static RepositoryRequest getRepositoryRequest( MavenSession session, MavenProject project )
067    {
068        RepositoryRequest request = new DefaultRepositoryRequest();
069
070        request.setLocalRepository( session.getLocalRepository() );
071        if ( project != null )
072        {
073            request.setRemoteRepositories( project.getPluginArtifactRepositories() );
074        }
075        request.setOffline( session.isOffline() );
076        request.setForceUpdate( session.getRequest().isUpdateSnapshots() );
077
078        return request;
079    }
080
081    public boolean isOffline()
082    {
083        return offline;
084    }
085
086    public DefaultRepositoryRequest setOffline( boolean offline )
087    {
088        this.offline = offline;
089
090        return this;
091    }
092
093    public boolean isForceUpdate()
094    {
095        return forceUpdate;
096    }
097
098    public DefaultRepositoryRequest setForceUpdate( boolean forceUpdate )
099    {
100        this.forceUpdate = forceUpdate;
101
102        return this;
103    }
104
105    public ArtifactRepository getLocalRepository()
106    {
107        return localRepository;
108    }
109
110    public DefaultRepositoryRequest setLocalRepository( ArtifactRepository localRepository )
111    {
112        this.localRepository = localRepository;
113
114        return this;
115    }
116
117    public List<ArtifactRepository> getRemoteRepositories()
118    {
119        if ( remoteRepositories == null )
120        {
121            remoteRepositories = new ArrayList<ArtifactRepository>();
122        }
123
124        return remoteRepositories;
125    }
126
127    public DefaultRepositoryRequest setRemoteRepositories( List<ArtifactRepository> remoteRepositories )
128    {
129        this.remoteRepositories = remoteRepositories;
130
131        return this;
132    }
133
134}