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        requireNonNull( session, "session cannot be null" );
096        requireNonNull( repository, "repository cannot be null" );
097        PrioritizedComponents<LocalRepositoryManagerFactory> factories = new PrioritizedComponents<>( session );
098        for ( LocalRepositoryManagerFactory factory : this.managerFactories )
099        {
100            factories.add( factory, factory.getPriority() );
101        }
102
103        List<NoLocalRepositoryManagerException> errors = new ArrayList<>();
104        for ( PrioritizedComponent<LocalRepositoryManagerFactory> factory : factories.getEnabled() )
105        {
106            try
107            {
108                LocalRepositoryManager manager = factory.getComponent().newInstance( session, repository );
109
110                if ( LOGGER.isDebugEnabled() )
111                {
112                    StringBuilder buffer = new StringBuilder( 256 );
113                    buffer.append( "Using manager " ).append( manager.getClass().getSimpleName() );
114                    Utils.appendClassLoader( buffer, manager );
115                    buffer.append( " with priority " ).append( factory.getPriority() );
116                    buffer.append( " for " ).append( repository.getBasedir() );
117
118                    LOGGER.debug( buffer.toString() );
119                }
120
121                return manager;
122            }
123            catch ( NoLocalRepositoryManagerException e )
124            {
125                // continue and try next factory
126                errors.add( e );
127            }
128        }
129        if ( LOGGER.isDebugEnabled() && errors.size() > 1 )
130        {
131            for ( Exception e : errors )
132            {
133                LOGGER.debug( "Could not obtain local repository manager for {}", repository, e );
134            }
135        }
136
137        StringBuilder buffer = new StringBuilder( 256 );
138        if ( factories.isEmpty() )
139        {
140            buffer.append( "No local repository managers registered" );
141        }
142        else
143        {
144            buffer.append( "Cannot access " ).append( repository.getBasedir() );
145            buffer.append( " with type " ).append( repository.getContentType() );
146            buffer.append( " using the available factories " );
147            factories.list( buffer );
148        }
149
150        throw new NoLocalRepositoryManagerException( repository, buffer.toString(), errors.size() == 1 ? errors.get( 0 )
151                        : null );
152    }
153
154}