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 java.util.Set;
026
027import javax.inject.Inject;
028import javax.inject.Named;
029
030import org.eclipse.aether.RepositorySystemSession;
031import org.eclipse.aether.repository.RemoteRepository;
032import org.eclipse.aether.spi.connector.layout.RepositoryLayout;
033import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory;
034import org.eclipse.aether.spi.connector.layout.RepositoryLayoutProvider;
035import org.eclipse.aether.spi.locator.Service;
036import org.eclipse.aether.spi.locator.ServiceLocator;
037import org.eclipse.aether.spi.log.Logger;
038import org.eclipse.aether.spi.log.LoggerFactory;
039import org.eclipse.aether.spi.log.NullLoggerFactory;
040import org.eclipse.aether.transfer.NoRepositoryLayoutException;
041
042/**
043 */
044@Named
045public final class DefaultRepositoryLayoutProvider
046    implements RepositoryLayoutProvider, Service
047{
048
049    private Logger logger = NullLoggerFactory.LOGGER;
050
051    private Collection<RepositoryLayoutFactory> factories = new ArrayList<RepositoryLayoutFactory>();
052
053    public DefaultRepositoryLayoutProvider()
054    {
055        // enables default constructor
056    }
057
058    @Inject
059    DefaultRepositoryLayoutProvider( Set<RepositoryLayoutFactory> layoutFactories, LoggerFactory loggerFactory )
060    {
061        setLoggerFactory( loggerFactory );
062        setRepositoryLayoutFactories( layoutFactories );
063    }
064
065    public void initService( ServiceLocator locator )
066    {
067        setLoggerFactory( locator.getService( LoggerFactory.class ) );
068        setRepositoryLayoutFactories( locator.getServices( RepositoryLayoutFactory.class ) );
069    }
070
071    public DefaultRepositoryLayoutProvider setLoggerFactory( LoggerFactory loggerFactory )
072    {
073        this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() );
074        return this;
075    }
076
077    public DefaultRepositoryLayoutProvider addRepositoryLayoutFactory( RepositoryLayoutFactory factory )
078    {
079        if ( factory == null )
080        {
081            throw new IllegalArgumentException( "layout factory has not been specified" );
082        }
083        factories.add( factory );
084        return this;
085    }
086
087    public DefaultRepositoryLayoutProvider setRepositoryLayoutFactories( Collection<RepositoryLayoutFactory> factories )
088    {
089        if ( factories == null )
090        {
091            this.factories = new ArrayList<RepositoryLayoutFactory>();
092        }
093        else
094        {
095            this.factories = factories;
096        }
097        return this;
098    }
099
100    public RepositoryLayout newRepositoryLayout( RepositorySystemSession session, RemoteRepository repository )
101        throws NoRepositoryLayoutException
102    {
103        if ( repository == null )
104        {
105            throw new IllegalArgumentException( "remote repository has not been specified" );
106        }
107
108        PrioritizedComponents<RepositoryLayoutFactory> factories =
109            new PrioritizedComponents<RepositoryLayoutFactory>( session );
110        for ( RepositoryLayoutFactory factory : this.factories )
111        {
112            factories.add( factory, factory.getPriority() );
113        }
114
115        List<NoRepositoryLayoutException> errors = new ArrayList<NoRepositoryLayoutException>();
116        for ( PrioritizedComponent<RepositoryLayoutFactory> factory : factories.getEnabled() )
117        {
118            try
119            {
120                RepositoryLayout layout = factory.getComponent().newInstance( session, repository );
121                return layout;
122            }
123            catch ( NoRepositoryLayoutException e )
124            {
125                // continue and try next factory
126                errors.add( e );
127            }
128        }
129        if ( logger.isDebugEnabled() && errors.size() > 1 )
130        {
131            String msg = "Could not obtain layout factory for " + repository;
132            for ( Exception e : errors )
133            {
134                logger.debug( msg, e );
135            }
136        }
137
138        StringBuilder buffer = new StringBuilder( 256 );
139        if ( factories.isEmpty() )
140        {
141            buffer.append( "No layout factories registered" );
142        }
143        else
144        {
145            buffer.append( "Cannot access " ).append( repository.getUrl() );
146            buffer.append( " with type " ).append( repository.getContentType() );
147            buffer.append( " using the available layout factories: " );
148            factories.list( buffer );
149        }
150
151        throw new NoRepositoryLayoutException( repository, buffer.toString(), errors.size() == 1 ? errors.get( 0 )
152                        : null );
153    }
154
155}