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 | 2 | 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 | 1 | ArchetypeCatalog catalog = new ArchetypeCatalog(); |
57 | |
|
58 | |
try |
59 | |
{ |
60 | 1 | Map<String, String> repositories = getRepositoryMap(); |
61 | |
|
62 | 1 | Query pq = indexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression( "maven-archetype" ) ); |
63 | |
|
64 | 1 | FlatSearchRequest searchRequest = new FlatSearchRequest( pq ); |
65 | |
|
66 | 1 | FlatSearchResponse searchResponse = indexer.searchFlat( searchRequest ); |
67 | |
|
68 | 1 | for ( ArtifactInfo info : searchResponse.getResults() ) |
69 | |
{ |
70 | 4 | Archetype archetype = new Archetype(); |
71 | 4 | archetype.setGroupId( info.groupId ); |
72 | 4 | archetype.setArtifactId( info.artifactId ); |
73 | 4 | archetype.setVersion( info.version ); |
74 | 4 | archetype.setDescription( info.description ); |
75 | 4 | archetype.setRepository( repositories.get( info.repository ) ); |
76 | |
|
77 | 4 | catalog.addArchetype( archetype ); |
78 | 4 | } |
79 | |
} |
80 | 0 | catch ( Exception ex ) |
81 | |
{ |
82 | 0 | getLogger().error( "Unable to retrieve archetypes", ex ); |
83 | 1 | } |
84 | |
|
85 | 1 | return catalog; |
86 | |
} |
87 | |
|
88 | |
private Map<String, String> getRepositoryMap() |
89 | |
{ |
90 | |
|
91 | 1 | Map<String, String> repositories = new HashMap<String, String>(); |
92 | |
|
93 | 1 | for ( IndexingContext context : indexer.getIndexingContexts().values() ) |
94 | |
{ |
95 | 1 | String repositoryUrl = context.getRepositoryUrl(); |
96 | 1 | if ( repositoryUrl != null ) |
97 | |
{ |
98 | 1 | repositories.put( context.getId(), repositoryUrl ); |
99 | |
} |
100 | 1 | } |
101 | |
|
102 | 1 | return repositories; |
103 | |
} |
104 | |
|
105 | |
public void updateCatalog( Properties properties, Archetype archetype ) |
106 | |
{ |
107 | |
|
108 | 0 | } |
109 | |
} |