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.internal.impl;
20  
21  import java.nio.file.Path;
22  import java.util.List;
23  
24  import org.apache.maven.api.LocalRepository;
25  import org.apache.maven.api.RemoteRepository;
26  import org.apache.maven.api.Session;
27  import org.apache.maven.api.di.Inject;
28  import org.apache.maven.api.di.Named;
29  import org.apache.maven.api.di.Singleton;
30  import org.apache.maven.api.model.Repository;
31  import org.apache.maven.api.services.RepositoryFactory;
32  import org.eclipse.aether.impl.RemoteRepositoryManager;
33  import org.eclipse.aether.repository.RepositoryPolicy;
34  
35  import static org.apache.maven.internal.impl.Utils.nonNull;
36  
37  @Named
38  @Singleton
39  public class DefaultRepositoryFactory implements RepositoryFactory {
40  
41      final RemoteRepositoryManager remoteRepositoryManager;
42  
43      @Inject
44      public DefaultRepositoryFactory(RemoteRepositoryManager remoteRepositoryManager) {
45          this.remoteRepositoryManager = remoteRepositoryManager;
46      }
47  
48      @Override
49      public LocalRepository createLocal(Path path) {
50          return new DefaultLocalRepository(new org.eclipse.aether.repository.LocalRepository(path));
51      }
52  
53      @Override
54      public RemoteRepository createRemote(String id, String url) {
55          return new DefaultRemoteRepository(
56                  new org.eclipse.aether.repository.RemoteRepository.Builder(id, "default", url).build());
57      }
58  
59      @Override
60      public RemoteRepository createRemote(Repository repository) throws IllegalArgumentException {
61          return new DefaultRemoteRepository(new org.eclipse.aether.repository.RemoteRepository.Builder(
62                          repository.getId(), repository.getLayout(), repository.getUrl())
63                  .setReleasePolicy(buildRepositoryPolicy(repository.getReleases()))
64                  .setSnapshotPolicy(buildRepositoryPolicy(repository.getSnapshots()))
65                  .build());
66      }
67  
68      @Override
69      public List<RemoteRepository> aggregate(
70              Session session,
71              List<RemoteRepository> dominant,
72              List<RemoteRepository> recessive,
73              boolean processRecessive) {
74          InternalSession internalSession = InternalSession.from(nonNull(session, "session"));
75          List<org.eclipse.aether.repository.RemoteRepository> repos = remoteRepositoryManager.aggregateRepositories(
76                  internalSession.getSession(),
77                  internalSession.toRepositories(nonNull(dominant, "dominant")),
78                  internalSession.toRepositories(nonNull(recessive, "recessive")),
79                  processRecessive);
80          return repos.stream()
81                  .<RemoteRepository>map(DefaultRemoteRepository::new)
82                  .toList();
83      }
84  
85      public static org.eclipse.aether.repository.RepositoryPolicy buildRepositoryPolicy(
86              org.apache.maven.api.model.RepositoryPolicy policy) {
87          boolean enabled = true;
88          String updatePolicy = RepositoryPolicy.UPDATE_POLICY_DAILY;
89          String checksumPolicy = RepositoryPolicy.CHECKSUM_POLICY_FAIL;
90          if (policy != null) {
91              enabled = policy.isEnabled();
92              if (policy.getUpdatePolicy() != null) {
93                  updatePolicy = policy.getUpdatePolicy();
94              }
95              if (policy.getChecksumPolicy() != null) {
96                  checksumPolicy = policy.getChecksumPolicy();
97              }
98          }
99          return new org.eclipse.aether.repository.RepositoryPolicy(enabled, updatePolicy, checksumPolicy);
100     }
101 }