1 package org.apache.maven.index.archetype;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.Properties;
25
26 import org.apache.lucene.search.Query;
27 import org.apache.maven.archetype.catalog.Archetype;
28 import org.apache.maven.archetype.catalog.ArchetypeCatalog;
29 import org.apache.maven.archetype.source.ArchetypeDataSource;
30 import org.apache.maven.archetype.source.ArchetypeDataSourceException;
31 import org.apache.maven.index.ArtifactInfo;
32 import org.apache.maven.index.FlatSearchRequest;
33 import org.apache.maven.index.FlatSearchResponse;
34 import org.apache.maven.index.MAVEN;
35 import org.apache.maven.index.NexusIndexer;
36 import org.apache.maven.index.context.IndexingContext;
37 import org.apache.maven.index.expr.SourcedSearchExpression;
38 import org.codehaus.plexus.component.annotations.Component;
39 import org.codehaus.plexus.component.annotations.Requirement;
40 import org.codehaus.plexus.logging.AbstractLogEnabled;
41
42
43
44
45 @Component( role = ArchetypeDataSource.class, hint = "nexus" )
46 public class NexusArchetypeDataSource
47 extends AbstractLogEnabled
48 implements ArchetypeDataSource
49 {
50 @Requirement
51 private NexusIndexer indexer;
52
53 public ArchetypeCatalog getArchetypeCatalog( Properties properties )
54 throws ArchetypeDataSourceException
55 {
56 ArchetypeCatalog catalog = new ArchetypeCatalog();
57
58 try
59 {
60 Map<String, String> repositories = getRepositoryMap();
61
62 Query pq = indexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression( "maven-archetype" ) );
63
64 FlatSearchRequest searchRequest = new FlatSearchRequest( pq );
65
66 FlatSearchResponse searchResponse = indexer.searchFlat( searchRequest );
67
68 for ( ArtifactInfo info : searchResponse.getResults() )
69 {
70 Archetype archetype = new Archetype();
71 archetype.setGroupId( info.groupId );
72 archetype.setArtifactId( info.artifactId );
73 archetype.setVersion( info.version );
74 archetype.setDescription( info.description );
75 archetype.setRepository( repositories.get( info.repository ) );
76
77 catalog.addArchetype( archetype );
78 }
79 }
80 catch ( Exception ex )
81 {
82 getLogger().error( "Unable to retrieve archetypes", ex );
83 }
84
85 return catalog;
86 }
87
88 private Map<String, String> getRepositoryMap()
89 {
90
91 Map<String, String> repositories = new HashMap<String, String>();
92
93 for ( IndexingContext context : indexer.getIndexingContexts().values() )
94 {
95 String repositoryUrl = context.getRepositoryUrl();
96 if ( repositoryUrl != null )
97 {
98 repositories.put( context.getId(), repositoryUrl );
99 }
100 }
101
102 return repositories;
103 }
104
105 public void updateCatalog( Properties properties, Archetype archetype )
106 {
107
108 }
109 }