Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7b9ac0dc44e52fadecdab73ded7337f646863d19 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsp.core.internal.java.search;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IResourceProxyVisitor;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jst.jsp.core.internal.JSPCoreMessages;
import org.eclipse.jst.jsp.core.internal.provisional.contenttype.ContentTypeIdForJSP;

/**
 * Re-indexes the entire workspace.
 * Ensures the JSP Index is in a stable state before performing a search.
 * (like after a crash or if previous indexing was canceled)
 * 
 * @author pavery
 */
public class IndexWorkspaceJob extends Job {

	// for debugging
	static final boolean DEBUG;
	static {
		String value= Platform.getDebugOption("org.eclipse.jst.jsp.core/debug/jspindexmanager"); //$NON-NLS-1$
		DEBUG= value != null && value.equalsIgnoreCase("true"); //$NON-NLS-1$
	}
	
	/**
	 * Visitor that retrieves jsp project paths for all jsp files in the workspace,
	 * and adds the files to be indexed as they are encountered
	 */
	private class JSPFileVisitor implements IResourceProxyVisitor {
	    private List files = new ArrayList(); 
		
		// monitor from the Job
		IProgressMonitor fInnerMonitor = null;
		public JSPFileVisitor(IProgressMonitor monitor) {
			this.fInnerMonitor = monitor;
		}
		
		public boolean visit(IResourceProxy proxy) throws CoreException {
			
			// check job canceled
			if (this.fInnerMonitor != null && this.fInnerMonitor.isCanceled()) {
				setCanceledState();
				return false;
			}
			
			// check search support canceled
			if(JSPSearchSupport.getInstance().isCanceled()) {
				setCanceledState();
				return false;
			}
			
			if (proxy.getType() == IResource.FILE) {
				
				// https://w3.opensource.ibm.com/bugzilla/show_bug.cgi?id=3553
				// check this before description
				// check name before actually getting the file (less work)
				if(getJspContentType().isAssociatedWith(proxy.getName())) {
					IFile file = (IFile) proxy.requestResource();
					if(file.exists()) {
						
						if(DEBUG)
							System.out.println("(+) IndexWorkspaceJob adding file: " + file.getName()); //$NON-NLS-1$
						// this call will check the ContentTypeDescription, so don't need to do it here.
						//JSPSearchSupport.getInstance().addJspFile(file);
						this.files.add(file);
						this.fInnerMonitor.subTask(proxy.getName());
						
						// don't search deeper for files
						return false;
					}
				}
			}
			return true;
		}
		
		public final IFile[] getFiles() {
		    return (IFile[])this.files.toArray(new IFile[this.files.size()]);
		}
	}
	
	private IContentType fContentTypeJSP = null;
	
	public IndexWorkspaceJob() {
		// pa_TODO may want to say something like "Rebuilding JSP Index" to be more
		// descriptive instead of "Updating JSP Index" since they are 2 different things
		super(JSPCoreMessages.JSPIndexManager_0);
		setPriority(Job.LONG);
		setSystem(true);
	}

	IContentType getJspContentType() {
		if(this.fContentTypeJSP == null)
			this.fContentTypeJSP = Platform.getContentTypeManager().getContentType(ContentTypeIdForJSP.ContentTypeID_JSP);
		return this.fContentTypeJSP;
	}
	
	/**
	 * @see org eclipse.core.internal.jobs.InternalJob#run(org.eclipse.core.runtime.IProgressMonitor) 
	 * for similar method
	 */
	protected IStatus run(IProgressMonitor monitor) {
		
		IStatus status = Status.OK_STATUS;
		
		if(monitor.isCanceled()) {
			setCanceledState();
			return Status.CANCEL_STATUS;
		}
		
		if(DEBUG)
			System.out.println(" ^ IndexWorkspaceJob started: "); //$NON-NLS-1$
		
		long start = System.currentTimeMillis();
		
		try {
		    JSPFileVisitor visitor = new JSPFileVisitor(monitor);
		    // collect all jsp files
			ResourcesPlugin.getWorkspace().getRoot().accept(visitor, IResource.DEPTH_INFINITE);
			// request indexing
			// this is pretty much like faking an entire workspace resource delta
			JSPIndexManager.getInstance().indexFiles(visitor.getFiles());
		}
		catch (CoreException e) {
			if(DEBUG)
				e.printStackTrace();
		}
		finally {
			if(monitor != null)
				monitor.done();
		}
		long finish = System.currentTimeMillis();
		if(DEBUG)
			System.out.println(" ^ IndexWorkspaceJob finished\n   total time running: " + (finish - start)); //$NON-NLS-1$
		
		return status;
	}
	
	void setCanceledState() {
		JSPIndexManager.getInstance().setIndexState(JSPIndexManager.S_CANCELED);
	}
}

Back to the top