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;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import org.eclipse.aether.RepositorySystemSession;
026import org.eclipse.aether.repository.LocalRepository;
027import org.eclipse.aether.repository.LocalRepositoryManager;
028import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
029import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
030import org.eclipse.aether.util.ConfigUtils;
031
032import static java.util.Objects.requireNonNull;
033
034/**
035 * Creates enhanced local repository managers for repository types {@code "default"} or {@code "" (automatic)}. Enhanced
036 * local repository manager is built upon the classical Maven 2.0 local repository structure but additionally keeps
037 * track of from what repositories a cached artifact was resolved. Resolution of locally cached artifacts will be
038 * rejected in case the current resolution request does not match the known source repositories of an artifact, thereby
039 * emulating physically separated artifact caches per remote repository.
040 */
041@Singleton
042@Named(EnhancedLocalRepositoryManagerFactory.NAME)
043public class EnhancedLocalRepositoryManagerFactory implements LocalRepositoryManagerFactory {
044    public static final String NAME = "enhanced";
045    private static final String CONFIG_PROP_TRACKING_FILENAME = "aether.enhancedLocalRepository.trackingFilename";
046
047    private static final String DEFAULT_TRACKING_FILENAME = "_remote.repositories";
048
049    private float priority = 10.0f;
050
051    private final LocalPathComposer localPathComposer;
052
053    private final TrackingFileManager trackingFileManager;
054
055    private final LocalPathPrefixComposerFactory localPathPrefixComposerFactory;
056
057    @Inject
058    public EnhancedLocalRepositoryManagerFactory(
059            final LocalPathComposer localPathComposer,
060            final TrackingFileManager trackingFileManager,
061            final LocalPathPrefixComposerFactory localPathPrefixComposerFactory) {
062        this.localPathComposer = requireNonNull(localPathComposer);
063        this.trackingFileManager = requireNonNull(trackingFileManager);
064        this.localPathPrefixComposerFactory = requireNonNull(localPathPrefixComposerFactory);
065    }
066
067    @Override
068    public LocalRepositoryManager newInstance(RepositorySystemSession session, LocalRepository repository)
069            throws NoLocalRepositoryManagerException {
070        requireNonNull(session, "session cannot be null");
071        requireNonNull(repository, "repository cannot be null");
072
073        String trackingFilename = ConfigUtils.getString(session, "", CONFIG_PROP_TRACKING_FILENAME);
074        if (trackingFilename.isEmpty()
075                || trackingFilename.contains("/")
076                || trackingFilename.contains("\\")
077                || trackingFilename.contains("..")) {
078            trackingFilename = DEFAULT_TRACKING_FILENAME;
079        }
080
081        if ("".equals(repository.getContentType()) || "default".equals(repository.getContentType())) {
082            return new EnhancedLocalRepositoryManager(
083                    repository.getBasedir(),
084                    localPathComposer,
085                    trackingFilename,
086                    trackingFileManager,
087                    localPathPrefixComposerFactory.createComposer(session));
088        } else {
089            throw new NoLocalRepositoryManagerException(repository);
090        }
091    }
092
093    @Override
094    public float getPriority() {
095        return priority;
096    }
097
098    /**
099     * Sets the priority of this component.
100     *
101     * @param priority The priority.
102     * @return This component for chaining, never {@code null}.
103     */
104    public EnhancedLocalRepositoryManagerFactory setPriority(float priority) {
105        this.priority = priority;
106        return this;
107    }
108}