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.repository;
020
021import org.eclipse.aether.RepositorySystemSession;
022import org.eclipse.aether.artifact.Artifact;
023import org.eclipse.aether.metadata.Metadata;
024import org.eclipse.aether.resolution.ResolutionErrorPolicy;
025import org.eclipse.aether.resolution.ResolutionErrorPolicyRequest;
026
027import static java.util.Objects.requireNonNull;
028
029/**
030 * A resolution error policy that allows to control caching for artifacts and metadata at a global level.
031 */
032public final class SimpleResolutionErrorPolicy implements ResolutionErrorPolicy {
033
034    private final int artifactPolicy;
035
036    private final int metadataPolicy;
037
038    /**
039     * Creates a new error policy with the specified behavior for both artifacts and metadata.
040     *
041     * @param cacheNotFound {@code true} to enable caching of missing items, {@code false} to disable it.
042     * @param cacheTransferErrors {@code true} to enable chaching of transfer errors, {@code false} to disable it.
043     */
044    public SimpleResolutionErrorPolicy(boolean cacheNotFound, boolean cacheTransferErrors) {
045        this((cacheNotFound ? CACHE_NOT_FOUND : 0) | (cacheTransferErrors ? CACHE_TRANSFER_ERROR : 0));
046    }
047
048    /**
049     * Creates a new error policy with the specified bit mask for both artifacts and metadata.
050     *
051     * @param policy The bit mask describing the policy for artifacts and metadata.
052     */
053    public SimpleResolutionErrorPolicy(int policy) {
054        this(policy, policy);
055    }
056
057    /**
058     * Creates a new error policy with the specified bit masks for artifacts and metadata.
059     *
060     * @param artifactPolicy The bit mask describing the policy for artifacts.
061     * @param metadataPolicy The bit mask describing the policy for metadata.
062     */
063    public SimpleResolutionErrorPolicy(int artifactPolicy, int metadataPolicy) {
064        this.artifactPolicy = artifactPolicy;
065        this.metadataPolicy = metadataPolicy;
066    }
067
068    public int getArtifactPolicy(RepositorySystemSession session, ResolutionErrorPolicyRequest<Artifact> request) {
069        requireNonNull(session, "session cannot be null");
070        requireNonNull(request, "request cannot be null");
071        return artifactPolicy;
072    }
073
074    public int getMetadataPolicy(RepositorySystemSession session, ResolutionErrorPolicyRequest<Metadata> request) {
075        requireNonNull(session, "session cannot be null");
076        requireNonNull(request, "request cannot be null");
077        return metadataPolicy;
078    }
079}