001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.impl;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.util.ArrayList;
026import java.util.Collection;
027import java.util.List;
028import java.util.Set;
029
030import org.eclipse.aether.RepositorySystemSession;
031import org.eclipse.aether.impl.LocalRepositoryProvider;
032import org.eclipse.aether.repository.LocalRepository;
033import org.eclipse.aether.repository.LocalRepositoryManager;
034import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
035import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
036import org.eclipse.aether.spi.locator.Service;
037import org.eclipse.aether.spi.locator.ServiceLocator;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041import static java.util.Objects.requireNonNull;
042
043/**
044 */
045@Singleton
046@Named
047public class DefaultLocalRepositoryProvider implements LocalRepositoryProvider, Service {
048
049    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultLocalRepositoryProvider.class);
050
051    private Collection<LocalRepositoryManagerFactory> managerFactories = new ArrayList<>();
052
053    public DefaultLocalRepositoryProvider() {
054        // enables default constructor
055    }
056
057    @Inject
058    DefaultLocalRepositoryProvider(Set<LocalRepositoryManagerFactory> factories) {
059        setLocalRepositoryManagerFactories(factories);
060    }
061
062    public void initService(ServiceLocator locator) {
063        setLocalRepositoryManagerFactories(locator.getServices(LocalRepositoryManagerFactory.class));
064    }
065
066    public DefaultLocalRepositoryProvider addLocalRepositoryManagerFactory(LocalRepositoryManagerFactory factory) {
067        managerFactories.add(requireNonNull(factory, "local repository manager factory cannot be null"));
068        return this;
069    }
070
071    public DefaultLocalRepositoryProvider setLocalRepositoryManagerFactories(
072            Collection<LocalRepositoryManagerFactory> factories) {
073        if (factories == null) {
074            managerFactories = new ArrayList<>(2);
075        } else {
076            managerFactories = factories;
077        }
078        return this;
079    }
080
081    public LocalRepositoryManager newLocalRepositoryManager(RepositorySystemSession session, LocalRepository repository)
082            throws NoLocalRepositoryManagerException {
083        requireNonNull(session, "session cannot be null");
084        requireNonNull(repository, "repository cannot be null");
085        PrioritizedComponents<LocalRepositoryManagerFactory> factories = new PrioritizedComponents<>(session);
086        for (LocalRepositoryManagerFactory factory : this.managerFactories) {
087            factories.add(factory, factory.getPriority());
088        }
089
090        List<NoLocalRepositoryManagerException> errors = new ArrayList<>();
091        for (PrioritizedComponent<LocalRepositoryManagerFactory> factory : factories.getEnabled()) {
092            try {
093                LocalRepositoryManager manager = factory.getComponent().newInstance(session, repository);
094
095                if (LOGGER.isDebugEnabled()) {
096                    StringBuilder buffer = new StringBuilder(256);
097                    buffer.append("Using manager ").append(manager.getClass().getSimpleName());
098                    Utils.appendClassLoader(buffer, manager);
099                    buffer.append(" with priority ").append(factory.getPriority());
100                    buffer.append(" for ").append(repository.getBasedir());
101
102                    LOGGER.debug(buffer.toString());
103                }
104
105                return manager;
106            } catch (NoLocalRepositoryManagerException e) {
107                // continue and try next factory
108                errors.add(e);
109            }
110        }
111        if (LOGGER.isDebugEnabled() && errors.size() > 1) {
112            for (Exception e : errors) {
113                LOGGER.debug("Could not obtain local repository manager for {}", repository, e);
114            }
115        }
116
117        StringBuilder buffer = new StringBuilder(256);
118        if (factories.isEmpty()) {
119            buffer.append("No local repository managers registered");
120        } else {
121            buffer.append("Cannot access ").append(repository.getBasedir());
122            buffer.append(" with type ").append(repository.getContentType());
123            buffer.append(" using the available factories ");
124            factories.list(buffer);
125        }
126
127        throw new NoLocalRepositoryManagerException(
128                repository, buffer.toString(), errors.size() == 1 ? errors.get(0) : null);
129    }
130}