Coverage Report - org.apache.maven.archiva.dependency.graph.tasks.RefineConflictsVisitor
 
Classes in this File Line Coverage Branch Coverage Complexity
RefineConflictsVisitor
0%
0/55
0%
0/14
0
RefineConflictsVisitor$DepthComparator
0%
0/2
N/A
0
RefineConflictsVisitor$DistantNodeLocationPredicate
0%
0/8
0%
0/4
0
RefineConflictsVisitor$NodeLocation
0%
0/5
N/A
0
RefineConflictsVisitor$NodeLocationPredicate
0%
0/10
0%
0/2
0
RefineConflictsVisitor$NodeLocationVersionComparator
0%
0/8
0%
0/12
0
 
 1  
 package org.apache.maven.archiva.dependency.graph.tasks;
 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.Collections;
 25  
 import java.util.Comparator;
 26  
 import java.util.HashMap;
 27  
 import java.util.HashSet;
 28  
 import java.util.List;
 29  
 import java.util.Map;
 30  
 import java.util.Set;
 31  
 
 32  
 import org.apache.commons.collections.CollectionUtils;
 33  
 import org.apache.commons.collections.MapUtils;
 34  
 import org.apache.commons.collections.Predicate;
 35  
 import org.apache.commons.collections.comparators.ReverseComparator;
 36  
 import org.apache.commons.collections.functors.NotPredicate;
 37  
 import org.apache.commons.collections.list.TypedList;
 38  
 import org.apache.maven.archiva.dependency.graph.DependencyGraph;
 39  
 import org.apache.maven.archiva.dependency.graph.DependencyGraphEdge;
 40  
 import org.apache.maven.archiva.dependency.graph.DependencyGraphKeys;
 41  
 import org.apache.maven.archiva.dependency.graph.DependencyGraphNode;
 42  
 import org.apache.maven.archiva.dependency.graph.DependencyGraphUtils;
 43  
 import org.apache.maven.archiva.dependency.graph.walk.BaseVisitor;
 44  
 import org.apache.maven.archiva.dependency.graph.walk.DependencyGraphVisitor;
 45  
 import org.apache.maven.archiva.model.ArtifactReference;
 46  
 
 47  
 /**
 48  
  * RefineConflictsVisitor 
 49  
  *
 50  
  * @version $Id: RefineConflictsVisitor.java 755284 2009-03-17 15:36:42Z brett $
 51  
  */
 52  
 public class RefineConflictsVisitor
 53  
     extends BaseVisitor
 54  
     implements DependencyGraphVisitor
 55  
 {
 56  0
     class DepthComparator
 57  
         implements Comparator<NodeLocation>
 58  
     {
 59  
         public int compare( NodeLocation obj0, NodeLocation obj1 )
 60  
         {
 61  0
             return obj0.depth - obj1.depth;
 62  
         }
 63  
     }
 64  
 
 65  
     class NodeLocation
 66  
     {
 67  
         public ArtifactReference artifact;
 68  
 
 69  
         public DependencyGraphEdge edge;
 70  
 
 71  
         public int depth;
 72  
 
 73  
         public NodeLocation( ArtifactReference artifact, DependencyGraphEdge edge, int depth )
 74  0
         {
 75  0
             this.artifact = artifact;
 76  0
             this.edge = edge;
 77  0
             this.depth = depth;
 78  0
         }
 79  
     }
 80  
 
 81  
     class NodeLocationPredicate
 82  
         implements Predicate
 83  
     {
 84  
         private ArtifactReference artifact;
 85  
 
 86  
         public NodeLocationPredicate( ArtifactReference artifact )
 87  0
         {
 88  0
             this.artifact = artifact;
 89  0
         }
 90  
 
 91  
         public NodeLocationPredicate( DependencyGraphNode node )
 92  
         {
 93  0
             this( node.getArtifact() );
 94  0
         }
 95  
 
 96  
         public boolean evaluate( Object object )
 97  
         {
 98  0
             boolean satisfies = false;
 99  
 
 100  0
             if ( object instanceof NodeLocation )
 101  
             {
 102  0
                 NodeLocation nodeloc = (NodeLocation) object;
 103  0
                 satisfies = nodeloc.artifact.equals( artifact );
 104  
             }
 105  
 
 106  0
             return satisfies;
 107  
         }
 108  
 
 109  
     }
 110  
 
 111  0
     class NodeLocationVersionComparator
 112  
         implements Comparator<NodeLocation>
 113  
     {
 114  
         public int compare( NodeLocation o1, NodeLocation o2 )
 115  
         {
 116  0
             if ( o1 == null && o2 == null )
 117  
             {
 118  0
                 return 0;
 119  
             }
 120  
 
 121  0
             if ( o1 == null && o2 != null )
 122  
             {
 123  0
                 return 1;
 124  
             }
 125  
 
 126  0
             if ( o1 != null && o2 == null )
 127  
             {
 128  0
                 return -1;
 129  
             }
 130  
 
 131  
 //            String version1 = o1.artifact.getVersion();
 132  
 //            String version2 = o2.artifact.getVersion();
 133  
 //
 134  
 //            VersionComparator.getInstance().compare( version1, version2 );
 135  
             
 136  0
             return 0;
 137  
         }
 138  
     }
 139  
 
 140  
     class DistantNodeLocationPredicate
 141  
         implements Predicate
 142  
     {
 143  
         private int cutoff;
 144  
 
 145  
         public DistantNodeLocationPredicate( int distantCutoff )
 146  0
         {
 147  0
             this.cutoff = distantCutoff;
 148  0
         }
 149  
 
 150  
         public boolean evaluate( Object object )
 151  
         {
 152  0
             boolean satisfies = false;
 153  
 
 154  0
             if ( object instanceof NodeLocation )
 155  
             {
 156  0
                 NodeLocation nodeloc = (NodeLocation) object;
 157  0
                 satisfies = ( nodeloc.depth >= this.cutoff );
 158  
             }
 159  
 
 160  0
             return satisfies;
 161  
         }
 162  
     }
 163  
 
 164  
     private List<DependencyGraphNode> conflictingArtifacts;
 165  
 
 166  0
     private Map<String,NodeLocation> foundNodesMap = new HashMap<String, NodeLocation>();
 167  
 
 168  0
     private int currentDepth = 0;
 169  
 
 170  
     private DependencyGraph currentGraph;
 171  
 
 172  
     @SuppressWarnings("unchecked")
 173  
     public RefineConflictsVisitor()
 174  0
     {
 175  0
         conflictingArtifacts = TypedList.decorate( new ArrayList<ArtifactReference>(), ArtifactReference.class );
 176  0
     }
 177  
 
 178  
     public void discoverGraph( DependencyGraph graph )
 179  
     {
 180  0
         super.discoverGraph( graph );
 181  0
         this.currentGraph = graph;
 182  0
         this.foundNodesMap.clear();
 183  0
     }
 184  
 
 185  
     public void discoverNode( DependencyGraphNode node )
 186  
     {
 187  0
         super.discoverNode( node );
 188  
 
 189  0
         currentDepth++;
 190  
 
 191  0
         List<DependencyGraphEdge> edgesFrom = currentGraph.getEdgesFrom( node );
 192  0
         for ( DependencyGraphEdge edge : edgesFrom )
 193  
         {
 194  0
             if ( this.conflictingArtifacts.contains( edge.getNodeTo() ) )
 195  
             {
 196  0
                 String nodeKey = DependencyGraphKeys.toKey( edge.getNodeTo() );
 197  
                 // Check for existing NodeLocation with same key
 198  0
                 NodeLocation nodeloc = this.foundNodesMap.get( nodeKey );
 199  
 
 200  0
                 if ( ( nodeloc == null ) || ( currentDepth < nodeloc.depth ) )
 201  
                 {
 202  0
                     nodeloc = new NodeLocation( edge.getNodeTo(), edge, currentDepth );
 203  0
                     this.foundNodesMap.put( nodeKey, nodeloc );
 204  
                 }
 205  0
             }
 206  
         }
 207  0
     }
 208  
 
 209  
     public void finishGraph( DependencyGraph graph )
 210  
     {
 211  0
         super.finishGraph( graph );
 212  
 
 213  0
         if ( MapUtils.isEmpty( this.foundNodesMap ) )
 214  
         {
 215  0
             return;
 216  
         }
 217  
 
 218  
         // Find winning node.
 219  0
         ArtifactReference winningArtifact = findWinningArtifact( this.foundNodesMap.values() );
 220  0
         DependencyGraphNode winningNode = graph.getNode( winningArtifact );
 221  
 
 222  
         // Gather up Losing Nodes.
 223  0
         Set<NodeLocation> losingNodes = new HashSet<NodeLocation>();
 224  0
         Predicate losersPredicate = NotPredicate.getInstance( new NodeLocationPredicate( winningArtifact ) );
 225  0
         CollectionUtils.select( this.foundNodesMap.values(), losersPredicate, losingNodes );
 226  
 
 227  
         // Swing losing nodes to winning node.
 228  0
         for ( NodeLocation losingNodeLoc : losingNodes )
 229  
         {
 230  0
             DependencyGraphNode losingNode = graph.getNode( losingNodeLoc.artifact );
 231  0
             DependencyGraphUtils.collapseNodes( graph, losingNode, winningNode );
 232  0
         }
 233  0
     }
 234  
 
 235  
     @SuppressWarnings("unchecked")
 236  
     private ArtifactReference findWinningArtifact( Collection<NodeLocation> nodes )
 237  
     {
 238  0
         List<NodeLocation> remainingNodes = new ArrayList<NodeLocation>();
 239  0
         remainingNodes.addAll( nodes );
 240  
 
 241  
         /* .\ Filter by Depth \.____________________________________________________ */
 242  
 
 243  
         // Sort by depth.
 244  0
         Collections.sort( remainingNodes, new DepthComparator() );
 245  
 
 246  
         // Determine 'closest' node depth.
 247  0
         NodeLocation nearestNode = remainingNodes.get( 0 );
 248  0
         int nearest = nearestNode.depth;
 249  
 
 250  
         // Filter out distant nodes. 
 251  0
         Predicate distantLocations = new DistantNodeLocationPredicate( nearest );
 252  0
         CollectionUtils.filter( remainingNodes, distantLocations );
 253  
 
 254  
         // Do we have 1 node left?
 255  0
         if ( remainingNodes.size() == 1 )
 256  
         {
 257  
             // A winner!
 258  0
             NodeLocation nodeloc = remainingNodes.get( 0 );
 259  0
             return nodeloc.artifact;
 260  
         }
 261  
 
 262  
         /* .\ Filter by Newest Version \.___________________________________________ */
 263  
 
 264  
         // We have 2 or more nodes that are equal distance from the root.
 265  
         // Determine which one is 'newest' based on version id.
 266  0
         Collections.sort( remainingNodes, new ReverseComparator( new NodeLocationVersionComparator() ) );
 267  
 
 268  0
         NodeLocation nodeloc = remainingNodes.get( 0 );
 269  0
         return nodeloc.artifact;
 270  
     }
 271  
 
 272  
     public void finishNode( DependencyGraphNode node )
 273  
     {
 274  0
         super.finishNode( node );
 275  0
         currentDepth--;
 276  0
     }
 277  
 
 278  
     public List<DependencyGraphNode> getConflictingArtifacts()
 279  
     {
 280  0
         return conflictingArtifacts;
 281  
     }
 282  
 
 283  
     public void addAllConflictingArtifacts( Collection<DependencyGraphNode> nodes )
 284  
     {
 285  0
         this.conflictingArtifacts.addAll( nodes );
 286  0
     }
 287  
 
 288  
     public void resetConflictingArtifacts()
 289  
     {
 290  0
         this.conflictingArtifacts.clear();
 291  0
     }
 292  
 }