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  import org.apache.maven.artifact.UnknownRepositoryLayoutException;
24  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
25  import org.apache.maven.plugin.LegacySupport;
26  import org.apache.maven.repository.RepositorySystem;
27  import org.codehaus.plexus.component.annotations.Component;
28  import org.codehaus.plexus.component.annotations.Requirement;
29  import org.eclipse.aether.RepositorySystemSession;
30  
31  /**
32   * @author jdcasey
33   */
34  @Component(role = ArtifactRepositoryFactory.class)
35  public class DefaultArtifactRepositoryFactory implements ArtifactRepositoryFactory {
36  
37      @Requirement
38      private org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory factory;
39  
40      @Requirement
41      private LegacySupport legacySupport;
42  
43      @Requirement
44      private RepositorySystem repositorySystem;
45  
46      public ArtifactRepositoryLayout getLayout(String layoutId) throws UnknownRepositoryLayoutException {
47          return factory.getLayout(layoutId);
48      }
49  
50      public ArtifactRepository createDeploymentArtifactRepository(
51              String id, String url, String layoutId, boolean uniqueVersion) throws UnknownRepositoryLayoutException {
52          return injectSession(factory.createDeploymentArtifactRepository(id, url, layoutId, uniqueVersion), false);
53      }
54  
55      public ArtifactRepository createDeploymentArtifactRepository(
56              String id, String url, ArtifactRepositoryLayout repositoryLayout, boolean uniqueVersion) {
57          return injectSession(
58                  factory.createDeploymentArtifactRepository(id, url, repositoryLayout, uniqueVersion), false);
59      }
60  
61      public ArtifactRepository createArtifactRepository(
62              String id,
63              String url,
64              String layoutId,
65              ArtifactRepositoryPolicy snapshots,
66              ArtifactRepositoryPolicy releases)
67              throws UnknownRepositoryLayoutException {
68          return injectSession(factory.createArtifactRepository(id, url, layoutId, snapshots, releases), true);
69      }
70  
71      public ArtifactRepository createArtifactRepository(
72              String id,
73              String url,
74              ArtifactRepositoryLayout repositoryLayout,
75              ArtifactRepositoryPolicy snapshots,
76              ArtifactRepositoryPolicy releases) {
77          return injectSession(factory.createArtifactRepository(id, url, repositoryLayout, snapshots, releases), true);
78      }
79  
80      public void setGlobalUpdatePolicy(String updatePolicy) {
81          factory.setGlobalUpdatePolicy(updatePolicy);
82      }
83  
84      public void setGlobalChecksumPolicy(String checksumPolicy) {
85          factory.setGlobalChecksumPolicy(checksumPolicy);
86      }
87  
88      private ArtifactRepository injectSession(ArtifactRepository repository, boolean mirrors) {
89          RepositorySystemSession session = legacySupport.getRepositorySession();
90  
91          if (session != null && repository != null && !isLocalRepository(repository)) {
92              List<ArtifactRepository> repositories = Arrays.asList(repository);
93  
94              if (mirrors) {
95                  repositorySystem.injectMirror(session, repositories);
96              }
97  
98              repositorySystem.injectProxy(session, repositories);
99  
100             repositorySystem.injectAuthentication(session, repositories);
101         }
102 
103         return repository;
104     }
105 
106     private boolean isLocalRepository(ArtifactRepository repository) {
107         // unfortunately, the API doesn't allow to tell a remote repo and the local repo apart...
108         return "local".equals(repository.getId());
109     }
110 }