1 11 package org.eclipse.jdt.internal.core.search.indexing; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.net.URI ; 16 import java.util.Enumeration ; 17 import java.util.zip.ZipEntry ; 18 import java.util.zip.ZipFile ; 19 20 import org.eclipse.core.resources.IFile; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.core.runtime.IProgressMonitor; 24 import org.eclipse.core.runtime.Path; 25 import org.eclipse.jdt.core.search.SearchEngine; 26 import org.eclipse.jdt.core.search.SearchParticipant; 27 import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable; 28 import org.eclipse.jdt.internal.compiler.util.Util; 29 import org.eclipse.jdt.internal.core.JavaModelManager; 30 import org.eclipse.jdt.internal.core.index.Index; 31 import org.eclipse.jdt.internal.core.search.JavaSearchDocument; 32 import org.eclipse.jdt.internal.core.search.processing.JobManager; 33 34 class AddJarFileToIndex extends IndexRequest { 35 IFile resource; 36 37 public AddJarFileToIndex(IFile resource, IndexManager manager) { 38 super(resource.getFullPath(), manager); 39 this.resource = resource; 40 } 41 public AddJarFileToIndex(IPath jarPath, IndexManager manager) { 42 super(jarPath, manager); 44 } 45 public boolean equals(Object o) { 46 if (o instanceof AddJarFileToIndex) { 47 if (this.resource != null) 48 return this.resource.equals(((AddJarFileToIndex) o).resource); 49 if (this.containerPath != null) 50 return this.containerPath.equals(((AddJarFileToIndex) o).containerPath); 51 } 52 return false; 53 } 54 public int hashCode() { 55 if (this.resource != null) 56 return this.resource.hashCode(); 57 if (this.containerPath != null) 58 return this.containerPath.hashCode(); 59 return -1; 60 } 61 public boolean execute(IProgressMonitor progressMonitor) { 62 63 if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled()) return true; 64 65 try { 66 Index index = this.manager.getIndexForUpdate(this.containerPath, false, false ); 69 if (index != null) { 70 if (JobManager.VERBOSE) 71 org.eclipse.jdt.internal.core.util.Util.verbose("-> no indexing required (index already exists) for " + this.containerPath); return true; 73 } 74 75 index = this.manager.getIndexForUpdate(this.containerPath, true, true ); 76 if (index == null) { 77 if (JobManager.VERBOSE) 78 org.eclipse.jdt.internal.core.util.Util.verbose("-> index could not be created for " + this.containerPath); return true; 80 } 81 ReadWriteMonitor monitor = index.monitor; 82 if (monitor == null) { 83 if (JobManager.VERBOSE) 84 org.eclipse.jdt.internal.core.util.Util.verbose("-> index for " + this.containerPath + " just got deleted"); return true; } 87 ZipFile zip = null; 88 try { 89 Path zipFilePath = null; 92 93 monitor.enterWrite(); if (resource != null) { 95 URI location = this.resource.getLocationURI(); 96 if (location == null) return false; 97 if (JavaModelManager.ZIP_ACCESS_VERBOSE) 98 System.out.println("(" + Thread.currentThread() + ") [AddJarFileToIndex.execute()] Creating ZipFile on " + location.getPath()); File file = null; 100 try { 101 file = org.eclipse.jdt.internal.core.util.Util.toLocalFile(location, progressMonitor); 102 } catch (CoreException e) { 103 if (JobManager.VERBOSE) { 104 org.eclipse.jdt.internal.core.util.Util.verbose("-> failed to index " + location.getPath() + " because of the following exception:"); e.printStackTrace(); 106 } 107 } 108 if (file == null) { 109 if (JobManager.VERBOSE) 110 org.eclipse.jdt.internal.core.util.Util.verbose("-> failed to index " + location.getPath() + " because the file could not be fetched"); return false; 112 } 113 zip = new ZipFile (file); 114 zipFilePath = (Path) this.resource.getFullPath().makeRelative(); 115 } else { 117 if (JavaModelManager.ZIP_ACCESS_VERBOSE) 118 System.out.println("(" + Thread.currentThread() + ") [AddJarFileToIndex.execute()] Creating ZipFile on " + this.containerPath); zip = new ZipFile (this.containerPath.toFile()); 121 zipFilePath = (Path) this.containerPath; 122 } 124 125 if (this.isCancelled) { 126 if (JobManager.VERBOSE) 127 org.eclipse.jdt.internal.core.util.Util.verbose("-> indexing of " + zip.getName() + " has been cancelled"); return false; 129 } 130 131 if (JobManager.VERBOSE) 132 org.eclipse.jdt.internal.core.util.Util.verbose("-> indexing " + zip.getName()); long initialTime = System.currentTimeMillis(); 134 135 String [] paths = index.queryDocumentNames(""); if (paths != null) { 137 int max = paths.length; 138 143 String EXISTS = "OK"; String DELETED = "DELETED"; SimpleLookupTable indexedFileNames = new SimpleLookupTable(max == 0 ? 33 : max + 11); 146 for (int i = 0; i < max; i++) 147 indexedFileNames.put(paths[i], DELETED); 148 for (Enumeration e = zip.entries(); e.hasMoreElements();) { 149 ZipEntry ze = (ZipEntry ) e.nextElement(); 151 String zipEntryName = ze.getName(); 152 if (Util.isClassFileName(zipEntryName)) 153 indexedFileNames.put(zipEntryName, EXISTS); 154 } 155 boolean needToReindex = indexedFileNames.elementSize != max; if (!needToReindex) { 157 Object [] valueTable = indexedFileNames.valueTable; 158 for (int i = 0, l = valueTable.length; i < l; i++) { 159 if (valueTable[i] == DELETED) { 160 needToReindex = true; break; 162 } 163 } 164 if (!needToReindex) { 165 if (JobManager.VERBOSE) 166 org.eclipse.jdt.internal.core.util.Util.verbose("-> no indexing required (index is consistent with library) for " + zip.getName() + " (" + (System.currentTimeMillis() - initialTime) + "ms)"); this.manager.saveIndex(index); return true; 171 } 172 } 173 } 174 175 SearchParticipant participant = SearchEngine.getDefaultSearchParticipant(); 178 index = manager.recreateIndex(this.containerPath); 179 if (index == null) { 180 manager.removeIndex(this.containerPath); 182 return false; 183 } 184 185 for (Enumeration e = zip.entries(); e.hasMoreElements();) { 186 if (this.isCancelled) { 187 if (JobManager.VERBOSE) 188 org.eclipse.jdt.internal.core.util.Util.verbose("-> indexing of " + zip.getName() + " has been cancelled"); return false; 190 } 191 192 ZipEntry ze = (ZipEntry ) e.nextElement(); 194 if (Util.isClassFileName(ze.getName())) { 195 final byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getZipEntryByteContent(ze, zip); 196 JavaSearchDocument entryDocument = new JavaSearchDocument(ze, zipFilePath, classFileBytes, participant); 197 this.manager.indexDocument(entryDocument, participant, index, this.containerPath); 198 } 199 } 200 this.manager.saveIndex(index); 201 if (JobManager.VERBOSE) 202 org.eclipse.jdt.internal.core.util.Util.verbose("-> done indexing of " + zip.getName() + " (" + (System.currentTimeMillis() - initialTime) + "ms)"); } finally { 206 if (zip != null) { 207 if (JavaModelManager.ZIP_ACCESS_VERBOSE) 208 System.out.println("(" + Thread.currentThread() + ") [AddJarFileToIndex.execute()] Closing ZipFile " + zip); zip.close(); 210 } 211 monitor.exitWrite(); } 213 } catch (IOException e) { 214 if (JobManager.VERBOSE) { 215 org.eclipse.jdt.internal.core.util.Util.verbose("-> failed to index " + this.containerPath + " because of the following exception:"); e.printStackTrace(); 217 } 218 manager.removeIndex(this.containerPath); 219 return false; 220 } 221 return true; 222 } 223 protected Integer updatedIndexState() { 224 return IndexManager.REBUILDING_STATE; 225 } 226 public String toString() { 227 return "indexing " + this.containerPath.toString(); } 229 } 230 | Popular Tags |