View Javadoc

1   package org.apache.maven.index.archetype;
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.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          // can't cache this because indexes can be changed
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          // TODO maybe update index
99      }
100 
101     // ==
102 
103     protected abstract List<IndexingContext> getIndexingContexts();
104 }