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 ) )
80              {
81                  if ( transitive )
82                  {
83                      getDownstreamProjects( id, projectIds, transitive );
84                  }
85              }
86          }
87      }
88  
89      public List<MavenProject> getUpstreamProjects( MavenProject project, boolean transitive )
90      {
91          if ( project == null )
92          {
93              throw new IllegalArgumentException( "project missing" );
94          }
95  
96          Collection<String> projectIds = new HashSet<String>();
97  
98          getUpstreamProjects( ProjectSorter.getId( project ), projectIds, transitive );
99  
100         return getProjects( projectIds );
101     }
102 
103     private void getUpstreamProjects( String projectId, Collection<String> projectIds, boolean transitive )
104     {
105         for ( String id : sorter.getDependencies( projectId ) )
106         {
107             if ( projectIds.add( id ) && transitive )
108             {
109                 getUpstreamProjects( id, projectIds, transitive );
110             }
111         }
112     }
113 
114     private List<MavenProject> getProjects( Collection<String> projectIds )
115     {
116         List<MavenProject> projects = new ArrayList<MavenProject>( projectIds.size() );
117 
118         for ( String projectId : projectIds )
119         {
120             MavenProject project = sorter.getProjectMap().get( projectId );
121 
122             if ( project != null )
123             {
124                 projects.add( project );
125             }
126         }
127 
128         return projects;
129     }
130 
131     @Override
132     public String toString()
133     {
134         return sorter.getSortedProjects().toString();
135     }
136 
137 }