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 java.util.LinkedHashSet;
22  import java.util.List;
23  import java.util.Set;
24  import javax.inject.Inject;
25  import javax.inject.Named;
26  import javax.inject.Singleton;
27  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
28  import org.apache.maven.artifact.resolver.filter.ExclusionSetFilter;
29  import org.apache.maven.extension.internal.CoreExports;
30  
31  /**
32   * @author Jason van Zyl
33   */
34  @Named
35  @Singleton
36  @SuppressWarnings("deprecation")
37  public class DefaultArtifactFilterManager implements ArtifactFilterManager {
38  
39      // this is a live injected collection
40      protected final List<ArtifactFilterManagerDelegate> delegates;
41  
42      protected Set<String> excludedArtifacts;
43  
44      private final Set<String> coreArtifacts;
45  
46      @Inject
47      public DefaultArtifactFilterManager(List<ArtifactFilterManagerDelegate> delegates, CoreExports coreExports) {
48          this.delegates = delegates;
49          this.coreArtifacts = coreExports.getExportedArtifacts();
50      }
51  
52      private synchronized Set<String> getExcludedArtifacts() {
53          if (excludedArtifacts == null) {
54              excludedArtifacts = new LinkedHashSet<>(coreArtifacts);
55          }
56          return excludedArtifacts;
57      }
58  
59      /**
60       * Returns the artifact filter for the core + extension artifacts.
61       *
62       * @see org.apache.maven.ArtifactFilterManager#getArtifactFilter()
63       */
64      public ArtifactFilter getArtifactFilter() {
65          Set<String> excludes = new LinkedHashSet<>(getExcludedArtifacts());
66  
67          for (ArtifactFilterManagerDelegate delegate : delegates) {
68              delegate.addExcludes(excludes);
69          }
70  
71          return new ExclusionSetFilter(excludes);
72      }
73  
74      /**
75       * Returns the artifact filter for the standard core artifacts.
76       *
77       * @see org.apache.maven.ArtifactFilterManager#getCoreArtifactFilter()
78       */
79      public ArtifactFilter getCoreArtifactFilter() {
80          return new ExclusionSetFilter(getCoreArtifactExcludes());
81      }
82  
83      public void excludeArtifact(String artifactId) {
84          getExcludedArtifacts().add(artifactId);
85      }
86  
87      public Set<String> getCoreArtifactExcludes() {
88          Set<String> excludes = new LinkedHashSet<>(coreArtifacts);
89  
90          for (ArtifactFilterManagerDelegate delegate : delegates) {
91              delegate.addCoreExcludes(excludes);
92          }
93  
94          return excludes;
95      }
96  }