1 package org.apache.maven.archiva.dependency;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
40 import java.util.ArrayList;
41 import java.util.Iterator;
42 import java.util.List;
45 * DependencyGraphFactory
49 public class DependencyGraphFactory
51 private GraphTask taskFlagCyclicEdges;
53 private PopulateGraphMasterTask taskPopulateGraph;
55 private ReduceScopeTask taskReduceScope;
57 private List listeners;
59 private DependencyGraphBuilder graphBuilder;
63 public DependencyGraphFactory()
65 listeners = new ArrayList();
67 taskFlagCyclicEdges = new FlagCyclicEdgesTask();
68 taskPopulateGraph = new PopulateGraphMasterTask();
69 taskReduceScope = new ReduceScopeTask( DependencyScope.TEST );
71 tasks = new ArrayList();
73 /* Take the basic graph, and expand the nodes fully, including depman.
75 tasks.add( taskPopulateGraph );
77 /* Identify, flag, and disable excluded edges.
79 tasks.add( new FlagExcludedEdgesTask() );
81 /* Reduce the edges of the graph to only those that are enabled.
83 tasks.add( new ReduceEnabledEdgesTask() );
85 /* Identify dependencies that conflict, resolve to single node.
88 * 1) filter the distant conflicts away for the nearer ones.
89 * 2) same distance nodes will pick 'newest' version.
91 * This can cause a collapsing of node versions.
93 tasks.add( new RefineConflictsTask() );
95 /* Reduce the scope of the graph to those visible by the 'test' scope.
97 tasks.add( taskReduceScope );
99 /* Reduce the edges of the graph. Use the transitive reduction algorithm
100 * to remove redundant edges.
102 tasks.add( new ReduceTransitiveEdgesTask() );
104 /* Update the scopes of the edges to conform to the parent setting.
106 tasks.add( new UpdateScopesTask() );
109 public void addGraphListener( GraphListener listener )
111 this.listeners.add( listener );
115 * Get the Graph for a specific Versioned Project Reference.
117 * @param versionedProjectReference
120 public DependencyGraph getGraph( VersionedReference versionedProjectReference )
121 throws GraphTaskException
123 DependencyGraph graph = graphBuilder.createGraph( versionedProjectReference );
125 triggerGraphPhase( GraphPhaseEvent.GRAPH_NEW, null, graph );
127 Iterator it = this.tasks.iterator();
128 while ( it.hasNext() )
130 GraphTask task = (GraphTask) it.next();
133 triggerGraphPhase( GraphPhaseEvent.GRAPH_TASK_PRE, task, graph );
134 task.executeTask( graph );
135 if ( task instanceof PotentialCyclicEdgeProducer )
137 taskFlagCyclicEdges.executeTask( graph );
139 triggerGraphPhase( GraphPhaseEvent.GRAPH_TASK_POST, task, graph );
141 catch ( GraphTaskException e )
143 triggerGraphError( e, graph );
146 catch ( Exception e )
148 GraphTaskException gte = new GraphTaskException( e.getMessage(), e );
149 triggerGraphError( gte, graph );
154 triggerGraphPhase( GraphPhaseEvent.GRAPH_DONE, null, graph );
159 public void removeGraphListener( GraphListener listener )
161 this.listeners.remove( listener );
164 public void setDesiredScope( String scope )
166 taskReduceScope.setScope( scope );
169 public void setGraphBuilder( DependencyGraphBuilder graphBuilder )
171 this.graphBuilder = graphBuilder;
172 taskPopulateGraph.setBuilder( graphBuilder );
175 private void triggerGraphError( GraphTaskException e, DependencyGraph graph )
177 Iterator it = listeners.iterator();
178 while ( it.hasNext() )
180 GraphListener listener = (GraphListener) it.next();
181 listener.graphError( e, graph );
185 private void triggerGraphPhase( int type, GraphTask task, DependencyGraph graph )
187 GraphPhaseEvent evt = new GraphPhaseEvent( type, task, graph );
189 Iterator it = listeners.iterator();
190 while ( it.hasNext() )
192 GraphListener listener = (GraphListener) it.next();
193 listener.graphPhaseEvent( evt );