KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.net.URI JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.zip.ZipEntry JavaDoc;
18 import java.util.zip.ZipFile JavaDoc;
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         // external JAR scenario - no resource
43
super(jarPath, manager);
44     }
45     public boolean equals(Object JavaDoc 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             // if index is already cached, then do not perform any check
67
// MUST reset the IndexManager if a jar file is changed
68
Index index = this.manager.getIndexForUpdate(this.containerPath, false, /*do not reuse index file*/ false /*do not create if none*/);
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); //$NON-NLS-1$
72
return true;
73             }
74
75             index = this.manager.getIndexForUpdate(this.containerPath, true, /*reuse index file*/ true /*create if none*/);
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); //$NON-NLS-1$
79
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"); //$NON-NLS-1$//$NON-NLS-2$
85
return true; // index got deleted since acquired
86
}
87             ZipFile JavaDoc zip = null;
88             try {
89                 // this path will be a relative path to the workspace in case the zipfile in the workspace otherwise it will be a path in the
90
// local file system
91
Path zipFilePath = null;
92
93                 monitor.enterWrite(); // ask permission to write
94
if (resource != null) {
95                     URI JavaDoc 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()); //$NON-NLS-1$ //$NON-NLS-2$
99
File JavaDoc 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:"); //$NON-NLS-1$ //$NON-NLS-2$
105
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"); //$NON-NLS-1$ //$NON-NLS-2$
111
return false;
112                     }
113                     zip = new ZipFile JavaDoc(file);
114                     zipFilePath = (Path) this.resource.getFullPath().makeRelative();
115                     // absolute path relative to the workspace
116
} else {
117                     if (JavaModelManager.ZIP_ACCESS_VERBOSE)
118                         System.out.println("(" + Thread.currentThread() + ") [AddJarFileToIndex.execute()] Creating ZipFile on " + this.containerPath); //$NON-NLS-1$ //$NON-NLS-2$
119
// external file -> it is ok to use toFile()
120
zip = new ZipFile JavaDoc(this.containerPath.toFile());
121                     zipFilePath = (Path) this.containerPath;
122                     // path is already canonical since coming from a library classpath entry
123
}
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"); //$NON-NLS-1$ //$NON-NLS-2$
128
return false;
129                 }
130
131                 if (JobManager.VERBOSE)
132                     org.eclipse.jdt.internal.core.util.Util.verbose("-> indexing " + zip.getName()); //$NON-NLS-1$
133
long initialTime = System.currentTimeMillis();
134
135                 String JavaDoc[] paths = index.queryDocumentNames(""); // all file names //$NON-NLS-1$
136
if (paths != null) {
137                     int max = paths.length;
138                     /* check integrity of the existing index file
139                      * if the length is equal to 0, we want to index the whole jar again
140                      * If not, then we want to check that there is no missing entry, if
141                      * one entry is missing then we recreate the index
142                      */

143                     String JavaDoc EXISTS = "OK"; //$NON-NLS-1$
144
String JavaDoc DELETED = "DELETED"; //$NON-NLS-1$
145
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 JavaDoc e = zip.entries(); e.hasMoreElements();) {
149                         // iterate each entry to index it
150
ZipEntry JavaDoc ze = (ZipEntry JavaDoc) e.nextElement();
151                         String JavaDoc zipEntryName = ze.getName();
152                         if (Util.isClassFileName(zipEntryName))
153                             indexedFileNames.put(zipEntryName, EXISTS);
154                     }
155                     boolean needToReindex = indexedFileNames.elementSize != max; // a new file was added
156
if (!needToReindex) {
157                         Object JavaDoc[] valueTable = indexedFileNames.valueTable;
158                         for (int i = 0, l = valueTable.length; i < l; i++) {
159                             if (valueTable[i] == DELETED) {
160                                 needToReindex = true; // a file was deleted so re-index
161
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 " //$NON-NLS-1$
167
+ zip.getName() + " (" //$NON-NLS-1$
168
+ (System.currentTimeMillis() - initialTime) + "ms)"); //$NON-NLS-1$
169
this.manager.saveIndex(index); // to ensure its placed into the saved state
170
return true;
171                         }
172                     }
173                 }
174
175                 // Index the jar for the first time or reindex the jar in case the previous index file has been corrupted
176
// index already existed: recreate it so that we forget about previous entries
177
SearchParticipant participant = SearchEngine.getDefaultSearchParticipant();
178                 index = manager.recreateIndex(this.containerPath);
179                 if (index == null) {
180                     // failed to recreate index, see 73330
181
manager.removeIndex(this.containerPath);
182                     return false;
183                 }
184
185                 for (Enumeration JavaDoc 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"); //$NON-NLS-1$ //$NON-NLS-2$
189
return false;
190                     }
191
192                     // iterate each entry to index it
193
ZipEntry JavaDoc ze = (ZipEntry JavaDoc) 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 " //$NON-NLS-1$
203
+ zip.getName() + " (" //$NON-NLS-1$
204
+ (System.currentTimeMillis() - initialTime) + "ms)"); //$NON-NLS-1$
205
} finally {
206                 if (zip != null) {
207                     if (JavaModelManager.ZIP_ACCESS_VERBOSE)
208                         System.out.println("(" + Thread.currentThread() + ") [AddJarFileToIndex.execute()] Closing ZipFile " + zip); //$NON-NLS-1$ //$NON-NLS-2$
209
zip.close();
210                 }
211                 monitor.exitWrite(); // free write lock
212
}
213         } catch (IOException JavaDoc e) {
214             if (JobManager.VERBOSE) {
215                 org.eclipse.jdt.internal.core.util.Util.verbose("-> failed to index " + this.containerPath + " because of the following exception:"); //$NON-NLS-1$ //$NON-NLS-2$
216
e.printStackTrace();
217             }
218             manager.removeIndex(this.containerPath);
219             return false;
220         }
221         return true;
222     }
223     protected Integer JavaDoc updatedIndexState() {
224         return IndexManager.REBUILDING_STATE;
225     }
226     public String JavaDoc toString() {
227         return "indexing " + this.containerPath.toString(); //$NON-NLS-1$
228
}
229 }
230
Popular Tags