View Javadoc

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      {
73          nodes = new ArrayList<Artifact>();
74          artifacts = new HashMap<String, Artifact>();
75          for ( Artifact a : runtimeArtifacts )
76          {
77              artifacts.put( a.getDependencyConflictId(), a );
78          }
79          this.examinerCache = examinerCache;
80          this.explicitLibs = explicitLibraries;
81          this.log = log;
82          this.root = root;
83          this.useOsgiDependencies = useOsgiDependencies;
84          duplicates = new HashSet<String>();
85          conflicts = new HashSet<String>();
86          includes = new HashSet<String>();
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      public boolean visit( DependencyNode node )
93      {
94          if ( throwable != null )
95          {
96              return false;
97          }
98          if ( root == node )
99          {
100             return true;
101         }
102         try
103         {
104             Artifact artifact = node.getArtifact();
105             if ( !artifacts.containsKey( artifact.getDependencyConflictId() ) )
106             {
107                 //ignore non-runtime stuff..
108                 return false;
109             }
110             // somehow the transitive artifacts in the  tree are not always resolved?
111             artifact = artifacts.get( artifact.getDependencyConflictId() );
112 
113             ExamineManifest depExaminator = examinerCache.get( artifact );
114             if ( depExaminator == null )
115             {
116                 depExaminator = new ExamineManifest( log );
117                 depExaminator.setArtifactFile( artifact.getFile() );
118                 depExaminator.checkFile();
119                 examinerCache.put( artifact, depExaminator );
120             }
121             if ( AbstractNbmMojo.matchesLibrary( artifact, explicitLibs, depExaminator, log, useOsgiDependencies ) )
122             {
123                 if ( depExaminator.isNetBeansModule() )
124                 {
125                     log.warn(
126                         "You are using a NetBeans Module as a Library (classpath extension): " + artifact.getId() );
127                 }
128 
129                 nodes.add( artifact );
130                 includes.add( artifact.getDependencyConflictId() );
131                 // if a library, iterate to it's child nodes.
132                 return true;
133             }
134         }
135         catch ( MojoExecutionException mojoExecutionException )
136         {
137             throwable = mojoExecutionException;
138         }
139         //don't bother iterating to childs if the current node is not a library.
140         return false;
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     public boolean endVisit( DependencyNode node )
147     {
148         if ( throwable != null )
149         {
150             return false;
151         }
152         if ( node == root )
153         {
154             if ( nodes.size() > 0 )
155             {
156                 log.info( "Adding on module's Class-Path:" );
157                 for ( Artifact inc : nodes )
158                 {
159                     log.info( "    " + inc.getId() );
160                 }
161             }
162         }
163         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         if ( throwable != null )
175         {
176             throw throwable;
177         }
178         return nodes;
179     }
180 }