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 org.apache.commons.collections.CollectionUtils;
23 import org.apache.commons.collections.Predicate;
24 import org.apache.commons.collections.Transformer;
25 import org.apache.commons.collections.functors.AndPredicate;
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.RepositoryConfiguration;
36 import org.apache.maven.archiva.configuration.functors.IndexedRepositoryPredicate;
37 import org.apache.maven.archiva.configuration.functors.LocalRepositoryPredicate;
38 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
39 import org.apache.maven.archiva.indexer.bytecode.BytecodeHandlers;
40 import org.apache.maven.archiva.indexer.filecontent.FileContentHandlers;
41 import org.apache.maven.archiva.indexer.functors.UserAllowedToSearchRepositoryPredicate;
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.apache.maven.archiva.repository.ArchivaConfigurationAdaptor;
48 import org.codehaus.plexus.logging.AbstractLogEnabled;
49 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
50 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
51 import org.codehaus.plexus.registry.Registry;
52 import org.codehaus.plexus.registry.RegistryListener;
54 import java.io.IOException;
55 import java.util.ArrayList;
56 import java.util.Collection;
57 import java.util.List;
60 * DefaultCrossRepositorySearch
62 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
64 * @plexus.component role="org.apache.maven.archiva.indexer.search.CrossRepositorySearch" role-hint="default"
66 public class DefaultCrossRepositorySearch
67 extends AbstractLogEnabled
68 implements CrossRepositorySearch, RegistryListener, Initializable
71 * @plexus.requirement role-hint="bytecode"
73 private Transformer bytecodeIndexTransformer;
76 * @plexus.requirement role-hint="filecontent"
78 private Transformer filecontentIndexTransformer;
81 * @plexus.requirement role-hint="hashcodes"
83 private Transformer hashcodesIndexTransformer;
86 * @plexus.requirement role-hint="searchable"
88 private Transformer searchableTransformer;
91 * @plexus.requirement role-hint="index-exists"
93 private Predicate indexExistsPredicate;
98 private ArchivaConfiguration configuration;
100 private List localIndexedRepositories = new ArrayList();
102 public SearchResults searchForChecksum( String checksum, SearchResultLimits limits )
104 List indexes = getHashcodeIndexes();
108 QueryParser parser = new MultiFieldQueryParser( new String[] { HashcodesKeys.MD5, HashcodesKeys.SHA1 },
109 new HashcodesHandlers().getAnalyzer() );
110 LuceneQuery query = new LuceneQuery( parser.parse( checksum ) );
111 SearchResults results = searchAll( query, limits, indexes );
112 results.getRepositories().addAll( this.localIndexedRepositories );
116 catch ( ParseException e )
118 getLogger().warn( "Unable to parse query [" + checksum + "]: " + e.getMessage(), e );
122 return new SearchResults();
125 public SearchResults searchForBytecode( String term, SearchResultLimits limits )
127 List indexes = getHashcodeIndexes();
131 QueryParser parser = new BytecodeHandlers().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 getLogger().warn( "Unable to parse query [" + term + "]: " + e.getMessage(), e );
144 return new SearchResults();
147 public SearchResults searchForTerm( String term, SearchResultLimits limits )
149 List indexes = getFileContentIndexes();
153 QueryParser parser = new FileContentHandlers().getQueryParser();
154 LuceneQuery query = new LuceneQuery( parser.parse( term ) );
155 SearchResults results = searchAll( query, limits, indexes );
156 results.getRepositories().addAll( this.localIndexedRepositories );
160 catch ( ParseException e )
162 getLogger().warn( "Unable to parse query [" + term + "]: " + e.getMessage(), e );
166 return new SearchResults();
169 private SearchResults searchAll( LuceneQuery luceneQuery, SearchResultLimits limits, List indexes )
171 org.apache.lucene.search.Query specificQuery = luceneQuery.getLuceneQuery();
173 SearchResults results = new SearchResults();
175 if ( indexes.isEmpty() )
177 // No point going any further.
181 // Setup the converter
182 LuceneEntryConverter converter = null;
183 RepositoryContentIndex index = (RepositoryContentIndex) indexes.get( 0 );
184 converter = index.getEntryConverter();
186 // Process indexes into an array of Searchables.
187 List searchableList = new ArrayList( indexes );
188 CollectionUtils.transform( searchableList, searchableTransformer );
190 Searchable searchables[] = new Searchable[searchableList.size()];
191 searchableList.toArray( searchables );
193 MultiSearcher searcher = null;
197 // Create a multi-searcher for looking up the information.
198 searcher = new MultiSearcher( searchables );
200 // Perform the search.
201 Hits hits = searcher.search( specificQuery );
203 int hitCount = hits.length();
205 // Now process the limits.
206 results.setLimits( limits );
207 results.setTotalHits( hitCount );
209 int fetchCount = limits.getPageSize();
210 int offset = ( limits.getSelectedPage() * limits.getPageSize() );
212 if ( limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
214 fetchCount = hitCount;
219 if ( offset < hitCount )
221 // only process if the offset is within the hit count.
222 for ( int i = 0; i <= fetchCount; i++ )
224 // Stop fetching if we are past the total # of available hits.
225 if ( offset + i >= hitCount )
232 Document doc = hits.doc( offset + i );
233 LuceneRepositoryContentRecord record = converter.convert( doc );
234 results.addHit( record );
236 catch ( java.text.ParseException e )
238 getLogger().warn( "Unable to parse document into record: " + e.getMessage(), e );
244 catch ( IOException e )
246 getLogger().error( "Unable to setup multi-search: " + e.getMessage(), e );
254 catch ( IOException ie )
256 getLogger().error( "Unable to close index searcher: " + ie.getMessage(), ie );
263 private Predicate getAllowedToSearchReposPredicate()
265 return new UserAllowedToSearchRepositoryPredicate();
268 public List getBytecodeIndexes()
270 List ret = new ArrayList();
272 synchronized ( this.localIndexedRepositories )
274 ret.addAll( CollectionUtils.select( this.localIndexedRepositories, getAllowedToSearchReposPredicate() ) );
275 CollectionUtils.transform( ret, bytecodeIndexTransformer );
276 CollectionUtils.filter( ret, indexExistsPredicate );
282 public List getFileContentIndexes()
284 List ret = new ArrayList();
286 synchronized ( this.localIndexedRepositories )
288 ret.addAll( CollectionUtils.select( this.localIndexedRepositories, getAllowedToSearchReposPredicate() ) );
289 CollectionUtils.transform( ret, filecontentIndexTransformer );
290 CollectionUtils.filter( ret, indexExistsPredicate );
296 public List getHashcodeIndexes()
298 List ret = new ArrayList();
300 synchronized ( this.localIndexedRepositories )
302 ret.addAll( CollectionUtils.select( this.localIndexedRepositories, getAllowedToSearchReposPredicate() ) );
303 CollectionUtils.transform( ret, hashcodesIndexTransformer );
304 CollectionUtils.filter( ret, indexExistsPredicate );
310 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
312 if ( ConfigurationNames.isRepositories( propertyName ) )
318 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
320 /* Nothing to do here */
323 private void initRepositories()
325 synchronized ( this.localIndexedRepositories )
327 this.localIndexedRepositories.clear();
329 Predicate localIndexedRepos = AndPredicate.getInstance( LocalRepositoryPredicate.getInstance(),
330 IndexedRepositoryPredicate.getInstance() );
332 Collection repos = CollectionUtils.select( configuration.getConfiguration().getRepositories(),
335 Transformer toArchivaRepository = new Transformer()
338 public Object transform( Object input )
340 if ( input instanceof RepositoryConfiguration )
342 return ArchivaConfigurationAdaptor.toArchivaRepository( (RepositoryConfiguration) input );
348 CollectionUtils.transform( repos, toArchivaRepository );
350 this.localIndexedRepositories.addAll( repos );
354 public void initialize()
355 throws InitializationException
358 configuration.addChangeListener( this );