View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.LinkedHashSet;
26  import java.util.List;
27  import java.util.Set;
28  
29  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
30  import org.apache.maven.artifact.resolver.filter.ExclusionSetFilter;
31  import org.apache.maven.extension.internal.CoreExports;
32  
33  /**
34   * @author Jason van Zyl
35   */
36  @Named
37  @Singleton
38  @SuppressWarnings("deprecation")
39  public class DefaultArtifactFilterManager implements ArtifactFilterManager {
40  
41      // this is a live injected collection
42      protected final List<ArtifactFilterManagerDelegate> delegates;
43  
44      protected Set<String> excludedArtifacts;
45  
46      private final Set<String> coreArtifacts;
47  
48      @Inject
49      public DefaultArtifactFilterManager(List<ArtifactFilterManagerDelegate> delegates, CoreExports coreExports) {
50          this.delegates = delegates;
51          this.coreArtifacts = coreExports.getExportedArtifacts();
52      }
53  
54      private synchronized Set<String> getExcludedArtifacts() {
55          if (excludedArtifacts == null) {
56              excludedArtifacts = new LinkedHashSet<>(coreArtifacts);
57          }
58          return excludedArtifacts;
59      }
60  
61      /**
62       * Returns the artifact filter for the core + extension artifacts.
63       *
64       * @see org.apache.maven.ArtifactFilterManager#getArtifactFilter()
65       */
66      public ArtifactFilter getArtifactFilter() {
67          Set<String> excludes = new LinkedHashSet<>(getExcludedArtifacts());
68  
69          for (ArtifactFilterManagerDelegate delegate : delegates) {
70              delegate.addExcludes(excludes);
71          }
72  
73          return new ExclusionSetFilter(excludes);
74      }
75  
76      /**
77       * Returns the artifact filter for the standard core artifacts.
78       *
79       * @see org.apache.maven.ArtifactFilterManager#getCoreArtifactFilter()
80       */
81      public ArtifactFilter getCoreArtifactFilter() {
82          return new ExclusionSetFilter(getCoreArtifactExcludes());
83      }
84  
85      public void excludeArtifact(String artifactId) {
86          getExcludedArtifacts().add(artifactId);
87      }
88  
89      public Set<String> getCoreArtifactExcludes() {
90          Set<String> excludes = new LinkedHashSet<>(coreArtifacts);
91  
92          for (ArtifactFilterManagerDelegate delegate : delegates) {
93              delegate.addCoreExcludes(excludes);
94          }
95  
96          return excludes;
97      }
98  }