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 javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.apache.maven.artifact.UnknownRepositoryLayoutException;
29  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
30  import org.apache.maven.plugin.LegacySupport;
31  import org.apache.maven.repository.RepositorySystem;
32  import org.codehaus.plexus.PlexusContainer;
33  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
34  import org.eclipse.aether.RepositorySystemSession;
35  
36  /**
37   */
38  @Named
39  @Singleton
40  @Deprecated
41  public class DefaultArtifactRepositoryFactory implements ArtifactRepositoryFactory {
42  
43      @Inject
44      private org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory factory;
45  
46      @Inject
47      private LegacySupport legacySupport;
48  
49      @Inject
50      private PlexusContainer container;
51  
52      public ArtifactRepositoryLayout getLayout(String layoutId) throws UnknownRepositoryLayoutException {
53          return factory.getLayout(layoutId);
54      }
55  
56      public ArtifactRepository createDeploymentArtifactRepository(
57              String id, String url, String layoutId, boolean uniqueVersion) throws UnknownRepositoryLayoutException {
58          return injectSession(factory.createDeploymentArtifactRepository(id, url, layoutId, uniqueVersion), false);
59      }
60  
61      public ArtifactRepository createDeploymentArtifactRepository(
62              String id, String url, ArtifactRepositoryLayout repositoryLayout, boolean uniqueVersion) {
63          return injectSession(
64                  factory.createDeploymentArtifactRepository(id, url, repositoryLayout, uniqueVersion), false);
65      }
66  
67      public ArtifactRepository createArtifactRepository(
68              String id,
69              String url,
70              String layoutId,
71              ArtifactRepositoryPolicy snapshots,
72              ArtifactRepositoryPolicy releases)
73              throws UnknownRepositoryLayoutException {
74          return injectSession(factory.createArtifactRepository(id, url, layoutId, snapshots, releases), true);
75      }
76  
77      public ArtifactRepository createArtifactRepository(
78              String id,
79              String url,
80              ArtifactRepositoryLayout repositoryLayout,
81              ArtifactRepositoryPolicy snapshots,
82              ArtifactRepositoryPolicy releases) {
83          return injectSession(factory.createArtifactRepository(id, url, repositoryLayout, snapshots, releases), true);
84      }
85  
86      public void setGlobalUpdatePolicy(String updatePolicy) {
87          factory.setGlobalUpdatePolicy(updatePolicy);
88      }
89  
90      public void setGlobalChecksumPolicy(String checksumPolicy) {
91          factory.setGlobalChecksumPolicy(checksumPolicy);
92      }
93  
94      private ArtifactRepository injectSession(ArtifactRepository repository, boolean mirrors) {
95          RepositorySystemSession session = legacySupport.getRepositorySession();
96  
97          if (session != null && repository != null && !isLocalRepository(repository)) {
98              List<ArtifactRepository> repositories = Arrays.asList(repository);
99  
100             RepositorySystem repositorySystem;
101             try {
102                 repositorySystem = container.lookup(RepositorySystem.class);
103             } catch (ComponentLookupException e) {
104                 throw new IllegalStateException("Unable to lookup " + RepositorySystem.class.getName());
105             }
106 
107             if (mirrors) {
108                 repositorySystem.injectMirror(session, repositories);
109             }
110 
111             repositorySystem.injectProxy(session, repositories);
112 
113             repositorySystem.injectAuthentication(session, repositories);
114         }
115 
116         return repository;
117     }
118 
119     private boolean isLocalRepository(ArtifactRepository repository) {
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 }