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    @Override
061    public void filterVersions(VersionFilterContext context) {
062        if (isEnabled(context.getSession())) {
063            filter.filterVersions(context);
064        }
065    }
066
067    @Override
068    public VersionFilter deriveChildFilter(DependencyCollectionContext context) {
069        if (!isEnabled(context.getSession())) {
070            Artifact artifact = context.getArtifact();
071            if (artifact == null) {
072                // no root artifact to test, allow snapshots and recheck once we reach the direct dependencies
073                return this;
074            }
075            if (artifact.isSnapshot()) {
076                // root is a snapshot, allow snapshots all the way down
077                return null;
078            }
079        }
080        // artifact is a non-snapshot or filter explicitly enabled, block snapshots all the way down
081        return filter;
082    }
083
084    @Override
085    public boolean equals(Object obj) {
086        if (this == obj) {
087            return true;
088        } else if (null == obj || !getClass().equals(obj.getClass())) {
089            return false;
090        }
091        return true;
092    }
093
094    @Override
095    public int hashCode() {
096        return getClass().hashCode();
097    }
098}