]> source.dussan.org Git - archiva.git/blob
b5aa3eb9d6faa5b5ddaad2fd344a5fcaac146c74
[archiva.git] /
1 package org.apache.archiva.consumers.dependencytree;
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.io.File;
23 import java.io.FileWriter;
24 import java.io.IOException;
25 import java.lang.reflect.Field;
26 import java.net.MalformedURLException;
27 import java.util.Collections;
28 import java.util.Date;
29 import java.util.HashMap;
30 import java.util.List;
31
32 import org.apache.commons.io.IOUtils;
33 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
35 import org.apache.maven.archiva.consumers.ConsumerException;
36 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
37 import org.apache.maven.artifact.Artifact;
38 import org.apache.maven.artifact.factory.ArtifactFactory;
39 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
40 import org.apache.maven.artifact.repository.ArtifactRepository;
41 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
42 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
43 import org.apache.maven.artifact.resolver.ArtifactCollector;
44 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
45 import org.apache.maven.project.DefaultMavenProjectBuilder;
46 import org.apache.maven.project.MavenProject;
47 import org.apache.maven.project.MavenProjectBuilder;
48 import org.apache.maven.project.ProjectBuildingException;
49 import org.apache.maven.shared.dependency.tree.DependencyNode;
50 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
51 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
52 import org.apache.maven.shared.dependency.tree.filter.AncestorOrSelfDependencyNodeFilter;
53 import org.apache.maven.shared.dependency.tree.filter.DependencyNodeFilter;
54 import org.apache.maven.shared.dependency.tree.filter.StateDependencyNodeFilter;
55 import org.apache.maven.shared.dependency.tree.traversal.BuildingDependencyNodeVisitor;
56 import org.apache.maven.shared.dependency.tree.traversal.CollectingDependencyNodeVisitor;
57 import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor;
58 import org.apache.maven.shared.dependency.tree.traversal.FilteringDependencyNodeVisitor;
59 import org.dom4j.Document;
60 import org.dom4j.DocumentHelper;
61 import org.dom4j.Element;
62 import org.dom4j.io.OutputFormat;
63 import org.dom4j.io.XMLWriter;
64
65 /**
66  * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
67  *                   role-hint="dependency-tree-generator" instantiation-strategy="per-lookup"
68  */
69 public class DependencyTreeGeneratorConsumer
70     extends AbstractMonitoredConsumer
71     implements KnownRepositoryContentConsumer
72 {
73     /** @plexus.configuration */
74     private File generatedRepositoryLocation;
75
76     /** @plexus.configuration */
77     private File localRepository;
78
79     /** @plexus.requirement */
80     private DependencyTreeBuilder dependencyTreeBuilder;
81
82     /** @plexus.requirement */
83     private ArtifactFactory artifactFactory;
84
85     /** @plexus.requirement role-hint="maven" */
86     private ArtifactMetadataSource artifactMetadataSource;
87
88     /** @plexus.requirement */
89     private ArtifactCollector artifactCollector;
90
91     /** @plexus.requirement */
92     private MavenProjectBuilder projectBuilder;
93     
94     /** @plexus.requirement */
95     private ArtifactRepositoryFactory artifactRepositoryFactory;
96
97     private String repositoryLocation;
98
99     private final DefaultRepositoryLayout layout = new DefaultRepositoryLayout();
100
101     private ArtifactRepository localArtifactRepository;
102
103     private Field rawProjectCacheField;
104
105     private Field processedProjectCacheField;
106
107     private List<String> includes = Collections.singletonList( "**/*.pom" );
108
109     public String getDescription()
110     {
111         return "Generate dependency tree metadata for tracking changes across algorithms";
112     }
113
114     public String getId()
115     {
116         return "dependency-tree-generator";
117     }
118
119     public boolean isPermanent()
120     {
121         return false;
122     }
123
124     public void setGeneratedRepositoryLocation( File generatedRepositoryLocation )
125     {
126         this.generatedRepositoryLocation = generatedRepositoryLocation;
127     }
128
129     public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
130         throws ConsumerException
131     {
132         repositoryLocation = repository.getLocation();
133
134         if ( generatedRepositoryLocation == null )
135         {
136             generatedRepositoryLocation = new File( repositoryLocation );
137         }
138
139         if ( localRepository == null )
140         {
141             // This is a bit crappy, it would be better to operate entirely within
142             // the base repository, but would need to adjust maven-artifact
143             localRepository = new File( System.getProperty( "user.home" ), ".m2/repository" );
144             try
145             {
146                 localArtifactRepository =
147                     artifactRepositoryFactory.createArtifactRepository( "local",
148                                                                         localRepository.toURL().toExternalForm(),
149                                                                         layout, null, null );
150             }
151             catch ( MalformedURLException e )
152             {
153                 throw new ConsumerException( e.getMessage(), e );
154             }
155         }
156     }
157
158     public void completeScan()
159     {
160     }
161
162     public List getExcludes()
163     {
164         return null;
165     }
166
167     public List getIncludes()
168     {
169         return includes;
170     }
171
172     public void processFile( String path )
173         throws ConsumerException
174     {
175         MavenProject project;
176         try
177         {
178             project = projectBuilder.build( new File( repositoryLocation, path ), localArtifactRepository, null, false );
179
180             // manually flush out the cache for memory concerns and more accurate building
181             flushProjectCache( projectBuilder );
182         }
183         catch ( ProjectBuildingException e )
184         {
185             throw new ConsumerException( e.getMessage(), e );
186         }
187
188         DependencyNode rootNode;
189         try
190         {
191             // TODO: do this for different values of new ScopeArtifactFilter( scope )
192             ArtifactFilter artifactFilter = null;
193
194             rootNode =
195                 dependencyTreeBuilder.buildDependencyTree( project, localArtifactRepository, artifactFactory,
196                                                            artifactMetadataSource, artifactFilter, artifactCollector );
197         }
198         catch ( DependencyTreeBuilderException e )
199         {
200             throw new ConsumerException( e.getMessage(), e );
201         }
202
203         Document document = DocumentHelper.createDocument();
204         DependencyNodeVisitor visitor = new XmlSerializingDependencyNodeVisitor( document );
205
206         // TODO: remove the need for this when the serializer can calculate last nodes from visitor calls only
207         visitor = new BuildingDependencyNodeVisitor( visitor );
208
209         CollectingDependencyNodeVisitor collectingVisitor = new CollectingDependencyNodeVisitor();
210         DependencyNodeVisitor firstPassVisitor =
211             new FilteringDependencyNodeVisitor( collectingVisitor, StateDependencyNodeFilter.INCLUDED );
212         rootNode.accept( firstPassVisitor );
213
214         DependencyNodeFilter secondPassFilter = new AncestorOrSelfDependencyNodeFilter( collectingVisitor.getNodes() );
215         visitor = new FilteringDependencyNodeVisitor( visitor, secondPassFilter );
216
217         rootNode.accept( visitor );
218
219         FileWriter writer = null;
220         try
221         {
222             Artifact artifact =
223                 artifactFactory.createProjectArtifact( project.getGroupId(), project.getArtifactId(),
224                                                        project.getVersion() );
225
226             String p = layout.pathOf( artifact );
227             if ( !p.equals( path ) )
228             {
229                 throw new ConsumerException( "Bad path: " + p + "; should be: " + path );
230             }
231
232             File generatedFile = new File( generatedRepositoryLocation, p + ".xml" );
233             generatedFile.getParentFile().mkdirs();
234             writer = new FileWriter( generatedFile );
235             OutputFormat format = OutputFormat.createPrettyPrint();
236             XMLWriter w = new XMLWriter( writer, format );
237             w.write( document );
238         }
239         catch ( IOException e )
240         {
241             throw new ConsumerException( e.getMessage(), e );
242         }
243         finally
244         {
245             IOUtils.closeQuietly( writer );
246         }
247     }
248
249     private void flushProjectCache( MavenProjectBuilder projectBuilder )
250     {
251         try
252         {
253             if ( rawProjectCacheField == null )
254             {
255                 rawProjectCacheField = DefaultMavenProjectBuilder.class.getDeclaredField( "rawProjectCache" );
256                 rawProjectCacheField.setAccessible( true );
257             }
258
259             if ( processedProjectCacheField == null )
260             {
261                 processedProjectCacheField =
262                     DefaultMavenProjectBuilder.class.getDeclaredField( "processedProjectCache" );
263                 processedProjectCacheField.setAccessible( true );
264             }
265
266             rawProjectCacheField.set( projectBuilder, new HashMap() );
267
268             processedProjectCacheField.set( projectBuilder, new HashMap() );
269         }
270         catch ( Exception e )
271         {
272             throw new RuntimeException( e );
273         }
274     }
275
276     private static class XmlSerializingDependencyNodeVisitor
277         implements DependencyNodeVisitor
278     {
279         private Element xmlNode;
280
281         public XmlSerializingDependencyNodeVisitor( Document document )
282         {
283             xmlNode = document.addElement( "tree" );
284         }
285
286         // DependencyNodeVisitor methods ------------------------------------------
287
288         /*
289          * @see org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor#visit(org.apache.maven.shared.dependency.tree.DependencyNode)
290          */
291         public boolean visit( DependencyNode node )
292         {
293             Element dependency = xmlNode.addElement( "dependency" );
294
295             Artifact artifact = node.getArtifact();
296             dependency.addElement( "groupId" ).setText( artifact.getGroupId() );
297             dependency.addElement( "artifactId" ).setText( artifact.getArtifactId() );
298             dependency.addElement( "type" ).setText( artifact.getType() );
299             dependency.addElement( "version" ).setText( artifact.getVersion() );
300             if ( artifact.getScope() != null )
301             {
302                 dependency.addElement( "scope" ).setText( artifact.getScope() );
303             }
304             if ( artifact.getClassifier() != null )
305             {
306                 dependency.addElement( "classifier" ).setText( artifact.getClassifier() );
307             }
308
309             xmlNode = dependency.addElement( "dependencies" );
310
311             return true;
312         }
313
314         /*
315          * @see org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor#endVisit(org.apache.maven.shared.dependency.tree.DependencyNode)
316          */
317         public boolean endVisit( DependencyNode node )
318         {
319             Element e = xmlNode.getParent();
320
321             if ( !xmlNode.hasContent() )
322             {
323                 e.remove( xmlNode );
324             }
325
326             xmlNode = e.getParent();
327
328             return true;
329         }
330     }
331
332     public void setIncludes( List<String> includes )
333     {
334         this.includes = includes;
335     }
336 }