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.version;
20  
21  import org.eclipse.aether.ConfigurationProperties;
22  import org.eclipse.aether.RepositorySystemSession;
23  import org.eclipse.aether.artifact.Artifact;
24  import org.eclipse.aether.collection.DependencyCollectionContext;
25  import org.eclipse.aether.collection.VersionFilter;
26  import org.eclipse.aether.util.ConfigUtils;
27  
28  /**
29   * A version filter that blocks "*-SNAPSHOT" versions if the
30   * {@link org.eclipse.aether.collection.CollectRequest#getRootArtifact() root artifact} of the dependency graph is not a
31   * snapshot. Alternatively, this filter can be forced to always ban snapshot versions by setting the boolean
32   * {@link RepositorySystemSession#getConfigProperties() configuration property} {@link #CONFIG_PROP_ENABLE} to
33   * {@code true}.
34   */
35  public final class ContextualSnapshotVersionFilter implements VersionFilter {
36      /**
37       * The key in the repository session's {@link RepositorySystemSession#getConfigProperties() configuration
38       * properties} used to store a {@link Boolean} flag whether this filter should be forced to ban snapshots. By
39       * default, snapshots are only filtered if the root artifact is not a snapshot.
40       *
41       * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
42       * @configurationType {@link java.lang.Boolean}
43       * @configurationDefaultValue false
44       */
45      public static final String CONFIG_PROP_ENABLE = ConfigurationProperties.PREFIX_AETHER + "snapshotFilter";
46  
47      private final SnapshotVersionFilter filter;
48  
49      /**
50       * Creates a new instance of this version filter.
51       */
52      public ContextualSnapshotVersionFilter() {
53          filter = new SnapshotVersionFilter();
54      }
55  
56      private boolean isEnabled(RepositorySystemSession session) {
57          return ConfigUtils.getBoolean(session, false, CONFIG_PROP_ENABLE);
58      }
59  
60      @Override
61      public void filterVersions(VersionFilterContext context) {
62          if (isEnabled(context.getSession())) {
63              filter.filterVersions(context);
64          }
65      }
66  
67      @Override
68      public VersionFilter deriveChildFilter(DependencyCollectionContext context) {
69          if (!isEnabled(context.getSession())) {
70              Artifact artifact = context.getArtifact();
71              if (artifact == null) {
72                  // no root artifact to test, allow snapshots and recheck once we reach the direct dependencies
73                  return this;
74              }
75              if (artifact.isSnapshot()) {
76                  // root is a snapshot, allow snapshots all the way down
77                  return null;
78              }
79          }
80          // artifact is a non-snapshot or filter explicitly enabled, block snapshots all the way down
81          return filter;
82      }
83  
84      @Override
85      public boolean equals(Object obj) {
86          if (this == obj) {
87              return true;
88          } else if (null == obj || !getClass().equals(obj.getClass())) {
89              return false;
90          }
91          return true;
92      }
93  
94      @Override
95      public int hashCode() {
96          return getClass().hashCode();
97      }
98  }