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.index.Term;
28 import org.apache.lucene.queryParser.MultiFieldQueryParser;
29 import org.apache.lucene.queryParser.ParseException;
30 import org.apache.lucene.queryParser.QueryParser;
31 import org.apache.lucene.search.BooleanClause;
32 import org.apache.lucene.search.BooleanQuery;
33 import org.apache.lucene.search.Filter;
34 import org.apache.lucene.search.Hits;
35 import org.apache.lucene.search.MultiSearcher;
36 import org.apache.lucene.search.Query;
37 import org.apache.lucene.search.QueryWrapperFilter;
38 import org.apache.lucene.search.Searchable;
39 import org.apache.lucene.search.TermQuery;
40 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
41 import org.apache.maven.archiva.configuration.ConfigurationNames;
42 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
43 import org.apache.maven.archiva.indexer.ArtifactKeys;
44 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
45 import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory;
46 import org.apache.maven.archiva.indexer.RepositoryIndexException;
47 import org.apache.maven.archiva.indexer.RepositoryIndexSearchException;
48 import org.apache.maven.archiva.indexer.bytecode.BytecodeHandlers;
49 import org.apache.maven.archiva.indexer.bytecode.BytecodeKeys;
50 import org.apache.maven.archiva.indexer.filecontent.FileContentHandlers;
51 import org.apache.maven.archiva.indexer.hashcodes.HashcodesHandlers;
52 import org.apache.maven.archiva.indexer.hashcodes.HashcodesKeys;
53 import org.apache.maven.archiva.indexer.lucene.LuceneEntryConverter;
54 import org.apache.maven.archiva.indexer.lucene.LuceneQuery;
55 import org.apache.maven.archiva.indexer.lucene.LuceneRepositoryContentRecord;
56 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
57 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
58 import org.codehaus.plexus.registry.Registry;
59 import org.codehaus.plexus.registry.RegistryListener;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
64 * DefaultCrossRepositorySearch
66 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
68 * @plexus.component role="org.apache.maven.archiva.indexer.search.CrossRepositorySearch" role-hint="default"
70 public class DefaultCrossRepositorySearch
71 implements CrossRepositorySearch, RegistryListener, Initializable
73 private Logger log = LoggerFactory.getLogger( DefaultCrossRepositorySearch.class );
76 * @plexus.requirement role-hint="lucene"
78 private RepositoryContentIndexFactory indexFactory;
83 private ArchivaConfiguration configuration;
85 private List<ManagedRepositoryConfiguration> localIndexedRepositories = new ArrayList<ManagedRepositoryConfiguration>();
87 public SearchResults executeFilteredSearch( String principal, List<String> selectedRepos, String groupId,
88 String artifactId, String version, String className,
89 SearchResultLimits limits )
91 List<RepositoryContentIndex> indexes = getBytecodeIndexes( principal, selectedRepos );
92 SearchResults results = new SearchResults();
93 BooleanQuery booleanQuery = new BooleanQuery();
95 if ( groupId != null && groupId.length() > 0 )
97 parseAndAdd( booleanQuery, ArtifactKeys.GROUPID, groupId, "\\.|-" );
100 if ( artifactId != null && artifactId.length() > 0 )
102 parseAndAdd( booleanQuery, ArtifactKeys.ARTIFACTID, artifactId, "\\.|-" );
105 if ( version != null && version.length() > 0 )
107 parseAndAdd( booleanQuery, ArtifactKeys.VERSION, version, "\\.|-" );
110 if ( className != null && className.length() > 0 )
116 new MultiFieldQueryParser( new String[] { BytecodeKeys.CLASSES, BytecodeKeys.FILES,
117 BytecodeKeys.METHODS }, new BytecodeHandlers().getAnalyzer() );
118 booleanQuery.add( parser.parse( className ), BooleanClause.Occur.MUST );
120 catch ( ParseException e )
126 LuceneQuery query = new LuceneQuery( booleanQuery );
127 results = searchAll( query, limits, indexes, null );
128 results.getRepositories().add( this.localIndexedRepositories );
133 public SearchResults searchForChecksum( String principal, List<String> selectedRepos, String checksum,
134 SearchResultLimits limits )
136 List<RepositoryContentIndex> indexes = getHashcodeIndexes( principal, selectedRepos );
140 QueryParser parser = new MultiFieldQueryParser( new String[]{HashcodesKeys.MD5, HashcodesKeys.SHA1},
141 new HashcodesHandlers().getAnalyzer() );
142 LuceneQuery query = new LuceneQuery( parser.parse( checksum ) );
143 SearchResults results = searchAll( query, limits, indexes, null );
144 results.getRepositories().addAll( this.localIndexedRepositories );
148 catch ( ParseException e )
150 log.warn( "Unable to parse query [" + checksum + "]: " + e.getMessage(), e );
154 return new SearchResults();
157 public SearchResults searchForBytecode( String principal, List<String> selectedRepos, String term, SearchResultLimits limits )
159 List<RepositoryContentIndex> indexes = getBytecodeIndexes( principal, selectedRepos );
163 QueryParser parser = new BytecodeHandlers().getQueryParser();
164 LuceneQuery query = new LuceneQuery( parser.parse( term ) );
165 SearchResults results = searchAll( query, limits, indexes, null );
166 results.getRepositories().addAll( this.localIndexedRepositories );
170 catch ( ParseException e )
172 log.warn( "Unable to parse query [" + term + "]: " + e.getMessage(), e );
176 return new SearchResults();
179 public SearchResults searchForTerm( String principal, List<String> selectedRepos, String term, SearchResultLimits limits )
181 return searchForTerm( principal, selectedRepos, term, limits, null );
184 public SearchResults searchForTerm( String principal, List<String> selectedRepos, String term,
185 SearchResultLimits limits, List<String> previousSearchTerms )
187 List<RepositoryContentIndex> indexes = getFileContentIndexes( principal, selectedRepos );
191 QueryParser parser = new FileContentHandlers().getQueryParser();
192 LuceneQuery query = null;
193 SearchResults results = null;
194 if ( previousSearchTerms == null || previousSearchTerms.isEmpty() )
196 query = new LuceneQuery( parser.parse( term ) );
197 results = searchAll( query, limits, indexes, null );
201 // AND the previous search terms
202 BooleanQuery booleanQuery = new BooleanQuery();
203 for ( String previousSearchTerm : previousSearchTerms )
205 booleanQuery.add( parser.parse( previousSearchTerm ), BooleanClause.Occur.MUST );
208 query = new LuceneQuery( booleanQuery );
209 Filter filter = new QueryWrapperFilter( parser.parse( term ) );
210 results = searchAll( query, limits, indexes, filter );
212 results.getRepositories().addAll( this.localIndexedRepositories );
216 catch ( ParseException e )
218 log.warn( "Unable to parse query [" + term + "]: " + e.getMessage(), e );
222 return new SearchResults();
225 private SearchResults searchAll( LuceneQuery luceneQuery, SearchResultLimits limits, List<RepositoryContentIndex> indexes, Filter filter )
227 org.apache.lucene.search.Query specificQuery = luceneQuery.getLuceneQuery();
229 SearchResults results = new SearchResults();
231 if ( indexes.isEmpty() )
233 // No point going any further.
237 // Setup the converter
238 LuceneEntryConverter converter = null;
239 RepositoryContentIndex index = indexes.get( 0 );
240 converter = index.getEntryConverter();
242 // Process indexes into an array of Searchables.
243 List<Searchable> searchableList = toSearchables( indexes );
245 Searchable searchables[] = new Searchable[searchableList.size()];
246 searchableList.toArray( searchables );
248 MultiSearcher searcher = null;
252 // Create a multi-searcher for looking up the information.
253 searcher = new MultiSearcher( searchables );
255 // Perform the search.
257 if ( filter != null )
259 hits = searcher.search( specificQuery, filter );
263 hits = searcher.search( specificQuery );
266 int hitCount = hits.length();
268 // Now process the limits.
269 results.setLimits( limits );
270 results.setTotalHits( hitCount );
272 int fetchCount = limits.getPageSize();
273 int offset = ( limits.getSelectedPage() * limits.getPageSize() );
275 if ( limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
277 fetchCount = hitCount;
282 if ( offset < hitCount )
284 // only process if the offset is within the hit count.
285 for ( int i = 0; i < fetchCount; i++ )
287 // Stop fetching if we are past the total # of available hits.
288 if ( offset + i >= hitCount )
295 Document doc = hits.doc( offset + i );
296 LuceneRepositoryContentRecord record = converter.convert( doc );
297 results.addHit( record );
299 catch ( java.text.ParseException e )
301 log.warn( "Unable to parse document into record: " + e.getMessage(), e );
307 catch ( IOException e )
309 log.error( "Unable to setup multi-search: " + e.getMessage(), e );
315 if ( searcher != null )
320 catch ( IOException ie )
322 log.error( "Unable to close index searcher: " + ie.getMessage(), ie );
329 private List<Searchable> toSearchables( List<RepositoryContentIndex> indexes )
331 List<Searchable> searchableList = new ArrayList<Searchable>();
332 for ( RepositoryContentIndex contentIndex : indexes )
336 searchableList.add( contentIndex.getSearchable() );
338 catch ( RepositoryIndexSearchException e )
340 log.warn( "Unable to get searchable for index [" + contentIndex.getId() + "] :"
341 + e.getMessage(), e );
344 return searchableList;
347 public List<RepositoryContentIndex> getBytecodeIndexes( String principal, List<String> selectedRepos )
349 List<RepositoryContentIndex> ret = new ArrayList<RepositoryContentIndex>();
351 for ( ManagedRepositoryConfiguration repoConfig : localIndexedRepositories )
353 // Only used selected repo
354 if ( selectedRepos.contains( repoConfig.getId() ) )
356 RepositoryContentIndex index = indexFactory.createBytecodeIndex( repoConfig );
358 if ( indexExists( index ) )
368 public List<RepositoryContentIndex> getFileContentIndexes( String principal, List<String> selectedRepos )
370 List<RepositoryContentIndex> ret = new ArrayList<RepositoryContentIndex>();
372 for ( ManagedRepositoryConfiguration repoConfig : localIndexedRepositories )
374 // Only used selected repo
375 if ( selectedRepos.contains( repoConfig.getId() ) )
377 RepositoryContentIndex index = indexFactory.createFileContentIndex( repoConfig );
379 if ( indexExists( index ) )
389 public List<RepositoryContentIndex> getHashcodeIndexes( String principal, List<String> selectedRepos )
391 List<RepositoryContentIndex> ret = new ArrayList<RepositoryContentIndex>();
393 for ( ManagedRepositoryConfiguration repoConfig : localIndexedRepositories )
395 // Only used selected repo
396 if ( selectedRepos.contains( repoConfig.getId() ) )
398 RepositoryContentIndex index = indexFactory.createHashcodeIndex( repoConfig );
400 if ( indexExists( index ) )
410 private boolean indexExists( RepositoryContentIndex index )
414 return index.exists();
416 catch ( RepositoryIndexException e )
419 "Repository Content Index [" + index.getId() + "] for repository ["
420 + index.getRepository().getId() + "] does not exist yet in ["
421 + index.getIndexDirectory().getAbsolutePath() + "]." );
426 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
428 if ( ConfigurationNames.isManagedRepositories( propertyName ) )
434 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
436 /* Nothing to do here */
439 private void initRepositories()
441 synchronized ( this.localIndexedRepositories )
443 this.localIndexedRepositories.clear();
445 List<ManagedRepositoryConfiguration> repos = configuration.getConfiguration().getManagedRepositories();
446 for ( ManagedRepositoryConfiguration repo : repos )
448 if ( repo.isScanned() )
450 localIndexedRepositories.add( repo );
456 private void parseAndAdd( BooleanQuery query, String key, String value, String delimiter )
458 if ( value != null && value.length() > 0 )
460 String[] terms = value.split( delimiter );
461 for ( int i = 0; i < terms.length; i++ )
463 Term valueTerm = new Term( key, terms[i] );
464 Query valueQuery = new TermQuery( valueTerm );
465 query.add( valueQuery, BooleanClause.Occur.MUST );
470 Term valueTerm = new Term( key, value );
471 Query valueQuery = new TermQuery( valueTerm );
472 query.add( valueQuery, BooleanClause.Occur.MUST );
476 public void initialize()
477 throws InitializationException
480 configuration.addChangeListener( this );