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 javax.inject.Inject;
28  
29  import org.apache.lucene.search.Query;
30  import org.apache.maven.archetype.catalog.Archetype;
31  import org.apache.maven.archetype.catalog.ArchetypeCatalog;
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.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  /**
43   * Support class to implement {@code org.apache.maven.archetype.source.ArchetypeDataSource} interface. Extend this class
44   * to suit your needs.
45   */
46  public abstract class AbstractArchetypeDataSource
47  {
48  
49      private final Logger logger = LoggerFactory.getLogger( getClass() );
50  
51      protected Logger getLogger()
52      {
53          return logger;
54      }
55  
56      private final Indexer indexer;
57  
58  
59      @Inject
60      protected AbstractArchetypeDataSource( Indexer indexer )
61      {
62          this.indexer = indexer;
63      }
64  
65      public ArchetypeCatalog getArchetypeCatalog( final Properties properties )
66      {
67          final ArchetypeCatalog catalog = new ArchetypeCatalog();
68          try
69          {
70              final Map<String, String> repositories = getRepositoryMap();
71              final Query pq =
72                  indexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression( "maven-archetype" ) );
73              final FlatSearchRequest searchRequest = new FlatSearchRequest( pq );
74              searchRequest.setContexts( getIndexingContexts() );
75              final FlatSearchResponse searchResponse = indexer.searchFlat( searchRequest );
76              for ( ArtifactInfo info : searchResponse.getResults() )
77              {
78                  Archetype archetype = new Archetype();
79                  archetype.setGroupId( info.getGroupId() );
80                  archetype.setArtifactId( info.getArtifactId() );
81                  archetype.setVersion( info.getVersion() );
82                  archetype.setDescription( info.getDescription() );
83                  archetype.setRepository( repositories.get( info.getRepository() ) );
84                  catalog.addArchetype( archetype );
85              }
86          }
87          catch ( Exception ex )
88          {
89              getLogger().error( "Unable to retrieve archetypes", ex );
90          }
91  
92          return catalog;
93      }
94  
95      private Map<String, String> getRepositoryMap()
96      {
97          // can't cache this because indexes can be changed
98          Map<String, String> repositories = new HashMap<>();
99  
100         for ( IndexingContext context : getIndexingContexts() )
101         {
102             String repositoryUrl = context.getRepositoryUrl();
103             if ( repositoryUrl != null )
104             {
105                 repositories.put( context.getId(), repositoryUrl );
106             }
107         }
108 
109         return repositories;
110     }
111 
112     public void updateCatalog( Properties properties, Archetype archetype )
113     {
114         // TODO maybe update index
115     }
116 
117     // ==
118 
119     protected abstract List<IndexingContext> getIndexingContexts();
120 }