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.collect;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.util.Map;
026
027import org.eclipse.aether.ConfigurationProperties;
028import org.eclipse.aether.RepositorySystemSession;
029import org.eclipse.aether.collection.CollectRequest;
030import org.eclipse.aether.collection.CollectResult;
031import org.eclipse.aether.collection.DependencyCollectionException;
032import org.eclipse.aether.impl.DependencyCollector;
033import org.eclipse.aether.util.ConfigUtils;
034
035import static java.util.Objects.requireNonNull;
036
037/**
038 * Default implementation of {@link DependencyCollector} that merely indirect to selected delegate.
039 */
040@Singleton
041@Named
042public class DefaultDependencyCollector implements DependencyCollector {
043
044    public static final String CONFIG_PROPS_PREFIX = ConfigurationProperties.PREFIX_AETHER + "dependencyCollector.";
045
046    /**
047     * The name of the dependency collector implementation to use: depth-first (original) named "df", and
048     * breadth-first (new in 1.8.0) named "bf". Both collectors produce equivalent results, but they may differ
049     * performance wise, depending on project being applied to. Our experience shows that existing "df" is well
050     * suited for smaller to medium size projects, while "bf" may perform better on huge projects with many
051     * dependencies. Experiment (and come back to us!) to figure out which one suits you the better.
052     *
053     * @since 1.8.0
054     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
055     * @configurationType {@link java.lang.String}
056     * @configurationDefaultValue {@link #DEFAULT_COLLECTOR_IMPL}
057     */
058    public static final String CONFIG_PROP_COLLECTOR_IMPL = CONFIG_PROPS_PREFIX + "impl";
059
060    public static final String DEFAULT_COLLECTOR_IMPL =
061            org.eclipse.aether.internal.impl.collect.bf.BfDependencyCollector.NAME;
062
063    private final Map<String, DependencyCollectorDelegate> delegates;
064
065    @Inject
066    public DefaultDependencyCollector(Map<String, DependencyCollectorDelegate> delegates) {
067        this.delegates = requireNonNull(delegates);
068    }
069
070    @Override
071    public CollectResult collectDependencies(RepositorySystemSession session, CollectRequest request)
072            throws DependencyCollectionException {
073        String delegateName = ConfigUtils.getString(session, DEFAULT_COLLECTOR_IMPL, CONFIG_PROP_COLLECTOR_IMPL);
074        DependencyCollectorDelegate delegate = delegates.get(delegateName);
075        if (delegate == null) {
076            throw new IllegalArgumentException(
077                    "Unknown collector impl: '" + delegateName + "', known implementations are " + delegates.keySet());
078        }
079        return delegate.collectDependencies(session, request);
080    }
081}