001package org.eclipse.aether.internal.impl;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import javax.inject.Inject;
023import javax.inject.Named;
024import javax.inject.Singleton;
025
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.spi.locator.Service;
032import org.eclipse.aether.spi.locator.ServiceLocator;
033import org.eclipse.aether.util.ConfigUtils;
034
035import static java.util.Objects.requireNonNull;
036
037/**
038 * Creates enhanced local repository managers for repository types {@code "default"} or {@code "" (automatic)}. Enhanced
039 * local repository manager is built upon the classical Maven 2.0 local repository structure but additionally keeps
040 * track of from what repositories a cached artifact was resolved. Resolution of locally cached artifacts will be
041 * rejected in case the current resolution request does not match the known source repositories of an artifact, thereby
042 * emulating physically separated artifact caches per remote repository.
043 */
044@Singleton
045@Named( "enhanced" )
046public class EnhancedLocalRepositoryManagerFactory
047    implements LocalRepositoryManagerFactory, Service
048{
049    private static final String CONFIG_PROP_TRACKING_FILENAME = "aether.enhancedLocalRepository.trackingFilename";
050
051    private static final String DEFAULT_TRACKING_FILENAME = "_remote.repositories";
052
053    private float priority = 10.0f;
054
055    private LocalPathComposer localPathComposer;
056
057    private TrackingFileManager trackingFileManager;
058
059    private LocalPathPrefixComposerFactory localPathPrefixComposerFactory;
060
061    public EnhancedLocalRepositoryManagerFactory()
062    {
063        // no arg ctor for ServiceLocator
064    }
065
066    @Inject
067    public EnhancedLocalRepositoryManagerFactory( final LocalPathComposer localPathComposer,
068                   final TrackingFileManager trackingFileManager,
069                   final LocalPathPrefixComposerFactory localPathPrefixComposerFactory )
070    {
071        this.localPathComposer = requireNonNull( localPathComposer );
072        this.trackingFileManager = requireNonNull( trackingFileManager );
073        this.localPathPrefixComposerFactory = requireNonNull( localPathPrefixComposerFactory );
074    }
075
076    @Override
077    public void initService( final ServiceLocator locator )
078    {
079        this.localPathComposer = requireNonNull( locator.getService( LocalPathComposer.class ) );
080        this.trackingFileManager = requireNonNull( locator.getService( TrackingFileManager.class ) );
081        this.localPathPrefixComposerFactory = new DefaultLocalPathPrefixComposerFactory();
082    }
083
084    @Override
085    public LocalRepositoryManager newInstance( RepositorySystemSession session, LocalRepository repository )
086        throws NoLocalRepositoryManagerException
087    {
088        requireNonNull( session, "session cannot be null" );
089        requireNonNull( repository, "repository cannot be null" );
090
091        String trackingFilename = ConfigUtils.getString( session, "", CONFIG_PROP_TRACKING_FILENAME );
092        if ( trackingFilename.isEmpty() || trackingFilename.contains( "/" ) || trackingFilename.contains( "\\" )
093                || trackingFilename.contains( ".." ) )
094        {
095            trackingFilename = DEFAULT_TRACKING_FILENAME;
096        }
097
098        if ( "".equals( repository.getContentType() ) || "default".equals( repository.getContentType() ) )
099        {
100            return new EnhancedLocalRepositoryManager(
101                    repository.getBasedir(),
102                    localPathComposer,
103                    trackingFilename,
104                    trackingFileManager,
105                    localPathPrefixComposerFactory.createComposer( session )
106            );
107        }
108        else
109        {
110            throw new NoLocalRepositoryManagerException( repository );
111        }
112    }
113
114    @Override
115    public float getPriority()
116    {
117        return priority;
118    }
119
120    /**
121     * Sets the priority of this component.
122     *
123     * @param priority The priority.
124     * @return This component for chaining, never {@code null}.
125     */
126    public EnhancedLocalRepositoryManagerFactory setPriority( float priority )
127    {
128        this.priority = priority;
129        return this;
130    }
131
132}