View Javadoc
1   package org.eclipse.aether.util.graph.version;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
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
36      implements VersionFilter
37  {
38  
39      /**
40       * The key in the repository session's {@link RepositorySystemSession#getConfigProperties() configuration
41       * properties} used to store a {@link Boolean} flag whether this filter should be forced to ban snapshots. By
42       * default, snapshots are only filtered if the root artifact is not a snapshot.
43       */
44      public static final String CONFIG_PROP_ENABLE = "aether.snapshotFilter";
45  
46      private final SnapshotVersionFilter filter;
47  
48      /**
49       * Creates a new instance of this version filter.
50       */
51      public ContextualSnapshotVersionFilter()
52      {
53          filter = new SnapshotVersionFilter();
54      }
55  
56      private boolean isEnabled( RepositorySystemSession session )
57      {
58          return ConfigUtils.getBoolean( session, false, CONFIG_PROP_ENABLE );
59      }
60  
61      public void filterVersions( VersionFilterContext context )
62      {
63          if ( isEnabled( context.getSession() ) )
64          {
65              filter.filterVersions( context );
66          }
67      }
68  
69      public VersionFilter deriveChildFilter( DependencyCollectionContext context )
70      {
71          if ( !isEnabled( context.getSession() ) )
72          {
73              Artifact artifact = context.getArtifact();
74              if ( artifact == null )
75              {
76                  // no root artifact to test, allow snapshots and recheck once we reach the direct dependencies
77                  return this;
78              }
79              if ( artifact.isSnapshot() )
80              {
81                  // root is a snapshot, allow snapshots all the way down
82                  return null;
83              }
84          }
85          // artifact is a non-snapshot or filter explicitly enabled, block snapshots all the way down
86          return filter;
87      }
88  
89      @Override
90      public boolean equals( Object obj )
91      {
92          if ( this == obj )
93          {
94              return true;
95          }
96          else if ( null == obj || !getClass().equals( obj.getClass() ) )
97          {
98              return false;
99          }
100         return true;
101     }
102 
103     @Override
104     public int hashCode()
105     {
106         return getClass().hashCode();
107     }
108 
109 }