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