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