View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.index.archetype;
20  
21  import javax.inject.Inject;
22  
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Properties;
27  
28  import org.apache.lucene.search.Query;
29  import org.apache.maven.archetype.catalog.Archetype;
30  import org.apache.maven.archetype.catalog.ArchetypeCatalog;
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.Indexer;
35  import org.apache.maven.index.MAVEN;
36  import org.apache.maven.index.context.IndexingContext;
37  import org.apache.maven.index.expr.SourcedSearchExpression;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  /**
42   * Support class to implement {@code org.apache.maven.archetype.source.ArchetypeDataSource} interface. Extend this class
43   * to suit your needs.
44   */
45  public abstract class AbstractArchetypeDataSource {
46  
47      private final Logger logger = LoggerFactory.getLogger(getClass());
48  
49      protected Logger getLogger() {
50          return logger;
51      }
52  
53      private final Indexer indexer;
54  
55      @Inject
56      protected AbstractArchetypeDataSource(Indexer indexer) {
57          this.indexer = indexer;
58      }
59  
60      public ArchetypeCatalog getArchetypeCatalog(final Properties properties) {
61          final ArchetypeCatalog catalog = new ArchetypeCatalog();
62          try {
63              final Map<String, String> repositories = getRepositoryMap();
64              final Query pq = indexer.constructQuery(MAVEN.PACKAGING, new SourcedSearchExpression("maven-archetype"));
65              final FlatSearchRequest searchRequest = new FlatSearchRequest(pq);
66              searchRequest.setContexts(getIndexingContexts());
67              final FlatSearchResponse searchResponse = indexer.searchFlat(searchRequest);
68              for (ArtifactInfo info : searchResponse.getResults()) {
69                  Archetype archetype = new Archetype();
70                  archetype.setGroupId(info.getGroupId());
71                  archetype.setArtifactId(info.getArtifactId());
72                  archetype.setVersion(info.getVersion());
73                  archetype.setDescription(info.getDescription());
74                  archetype.setRepository(repositories.get(info.getRepository()));
75                  catalog.addArchetype(archetype);
76              }
77          } catch (Exception ex) {
78              getLogger().error("Unable to retrieve archetypes", ex);
79          }
80  
81          return catalog;
82      }
83  
84      private Map<String, String> getRepositoryMap() {
85          // can't cache this because indexes can be changed
86          Map<String, String> repositories = new HashMap<>();
87  
88          for (IndexingContext context : getIndexingContexts()) {
89              String repositoryUrl = context.getRepositoryUrl();
90              if (repositoryUrl != null) {
91                  repositories.put(context.getId(), repositoryUrl);
92              }
93          }
94  
95          return repositories;
96      }
97  
98      public void updateCatalog(Properties properties, Archetype archetype) {
99          // TODO maybe update index
100     }
101 
102     // ==
103 
104     protected abstract List<IndexingContext> getIndexingContexts();
105 }