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