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.examples.services.impl;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.LinkedHashMap;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.apache.lucene.queryparser.classic.ParseException;
30  import org.apache.maven.index.ArtifactInfo;
31  import org.apache.maven.index.examples.indexing.RepositoryIndexManager;
32  import org.apache.maven.index.examples.indexing.RepositoryIndexer;
33  import org.apache.maven.index.examples.indexing.SearchRequest;
34  import org.apache.maven.index.examples.indexing.SearchResults;
35  import org.apache.maven.index.examples.services.ArtifactIndexingService;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  import org.springframework.beans.factory.annotation.Autowired;
39  import org.springframework.stereotype.Component;
40  
41  /**
42   * @author mtodorov
43   */
44  @Component
45  public class ArtifactIndexingServiceImpl implements ArtifactIndexingService {
46  
47      private static final Logger LOGGER = LoggerFactory.getLogger(ArtifactIndexingServiceImpl.class);
48  
49      @Autowired
50      private RepositoryIndexManager repositoryIndexManager;
51  
52      @Override
53      public void addToIndex(
54              String repositoryId,
55              File artifactFile,
56              String groupId,
57              String artifactId,
58              String version,
59              String extension,
60              String classifier)
61              throws IOException {
62          final RepositoryIndexer indexer = repositoryIndexManager.getRepositoryIndex(repositoryId);
63  
64          indexer.addArtifactToIndex(repositoryId, artifactFile, groupId, artifactId, version, extension, classifier);
65      }
66  
67      @Override
68      public void deleteFromIndex(
69              String repositoryId, String groupId, String artifactId, String version, String extension, String classifier)
70              throws IOException {
71          final RepositoryIndexer indexer = repositoryIndexManager.getRepositoryIndex(repositoryId);
72          if (indexer != null) {
73              indexer.delete(
74                      Arrays.asList(new ArtifactInfo(repositoryId, groupId, artifactId, version, classifier, extension)));
75          }
76      }
77  
78      @Override
79      public SearchResults search(SearchRequest searchRequest) throws IOException, ParseException {
80          SearchResults searchResults = new SearchResults();
81  
82          final String repositoryId = searchRequest.getRepository();
83  
84          if (repositoryId != null && !repositoryId.isEmpty()) {
85              LOGGER.debug("Repository: {}", repositoryId);
86  
87              final Map<String, Collection<ArtifactInfo>> resultsMap =
88                      getResultsMap(repositoryId, searchRequest.getQuery());
89  
90              if (!resultsMap.isEmpty()) {
91                  searchResults.setResults(resultsMap);
92              }
93  
94              if (LOGGER.isDebugEnabled()) {
95                  int results = resultsMap.entrySet().iterator().next().getValue().size();
96  
97                  LOGGER.debug("Results: {}", results);
98              }
99          } else {
100             Map<String, Collection<ArtifactInfo>> resultsMap = new LinkedHashMap<>();
101             for (String repoId : repositoryIndexManager.getIndexes().keySet()) {
102                 LOGGER.debug("Repository: {}", repoId);
103 
104                 final RepositoryIndexer repositoryIndex = repositoryIndexManager.getRepositoryIndex(repoId);
105                 if (repositoryIndex != null) {
106                     final Set<ArtifactInfo> artifactInfoResults =
107                             repositoryIndexManager.getRepositoryIndex(repoId).search(searchRequest.getQuery());
108 
109                     if (!artifactInfoResults.isEmpty()) {
110                         resultsMap.put(repoId, artifactInfoResults);
111                     }
112 
113                     LOGGER.debug("Results: {}", artifactInfoResults.size());
114                 }
115             }
116 
117             searchResults.setResults(resultsMap);
118         }
119 
120         return searchResults;
121     }
122 
123     @Override
124     public boolean contains(SearchRequest searchRequest) throws IOException, ParseException {
125         return !getResultsMap(searchRequest.getRepository(), searchRequest.getQuery())
126                 .isEmpty();
127     }
128 
129     public Map<String, Collection<ArtifactInfo>> getResultsMap(String repositoryId, String query)
130             throws IOException, ParseException {
131         Map<String, Collection<ArtifactInfo>> resultsMap = new LinkedHashMap<>();
132         final Set<ArtifactInfo> artifactInfoResults =
133                 repositoryIndexManager.getRepositoryIndex(repositoryId).search(query);
134 
135         if (!artifactInfoResults.isEmpty()) {
136             resultsMap.put(repositoryId, artifactInfoResults);
137         }
138 
139         return resultsMap;
140     }
141 
142     public RepositoryIndexManager getRepositoryIndexManager() {
143         return repositoryIndexManager;
144     }
145 
146     public void setRepositoryIndexManager(RepositoryIndexManager repositoryIndexManager) {
147         this.repositoryIndexManager = repositoryIndexManager;
148     }
149 }