View Javadoc

1   package org.apache.maven.lifecycle.internal;
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 org.apache.maven.execution.ProjectDependencyGraph;
23  import org.apache.maven.project.MavenProject;
24  
25  import java.util.ArrayList;
26  import java.util.HashSet;
27  import java.util.List;
28  
29  /**
30   * Presents a view of the Dependency Graph that is suited for concurrent building.
31   * 
32   * @since 3.0
33   * @author Kristian Rosenvold
34   *         <p/>
35   *         NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
36   */
37  public class ConcurrencyDependencyGraph
38  {
39  
40      private final ProjectBuildList projectBuilds;
41  
42      private final ProjectDependencyGraph projectDependencyGraph;
43  
44      private final HashSet<MavenProject> finishedProjects = new HashSet<MavenProject>();
45  
46  
47      public ConcurrencyDependencyGraph( ProjectBuildList projectBuilds, ProjectDependencyGraph projectDependencyGraph )
48      {
49          this.projectDependencyGraph = projectDependencyGraph;
50          this.projectBuilds = projectBuilds;
51      }
52  
53  
54      public int getNumberOfBuilds()
55      {
56          return projectBuilds.size();
57      }
58  
59      /**
60       * Gets all the builds that have no reactor-dependencies
61       *
62       * @return A list of all the initial builds
63       */
64  
65      public List<MavenProject> getRootSchedulableBuilds()
66      {
67          List<MavenProject> result = new ArrayList<MavenProject>();
68          for ( ProjectSegment projectBuild : projectBuilds )
69          {
70              if ( projectDependencyGraph.getUpstreamProjects( projectBuild.getProject(), false ).size() == 0 )
71              {
72                  result.add( projectBuild.getProject() );
73              }
74          }
75          return result;
76      }
77  
78      /**
79       * Marks the provided project as finished. Returns a list of
80       *
81       * @param mavenProject The project
82       * @return The list of builds that are eligible for starting now that the provided project is done
83       */
84      public List<MavenProject> markAsFinished( MavenProject mavenProject )
85      {
86          finishedProjects.add( mavenProject );
87          return getSchedulableNewProcesses( mavenProject );
88      }
89  
90      private List<MavenProject> getSchedulableNewProcesses( MavenProject finishedProject )
91      {
92          List<MavenProject> result = new ArrayList<MavenProject>();
93          // schedule dependent projects, if all of their requirements are met
94          for ( MavenProject dependentProject : projectDependencyGraph.getDownstreamProjects( finishedProject, false ) )
95          {
96              final List<MavenProject> upstreamProjects =
97                  projectDependencyGraph.getUpstreamProjects( dependentProject, false );
98              if ( finishedProjects.containsAll( upstreamProjects ) )
99              {
100                 result.add( dependentProject );
101             }
102         }
103         return result;
104     }
105 
106     public ProjectBuildList getProjectBuilds()
107     {
108         return projectBuilds;
109     }
110 }