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
47 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
50 * @plexus.component role="org.apache.maven.archiva.dependency.DependencyGraphFactory"
52 public class DependencyGraphFactory
54 private GraphTask taskFlagCyclicEdges;
56 private PopulateGraphMasterTask taskPopulateGraph;
58 private ReduceScopeTask taskReduceScope;
60 private List listeners;
62 private DependencyGraphBuilder graphBuilder;
66 public DependencyGraphFactory()
68 listeners = new ArrayList();
70 taskFlagCyclicEdges = new FlagCyclicEdgesTask();
71 taskPopulateGraph = new PopulateGraphMasterTask();
72 taskReduceScope = new ReduceScopeTask( DependencyScope.TEST );
74 tasks = new ArrayList();
76 /* Take the basic graph, and expand the nodes fully, including depman.
78 tasks.add( taskPopulateGraph );
80 /* Identify, flag, and disable excluded edges.
82 tasks.add( new FlagExcludedEdgesTask() );
84 /* Reduce the edges of the graph to only those that are enabled.
86 tasks.add( new ReduceEnabledEdgesTask() );
88 /* Identify dependencies that conflict, resolve to single node.
91 * 1) filter the distant conflicts away for the nearer ones.
92 * 2) same distance nodes will pick 'newest' version.
94 * This can cause a collapsing of node versions.
96 tasks.add( new RefineConflictsTask() );
98 /* Reduce the scope of the graph to those visible by the 'test' scope.
100 tasks.add( taskReduceScope );
102 /* Reduce the edges of the graph. Use the transitive reduction algorithm
103 * to remove redundant edges.
105 tasks.add( new ReduceTransitiveEdgesTask() );
107 /* Update the scopes of the edges to conform to the parent setting.
109 tasks.add( new UpdateScopesTask() );
112 public void addGraphListener( GraphListener listener )
114 this.listeners.add( listener );
118 * Get the Graph for a specific Versioned Project Reference.
120 * @param versionedProjectReference
123 public DependencyGraph getGraph( VersionedReference versionedProjectReference )
124 throws GraphTaskException
126 DependencyGraph graph = graphBuilder.createGraph( versionedProjectReference );
128 triggerGraphPhase( GraphPhaseEvent.GRAPH_NEW, null, graph );
130 Iterator it = this.tasks.iterator();
131 while ( it.hasNext() )
133 GraphTask task = (GraphTask) it.next();
136 triggerGraphPhase( GraphPhaseEvent.GRAPH_TASK_PRE, task, graph );
137 task.executeTask( graph );
138 if ( task instanceof PotentialCyclicEdgeProducer )
140 taskFlagCyclicEdges.executeTask( graph );
142 triggerGraphPhase( GraphPhaseEvent.GRAPH_TASK_POST, task, graph );
144 catch ( GraphTaskException e )
146 triggerGraphError( e, graph );
149 catch ( Exception e )
151 GraphTaskException gte = new GraphTaskException( e.getMessage(), e );
152 triggerGraphError( gte, graph );
157 triggerGraphPhase( GraphPhaseEvent.GRAPH_DONE, null, graph );
162 public void removeGraphListener( GraphListener listener )
164 this.listeners.remove( listener );
167 public void setDesiredScope( String scope )
169 taskReduceScope.setScope( scope );
172 public void setGraphBuilder( DependencyGraphBuilder graphBuilder )
174 this.graphBuilder = graphBuilder;
175 taskPopulateGraph.setBuilder( graphBuilder );
178 private void triggerGraphError( GraphTaskException e, DependencyGraph graph )
180 Iterator it = listeners.iterator();
181 while ( it.hasNext() )
183 GraphListener listener = (GraphListener) it.next();
184 listener.graphError( e, graph );
188 private void triggerGraphPhase( int type, GraphTask task, DependencyGraph graph )
190 GraphPhaseEvent evt = new GraphPhaseEvent( type, task, graph );
192 Iterator it = listeners.iterator();
193 while ( it.hasNext() )
195 GraphListener listener = (GraphListener) it.next();
196 listener.graphPhaseEvent( evt );