KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > search > indexing > IndexAllProject


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.search.indexing;
12
13 import java.io.IOException JavaDoc;
14 import java.net.URI JavaDoc;
15 import java.util.HashSet JavaDoc;
16
17 import org.eclipse.core.filesystem.EFS;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.resources.IResourceProxy;
22 import org.eclipse.core.resources.IResourceProxyVisitor;
23 import org.eclipse.core.resources.IWorkspaceRoot;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.jdt.core.IClasspathEntry;
28 import org.eclipse.jdt.core.JavaCore;
29 import org.eclipse.jdt.internal.compiler.SourceElementParser;
30 import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
31 import org.eclipse.jdt.internal.core.ClasspathEntry;
32 import org.eclipse.jdt.internal.core.JavaProject;
33 import org.eclipse.jdt.internal.core.index.Index;
34 import org.eclipse.jdt.internal.core.search.processing.JobManager;
35 import org.eclipse.jdt.internal.core.util.Util;
36
37 public class IndexAllProject extends IndexRequest {
38     IProject project;
39
40     public IndexAllProject(IProject project, IndexManager manager) {
41         super(project.getFullPath(), manager);
42         this.project = project;
43     }
44     public boolean equals(Object JavaDoc o) {
45         if (o instanceof IndexAllProject)
46             return this.project.equals(((IndexAllProject) o).project);
47         return false;
48     }
49     /**
50      * Ensure consistency of a project index. Need to walk all nested resources,
51      * and discover resources which have either been changed, added or deleted
52      * since the index was produced.
53      */

54     public boolean execute(IProgressMonitor progressMonitor) {
55
56         if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled()) return true;
57         if (!project.isAccessible()) return true; // nothing to do
58

59         ReadWriteMonitor monitor = null;
60         try {
61             // Get source folder entries. Libraries are done as a separate job
62
JavaProject javaProject = (JavaProject)JavaCore.create(this.project);
63             // Do not create marker while getting raw classpath (see bug 41859)
64
IClasspathEntry[] entries = javaProject.getRawClasspath();
65             int length = entries.length;
66             IClasspathEntry[] sourceEntries = new IClasspathEntry[length];
67             int sourceEntriesNumber = 0;
68             for (int i = 0; i < length; i++) {
69                 IClasspathEntry entry = entries[i];
70                 if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE)
71                     sourceEntries[sourceEntriesNumber++] = entry;
72             }
73             if (sourceEntriesNumber == 0) {
74                 IPath projectPath = javaProject.getPath();
75                 for (int i = 0; i < length; i++) {
76                     IClasspathEntry entry = entries[i];
77                     if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY && entry.getPath().equals(projectPath)) {
78                         // the project is also a library folder (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=89815)
79
// ensure a job exists to index it as a binary folder
80
this.manager.indexLibrary(projectPath, this.project);
81                         return true;
82                     }
83                 }
84
85                 // nothing to index but want to save an empty index file so its not 'rebuilt' when part of a search request
86
Index index = this.manager.getIndexForUpdate(this.containerPath, true, /*reuse index file*/ true /*create if none*/);
87                 if (index != null)
88                     this.manager.saveIndex(index);
89                 return true;
90             }
91             if (sourceEntriesNumber != length)
92                 System.arraycopy(sourceEntries, 0, sourceEntries = new IClasspathEntry[sourceEntriesNumber], 0, sourceEntriesNumber);
93     
94             Index index = this.manager.getIndexForUpdate(this.containerPath, true, /*reuse index file*/ true /*create if none*/);
95             if (index == null) return true;
96             monitor = index.monitor;
97             if (monitor == null) return true; // index got deleted since acquired
98

99             monitor.enterRead(); // ask permission to read
100

101             String JavaDoc[] paths = index.queryDocumentNames(""); // all file names //$NON-NLS-1$
102
int max = paths == null ? 0 : paths.length;
103             final SimpleLookupTable indexedFileNames = new SimpleLookupTable(max == 0 ? 33 : max + 11);
104             final String JavaDoc OK = "OK"; //$NON-NLS-1$
105
final String JavaDoc DELETED = "DELETED"; //$NON-NLS-1$
106
if (paths != null) {
107                 for (int i = 0; i < max; i++)
108                     indexedFileNames.put(paths[i], DELETED);
109             }
110             final long indexLastModified = max == 0 ? 0L : index.getIndexFile().lastModified();
111
112             IWorkspaceRoot root = this.project.getWorkspace().getRoot();
113             for (int i = 0; i < sourceEntriesNumber; i++) {
114                 if (this.isCancelled) return false;
115
116                 IClasspathEntry entry = sourceEntries[i];
117                 IResource sourceFolder = root.findMember(entry.getPath());
118                 if (sourceFolder != null) {
119                     
120                     // collect output locations if source is project (see http://bugs.eclipse.org/bugs/show_bug.cgi?id=32041)
121
final HashSet JavaDoc outputs = new HashSet JavaDoc();
122                     if (sourceFolder.getType() == IResource.PROJECT) {
123                         // Do not create marker while getting output location (see bug 41859)
124
outputs.add(javaProject.getOutputLocation());
125                         for (int j = 0; j < sourceEntriesNumber; j++) {
126                             IPath output = sourceEntries[j].getOutputLocation();
127                             if (output != null) {
128                                 outputs.add(output);
129                             }
130                         }
131                     }
132                     final boolean hasOutputs = !outputs.isEmpty();
133                     
134                     final char[][] inclusionPatterns = ((ClasspathEntry) entry).fullInclusionPatternChars();
135                     final char[][] exclusionPatterns = ((ClasspathEntry) entry).fullExclusionPatternChars();
136                     if (max == 0) {
137                         sourceFolder.accept(
138                             new IResourceProxyVisitor() {
139                                 public boolean visit(IResourceProxy proxy) {
140                                     if (isCancelled) return false;
141                                     switch(proxy.getType()) {
142                                         case IResource.FILE :
143                                             if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(proxy.getName())) {
144                                                 IFile file = (IFile) proxy.requestResource();
145                                                 if (exclusionPatterns != null || inclusionPatterns != null)
146                                                     if (Util.isExcluded(file, inclusionPatterns, exclusionPatterns))
147                                                         return false;
148                                                 indexedFileNames.put(Util.relativePath(file.getFullPath(), 1/*remove project segment*/), file);
149                                             }
150                                             return false;
151                                         case IResource.FOLDER :
152                                             if (exclusionPatterns != null && inclusionPatterns == null) {
153                                                 // if there are inclusion patterns then we must walk the children
154
if (Util.isExcluded(proxy.requestFullPath(), inclusionPatterns, exclusionPatterns, true))
155                                                     return false;
156                                             }
157                                             if (hasOutputs && outputs.contains(proxy.requestFullPath()))
158                                                 return false;
159                                     }
160                                     return true;
161                                 }
162                             },
163                             IResource.NONE
164                         );
165                     } else {
166                         sourceFolder.accept(
167                             new IResourceProxyVisitor() {
168                                 public boolean visit(IResourceProxy proxy) throws CoreException {
169                                     if (isCancelled) return false;
170                                     switch(proxy.getType()) {
171                                         case IResource.FILE :
172                                             if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(proxy.getName())) {
173                                                 IFile file = (IFile) proxy.requestResource();
174                                                 URI JavaDoc location = file.getLocationURI();
175                                                 if (location == null) return false;
176                                                 if (exclusionPatterns != null || inclusionPatterns != null)
177                                                     if (Util.isExcluded(file, inclusionPatterns, exclusionPatterns))
178                                                         return false;
179                                                 String JavaDoc relativePathString = Util.relativePath(file.getFullPath(), 1/*remove project segment*/);
180                                                 indexedFileNames.put(relativePathString,
181                                                     indexedFileNames.get(relativePathString) == null
182                                                             || indexLastModified < EFS.getStore(location).fetchInfo().getLastModified()
183                                                         ? (Object JavaDoc) file
184                                                         : (Object JavaDoc) OK);
185                                             }
186                                             return false;
187                                         case IResource.FOLDER :
188                                             if (exclusionPatterns != null || inclusionPatterns != null)
189                                                 if (Util.isExcluded(proxy.requestResource(), inclusionPatterns, exclusionPatterns))
190                                                     return false;
191                                             if (hasOutputs && outputs.contains(proxy.requestFullPath()))
192                                                 return false;
193                                     }
194                                     return true;
195                                 }
196                             },
197                             IResource.NONE
198                         );
199                     }
200                 }
201             }
202             
203             SourceElementParser parser = this.manager.getSourceElementParser(javaProject, null/*requestor will be set by indexer*/);
204             Object JavaDoc[] names = indexedFileNames.keyTable;
205             Object JavaDoc[] values = indexedFileNames.valueTable;
206             for (int i = 0, namesLength = names.length; i < namesLength; i++) {
207                 String JavaDoc name = (String JavaDoc) names[i];
208                 if (name != null) {
209                     if (this.isCancelled) return false;
210
211                     Object JavaDoc value = values[i];
212                     if (value != OK) {
213                         if (value == DELETED)
214                             this.manager.remove(name, this.containerPath);
215                         else
216                             this.manager.addSource((IFile) value, this.containerPath, parser);
217                     }
218                 }
219             }
220
221             // request to save index when all cus have been indexed... also sets state to SAVED_STATE
222
this.manager.request(new SaveIndex(this.containerPath, this.manager));
223         } catch (CoreException e) {
224             if (JobManager.VERBOSE) {
225                 Util.verbose("-> failed to index " + this.project + " because of the following exception:", System.err); //$NON-NLS-1$ //$NON-NLS-2$
226
e.printStackTrace();
227             }
228             this.manager.removeIndex(this.containerPath);
229             return false;
230         } catch (IOException JavaDoc e) {
231             if (JobManager.VERBOSE) {
232                 Util.verbose("-> failed to index " + this.project + " because of the following exception:", System.err); //$NON-NLS-1$ //$NON-NLS-2$
233
e.printStackTrace();
234             }
235             this.manager.removeIndex(this.containerPath);
236             return false;
237         } finally {
238             if (monitor != null)
239                 monitor.exitRead(); // free read lock
240
}
241         return true;
242     }
243     public int hashCode() {
244         return this.project.hashCode();
245     }
246     protected Integer JavaDoc updatedIndexState() {
247         return IndexManager.REBUILDING_STATE;
248     }
249     public String JavaDoc toString() {
250         return "indexing project " + this.project.getFullPath(); //$NON-NLS-1$
251
}
252 }
253
Popular Tags