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.internal.impl.synccontext.named;
020
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.List;
024
025import org.eclipse.aether.RepositorySystemSession;
026import org.eclipse.aether.artifact.Artifact;
027import org.eclipse.aether.metadata.Metadata;
028import org.eclipse.aether.named.NamedLockKey;
029import org.eclipse.aether.spi.locking.LockingInhibitor;
030
031import static java.util.Objects.requireNonNull;
032
033/**
034 * Wrapping {@link NameMapper} class that applies discovered {@link org.eclipse.aether.spi.locking.LockingInhibitor}.
035 *
036 * @since 2.0.14
037 */
038public class InhibitingNameMapper implements NameMapper {
039    private final NameMapper delegate;
040    private final List<LockingInhibitor> lockingInhibitors;
041
042    public InhibitingNameMapper(NameMapper delegate, List<LockingInhibitor> lockingInhibitors) {
043        this.delegate = requireNonNull(delegate);
044        this.lockingInhibitors = requireNonNull(lockingInhibitors);
045    }
046
047    @Override
048    public boolean isFileSystemFriendly() {
049        return delegate.isFileSystemFriendly();
050    }
051
052    @Override
053    public Collection<NamedLockKey> nameLocks(
054            RepositorySystemSession session,
055            Collection<? extends Artifact> artifacts,
056            Collection<? extends Metadata> metadatas) {
057        if (lockingInhibitors.isEmpty()) {
058            return delegate.nameLocks(session, artifacts, metadatas);
059        }
060        if (artifacts != null && !artifacts.isEmpty()) {
061            List<Artifact> filtered = new ArrayList<>(artifacts.size());
062            for (Artifact a : artifacts) {
063                if (!isArtifactInhibited(a)) {
064                    filtered.add(a);
065                }
066            }
067            artifacts = filtered;
068        }
069        if (metadatas != null && !metadatas.isEmpty()) {
070            List<Metadata> filtered = new ArrayList<>(metadatas.size());
071            for (Metadata m : metadatas) {
072                if (!isMetadataInhibited(m)) {
073                    filtered.add(m);
074                }
075            }
076            metadatas = filtered;
077        }
078        return delegate.nameLocks(session, artifacts, metadatas);
079    }
080
081    private boolean isArtifactInhibited(Artifact artifact) {
082        for (LockingInhibitor inhibitor : lockingInhibitors) {
083            if (inhibitor.preventArtifactLocking(artifact)) {
084                return true;
085            }
086        }
087        return false;
088    }
089
090    private boolean isMetadataInhibited(Metadata metadata) {
091        for (LockingInhibitor inhibitor : lockingInhibitors) {
092            if (inhibitor.preventMetadataLocking(metadata)) {
093                return true;
094            }
095        }
096        return false;
097    }
098}