001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.util.graph.version; 020 021import org.eclipse.aether.RepositorySystemSession; 022import org.eclipse.aether.artifact.Artifact; 023import org.eclipse.aether.collection.DependencyCollectionContext; 024import org.eclipse.aether.collection.VersionFilter; 025import org.eclipse.aether.util.ConfigUtils; 026 027/** 028 * A version filter that blocks "*-SNAPSHOT" versions if the 029 * {@link org.eclipse.aether.collection.CollectRequest#getRootArtifact() root artifact} of the dependency graph is not a 030 * snapshot. Alternatively, this filter can be forced to always ban snapshot versions by setting the boolean 031 * {@link RepositorySystemSession#getConfigProperties() configuration property} {@link #CONFIG_PROP_ENABLE} to 032 * {@code true}. 033 */ 034public final class ContextualSnapshotVersionFilter implements VersionFilter { 035 036 /** 037 * The key in the repository session's {@link RepositorySystemSession#getConfigProperties() configuration 038 * properties} used to store a {@link Boolean} flag whether this filter should be forced to ban snapshots. By 039 * default, snapshots are only filtered if the root artifact is not a snapshot. 040 */ 041 public static final String CONFIG_PROP_ENABLE = "aether.snapshotFilter"; 042 043 private final SnapshotVersionFilter filter; 044 045 /** 046 * Creates a new instance of this version filter. 047 */ 048 public ContextualSnapshotVersionFilter() { 049 filter = new SnapshotVersionFilter(); 050 } 051 052 private boolean isEnabled(RepositorySystemSession session) { 053 return ConfigUtils.getBoolean(session, false, CONFIG_PROP_ENABLE); 054 } 055 056 public void filterVersions(VersionFilterContext context) { 057 if (isEnabled(context.getSession())) { 058 filter.filterVersions(context); 059 } 060 } 061 062 public VersionFilter deriveChildFilter(DependencyCollectionContext context) { 063 if (!isEnabled(context.getSession())) { 064 Artifact artifact = context.getArtifact(); 065 if (artifact == null) { 066 // no root artifact to test, allow snapshots and recheck once we reach the direct dependencies 067 return this; 068 } 069 if (artifact.isSnapshot()) { 070 // root is a snapshot, allow snapshots all the way down 071 return null; 072 } 073 } 074 // artifact is a non-snapshot or filter explicitly enabled, block snapshots all the way down 075 return filter; 076 } 077 078 @Override 079 public boolean equals(Object obj) { 080 if (this == obj) { 081 return true; 082 } else if (null == obj || !getClass().equals(obj.getClass())) { 083 return false; 084 } 085 return true; 086 } 087 088 @Override 089 public int hashCode() { 090 return getClass().hashCode(); 091 } 092}