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.ConfigurationProperties; 022import org.eclipse.aether.RepositorySystemSession; 023import org.eclipse.aether.artifact.Artifact; 024import org.eclipse.aether.collection.DependencyCollectionContext; 025import org.eclipse.aether.collection.VersionFilter; 026import org.eclipse.aether.util.ConfigUtils; 027 028/** 029 * A version filter that blocks "*-SNAPSHOT" versions if the 030 * {@link org.eclipse.aether.collection.CollectRequest#getRootArtifact() root artifact} of the dependency graph is not a 031 * snapshot. Alternatively, this filter can be forced to always ban snapshot versions by setting the boolean 032 * {@link RepositorySystemSession#getConfigProperties() configuration property} {@link #CONFIG_PROP_ENABLE} to 033 * {@code true}. 034 */ 035public final class ContextualSnapshotVersionFilter implements VersionFilter { 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 * @configurationSource {@link RepositorySystemSession#getConfigProperties()} 042 * @configurationType {@link java.lang.Boolean} 043 * @configurationDefaultValue false 044 */ 045 public static final String CONFIG_PROP_ENABLE = ConfigurationProperties.PREFIX_AETHER + "snapshotFilter"; 046 047 private final SnapshotVersionFilter filter; 048 049 /** 050 * Creates a new instance of this version filter. 051 */ 052 public ContextualSnapshotVersionFilter() { 053 filter = new SnapshotVersionFilter(); 054 } 055 056 private boolean isEnabled(RepositorySystemSession session) { 057 return ConfigUtils.getBoolean(session, false, CONFIG_PROP_ENABLE); 058 } 059 060 public void filterVersions(VersionFilterContext context) { 061 if (isEnabled(context.getSession())) { 062 filter.filterVersions(context); 063 } 064 } 065 066 public VersionFilter deriveChildFilter(DependencyCollectionContext context) { 067 if (!isEnabled(context.getSession())) { 068 Artifact artifact = context.getArtifact(); 069 if (artifact == null) { 070 // no root artifact to test, allow snapshots and recheck once we reach the direct dependencies 071 return this; 072 } 073 if (artifact.isSnapshot()) { 074 // root is a snapshot, allow snapshots all the way down 075 return null; 076 } 077 } 078 // artifact is a non-snapshot or filter explicitly enabled, block snapshots all the way down 079 return filter; 080 } 081 082 @Override 083 public boolean equals(Object obj) { 084 if (this == obj) { 085 return true; 086 } else if (null == obj || !getClass().equals(obj.getClass())) { 087 return false; 088 } 089 return true; 090 } 091 092 @Override 093 public int hashCode() { 094 return getClass().hashCode(); 095 } 096}