View Javadoc
1   package org.eclipse.aether.internal.impl.collect;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import javax.inject.Inject;
23  import javax.inject.Named;
24  import javax.inject.Singleton;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.eclipse.aether.RepositorySystemSession;
30  import org.eclipse.aether.collection.CollectRequest;
31  import org.eclipse.aether.collection.CollectResult;
32  import org.eclipse.aether.collection.DependencyCollectionException;
33  import org.eclipse.aether.impl.DependencyCollector;
34  import org.eclipse.aether.internal.impl.collect.bf.BfDependencyCollector;
35  import org.eclipse.aether.internal.impl.collect.df.DfDependencyCollector;
36  import org.eclipse.aether.spi.locator.Service;
37  import org.eclipse.aether.spi.locator.ServiceLocator;
38  import org.eclipse.aether.util.ConfigUtils;
39  
40  import static java.util.Objects.requireNonNull;
41  
42  /**
43   * Default implementation of {@link DependencyCollector} that merely indirect to selected delegate.
44   */
45  @Singleton
46  @Named
47  public class DefaultDependencyCollector
48          implements DependencyCollector, Service
49  {
50      private static final String CONFIG_PROP_COLLECTOR_IMPL = "aether.collector.impl";
51  
52      private static final String DEFAULT_COLLECTOR_IMPL = DfDependencyCollector.NAME;
53  
54      private final Map<String, DependencyCollectorDelegate> delegates;
55  
56      /**
57       * Default ctor for SL.
58       *
59       * @deprecated SL is to be removed.
60       */
61      @Deprecated
62      public DefaultDependencyCollector()
63      {
64          this.delegates = new HashMap<>();
65      }
66  
67      @Inject
68      public DefaultDependencyCollector( Map<String, DependencyCollectorDelegate> delegates )
69      {
70          this.delegates = requireNonNull( delegates );
71      }
72  
73      @Override
74      public void initService( ServiceLocator locator )
75      {
76          BfDependencyCollector bf = new BfDependencyCollector();
77          bf.initService( locator );
78          DfDependencyCollector df = new DfDependencyCollector();
79          df.initService( locator );
80          this.delegates.put( BfDependencyCollector.NAME, bf );
81          this.delegates.put( DfDependencyCollector.NAME, df );
82      }
83  
84      @Override
85      public CollectResult collectDependencies( RepositorySystemSession session, CollectRequest request )
86              throws DependencyCollectionException
87      {
88          String delegateName = ConfigUtils.getString( session, DEFAULT_COLLECTOR_IMPL, CONFIG_PROP_COLLECTOR_IMPL );
89          DependencyCollectorDelegate delegate = delegates.get( delegateName );
90          if ( delegate == null )
91          {
92              throw new IllegalArgumentException( "Unknown collector impl: '" + delegateName
93                      + "', known implementations are " + delegates.keySet() );
94          }
95          return delegate.collectDependencies( session, request );
96      }
97  }