KickJava   Java API By Example, From Geeks To Geeks.

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


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
16 import org.eclipse.core.filesystem.EFS;
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.IResourceProxy;
21 import org.eclipse.core.resources.IResourceProxyVisitor;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
25 import org.eclipse.jdt.internal.core.index.Index;
26 import org.eclipse.jdt.internal.core.search.processing.JobManager;
27 import org.eclipse.jdt.internal.core.util.Util;
28
29 public class IndexBinaryFolder extends IndexRequest {
30     IContainer folder;
31
32     public IndexBinaryFolder(IContainer folder, IndexManager manager) {
33         super(folder.getFullPath(), manager);
34         this.folder = folder;
35     }
36     public boolean equals(Object JavaDoc o) {
37         if (o instanceof IndexBinaryFolder)
38             return this.folder.equals(((IndexBinaryFolder) o).folder);
39         return false;
40     }
41     /**
42      * Ensure consistency of a folder index. Need to walk all nested resources,
43      * and discover resources which have either been changed, added or deleted
44      * since the index was produced.
45      */

46     public boolean execute(IProgressMonitor progressMonitor) {
47
48         if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled()) return true;
49         if (!this.folder.isAccessible()) return true; // nothing to do
50

51         Index index = this.manager.getIndexForUpdate(this.containerPath, true, /*reuse index file*/ true /*create if none*/);
52         if (index == null) return true;
53         ReadWriteMonitor monitor = index.monitor;
54         if (monitor == null) return true; // index got deleted since acquired
55

56         try {
57             monitor.enterRead(); // ask permission to read
58

59             String JavaDoc[] paths = index.queryDocumentNames(""); // all file names //$NON-NLS-1$
60
int max = paths == null ? 0 : paths.length;
61             final SimpleLookupTable indexedFileNames = new SimpleLookupTable(max==0 ? 33 : max+11);
62             final String JavaDoc OK = "OK"; //$NON-NLS-1$
63
final String JavaDoc DELETED = "DELETED"; //$NON-NLS-1$
64
if (paths == null) {
65                 this.folder.accept(new IResourceProxyVisitor() {
66                     public boolean visit(IResourceProxy proxy) {
67                         if (isCancelled) return false;
68                         if (proxy.getType() == IResource.FILE) {
69                             if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(proxy.getName())) {
70                                 IFile file = (IFile) proxy.requestResource();
71                                 String JavaDoc containerRelativePath = Util.relativePath(file.getFullPath(), containerPath.segmentCount());
72                                 indexedFileNames.put(containerRelativePath, file);
73                             }
74                             return false;
75                         }
76                         return true;
77                     }
78                 }, IResource.NONE);
79             } else {
80                 for (int i = 0; i < max; i++) {
81                     indexedFileNames.put(paths[i], DELETED);
82                 }
83                 final long indexLastModified = index.getIndexFile().lastModified();
84                 this.folder.accept(
85                     new IResourceProxyVisitor() {
86                         public boolean visit(IResourceProxy proxy) throws CoreException {
87                             if (isCancelled) return false;
88                             if (proxy.getType() == IResource.FILE) {
89                                 if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(proxy.getName())) {
90                                     IFile file = (IFile) proxy.requestResource();
91                                     URI JavaDoc location = file.getLocationURI();
92                                     if (location != null) {
93                                         String JavaDoc containerRelativePath = Util.relativePath(file.getFullPath(), containerPath.segmentCount());
94                                         indexedFileNames.put(containerRelativePath,
95                                             indexedFileNames.get(containerRelativePath) == null
96                                                     || indexLastModified <
97                                                     EFS.getStore(location).fetchInfo().getLastModified()
98                                                 ? (Object JavaDoc) file
99                                                 : (Object JavaDoc) OK);
100                                     }
101                                 }
102                                 return false;
103                             }
104                             return true;
105                         }
106                     },
107                     IResource.NONE
108                 );
109             }
110
111             Object JavaDoc[] names = indexedFileNames.keyTable;
112             Object JavaDoc[] values = indexedFileNames.valueTable;
113             for (int i = 0, length = names.length; i < length; i++) {
114                 String JavaDoc name = (String JavaDoc) names[i];
115                 if (name != null) {
116                     if (this.isCancelled) return false;
117
118                     Object JavaDoc value = values[i];
119                     if (value != OK) {
120                         if (value == DELETED)
121                             this.manager.remove(name, this.containerPath);
122                         else {
123                             this.manager.addBinary((IFile) value, this.containerPath);
124                         }
125                     }
126                 }
127             }
128
129             // request to save index when all class files have been indexed... also sets state to SAVED_STATE
130
this.manager.request(new SaveIndex(this.containerPath, this.manager));
131         } catch (CoreException e) {
132             if (JobManager.VERBOSE) {
133                 Util.verbose("-> failed to index " + this.folder + " because of the following exception:", System.err); //$NON-NLS-1$ //$NON-NLS-2$
134
e.printStackTrace();
135             }
136             this.manager.removeIndex(this.containerPath);
137             return false;
138         } catch (IOException JavaDoc e) {
139             if (JobManager.VERBOSE) {
140                 Util.verbose("-> failed to index " + this.folder + " because of the following exception:", System.err); //$NON-NLS-1$ //$NON-NLS-2$
141
e.printStackTrace();
142             }
143             this.manager.removeIndex(this.containerPath);
144             return false;
145         } finally {
146             monitor.exitRead(); // free read lock
147
}
148         return true;
149     }
150     public int hashCode() {
151         return this.folder.hashCode();
152     }
153     protected Integer JavaDoc updatedIndexState() {
154         return IndexManager.REBUILDING_STATE;
155     }
156     public String JavaDoc toString() {
157         return "indexing binary folder " + this.folder.getFullPath(); //$NON-NLS-1$
158
}
159 }
160
Popular Tags