]> source.dussan.org Git - archiva.git/blob
66d3066ea754de61ef86d16dddc207b2a459d807
[archiva.git] /
1 package org.apache.maven.archiva.dependency;
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.archiva.dependency.graph.DependencyGraph;
23 import org.apache.maven.archiva.dependency.graph.DependencyGraphBuilder;
24 import org.apache.maven.archiva.dependency.graph.GraphListener;
25 import org.apache.maven.archiva.dependency.graph.GraphPhaseEvent;
26 import org.apache.maven.archiva.dependency.graph.GraphTask;
27 import org.apache.maven.archiva.dependency.graph.GraphTaskException;
28 import org.apache.maven.archiva.dependency.graph.PotentialCyclicEdgeProducer;
29 import org.apache.maven.archiva.dependency.graph.tasks.FlagCyclicEdgesTask;
30 import org.apache.maven.archiva.dependency.graph.tasks.FlagExcludedEdgesTask;
31 import org.apache.maven.archiva.dependency.graph.tasks.PopulateGraphMasterTask;
32 import org.apache.maven.archiva.dependency.graph.tasks.ReduceEnabledEdgesTask;
33 import org.apache.maven.archiva.dependency.graph.tasks.ReduceScopeTask;
34 import org.apache.maven.archiva.dependency.graph.tasks.ReduceTransitiveEdgesTask;
35 import org.apache.maven.archiva.dependency.graph.tasks.RefineConflictsTask;
36 import org.apache.maven.archiva.dependency.graph.tasks.UpdateScopesTask;
37 import org.apache.maven.archiva.model.DependencyScope;
38 import org.apache.maven.archiva.model.VersionedReference;
39
40 import java.util.ArrayList;
41 import java.util.Iterator;
42 import java.util.List;
43
44 /**
45  * DependencyGraphFactory 
46  *
47  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
48  * @version $Id$
49  * 
50  * @plexus.component role="org.apache.maven.archiva.dependency.DependencyGraphFactory"
51  */
52 public class DependencyGraphFactory
53 {
54     private GraphTask taskFlagCyclicEdges;
55
56     private PopulateGraphMasterTask taskPopulateGraph;
57
58     private ReduceScopeTask taskReduceScope;
59
60     private List listeners;
61
62     private DependencyGraphBuilder graphBuilder;
63
64     private List tasks;
65
66     public DependencyGraphFactory()
67     {
68         listeners = new ArrayList();
69
70         taskFlagCyclicEdges = new FlagCyclicEdgesTask();
71         taskPopulateGraph = new PopulateGraphMasterTask();
72         taskReduceScope = new ReduceScopeTask( DependencyScope.TEST );
73
74         tasks = new ArrayList();
75
76         /* Take the basic graph, and expand the nodes fully, including depman.
77          */
78         tasks.add( taskPopulateGraph );
79
80         /* Identify, flag, and disable excluded edges.
81          */
82         tasks.add( new FlagExcludedEdgesTask() );
83
84         /* Reduce the edges of the graph to only those that are enabled.
85          */
86         tasks.add( new ReduceEnabledEdgesTask() );
87
88         /* Identify dependencies that conflict, resolve to single node.
89          * 
90          * This will ...
91          * 1) filter the distant conflicts away for the nearer ones.
92          * 2) same distance nodes will pick 'newest' version.
93          * 
94          * This can cause a collapsing of node versions.
95          */
96         tasks.add( new RefineConflictsTask() );
97
98         /* Reduce the scope of the graph to those visible by the 'test' scope.
99          */
100         tasks.add( taskReduceScope );
101
102         /* Reduce the edges of the graph.  Use the transitive reduction algorithm
103          * to remove redundant edges.
104          */
105         tasks.add( new ReduceTransitiveEdgesTask() );
106
107         /* Update the scopes of the edges to conform to the parent setting. 
108          */
109         tasks.add( new UpdateScopesTask() );
110     }
111
112     public void addGraphListener( GraphListener listener )
113     {
114         this.listeners.add( listener );
115     }
116
117     /**
118      * Get the Graph for a specific Versioned Project Reference.
119      * 
120      * @param versionedProjectReference
121      * @return
122      */
123     public DependencyGraph getGraph( VersionedReference versionedProjectReference )
124         throws GraphTaskException
125     {
126         DependencyGraph graph = graphBuilder.createGraph( versionedProjectReference );
127
128         triggerGraphPhase( GraphPhaseEvent.GRAPH_NEW, null, graph );
129
130         Iterator it = this.tasks.iterator();
131         while ( it.hasNext() )
132         {
133             GraphTask task = (GraphTask) it.next();
134             try
135             {
136                 triggerGraphPhase( GraphPhaseEvent.GRAPH_TASK_PRE, task, graph );
137                 task.executeTask( graph );
138                 if ( task instanceof PotentialCyclicEdgeProducer )
139                 {
140                     taskFlagCyclicEdges.executeTask( graph );
141                 }
142                 triggerGraphPhase( GraphPhaseEvent.GRAPH_TASK_POST, task, graph );
143             }
144             catch ( GraphTaskException e )
145             {
146                 triggerGraphError( e, graph );
147                 throw e;
148             }
149             catch ( Exception e )
150             {
151                 GraphTaskException gte = new GraphTaskException( e.getMessage(), e );
152                 triggerGraphError( gte, graph );
153                 throw gte;
154             }
155         }
156
157         triggerGraphPhase( GraphPhaseEvent.GRAPH_DONE, null, graph );
158
159         return graph;
160     }
161
162     public void removeGraphListener( GraphListener listener )
163     {
164         this.listeners.remove( listener );
165     }
166
167     public void setDesiredScope( String scope )
168     {
169         taskReduceScope.setScope( scope );
170     }
171
172     public void setGraphBuilder( DependencyGraphBuilder graphBuilder )
173     {
174         this.graphBuilder = graphBuilder;
175         taskPopulateGraph.setBuilder( graphBuilder );
176     }
177
178     private void triggerGraphError( GraphTaskException e, DependencyGraph graph )
179     {
180         Iterator it = listeners.iterator();
181         while ( it.hasNext() )
182         {
183             GraphListener listener = (GraphListener) it.next();
184             listener.graphError( e, graph );
185         }
186     }
187
188     private void triggerGraphPhase( int type, GraphTask task, DependencyGraph graph )
189     {
190         GraphPhaseEvent evt = new GraphPhaseEvent( type, task, graph );
191
192         Iterator it = listeners.iterator();
193         while ( it.hasNext() )
194         {
195             GraphListener listener = (GraphListener) it.next();
196             listener.graphPhaseEvent( evt );
197         }
198     }
199
200 }