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