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      public void filterVersions(VersionFilterContext context) {
61          if (isEnabled(context.getSession())) {
62              filter.filterVersions(context);
63          }
64      }
65  
66      public VersionFilter deriveChildFilter(DependencyCollectionContext context) {
67          if (!isEnabled(context.getSession())) {
68              Artifact artifact = context.getArtifact();
69              if (artifact == null) {
70                  // no root artifact to test, allow snapshots and recheck once we reach the direct dependencies
71                  return this;
72              }
73              if (artifact.isSnapshot()) {
74                  // root is a snapshot, allow snapshots all the way down
75                  return null;
76              }
77          }
78          // artifact is a non-snapshot or filter explicitly enabled, block snapshots all the way down
79          return filter;
80      }
81  
82      @Override
83      public boolean equals(Object obj) {
84          if (this == obj) {
85              return true;
86          } else if (null == obj || !getClass().equals(obj.getClass())) {
87              return false;
88          }
89          return true;
90      }
91  
92      @Override
93      public int hashCode() {
94          return getClass().hashCode();
95      }
96  }