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.HashMap;
026import java.util.Map;
027
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.internal.impl.collect.bf.BfDependencyCollector;
034import org.eclipse.aether.internal.impl.collect.df.DfDependencyCollector;
035import org.eclipse.aether.spi.locator.Service;
036import org.eclipse.aether.spi.locator.ServiceLocator;
037import org.eclipse.aether.util.ConfigUtils;
038
039import static java.util.Objects.requireNonNull;
040
041/**
042 * Default implementation of {@link DependencyCollector} that merely indirect to selected delegate.
043 */
044@Singleton
045@Named
046public class DefaultDependencyCollector implements DependencyCollector, Service {
047    private static final String CONFIG_PROP_COLLECTOR_IMPL = "aether.dependencyCollector.impl";
048
049    private static final String DEFAULT_COLLECTOR_IMPL = DfDependencyCollector.NAME;
050
051    private final Map<String, DependencyCollectorDelegate> delegates;
052
053    /**
054     * Default ctor for SL.
055     *
056     * @deprecated SL is to be removed.
057     */
058    @Deprecated
059    public DefaultDependencyCollector() {
060        this.delegates = new HashMap<>();
061    }
062
063    @Inject
064    public DefaultDependencyCollector(Map<String, DependencyCollectorDelegate> delegates) {
065        this.delegates = requireNonNull(delegates);
066    }
067
068    @Override
069    public void initService(ServiceLocator locator) {
070        BfDependencyCollector bf = new BfDependencyCollector();
071        bf.initService(locator);
072        DfDependencyCollector df = new DfDependencyCollector();
073        df.initService(locator);
074        this.delegates.put(BfDependencyCollector.NAME, bf);
075        this.delegates.put(DfDependencyCollector.NAME, df);
076    }
077
078    @Override
079    public CollectResult collectDependencies(RepositorySystemSession session, CollectRequest request)
080            throws DependencyCollectionException {
081        String delegateName = ConfigUtils.getString(session, DEFAULT_COLLECTOR_IMPL, CONFIG_PROP_COLLECTOR_IMPL);
082        DependencyCollectorDelegate delegate = delegates.get(delegateName);
083        if (delegate == null) {
084            throw new IllegalArgumentException(
085                    "Unknown collector impl: '" + delegateName + "', known implementations are " + delegates.keySet());
086        }
087        return delegate.collectDependencies(session, request);
088    }
089}