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