View Javadoc

1   /***
2    * Copyright (C) 2008 Jeremy Thomerson (jthomerson@users.sourceforge.net)
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.vcaperture.tool.listeners;
18  
19  import java.util.Collection;
20  
21  import net.sf.vcaperture.model.AbstractRepository;
22  import net.sf.vcaperture.model.Revision;
23  import net.sf.vcaperture.services.IRepositoryService;
24  import net.sf.vcaperture.services.ISearchService;
25  import net.sf.vcaperture.util.spring.ApplicationContextFactory;
26  
27  import org.apache.log4j.Logger;
28  
29  /***
30   * Tool that indexes code so that it can be searched later by the web application.
31   * 
32   * @author Jeremy Thomerson (jthomerson@users.sourceforge.net)
33   */
34  public class CodeSearchTool extends AbstractToolListener {
35  
36  	private static final Logger LOGGER = Logger.getLogger(CodeSearchTool.class);
37  	public static final int DEFAULT_MAX_REVISION_PER_BATCH = 50;
38  
39  	private int mMaxRevisionsPerBatch = DEFAULT_MAX_REVISION_PER_BATCH;
40  
41  	public boolean processRepository(IRepositoryService repositoryService, AbstractRepository repo) {
42  		super.processRepository(repositoryService, repo);
43  
44  		ISearchService search = ApplicationContextFactory.getFactory().getContext().getBean(ISearchService.class);
45  		String startingRevision = search.getLastProcessedRevision(repo);
46  		getLogger().debug("Latest already-indexed revision: " + startingRevision);
47  		Collection<Revision> revisions = repo.getRevisions(startingRevision, mMaxRevisionsPerBatch);
48  		for (Revision rev : revisions) {
49  			getLogger().info("Indexing revision: " + rev.getName());
50  			search.indexRevision(repo, rev);
51  		}
52  		// TODO: this code is  shared with LocalStorageTool - both should subclass a MaxRevisionsTool
53  		// not complete if we had exactly mMaxRevisionsPerBatch (there are probably more)
54  		// TRUE, you could have EXACTLY mMaxRevisionsPerBatch, but if so, it doesn't hurt to run again
55  		return (revisions.size() >= mMaxRevisionsPerBatch);
56  	}
57  
58  	protected Logger getLogger() {
59  		return LOGGER;
60  	}
61  
62  	public int getMaxRevisionsPerBatch() {
63  		return mMaxRevisionsPerBatch;
64  	}
65  
66  	public void setMaxRevisionsPerBatch(int maxRevisionsPerBatch) {
67  		mMaxRevisionsPerBatch = maxRevisionsPerBatch;
68  	}
69  
70  }