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.eclipse.aether.util.graph.selector;
20  
21  import java.util.Arrays;
22  import java.util.Collection;
23  import java.util.Comparator;
24  import java.util.TreeSet;
25  
26  import org.eclipse.aether.artifact.Artifact;
27  import org.eclipse.aether.collection.DependencyCollectionContext;
28  import org.eclipse.aether.collection.DependencySelector;
29  import org.eclipse.aether.graph.Dependency;
30  import org.eclipse.aether.graph.Exclusion;
31  
32  import static java.util.Objects.requireNonNull;
33  
34  /**
35   * A dependency selector that applies exclusions based on artifact coordinates.
36   *
37   * @see Dependency#getExclusions()
38   */
39  public final class ExclusionDependencySelector implements DependencySelector {
40  
41      // sorted and dupe-free array, faster to iterate than LinkedHashSet
42      private final Exclusion[] exclusions;
43  
44      private int hashCode;
45  
46      /**
47       * Creates a new selector without any exclusions.
48       */
49      public ExclusionDependencySelector() {
50          this.exclusions = new Exclusion[0];
51      }
52  
53      /**
54       * Creates a new selector with the specified exclusions.
55       *
56       * @param exclusions The exclusions, may be {@code null}.
57       */
58      public ExclusionDependencySelector(Collection<Exclusion> exclusions) {
59          if (exclusions != null && !exclusions.isEmpty()) {
60              TreeSet<Exclusion> sorted = new TreeSet<>(ExclusionComparator.INSTANCE);
61              sorted.addAll(exclusions);
62              this.exclusions = sorted.toArray(new Exclusion[0]);
63          } else {
64              this.exclusions = new Exclusion[0];
65          }
66      }
67  
68      private ExclusionDependencySelector(Exclusion[] exclusions) {
69          this.exclusions = exclusions;
70      }
71  
72      public boolean selectDependency(Dependency dependency) {
73          requireNonNull(dependency, "dependency cannot be null");
74          Artifact artifact = dependency.getArtifact();
75          for (Exclusion exclusion : exclusions) {
76              if (matches(exclusion, artifact)) {
77                  return false;
78              }
79          }
80          return true;
81      }
82  
83      private boolean matches(Exclusion exclusion, Artifact artifact) {
84          if (!matches(exclusion.getArtifactId(), artifact.getArtifactId())) {
85              return false;
86          }
87          if (!matches(exclusion.getGroupId(), artifact.getGroupId())) {
88              return false;
89          }
90          if (!matches(exclusion.getExtension(), artifact.getExtension())) {
91              return false;
92          }
93          return matches(exclusion.getClassifier(), artifact.getClassifier());
94      }
95  
96      private boolean matches(String pattern, String value) {
97          return "*".equals(pattern) || pattern.equals(value);
98      }
99  
100     public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
101         requireNonNull(context, "context cannot be null");
102         Dependency dependency = context.getDependency();
103         Collection<Exclusion> exclusions = (dependency != null) ? dependency.getExclusions() : null;
104         if (exclusions == null || exclusions.isEmpty()) {
105             return this;
106         }
107 
108         Exclusion[] merged = this.exclusions;
109         int count = merged.length;
110         for (Exclusion exclusion : exclusions) {
111             int index = Arrays.binarySearch(merged, exclusion, ExclusionComparator.INSTANCE);
112             if (index < 0) {
113                 index = -(index + 1);
114                 if (count >= merged.length) {
115                     Exclusion[] tmp = new Exclusion[merged.length + exclusions.size()];
116                     System.arraycopy(merged, 0, tmp, 0, index);
117                     tmp[index] = exclusion;
118                     System.arraycopy(merged, index, tmp, index + 1, count - index);
119                     merged = tmp;
120                 } else {
121                     System.arraycopy(merged, index, merged, index + 1, count - index);
122                     merged[index] = exclusion;
123                 }
124                 count++;
125             }
126         }
127         if (merged == this.exclusions) {
128             return this;
129         }
130         if (merged.length != count) {
131             Exclusion[] tmp = new Exclusion[count];
132             System.arraycopy(merged, 0, tmp, 0, count);
133             merged = tmp;
134         }
135 
136         return new ExclusionDependencySelector(merged);
137     }
138 
139     @Override
140     public boolean equals(Object obj) {
141         if (this == obj) {
142             return true;
143         } else if (null == obj || !getClass().equals(obj.getClass())) {
144             return false;
145         }
146 
147         ExclusionDependencySelector that = (ExclusionDependencySelector) obj;
148         return Arrays.equals(exclusions, that.exclusions);
149     }
150 
151     @Override
152     public int hashCode() {
153         if (hashCode == 0) {
154             int hash = getClass().hashCode();
155             hash = hash * 31 + Arrays.hashCode(exclusions);
156             hashCode = hash;
157         }
158         return hashCode;
159     }
160 
161     @Override
162     public String toString() {
163         StringBuilder builder =
164                 new StringBuilder().append(this.getClass().getSimpleName()).append('(');
165         for (int i = 0; i < this.exclusions.length; i++) {
166             builder.append(this.exclusions[i]);
167             if (i < this.exclusions.length - 1) {
168                 builder.append(", ");
169             }
170         }
171         return builder.append(')').toString();
172     }
173 
174     private static class ExclusionComparator implements Comparator<Exclusion> {
175 
176         static final ExclusionComparator INSTANCE = new ExclusionComparator();
177 
178         public int compare(Exclusion e1, Exclusion e2) {
179             if (e1 == null) {
180                 return (e2 == null) ? 0 : 1;
181             } else if (e2 == null) {
182                 return -1;
183             }
184             int rel = e1.getArtifactId().compareTo(e2.getArtifactId());
185             if (rel == 0) {
186                 rel = e1.getGroupId().compareTo(e2.getGroupId());
187                 if (rel == 0) {
188                     rel = e1.getExtension().compareTo(e2.getExtension());
189                     if (rel == 0) {
190                         rel = e1.getClassifier().compareTo(e2.getClassifier());
191                     }
192                 }
193             }
194             return rel;
195         }
196     }
197 }