001    package 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    
022    import java.util.Arrays;
023    import java.util.List;
024    
025    import org.apache.maven.artifact.UnknownRepositoryLayoutException;
026    import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
027    import org.apache.maven.plugin.LegacySupport;
028    import org.apache.maven.repository.RepositorySystem;
029    import org.codehaus.plexus.component.annotations.Component;
030    import org.codehaus.plexus.component.annotations.Requirement;
031    import org.sonatype.aether.RepositorySystemSession;
032    
033    /**
034     * @author jdcasey
035     */
036    @Component( role = ArtifactRepositoryFactory.class )
037    public class DefaultArtifactRepositoryFactory
038        implements ArtifactRepositoryFactory
039    {
040    
041        @Requirement
042        private org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory factory;
043    
044        @Requirement
045        private LegacySupport legacySupport;
046    
047        @Requirement
048        private RepositorySystem repositorySystem;
049    
050        public ArtifactRepositoryLayout getLayout( String layoutId )
051            throws UnknownRepositoryLayoutException
052        {
053            return factory.getLayout( layoutId );
054        }
055    
056        public ArtifactRepository createDeploymentArtifactRepository( String id, String url, String layoutId,
057                                                                      boolean uniqueVersion )
058            throws UnknownRepositoryLayoutException
059        {
060            return injectSession( factory.createDeploymentArtifactRepository( id, url, layoutId, uniqueVersion ), false );
061        }
062    
063        public ArtifactRepository createDeploymentArtifactRepository( String id, String url,
064                                                                      ArtifactRepositoryLayout repositoryLayout,
065                                                                      boolean uniqueVersion )
066        {
067            return injectSession( factory.createDeploymentArtifactRepository( id, url, repositoryLayout, uniqueVersion ),
068                                  false );
069        }
070    
071        public ArtifactRepository createArtifactRepository( String id, String url, String layoutId,
072                                                            ArtifactRepositoryPolicy snapshots,
073                                                            ArtifactRepositoryPolicy releases )
074            throws UnknownRepositoryLayoutException
075        {
076            return injectSession( factory.createArtifactRepository( id, url, layoutId, snapshots, releases ), true );
077        }
078    
079        public ArtifactRepository createArtifactRepository( String id, String url,
080                                                            ArtifactRepositoryLayout repositoryLayout,
081                                                            ArtifactRepositoryPolicy snapshots,
082                                                            ArtifactRepositoryPolicy releases )
083        {
084            return injectSession( factory.createArtifactRepository( id, url, repositoryLayout, snapshots, releases ), true );
085        }
086    
087        public void setGlobalUpdatePolicy( String updatePolicy )
088        {
089            factory.setGlobalUpdatePolicy( updatePolicy );
090        }
091    
092        public void setGlobalChecksumPolicy( String checksumPolicy )
093        {
094            factory.setGlobalChecksumPolicy( checksumPolicy );
095        }
096    
097        private ArtifactRepository injectSession( ArtifactRepository repository, boolean mirrors )
098        {
099            RepositorySystemSession session = legacySupport.getRepositorySession();
100    
101            if ( session != null && repository != null && !isLocalRepository( repository ) )
102            {
103                List<ArtifactRepository> repositories = Arrays.asList( repository );
104    
105                if ( mirrors )
106                {
107                    repositorySystem.injectMirror( session, repositories );
108                }
109    
110                repositorySystem.injectProxy( session, repositories );
111    
112                repositorySystem.injectAuthentication( session, repositories );
113            }
114    
115            return repository;
116        }
117    
118        private boolean isLocalRepository( ArtifactRepository repository )
119        {
120            // unfortunately, the API doesn't allow to tell a remote repo and the local repo apart...
121            return "local".equals( repository.getId() );
122        }
123    
124    }