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.ArrayList;
023import java.util.Collection;
024import java.util.List;
025import static java.util.Objects.requireNonNull;
026import java.util.Set;
027
028import javax.inject.Inject;
029import javax.inject.Named;
030import javax.inject.Singleton;
031
032import org.eclipse.aether.RepositorySystemSession;
033import org.eclipse.aether.impl.LocalRepositoryProvider;
034import org.eclipse.aether.repository.LocalRepository;
035import org.eclipse.aether.repository.LocalRepositoryManager;
036import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
037import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
038import org.eclipse.aether.spi.locator.Service;
039import org.eclipse.aether.spi.locator.ServiceLocator;
040import org.slf4j.Logger;
041import org.slf4j.LoggerFactory;
042
043/**
044 */
045@Singleton
046@Named
047public class DefaultLocalRepositoryProvider
048    implements LocalRepositoryProvider, Service
049{
050
051    private static final Logger LOGGER = LoggerFactory.getLogger( DefaultLocalRepositoryProvider.class );
052
053    private Collection<LocalRepositoryManagerFactory> managerFactories = new ArrayList<>();
054
055    public DefaultLocalRepositoryProvider()
056    {
057        // enables default constructor
058    }
059
060    @Inject
061    DefaultLocalRepositoryProvider( Set<LocalRepositoryManagerFactory> factories )
062    {
063        setLocalRepositoryManagerFactories( factories );
064    }
065
066    public void initService( ServiceLocator locator )
067    {
068        setLocalRepositoryManagerFactories( locator.getServices( LocalRepositoryManagerFactory.class ) );
069    }
070
071    public DefaultLocalRepositoryProvider addLocalRepositoryManagerFactory( LocalRepositoryManagerFactory factory )
072    {
073        managerFactories.add( requireNonNull( factory, "local repository manager factory cannot be null" ) );
074        return this;
075    }
076
077    public DefaultLocalRepositoryProvider setLocalRepositoryManagerFactories(
078            Collection<LocalRepositoryManagerFactory> factories )
079    {
080        if ( factories == null )
081        {
082            managerFactories = new ArrayList<>( 2 );
083        }
084        else
085        {
086            managerFactories = factories;
087        }
088        return this;
089    }
090
091    public LocalRepositoryManager newLocalRepositoryManager( RepositorySystemSession session,
092                                                             LocalRepository repository )
093        throws NoLocalRepositoryManagerException
094    {
095        PrioritizedComponents<LocalRepositoryManagerFactory> factories = new PrioritizedComponents<>( session );
096        for ( LocalRepositoryManagerFactory factory : this.managerFactories )
097        {
098            factories.add( factory, factory.getPriority() );
099        }
100
101        List<NoLocalRepositoryManagerException> errors = new ArrayList<>();
102        for ( PrioritizedComponent<LocalRepositoryManagerFactory> factory : factories.getEnabled() )
103        {
104            try
105            {
106                LocalRepositoryManager manager = factory.getComponent().newInstance( session, repository );
107
108                if ( LOGGER.isDebugEnabled() )
109                {
110                    StringBuilder buffer = new StringBuilder( 256 );
111                    buffer.append( "Using manager " ).append( manager.getClass().getSimpleName() );
112                    Utils.appendClassLoader( buffer, manager );
113                    buffer.append( " with priority " ).append( factory.getPriority() );
114                    buffer.append( " for " ).append( repository.getBasedir() );
115
116                    LOGGER.debug( buffer.toString() );
117                }
118
119                return manager;
120            }
121            catch ( NoLocalRepositoryManagerException e )
122            {
123                // continue and try next factory
124                errors.add( e );
125            }
126        }
127        if ( LOGGER.isDebugEnabled() && errors.size() > 1 )
128        {
129            for ( Exception e : errors )
130            {
131                LOGGER.debug( "Could not obtain local repository manager for {}", repository, e );
132            }
133        }
134
135        StringBuilder buffer = new StringBuilder( 256 );
136        if ( factories.isEmpty() )
137        {
138            buffer.append( "No local repository managers registered" );
139        }
140        else
141        {
142            buffer.append( "Cannot access " ).append( repository.getBasedir() );
143            buffer.append( " with type " ).append( repository.getContentType() );
144            buffer.append( " using the available factories " );
145            factories.list( buffer );
146        }
147
148        throw new NoLocalRepositoryManagerException( repository, buffer.toString(), errors.size() == 1 ? errors.get( 0 )
149                        : null );
150    }
151
152}