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.lifecycle.internal;
20  
21  import java.util.Arrays;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.TreeSet;
25  
26  import org.apache.maven.project.MavenProject;
27  
28  /**
29   * <p>
30   * Context of dependency artifacts for a particular project.
31   * </p>
32   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
33   *
34   * @since 3.0
35   */
36  // TODO From a concurrency perspective, this class is not good. The combination of mutable/immutable state is not nice
37  public class DependencyContext {
38  
39      private static final Collection<?> UNRESOLVED = Arrays.asList();
40  
41      private final MavenProject project;
42  
43      private final Collection<String> scopesToCollectForCurrentProject;
44  
45      private final Collection<String> scopesToResolveForCurrentProject;
46  
47      private final Collection<String> scopesToCollectForAggregatedProjects;
48  
49      private final Collection<String> scopesToResolveForAggregatedProjects;
50  
51      private volatile Collection<?> lastDependencyArtifacts = UNRESOLVED;
52  
53      private volatile int lastDependencyArtifactCount = -1;
54  
55      public DependencyContext(
56              MavenProject project, Collection<String> scopesToCollect, Collection<String> scopesToResolve) {
57          this.project = project;
58          scopesToCollectForCurrentProject = scopesToCollect;
59          scopesToResolveForCurrentProject = scopesToResolve;
60          scopesToCollectForAggregatedProjects = Collections.synchronizedSet(new TreeSet<>());
61          scopesToResolveForAggregatedProjects = Collections.synchronizedSet(new TreeSet<>());
62      }
63  
64      public MavenProject getProject() {
65          return project;
66      }
67  
68      public Collection<String> getScopesToCollectForCurrentProject() {
69          return scopesToCollectForCurrentProject;
70      }
71  
72      public Collection<String> getScopesToResolveForCurrentProject() {
73          return scopesToResolveForCurrentProject;
74      }
75  
76      public Collection<String> getScopesToCollectForAggregatedProjects() {
77          return scopesToCollectForAggregatedProjects;
78      }
79  
80      public Collection<String> getScopesToResolveForAggregatedProjects() {
81          return scopesToResolveForAggregatedProjects;
82      }
83  
84      public boolean isResolutionRequiredForCurrentProject() {
85          return lastDependencyArtifacts != project.getDependencyArtifacts()
86                  || (lastDependencyArtifacts != null && lastDependencyArtifactCount != lastDependencyArtifacts.size());
87      }
88  
89      public boolean isResolutionRequiredForAggregatedProjects(
90              Collection<String> scopesToCollect, Collection<String> scopesToResolve) {
91          return scopesToCollectForAggregatedProjects.addAll(scopesToCollect)
92                  || scopesToResolveForAggregatedProjects.addAll(scopesToResolve);
93      }
94  
95      public void synchronizeWithProjectState() {
96          lastDependencyArtifacts = project.getDependencyArtifacts();
97          lastDependencyArtifactCount = (lastDependencyArtifacts != null) ? lastDependencyArtifacts.size() : 0;
98      }
99  }