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