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.resolver.filter;
20  
21  import java.lang.reflect.Proxy;
22  import java.nio.file.FileSystems;
23  import java.nio.file.Path;
24  import java.nio.file.PathMatcher;
25  import java.util.List;
26  import java.util.function.Predicate;
27  import java.util.stream.Collectors;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.model.Exclusion;
31  
32  /**
33   * Filter to exclude from a list of artifact patterns.
34   */
35  public class ExclusionArtifactFilter implements ArtifactFilter {
36  
37      private final List<Exclusion> exclusions;
38      private final List<Predicate<Artifact>> predicates;
39  
40      public ExclusionArtifactFilter(List<Exclusion> exclusions) {
41          this.exclusions = exclusions;
42          this.predicates =
43                  exclusions.stream().map(ExclusionArtifactFilter::toPredicate).collect(Collectors.toList());
44      }
45  
46      @Override
47      public boolean include(Artifact artifact) {
48          return predicates.stream().noneMatch(p -> p.test(artifact));
49      }
50  
51      private static Predicate<Artifact> toPredicate(Exclusion exclusion) {
52          PathMatcher groupId = FileSystems.getDefault().getPathMatcher("glob:" + exclusion.getGroupId());
53          PathMatcher artifactId = FileSystems.getDefault().getPathMatcher("glob:" + exclusion.getArtifactId());
54          Predicate<Artifact> predGroupId = a -> groupId.matches(createPathProxy(a.getGroupId()));
55          Predicate<Artifact> predArtifactId = a -> artifactId.matches(createPathProxy(a.getArtifactId()));
56          return predGroupId.and(predArtifactId);
57      }
58  
59      /**
60       * In order to reuse the glob matcher from the filesystem, we need
61       * to create Path instances.  Those are only used with the toString method.
62       * This hack works because the only system-dependent thing is the path
63       * separator which should not be part of the groupId or artifactId.
64       */
65      private static Path createPathProxy(String value) {
66          return (Path) Proxy.newProxyInstance(
67                  ExclusionArtifactFilter.class.getClassLoader(), new Class[] {Path.class}, (proxy1, method, args) -> {
68                      if ("toString".equals(method.getName())) {
69                          return value;
70                      }
71                      throw new UnsupportedOperationException();
72                  });
73      }
74  }