Coverage Report - org.apache.maven.archiva.dependency.graph.DependencyGraphUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
DependencyGraphUtils
0%
0/89
0%
0/42
0
 
 1  
 package org.apache.maven.archiva.dependency.graph;
 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.HashSet;
 23  
 import java.util.List;
 24  
 import java.util.Set;
 25  
 
 26  
 import org.apache.commons.collections.CollectionUtils;
 27  
 import org.apache.commons.collections.Predicate;
 28  
 import org.apache.commons.collections.functors.AndPredicate;
 29  
 import org.apache.commons.collections.functors.NotPredicate;
 30  
 import org.apache.commons.lang.StringUtils;
 31  
 import org.apache.maven.archiva.dependency.graph.functors.NodePredicate;
 32  
 import org.apache.maven.archiva.dependency.graph.functors.OrphanedNodePredicate;
 33  
 import org.apache.maven.archiva.model.ArchivaProjectModel;
 34  
 import org.apache.maven.archiva.model.ArtifactReference;
 35  
 import org.apache.maven.archiva.model.Dependency;
 36  
 import org.apache.maven.archiva.model.DependencyScope;
 37  
 import org.apache.maven.archiva.model.Exclusion;
 38  
 import org.apache.maven.archiva.model.VersionedReference;
 39  
 
 40  
 /**
 41  
  * Utilities for manipulating the DependencyGraph. 
 42  
  *
 43  
  * @version $Id: DependencyGraphUtils.java 755277 2009-03-17 15:18:35Z brett $
 44  
  */
 45  0
 public class DependencyGraphUtils
 46  
 {
 47  
     /**
 48  
      * Standard way to add a model to the graph.
 49  
      * 
 50  
      * NOTE: Used by archiva-repository-layer runtime and archiva-dependency-graph tests.
 51  
      * 
 52  
      * @param model the model to add
 53  
      * @param graph the graph to add it to
 54  
      * @param fromNode the node to add it from.
 55  
      */
 56  
     public static void addNodeFromModel( ArchivaProjectModel model, DependencyGraph graph, DependencyGraphNode fromNode )
 57  
     {
 58  0
         if ( model == null )
 59  
         {
 60  0
             throw new IllegalStateException( "Unable to add null model for "
 61  
                 + DependencyGraphKeys.toKey( fromNode.getArtifact() ) );
 62  
         }
 63  
 
 64  0
         if ( model.getRelocation() != null )
 65  
         {
 66  
             // We need to CHANGE this node.
 67  0
             ArtifactReference refTO = new ArtifactReference();
 68  
 
 69  0
             refTO.setGroupId( fromNode.getArtifact().getGroupId() );
 70  0
             refTO.setArtifactId( fromNode.getArtifact().getArtifactId() );
 71  0
             refTO.setVersion( fromNode.getArtifact().getVersion() );
 72  0
             refTO.setClassifier( fromNode.getArtifact().getClassifier() );
 73  0
             refTO.setType( fromNode.getArtifact().getType() );
 74  
 
 75  0
             VersionedReference relocation = model.getRelocation();
 76  
 
 77  0
             if ( StringUtils.isNotBlank( relocation.getGroupId() ) )
 78  
             {
 79  0
                 refTO.setGroupId( relocation.getGroupId() );
 80  
             }
 81  
 
 82  0
             if ( StringUtils.isNotBlank( relocation.getArtifactId() ) )
 83  
             {
 84  0
                 refTO.setArtifactId( relocation.getArtifactId() );
 85  
             }
 86  
 
 87  0
             if ( StringUtils.isNotBlank( relocation.getVersion() ) )
 88  
             {
 89  0
                 refTO.setVersion( relocation.getVersion() );
 90  
             }
 91  
 
 92  0
             DependencyGraphNode nodeTO = new DependencyGraphNode( refTO );
 93  
 
 94  0
             graph.addNode( nodeTO );
 95  0
             collapseNodes( graph, fromNode, nodeTO );
 96  0
             return;
 97  
         }
 98  
 
 99  0
         boolean isRootNode = graph.getRootNode().equals( fromNode );
 100  
 
 101  0
         if ( CollectionUtils.isNotEmpty( model.getDependencyManagement() ) )
 102  
         {
 103  0
             for ( Dependency dependency : model.getDependencyManagement() )
 104  
             {
 105  0
                 fromNode.addDependencyManagement( dependency );
 106  
             }
 107  
         }
 108  
 
 109  0
         if ( CollectionUtils.isNotEmpty( model.getDependencies() ) )
 110  
         {
 111  0
             for ( Dependency dependency : model.getDependencies() )
 112  
             {
 113  0
                 String scope = dependency.getScope();
 114  
 
 115  
                 // Test scopes *NOT* from root node can be skipped.
 116  0
                 if ( DependencyScope.TEST.equals( scope ) && !isRootNode )
 117  
                 {
 118  
                     // skip add of test scope
 119  0
                     continue;
 120  
                 }
 121  
 
 122  0
                 ArtifactReference artifactRef = new ArtifactReference();
 123  0
                 artifactRef.setGroupId( dependency.getGroupId() );
 124  0
                 artifactRef.setArtifactId( dependency.getArtifactId() );
 125  0
                 artifactRef.setVersion( dependency.getVersion() );
 126  0
                 artifactRef.setClassifier( dependency.getClassifier() );
 127  0
                 artifactRef.setType( dependency.getType() );
 128  
 
 129  0
                 DependencyGraphNode toNode = new DependencyGraphNode( artifactRef );
 130  
 
 131  0
                 if ( CollectionUtils.isNotEmpty( dependency.getExclusions() ) )
 132  
                 {
 133  0
                     for ( Exclusion exclusion : dependency.getExclusions() )
 134  
                     {
 135  0
                         toNode.addExclude( exclusion );
 136  
                     }
 137  
                 }
 138  
 
 139  0
                 if ( dependency.isFromParent() )
 140  
                 {
 141  0
                     toNode.setFromParent( true );
 142  
                 }
 143  
 
 144  
                 // Add node (to)
 145  0
                 graph.addNode( toNode );
 146  
 
 147  0
                 DependencyGraphEdge edge = new DependencyGraphEdge( fromNode.getArtifact(), toNode.getArtifact() );
 148  0
                 edge.setScope( StringUtils.defaultIfEmpty( dependency.getScope(), DependencyScope.COMPILE ) );
 149  
 
 150  0
                 if ( dependency.isOptional() )
 151  
                 {
 152  0
                     edge.setDisabled( true );
 153  0
                     edge.setDisabledType( DependencyGraph.DISABLED_OPTIONAL );
 154  0
                     edge.setDisabledReason( "Optional Dependency" );
 155  
                 }
 156  
 
 157  0
                 graph.addEdge( edge );
 158  0
             }
 159  
         }
 160  
 
 161  0
         fromNode.setResolved( true );
 162  0
         graph.addNode( fromNode );
 163  0
     }
 164  
 
 165  
     /**
 166  
      * Clean out any nodes that may have become orphaned in the graph.
 167  
      * 
 168  
      * @param graph the graph to check.
 169  
      */
 170  
     public static void cleanupOrphanedNodes( DependencyGraph graph )
 171  
     {
 172  0
         boolean done = false;
 173  
 
 174  0
         Predicate orphanedNodePredicate = new OrphanedNodePredicate( graph );
 175  0
         Predicate notRootNode = NotPredicate.getInstance( new NodePredicate( graph.getRootNode().getArtifact() ) );
 176  0
         Predicate orphanedChildNodePredicate = AndPredicate.getInstance( notRootNode, orphanedNodePredicate );
 177  
 
 178  0
         while ( !done )
 179  
         {
 180  
             // Find orphaned node.
 181  0
             DependencyGraphNode orphanedNode = (DependencyGraphNode) CollectionUtils.find( graph.getNodes(),
 182  
                                                                                            orphanedChildNodePredicate );
 183  
 
 184  0
             if ( orphanedNode == null )
 185  
             {
 186  0
                 done = true;
 187  0
                 break;
 188  
             }
 189  
 
 190  
             // Remove edges FROM orphaned node.
 191  0
             for ( DependencyGraphEdge edge : graph.getEdgesFrom( orphanedNode ) )
 192  
             {
 193  0
                 graph.removeEdge( edge );
 194  
             }
 195  
 
 196  
             // Remove orphaned node.
 197  0
             graph.removeNode( orphanedNode );
 198  0
         }
 199  0
     }
 200  
 
 201  
     /**
 202  
      * Functionaly similar to {@link #collapseVersions(DependencyGraph, ArtifactReference, String, String)}, but 
 203  
      * in a new, easier to use, format.
 204  
      * 
 205  
      * 1) Removes the FROM edges connected to the FROM node
 206  
      * 2) Moves the TO edges connected to the FROM node to the TO node.
 207  
      * 3) Removes the FROM node (which is now orphaned)  
 208  
      *  
 209  
      * @param graph the graph to perform operation on
 210  
      * @param nodeFrom the node to collapse from
 211  
      * @param nodeTo the node to collapse to
 212  
      */
 213  
     public static void collapseNodes( DependencyGraph graph, DependencyGraphNode nodeFROM, DependencyGraphNode nodeTO )
 214  
     {
 215  0
         Set<DependencyGraphEdge> edgesToRemove = new HashSet<DependencyGraphEdge>();
 216  
 
 217  
         // 1) Remove all of the edge.from references from nodeFROM
 218  0
         List<DependencyGraphEdge> fromEdges = graph.getEdgesFrom( nodeFROM );
 219  0
         if ( CollectionUtils.isNotEmpty( fromEdges ) )
 220  
         {
 221  0
             edgesToRemove.addAll( fromEdges );
 222  
         }
 223  
 
 224  
         // 2) Swing all of the edge.to references from nodeFROM to nodeTO.
 225  
         //        System.out.println( "Swinging incoming edges from " + nodeFROM );
 226  
         //        System.out.println( "                          to " + nodeTO );
 227  0
         List<DependencyGraphEdge> toEdges = graph.getEdgesTo( nodeFROM );
 228  0
         for ( DependencyGraphEdge edge : toEdges )
 229  
         {
 230  
             // Identify old edge to remove.
 231  0
             edgesToRemove.add( edge );
 232  
 
 233  
             // Clone edge, set edge.to and add to graph.
 234  0
             DependencyGraphEdge newedge = clone( edge );
 235  0
             newedge.setNodeTo( nodeTO );
 236  
             //            System.out.println( "   edge from: " + edge );
 237  
             //            System.out.println( "          to: " + newedge );
 238  0
             graph.addEdge( newedge );
 239  0
         }
 240  
 
 241  
         // Actually remove the old edges.
 242  0
         for ( DependencyGraphEdge edge : edgesToRemove )
 243  
         {
 244  0
             graph.removeEdge( edge );
 245  
         }
 246  
 
 247  
         // 3) Remove the nodeFROM
 248  0
         graph.removeNode( nodeFROM );
 249  0
     }
 250  
 
 251  
     /**
 252  
      * Create a clone of an edge.
 253  
      * 
 254  
      * @param edge the edge to clone.
 255  
      * @return the cloned edge.
 256  
      */
 257  
     public static DependencyGraphEdge clone( DependencyGraphEdge edge )
 258  
     {
 259  0
         DependencyGraphEdge cloned = new DependencyGraphEdge( edge.getNodeFrom(), edge.getNodeTo() );
 260  0
         cloned.setDisabled( edge.isDisabled() );
 261  0
         cloned.setDisabledReason( edge.getDisabledReason() );
 262  0
         cloned.setDisabledType( edge.getDisabledType() );
 263  0
         cloned.setScope( edge.getScope() );
 264  
 
 265  0
         return cloned;
 266  
     }
 267  
 }