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.spi.locator.Service;
031import org.eclipse.aether.spi.locator.ServiceLocator;
032import org.eclipse.aether.util.ConfigUtils;
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, Service {
046    public static final String NAME = "enhanced";
047    private static final String CONFIG_PROP_TRACKING_FILENAME = "aether.enhancedLocalRepository.trackingFilename";
048
049    private static final String DEFAULT_TRACKING_FILENAME = "_remote.repositories";
050
051    private float priority = 10.0f;
052
053    private LocalPathComposer localPathComposer;
054
055    private TrackingFileManager trackingFileManager;
056
057    private LocalPathPrefixComposerFactory localPathPrefixComposerFactory;
058
059    @Deprecated
060    public EnhancedLocalRepositoryManagerFactory() {
061        // no arg ctor for ServiceLocator
062    }
063
064    @Inject
065    public EnhancedLocalRepositoryManagerFactory(
066            final LocalPathComposer localPathComposer,
067            final TrackingFileManager trackingFileManager,
068            final LocalPathPrefixComposerFactory localPathPrefixComposerFactory) {
069        this.localPathComposer = requireNonNull(localPathComposer);
070        this.trackingFileManager = requireNonNull(trackingFileManager);
071        this.localPathPrefixComposerFactory = requireNonNull(localPathPrefixComposerFactory);
072    }
073
074    @Override
075    public void initService(final ServiceLocator locator) {
076        this.localPathComposer = requireNonNull(locator.getService(LocalPathComposer.class));
077        this.trackingFileManager = requireNonNull(locator.getService(TrackingFileManager.class));
078        this.localPathPrefixComposerFactory = new DefaultLocalPathPrefixComposerFactory();
079    }
080
081    @Override
082    public LocalRepositoryManager newInstance(RepositorySystemSession session, LocalRepository repository)
083            throws NoLocalRepositoryManagerException {
084        requireNonNull(session, "session cannot be null");
085        requireNonNull(repository, "repository cannot be null");
086
087        String trackingFilename = ConfigUtils.getString(session, "", CONFIG_PROP_TRACKING_FILENAME);
088        if (trackingFilename.isEmpty()
089                || trackingFilename.contains("/")
090                || trackingFilename.contains("\\")
091                || trackingFilename.contains("..")) {
092            trackingFilename = DEFAULT_TRACKING_FILENAME;
093        }
094
095        if ("".equals(repository.getContentType()) || "default".equals(repository.getContentType())) {
096            return new EnhancedLocalRepositoryManager(
097                    repository.getBasedir(),
098                    localPathComposer,
099                    trackingFilename,
100                    trackingFileManager,
101                    localPathPrefixComposerFactory.createComposer(session));
102        } else {
103            throw new NoLocalRepositoryManagerException(repository);
104        }
105    }
106
107    @Override
108    public float getPriority() {
109        return priority;
110    }
111
112    /**
113     * Sets the priority of this component.
114     *
115     * @param priority The priority.
116     * @return This component for chaining, never {@code null}.
117     */
118    public EnhancedLocalRepositoryManagerFactory setPriority(float priority) {
119        this.priority = priority;
120        return this;
121    }
122}