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 java.util.Objects;
023
024import javax.inject.Inject;
025import javax.inject.Named;
026import javax.inject.Singleton;
027
028import org.eclipse.aether.RepositorySystemSession;
029import org.eclipse.aether.repository.LocalRepository;
030import org.eclipse.aether.repository.LocalRepositoryManager;
031import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
032import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
033import org.eclipse.aether.spi.locator.Service;
034import org.eclipse.aether.spi.locator.ServiceLocator;
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( "enhanced" )
045public class EnhancedLocalRepositoryManagerFactory
046    implements LocalRepositoryManagerFactory, Service
047{
048    private float priority = 10.0f;
049
050    private TrackingFileManager trackingFileManager;
051
052    public EnhancedLocalRepositoryManagerFactory()
053    {
054        // no arg ctor for ServiceLocator
055    }
056
057    @Inject
058    public EnhancedLocalRepositoryManagerFactory( final TrackingFileManager trackingFileManager )
059    {
060        this.trackingFileManager = Objects.requireNonNull( trackingFileManager );
061    }
062
063    @Override
064    public void initService( final ServiceLocator locator )
065    {
066        this.trackingFileManager = Objects.requireNonNull( locator.getService( TrackingFileManager.class ) );
067    }
068
069    public LocalRepositoryManager newInstance( RepositorySystemSession session, LocalRepository repository )
070        throws NoLocalRepositoryManagerException
071    {
072        if ( "".equals( repository.getContentType() ) || "default".equals( repository.getContentType() ) )
073        {
074            return new EnhancedLocalRepositoryManager( repository.getBasedir(), session, trackingFileManager );
075        }
076        else
077        {
078            throw new NoLocalRepositoryManagerException( repository );
079        }
080    }
081
082    public float getPriority()
083    {
084        return priority;
085    }
086
087    /**
088     * Sets the priority of this component.
089     * 
090     * @param priority The priority.
091     * @return This component for chaining, never {@code null}.
092     */
093    public EnhancedLocalRepositoryManagerFactory setPriority( float priority )
094    {
095        this.priority = priority;
096        return this;
097    }
098
099}