1 package org.apache.maven.archiva.consumers.lucene;
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.ParseException;
28 import org.apache.lucene.queryParser.QueryParser;
29 import org.apache.lucene.search.Hits;
30 import org.apache.lucene.search.MultiSearcher;
31 import org.apache.lucene.search.Searchable;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
34 import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory;
35 import org.apache.maven.archiva.indexer.RepositoryIndexSearchException;
36 import org.apache.maven.archiva.indexer.bytecode.BytecodeHandlers;
37 import org.apache.maven.archiva.indexer.lucene.LuceneEntryConverter;
38 import org.apache.maven.archiva.indexer.lucene.LuceneQuery;
39 import org.apache.maven.archiva.indexer.lucene.LuceneRepositoryContentRecord;
40 import org.apache.maven.archiva.indexer.search.CrossRepositorySearch;
41 import org.apache.maven.archiva.indexer.search.SearchResultLimits;
42 import org.apache.maven.archiva.indexer.search.SearchResults;
45 * Searcher used for testing purposes only.
47 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
50 public class IndexJavaPublicMethodsCrossRepositorySearch
51 implements CrossRepositorySearch
53 private ManagedRepositoryConfiguration localIndexedRepo;
55 private RepositoryContentIndexFactory indexFactory;
57 public IndexJavaPublicMethodsCrossRepositorySearch()
62 public IndexJavaPublicMethodsCrossRepositorySearch( ManagedRepositoryConfiguration localIndexedRepo, RepositoryContentIndexFactory indexFactory )
64 this.localIndexedRepo = localIndexedRepo;
65 this.indexFactory = indexFactory;
68 public SearchResults searchForBytecode( String principal, List<String> selectedRepos, String term,
69 SearchResultLimits limits )
71 List<RepositoryContentIndex> indexes = new ArrayList<RepositoryContentIndex>();
72 indexes.add( indexFactory.createBytecodeIndex( localIndexedRepo ) );
76 QueryParser parser = new BytecodeHandlers().getQueryParser();
77 LuceneQuery query = new LuceneQuery( parser.parse( term ) );
78 SearchResults results = searchAll( query, limits, indexes );
79 results.getRepositories().add( localIndexedRepo );
83 catch ( ParseException e )
87 return new SearchResults();
90 public SearchResults searchForChecksum( String principal, List<String> selectedRepos, String checksum,
91 SearchResultLimits limits )
93 // TODO Auto-generated method stub
97 public SearchResults searchForTerm( String principal, List<String> selectedRepos, String term,
98 SearchResultLimits limits )
100 // TODO Auto-generated method stub
104 private SearchResults searchAll( LuceneQuery luceneQuery, SearchResultLimits limits, List<RepositoryContentIndex> indexes )
106 org.apache.lucene.search.Query specificQuery = luceneQuery.getLuceneQuery();
108 SearchResults results = new SearchResults();
110 if ( indexes.isEmpty() )
112 // No point going any further.
116 // Setup the converter
117 LuceneEntryConverter converter = null;
118 RepositoryContentIndex index = indexes.get( 0 );
119 converter = index.getEntryConverter();
121 // Process indexes into an array of Searchables.
122 List<Searchable> searchableList = toSearchables( indexes );
124 Searchable searchables[] = new Searchable[searchableList.size()];
125 searchableList.toArray( searchables );
127 MultiSearcher searcher = null;
131 // Create a multi-searcher for looking up the information.
132 searcher = new MultiSearcher( searchables );
134 // Perform the search.
135 Hits hits = searcher.search( specificQuery );
137 int hitCount = hits.length();
139 // Now process the limits.
140 results.setLimits( limits );
141 results.setTotalHits( hitCount );
143 int fetchCount = limits.getPageSize();
144 int offset = ( limits.getSelectedPage() * limits.getPageSize() );
146 if ( limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
148 fetchCount = hitCount;
153 if ( offset < hitCount )
155 // only process if the offset is within the hit count.
156 for ( int i = 0; i <= fetchCount; i++ )
158 // Stop fetching if we are past the total # of available hits.
159 if ( offset + i >= hitCount )
166 Document doc = hits.doc( offset + i );
167 LuceneRepositoryContentRecord record = converter.convert( doc );
168 results.addHit( record );
170 catch ( java.text.ParseException e )
178 catch ( IOException e )
186 if ( searcher != null )
191 catch ( IOException ie )
200 private List<Searchable> toSearchables( List<RepositoryContentIndex> indexes )
202 List<Searchable> searchableList = new ArrayList<Searchable>();
203 for ( RepositoryContentIndex contentIndex : indexes )
207 searchableList.add( contentIndex.getSearchable() );
209 catch ( RepositoryIndexSearchException e )
214 return searchableList;