View Javadoc
1   package org.apache.maven;
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 java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.HashSet;
25  import java.util.List;
26  
27  import org.apache.maven.execution.ProjectDependencyGraph;
28  import org.apache.maven.project.DuplicateProjectException;
29  import org.apache.maven.project.MavenProject;
30  import org.apache.maven.project.ProjectSorter;
31  import org.codehaus.plexus.util.dag.CycleDetectedException;
32  
33  /**
34   * Describes the inter-dependencies between projects in the reactor.
35   * 
36   * @author Benjamin Bentmann
37   */
38  class DefaultProjectDependencyGraph
39      implements ProjectDependencyGraph
40  {
41  
42      private ProjectSorter sorter;
43  
44      /**
45       * Creates a new project dependency graph based on the specified projects.
46       * 
47       * @param projects The projects to create the dependency graph with
48       * @throws DuplicateProjectException 
49       * @throws CycleDetectedException 
50       */
51      public DefaultProjectDependencyGraph( Collection<MavenProject> projects ) throws CycleDetectedException, DuplicateProjectException
52      {
53          this.sorter = new ProjectSorter( projects );
54      }
55  
56      public List<MavenProject> getSortedProjects()
57      {
58          return new ArrayList<MavenProject>( sorter.getSortedProjects() );
59      }
60  
61      public List<MavenProject> getDownstreamProjects( MavenProject project, boolean transitive )
62      {
63          if ( project == null )
64          {
65              throw new IllegalArgumentException( "project missing" );
66          }
67  
68          Collection<String> projectIds = new HashSet<String>();
69  
70          getDownstreamProjects( ProjectSorter.getId( project ), projectIds, transitive );
71  
72          return getProjects( projectIds );
73      }
74  
75      private void getDownstreamProjects( String projectId, Collection<String> projectIds, boolean transitive )
76      {
77          for ( String id : sorter.getDependents( projectId ) )
78          {
79              if ( projectIds.add( id ) && transitive )
80              {
81                  getDownstreamProjects( id, projectIds, transitive );
82              }
83          }
84      }
85  
86      public List<MavenProject> getUpstreamProjects( MavenProject project, boolean transitive )
87      {
88          if ( project == null )
89          {
90              throw new IllegalArgumentException( "project missing" );
91          }
92  
93          Collection<String> projectIds = new HashSet<String>();
94  
95          getUpstreamProjects( ProjectSorter.getId( project ), projectIds, transitive );
96  
97          return getProjects( projectIds );
98      }
99  
100     private void getUpstreamProjects( String projectId, Collection<String> projectIds, boolean transitive )
101     {
102         for ( String id : sorter.getDependencies( projectId ) )
103         {
104             if ( projectIds.add( id ) && transitive )
105             {
106                 getUpstreamProjects( id, projectIds, transitive );
107             }
108         }
109     }
110 
111     private List<MavenProject> getProjects( Collection<String> projectIds )
112     {
113         List<MavenProject> projects = new ArrayList<MavenProject>( projectIds.size() );
114 
115         for ( String projectId : projectIds )
116         {
117             MavenProject project = sorter.getProjectMap().get( projectId );
118 
119             if ( project != null )
120             {
121                 projects.add( project );
122             }
123         }
124 
125         return projects;
126     }
127 
128     @Override
129     public String toString()
130     {
131         return sorter.getSortedProjects().toString();
132     }
133 
134 }