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