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.RemoteRepositoryFilterManager;
032import org.eclipse.aether.impl.RepositoryConnectorProvider;
033import org.eclipse.aether.internal.impl.filter.FilteringRepositoryConnector;
034import org.eclipse.aether.repository.Authentication;
035import org.eclipse.aether.repository.Proxy;
036import org.eclipse.aether.repository.RemoteRepository;
037import org.eclipse.aether.spi.connector.RepositoryConnector;
038import org.eclipse.aether.spi.connector.RepositoryConnectorFactory;
039import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilter;
040import org.eclipse.aether.spi.locator.Service;
041import org.eclipse.aether.spi.locator.ServiceLocator;
042import org.eclipse.aether.transfer.NoRepositoryConnectorException;
043import org.slf4j.Logger;
044import org.slf4j.LoggerFactory;
045
046import static java.util.Objects.requireNonNull;
047
048/**
049 */
050@Singleton
051@Named
052public class DefaultRepositoryConnectorProvider implements RepositoryConnectorProvider, Service {
053
054    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRepositoryConnectorProvider.class);
055
056    private Collection<RepositoryConnectorFactory> connectorFactories = new ArrayList<>();
057
058    private RemoteRepositoryFilterManager remoteRepositoryFilterManager;
059
060    public DefaultRepositoryConnectorProvider() {
061        // enables default constructor
062    }
063
064    @Inject
065    DefaultRepositoryConnectorProvider(
066            Set<RepositoryConnectorFactory> connectorFactories,
067            RemoteRepositoryFilterManager remoteRepositoryFilterManager) {
068        setRepositoryConnectorFactories(connectorFactories);
069        setRemoteRepositoryFilterManager(remoteRepositoryFilterManager);
070    }
071
072    public void initService(ServiceLocator locator) {
073        setRepositoryConnectorFactories(locator.getServices(RepositoryConnectorFactory.class));
074        setRemoteRepositoryFilterManager(locator.getService(RemoteRepositoryFilterManager.class));
075    }
076
077    public DefaultRepositoryConnectorProvider addRepositoryConnectorFactory(RepositoryConnectorFactory factory) {
078        connectorFactories.add(requireNonNull(factory, "repository connector factory cannot be null"));
079        return this;
080    }
081
082    public DefaultRepositoryConnectorProvider setRepositoryConnectorFactories(
083            Collection<RepositoryConnectorFactory> factories) {
084        if (factories == null) {
085            this.connectorFactories = new ArrayList<>();
086        } else {
087            this.connectorFactories = factories;
088        }
089        return this;
090    }
091
092    public DefaultRepositoryConnectorProvider setRemoteRepositoryFilterManager(
093            RemoteRepositoryFilterManager remoteRepositoryFilterManager) {
094        this.remoteRepositoryFilterManager = requireNonNull(remoteRepositoryFilterManager);
095        return this;
096    }
097
098    public RepositoryConnector newRepositoryConnector(RepositorySystemSession session, RemoteRepository repository)
099            throws NoRepositoryConnectorException {
100        requireNonNull(repository, "remote repository cannot be null");
101
102        if (repository.isBlocked()) {
103            if (repository.getMirroredRepositories().isEmpty()) {
104                throw new NoRepositoryConnectorException(repository, "Blocked repository: " + repository);
105            } else {
106                throw new NoRepositoryConnectorException(
107                        repository, "Blocked mirror for repositories: " + repository.getMirroredRepositories());
108            }
109        }
110
111        RemoteRepositoryFilter filter = remoteRepositoryFilterManager.getRemoteRepositoryFilter(session);
112
113        PrioritizedComponents<RepositoryConnectorFactory> factories = new PrioritizedComponents<>(session);
114        for (RepositoryConnectorFactory factory : this.connectorFactories) {
115            factories.add(factory, factory.getPriority());
116        }
117
118        List<NoRepositoryConnectorException> errors = new ArrayList<>();
119        for (PrioritizedComponent<RepositoryConnectorFactory> factory : factories.getEnabled()) {
120            try {
121                RepositoryConnector connector = factory.getComponent().newInstance(session, repository);
122
123                if (LOGGER.isDebugEnabled()) {
124                    StringBuilder buffer = new StringBuilder(256);
125                    buffer.append("Using connector ")
126                            .append(connector.getClass().getSimpleName());
127                    Utils.appendClassLoader(buffer, connector);
128                    buffer.append(" with priority ").append(factory.getPriority());
129                    buffer.append(" for ").append(repository.getUrl());
130
131                    Authentication auth = repository.getAuthentication();
132                    if (auth != null) {
133                        buffer.append(" with ").append(auth);
134                    }
135
136                    Proxy proxy = repository.getProxy();
137                    if (proxy != null) {
138                        buffer.append(" via ")
139                                .append(proxy.getHost())
140                                .append(':')
141                                .append(proxy.getPort());
142
143                        auth = proxy.getAuthentication();
144                        if (auth != null) {
145                            buffer.append(" with ").append(auth);
146                        }
147                    }
148
149                    LOGGER.debug(buffer.toString());
150                }
151
152                if (filter != null) {
153                    return new FilteringRepositoryConnector(repository, connector, filter);
154                } else {
155                    return connector;
156                }
157            } catch (NoRepositoryConnectorException e) {
158                // continue and try next factory
159                errors.add(e);
160            }
161        }
162        if (LOGGER.isDebugEnabled() && errors.size() > 1) {
163            for (Exception e : errors) {
164                LOGGER.debug("Could not obtain connector factory for {}", repository, e);
165            }
166        }
167
168        StringBuilder buffer = new StringBuilder(256);
169        if (factories.isEmpty()) {
170            buffer.append("No connector factories available");
171        } else {
172            buffer.append("Cannot access ").append(repository.getUrl());
173            buffer.append(" with type ").append(repository.getContentType());
174            buffer.append(" using the available connector factories: ");
175            factories.list(buffer);
176        }
177
178        throw new NoRepositoryConnectorException(
179                repository, buffer.toString(), errors.size() == 1 ? errors.get(0) : null);
180    }
181}