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.util.List;
22  import java.util.Map;
23  import java.util.Optional;
24  import java.util.stream.Collectors;
25  
26  import org.apache.maven.api.Repository;
27  import org.apache.maven.api.Version;
28  import org.apache.maven.api.di.Inject;
29  import org.apache.maven.api.di.Named;
30  import org.apache.maven.api.di.Singleton;
31  import org.apache.maven.api.services.VersionRangeResolver;
32  import org.apache.maven.api.services.VersionRangeResolverException;
33  import org.apache.maven.api.services.VersionRangeResolverRequest;
34  import org.apache.maven.api.services.VersionRangeResolverResult;
35  import org.eclipse.aether.RepositorySystem;
36  import org.eclipse.aether.repository.ArtifactRepository;
37  import org.eclipse.aether.resolution.VersionRangeRequest;
38  import org.eclipse.aether.resolution.VersionRangeResolutionException;
39  import org.eclipse.aether.resolution.VersionRangeResult;
40  
41  import static org.apache.maven.internal.impl.Utils.map;
42  import static org.apache.maven.internal.impl.Utils.nonNull;
43  
44  @Named
45  @Singleton
46  public class DefaultVersionRangeResolver implements VersionRangeResolver {
47  
48      private final RepositorySystem repositorySystem;
49  
50      @Inject
51      public DefaultVersionRangeResolver(RepositorySystem repositorySystem) {
52          this.repositorySystem = repositorySystem;
53      }
54  
55      @Override
56      public VersionRangeResolverResult resolve(VersionRangeResolverRequest request)
57              throws VersionRangeResolverException {
58          nonNull(request, "request");
59          InternalSession session = InternalSession.from(request.getSession());
60  
61          try {
62              VersionRangeResult res = repositorySystem.resolveVersionRange(
63                      session.getSession(),
64                      new VersionRangeRequest(
65                              session.toArtifact(request.getArtifactCoordinates()),
66                              session.toRepositories(session.getRemoteRepositories()),
67                              null));
68  
69              Map<String, ArtifactRepository> repos = res.getVersions().stream()
70                      .filter(v -> res.getRepository(v) != null)
71                      .collect(Collectors.toMap(v -> v.toString(), res::getRepository));
72  
73              return new VersionRangeResolverResult() {
74                  @Override
75                  public List<Exception> getExceptions() {
76                      return res.getExceptions();
77                  }
78  
79                  @Override
80                  public List<Version> getVersions() {
81                      return map(res.getVersions(), v -> session.parseVersion(v.toString()));
82                  }
83  
84                  @Override
85                  public Optional<Repository> getRepository(Version version) {
86                      ArtifactRepository repo = repos.get(version.toString());
87                      if (repo instanceof org.eclipse.aether.repository.LocalRepository) {
88                          return Optional.of(
89                                  new DefaultLocalRepository((org.eclipse.aether.repository.LocalRepository) repo));
90                      } else if (repo instanceof org.eclipse.aether.repository.RemoteRepository) {
91                          return Optional.of(
92                                  new DefaultRemoteRepository((org.eclipse.aether.repository.RemoteRepository) repo));
93                      } else {
94                          return Optional.empty();
95                      }
96                  }
97              };
98          } catch (VersionRangeResolutionException e) {
99              throw new VersionRangeResolverException("Unable to resolve version range", e);
100         }
101     }
102 }