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 );
195 // Create a multi-searcher for looking up the information.
196 MultiSearcher searcher = new MultiSearcher( searchables );
198 // Perform the search.
199 Hits hits = searcher.search( specificQuery );
201 int hitCount = hits.length();
203 // Now process the limits.
204 results.setLimits( limits );
205 results.setTotalHits( hitCount );
207 int fetchCount = limits.getPageSize();
208 int offset = ( limits.getSelectedPage() * limits.getPageSize() );
210 if ( limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
212 fetchCount = hitCount;
217 if ( offset < hitCount )
219 // only process if the offset is within the hit count.
220 for ( int i = 0; i <= fetchCount; i++ )
222 // Stop fetching if we are past the total # of available hits.
223 if ( offset + i >= hitCount )
230 Document doc = hits.doc( offset + i );
231 LuceneRepositoryContentRecord record = converter.convert( doc );
232 results.addHit( record );
234 catch ( java.text.ParseException e )
236 getLogger().warn( "Unable to parse document into record: " + e.getMessage(), e );
241 catch ( IOException e )
243 getLogger().error( "Unable to setup multi-search: " + e.getMessage(), e );
249 private Predicate getAllowedToSearchReposPredicate()
251 return new UserAllowedToSearchRepositoryPredicate();
254 public List getBytecodeIndexes()
256 List ret = new ArrayList();
258 synchronized ( this.localIndexedRepositories )
260 ret.addAll( CollectionUtils.select( this.localIndexedRepositories, getAllowedToSearchReposPredicate() ) );
261 CollectionUtils.transform( ret, bytecodeIndexTransformer );
262 CollectionUtils.filter( ret, indexExistsPredicate );
268 public List getFileContentIndexes()
270 List ret = new ArrayList();
272 synchronized ( this.localIndexedRepositories )
274 ret.addAll( CollectionUtils.select( this.localIndexedRepositories, getAllowedToSearchReposPredicate() ) );
275 CollectionUtils.transform( ret, filecontentIndexTransformer );
276 CollectionUtils.filter( ret, indexExistsPredicate );
282 public List getHashcodeIndexes()
284 List ret = new ArrayList();
286 synchronized ( this.localIndexedRepositories )
288 ret.addAll( CollectionUtils.select( this.localIndexedRepositories, getAllowedToSearchReposPredicate() ) );
289 CollectionUtils.transform( ret, hashcodesIndexTransformer );
290 CollectionUtils.filter( ret, indexExistsPredicate );
296 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
298 if ( ConfigurationNames.isRepositories( propertyName ) )
304 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
306 /* Nothing to do here */
309 private void initRepositories()
311 synchronized ( this.localIndexedRepositories )
313 this.localIndexedRepositories.clear();
315 Predicate localIndexedRepos = AndPredicate.getInstance( LocalRepositoryPredicate.getInstance(),
316 IndexedRepositoryPredicate.getInstance() );
318 Collection repos = CollectionUtils.select( configuration.getConfiguration().getRepositories(),
321 Transformer toArchivaRepository = new Transformer()
324 public Object transform( Object input )
326 if ( input instanceof RepositoryConfiguration )
328 return ArchivaConfigurationAdaptor.toArchivaRepository( (RepositoryConfiguration) input );
334 CollectionUtils.transform( repos, toArchivaRepository );
336 this.localIndexedRepositories.addAll( repos );
340 public void initialize()
341 throws InitializationException
344 configuration.addChangeListener( this );