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.filter;
020
021import javax.inject.Named;
022import javax.inject.Singleton;
023
024import java.util.Optional;
025import java.util.function.Predicate;
026
027import org.eclipse.aether.RepositorySystemSession;
028import org.eclipse.aether.metadata.Metadata;
029import org.eclipse.aether.spi.locking.LockingInhibitor;
030import org.eclipse.aether.spi.locking.LockingInhibitorFactory;
031
032/**
033 * Locking inhibitor for RRF prefix files. They are perfect candidates, as they are created on remote, and locally are
034 * only cached and read, and they do not clash on local file system (local repo) either, as they are stored in a file
035 * that has origin repository factored in (as any metadata is).
036 *
037 * @since 2.0.14
038 */
039@Singleton
040@Named(PrefixesLockingInhibitorFactory.NAME)
041public class PrefixesLockingInhibitorFactory implements LockingInhibitorFactory, LockingInhibitor {
042    public static final String NAME = PrefixesRemoteRepositoryFilterSource.NAME;
043
044    /**
045     * Metadata predicate tailored to RRF prefixes.
046     */
047    private static final Predicate<Metadata> PREFIX_PREDICATE = m -> "".equals(m.getGroupId())
048            && "".equals(m.getArtifactId())
049            && "".equals(m.getVersion())
050            && PrefixesRemoteRepositoryFilterSource.PREFIX_FILE_TYPE.equals(m.getType());
051
052    @Override
053    public Optional<LockingInhibitor> newInstance(RepositorySystemSession session) {
054        return Optional.of(this);
055    }
056
057    @Override
058    public boolean preventMetadataLocking(Metadata metadata) {
059        return PREFIX_PREDICATE.test(metadata);
060    }
061}