Coverage Report - org.codehaus.mojo.nbm.CollectLibrariesNodeVisitor
 
Classes in this File Line Coverage Branch Coverage Complexity
CollectLibrariesNodeVisitor
0%
0/51
0%
0/24
5.75
 
 1  
 /* ==========================================================================
 2  
  * Copyright 2008 mkleint
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  *  Unless required by applicable law or agreed to in writing, software
 11  
  *  distributed under the License is distributed on an "AS IS" BASIS,
 12  
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  *  See the License for the specific language governing permissions and
 14  
  *  limitations under the License.
 15  
  * =========================================================================
 16  
  */
 17  
 package org.codehaus.mojo.nbm;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.HashMap;
 21  
 import java.util.HashSet;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 import java.util.Set;
 25  
 import org.apache.maven.artifact.Artifact;
 26  
 import org.apache.maven.plugin.MojoExecutionException;
 27  
 import org.apache.maven.plugin.logging.Log;
 28  
 import org.apache.maven.shared.dependency.graph.DependencyNode;
 29  
 import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
 30  
 import org.codehaus.mojo.nbm.utils.ExamineManifest;
 31  
 
 32  
 /**
 33  
  * A dependency node visitor that collects visited nodes that are known libraries or are
 34  
  * children of known libraries
 35  
  * @author milos kleint
 36  
  */
 37  
 public class CollectLibrariesNodeVisitor
 38  
     implements DependencyNodeVisitor
 39  
 {
 40  
 
 41  
     /**
 42  
      * The collected list of nodes.
 43  
      */
 44  
     private final List<Artifact> nodes;
 45  
 
 46  
     private Map<String, Artifact> artifacts;
 47  
 
 48  
     private Map<Artifact, ExamineManifest> examinerCache;
 49  
 
 50  
     private List<String> explicitLibs;
 51  
 
 52  
     private final Log log;
 53  
 
 54  
     private MojoExecutionException throwable;
 55  
 
 56  
     private DependencyNode root;
 57  
 
 58  
     private Set<String> duplicates;
 59  
 
 60  
     private Set<String> conflicts;
 61  
 
 62  
     private Set<String> includes;
 63  
 
 64  
     private final boolean useOsgiDependencies;
 65  
 
 66  
     /**
 67  
      * Creates a dependency node visitor that collects visited nodes for further processing.
 68  
      */
 69  
     public CollectLibrariesNodeVisitor( List<String> explicitLibraries,
 70  
         List<Artifact> runtimeArtifacts, Map<Artifact, ExamineManifest> examinerCache,
 71  
         Log log, DependencyNode root, boolean useOsgiDependencies )
 72  0
     {
 73  0
         nodes = new ArrayList<Artifact>();
 74  0
         artifacts = new HashMap<String, Artifact>();
 75  0
         for ( Artifact a : runtimeArtifacts )
 76  
         {
 77  0
             artifacts.put( a.getDependencyConflictId(), a );
 78  0
         }
 79  0
         this.examinerCache = examinerCache;
 80  0
         this.explicitLibs = explicitLibraries;
 81  0
         this.log = log;
 82  0
         this.root = root;
 83  0
         this.useOsgiDependencies = useOsgiDependencies;
 84  0
         duplicates = new HashSet<String>();
 85  0
         conflicts = new HashSet<String>();
 86  0
         includes = new HashSet<String>();
 87  0
     }
 88  
 
 89  
     /**
 90  
      * {@inheritDoc}
 91  
      */
 92  
     public boolean visit( DependencyNode node )
 93  
     {
 94  0
         if ( throwable != null )
 95  
         {
 96  0
             return false;
 97  
         }
 98  0
         if ( root == node )
 99  
         {
 100  0
             return true;
 101  
         }
 102  
         try
 103  
         {
 104  0
             Artifact artifact = node.getArtifact();
 105  0
             if ( !artifacts.containsKey( artifact.getDependencyConflictId() ) )
 106  
             {
 107  
                 //ignore non-runtime stuff..
 108  0
                 return false;
 109  
             }
 110  
             // somehow the transitive artifacts in the  tree are not always resolved?
 111  0
             artifact = artifacts.get( artifact.getDependencyConflictId() );
 112  
 
 113  0
             ExamineManifest depExaminator = examinerCache.get( artifact );
 114  0
             if ( depExaminator == null )
 115  
             {
 116  0
                 depExaminator = new ExamineManifest( log );
 117  0
                 depExaminator.setArtifactFile( artifact.getFile() );
 118  0
                 depExaminator.checkFile();
 119  0
                 examinerCache.put( artifact, depExaminator );
 120  
             }
 121  0
             if ( AbstractNbmMojo.matchesLibrary( artifact, explicitLibs, depExaminator, log, useOsgiDependencies ) )
 122  
             {
 123  0
                 if ( depExaminator.isNetBeansModule() )
 124  
                 {
 125  0
                     log.warn(
 126  
                         "You are using a NetBeans Module as a Library (classpath extension): " + artifact.getId() );
 127  
                 }
 128  
 
 129  0
                 nodes.add( artifact );
 130  0
                 includes.add( artifact.getDependencyConflictId() );
 131  
                 // if a library, iterate to it's child nodes.
 132  0
                 return true;
 133  
             }
 134  
         }
 135  0
         catch ( MojoExecutionException mojoExecutionException )
 136  
         {
 137  0
             throwable = mojoExecutionException;
 138  0
         }
 139  
         //don't bother iterating to childs if the current node is not a library.
 140  0
         return false;
 141  
     }
 142  
 
 143  
     /**
 144  
      * {@inheritDoc}
 145  
      */
 146  
     public boolean endVisit( DependencyNode node )
 147  
     {
 148  0
         if ( throwable != null )
 149  
         {
 150  0
             return false;
 151  
         }
 152  0
         if ( node == root )
 153  
         {
 154  0
             if ( nodes.size() > 0 )
 155  
             {
 156  0
                 log.info( "Adding on module's Class-Path:" );
 157  0
                 for ( Artifact inc : nodes )
 158  
                 {
 159  0
                     log.info( "    " + inc.getId() );
 160  0
                 }
 161  
             }
 162  
         }
 163  0
         return true;
 164  
     }
 165  
 
 166  
     /**
 167  
      * Gets the list of collected dependency nodes.
 168  
      * 
 169  
      * @return the list of collected dependency nodes
 170  
      */
 171  
     public List<Artifact> getArtifacts()
 172  
         throws MojoExecutionException
 173  
     {
 174  0
         if ( throwable != null )
 175  
         {
 176  0
             throw throwable;
 177  
         }
 178  0
         return nodes;
 179  
     }
 180  
 }