1 package org.apache.maven.archiva.indexer.search;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.apache.lucene.document.Document;
27 import org.apache.lucene.queryParser.MultiFieldQueryParser;
28 import org.apache.lucene.queryParser.ParseException;
29 import org.apache.lucene.queryParser.QueryParser;
30 import org.apache.lucene.search.Hits;
31 import org.apache.lucene.search.MultiSearcher;
32 import org.apache.lucene.search.Searchable;
33 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
34 import org.apache.maven.archiva.configuration.ConfigurationNames;
35 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
36 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
37 import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory;
38 import org.apache.maven.archiva.indexer.RepositoryIndexException;
39 import org.apache.maven.archiva.indexer.RepositoryIndexSearchException;
40 import org.apache.maven.archiva.indexer.bytecode.BytecodeHandlers;
41 import org.apache.maven.archiva.indexer.filecontent.FileContentHandlers;
42 import org.apache.maven.archiva.indexer.hashcodes.HashcodesHandlers;
43 import org.apache.maven.archiva.indexer.hashcodes.HashcodesKeys;
44 import org.apache.maven.archiva.indexer.lucene.LuceneEntryConverter;
45 import org.apache.maven.archiva.indexer.lucene.LuceneQuery;
46 import org.apache.maven.archiva.indexer.lucene.LuceneRepositoryContentRecord;
47 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
48 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
49 import org.codehaus.plexus.registry.Registry;
50 import org.codehaus.plexus.registry.RegistryListener;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * DefaultCrossRepositorySearch
57 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
59 * @plexus.component role="org.apache.maven.archiva.indexer.search.CrossRepositorySearch" role-hint="default"
61 public class DefaultCrossRepositorySearch
62 implements CrossRepositorySearch, RegistryListener, Initializable
64 private Logger log = LoggerFactory.getLogger( DefaultCrossRepositorySearch.class );
67 * @plexus.requirement role-hint="lucene"
69 private RepositoryContentIndexFactory indexFactory;
74 private ArchivaConfiguration configuration;
76 private List<ManagedRepositoryConfiguration> localIndexedRepositories = new ArrayList<ManagedRepositoryConfiguration>();
78 public SearchResults searchForChecksum( String principal, List<String> selectedRepos, String checksum, SearchResultLimits limits )
80 List<RepositoryContentIndex> indexes = getHashcodeIndexes( principal, selectedRepos );
84 QueryParser parser = new MultiFieldQueryParser( new String[]{HashcodesKeys.MD5, HashcodesKeys.SHA1},
85 new HashcodesHandlers().getAnalyzer() );
86 LuceneQuery query = new LuceneQuery( parser.parse( checksum ) );
87 SearchResults results = searchAll( query, limits, indexes );
88 results.getRepositories().addAll( this.localIndexedRepositories );
92 catch ( ParseException e )
94 log.warn( "Unable to parse query [" + checksum + "]: " + e.getMessage(), e );
98 return new SearchResults();
101 public SearchResults searchForBytecode( String principal, List<String> selectedRepos, String term, SearchResultLimits limits )
103 List<RepositoryContentIndex> indexes = getBytecodeIndexes( principal, selectedRepos );
107 QueryParser parser = new BytecodeHandlers().getQueryParser();
108 LuceneQuery query = new LuceneQuery( parser.parse( term ) );
109 SearchResults results = searchAll( query, limits, indexes );
110 results.getRepositories().addAll( this.localIndexedRepositories );
114 catch ( ParseException e )
116 log.warn( "Unable to parse query [" + term + "]: " + e.getMessage(), e );
120 return new SearchResults();
123 public SearchResults searchForTerm( String principal, List<String> selectedRepos, String term, SearchResultLimits limits )
125 List<RepositoryContentIndex> indexes = getFileContentIndexes( principal, selectedRepos );
126 List<RepositoryContentIndex> bytecodeIndices = getBytecodeIndexes( principal, selectedRepos );
127 indexes.addAll( bytecodeIndices );
131 QueryParser parser = new FileContentHandlers().getQueryParser();
132 LuceneQuery query = new LuceneQuery( parser.parse( term ) );
133 SearchResults results = searchAll( query, limits, indexes );
134 results.getRepositories().addAll( this.localIndexedRepositories );
138 catch ( ParseException e )
140 log.warn( "Unable to parse query [" + term + "]: " + e.getMessage(), e );
144 return new SearchResults();
147 private SearchResults searchAll( LuceneQuery luceneQuery, SearchResultLimits limits, List<RepositoryContentIndex> indexes )
149 org.apache.lucene.search.Query specificQuery = luceneQuery.getLuceneQuery();
151 SearchResults results = new SearchResults();
153 if ( indexes.isEmpty() )
155 // No point going any further.
159 // Setup the converter
160 LuceneEntryConverter converter = null;
161 RepositoryContentIndex index = indexes.get( 0 );
162 converter = index.getEntryConverter();
164 // Process indexes into an array of Searchables.
165 List<Searchable> searchableList = toSearchables( indexes );
167 Searchable searchables[] = new Searchable[searchableList.size()];
168 searchableList.toArray( searchables );
170 MultiSearcher searcher = null;
174 // Create a multi-searcher for looking up the information.
175 searcher = new MultiSearcher( searchables );
177 // Perform the search.
178 Hits hits = searcher.search( specificQuery );
180 int hitCount = hits.length();
182 // Now process the limits.
183 results.setLimits( limits );
184 results.setTotalHits( hitCount );
186 int fetchCount = limits.getPageSize();
187 int offset = ( limits.getSelectedPage() * limits.getPageSize() );
189 if ( limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
191 fetchCount = hitCount;
196 if ( offset < hitCount )
198 // only process if the offset is within the hit count.
199 for ( int i = 0; i < fetchCount; i++ )
201 // Stop fetching if we are past the total # of available hits.
202 if ( offset + i >= hitCount )
209 Document doc = hits.doc( offset + i );
210 LuceneRepositoryContentRecord record = converter.convert( doc );
211 results.addHit( record );
213 catch ( java.text.ParseException e )
215 log.warn( "Unable to parse document into record: " + e.getMessage(), e );
221 catch ( IOException e )
223 log.error( "Unable to setup multi-search: " + e.getMessage(), e );
229 if ( searcher != null )
234 catch ( IOException ie )
236 log.error( "Unable to close index searcher: " + ie.getMessage(), ie );
243 private List<Searchable> toSearchables( List<RepositoryContentIndex> indexes )
245 List<Searchable> searchableList = new ArrayList<Searchable>();
246 for ( RepositoryContentIndex contentIndex : indexes )
250 searchableList.add( contentIndex.getSearchable() );
252 catch ( RepositoryIndexSearchException e )
254 log.warn( "Unable to get searchable for index [" + contentIndex.getId() + "] :"
255 + e.getMessage(), e );
258 return searchableList;
261 public List<RepositoryContentIndex> getBytecodeIndexes( String principal, List<String> selectedRepos )
263 List<RepositoryContentIndex> ret = new ArrayList<RepositoryContentIndex>();
265 for ( ManagedRepositoryConfiguration repoConfig : localIndexedRepositories )
267 // Only used selected repo
268 if ( selectedRepos.contains( repoConfig.getId() ) )
270 RepositoryContentIndex index = indexFactory.createBytecodeIndex( repoConfig );
272 if ( indexExists( index ) )
282 public List<RepositoryContentIndex> getFileContentIndexes( String principal, List<String> selectedRepos )
284 List<RepositoryContentIndex> ret = new ArrayList<RepositoryContentIndex>();
286 for ( ManagedRepositoryConfiguration repoConfig : localIndexedRepositories )
288 // Only used selected repo
289 if ( selectedRepos.contains( repoConfig.getId() ) )
291 RepositoryContentIndex index = indexFactory.createFileContentIndex( repoConfig );
293 if ( indexExists( index ) )
303 public List<RepositoryContentIndex> getHashcodeIndexes( String principal, List<String> selectedRepos )
305 List<RepositoryContentIndex> ret = new ArrayList<RepositoryContentIndex>();
307 for ( ManagedRepositoryConfiguration repoConfig : localIndexedRepositories )
309 // Only used selected repo
310 if ( selectedRepos.contains( repoConfig.getId() ) )
312 RepositoryContentIndex index = indexFactory.createHashcodeIndex( repoConfig );
314 if ( indexExists( index ) )
324 private boolean indexExists( RepositoryContentIndex index )
328 return index.exists();
330 catch ( RepositoryIndexException e )
333 "Repository Content Index [" + index.getId() + "] for repository ["
334 + index.getRepository().getId() + "] does not exist yet in ["
335 + index.getIndexDirectory().getAbsolutePath() + "]." );
340 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
342 if ( ConfigurationNames.isManagedRepositories( propertyName ) )
348 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
350 /* Nothing to do here */
353 private void initRepositories()
355 synchronized ( this.localIndexedRepositories )
357 this.localIndexedRepositories.clear();
359 List<ManagedRepositoryConfiguration> repos = configuration.getConfiguration().getManagedRepositories();
360 for ( ManagedRepositoryConfiguration repo : repos )
362 if ( repo.isScanned() )
364 localIndexedRepositories.add( repo );
370 public void initialize()
371 throws InitializationException
374 configuration.addChangeListener( this );