View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.artifact.repository;
20  
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import org.apache.maven.artifact.UnknownRepositoryLayoutException;
25  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
26  import org.apache.maven.plugin.LegacySupport;
27  import org.apache.maven.repository.RepositorySystem;
28  import org.codehaus.plexus.component.annotations.Component;
29  import org.codehaus.plexus.component.annotations.Requirement;
30  import org.eclipse.aether.RepositorySystemSession;
31  
32  /**
33   * @author jdcasey
34   */
35  @Component(role = ArtifactRepositoryFactory.class)
36  public class DefaultArtifactRepositoryFactory implements ArtifactRepositoryFactory {
37  
38      @Requirement
39      private org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory factory;
40  
41      @Requirement
42      private LegacySupport legacySupport;
43  
44      @Requirement
45      private RepositorySystem repositorySystem;
46  
47      public ArtifactRepositoryLayout getLayout(String layoutId) throws UnknownRepositoryLayoutException {
48          return factory.getLayout(layoutId);
49      }
50  
51      public ArtifactRepository createDeploymentArtifactRepository(
52              String id, String url, String layoutId, boolean uniqueVersion) throws UnknownRepositoryLayoutException {
53          return injectSession(factory.createDeploymentArtifactRepository(id, url, layoutId, uniqueVersion), false);
54      }
55  
56      public ArtifactRepository createDeploymentArtifactRepository(
57              String id, String url, ArtifactRepositoryLayout repositoryLayout, boolean uniqueVersion) {
58          return injectSession(
59                  factory.createDeploymentArtifactRepository(id, url, repositoryLayout, uniqueVersion), false);
60      }
61  
62      public ArtifactRepository createArtifactRepository(
63              String id,
64              String url,
65              String layoutId,
66              ArtifactRepositoryPolicy snapshots,
67              ArtifactRepositoryPolicy releases)
68              throws UnknownRepositoryLayoutException {
69          return injectSession(factory.createArtifactRepository(id, url, layoutId, snapshots, releases), true);
70      }
71  
72      public ArtifactRepository createArtifactRepository(
73              String id,
74              String url,
75              ArtifactRepositoryLayout repositoryLayout,
76              ArtifactRepositoryPolicy snapshots,
77              ArtifactRepositoryPolicy releases) {
78          return injectSession(factory.createArtifactRepository(id, url, repositoryLayout, snapshots, releases), true);
79      }
80  
81      public void setGlobalUpdatePolicy(String updatePolicy) {
82          factory.setGlobalUpdatePolicy(updatePolicy);
83      }
84  
85      public void setGlobalChecksumPolicy(String checksumPolicy) {
86          factory.setGlobalChecksumPolicy(checksumPolicy);
87      }
88  
89      private ArtifactRepository injectSession(ArtifactRepository repository, boolean mirrors) {
90          RepositorySystemSession session = legacySupport.getRepositorySession();
91  
92          if (session != null && repository != null && !isLocalRepository(repository)) {
93              List<ArtifactRepository> repositories = Arrays.asList(repository);
94  
95              if (mirrors) {
96                  repositorySystem.injectMirror(session, repositories);
97              }
98  
99              repositorySystem.injectProxy(session, repositories);
100 
101             repositorySystem.injectAuthentication(session, repositories);
102         }
103 
104         return repository;
105     }
106 
107     private boolean isLocalRepository(ArtifactRepository repository) {
108         // unfortunately, the API doesn't allow to tell a remote repo and the local repo apart...
109         return "local".equals(repository.getId());
110     }
111 }