Example usage for org.eclipse.jdt.internal.core.search.indexing ReadWriteMonitor enterWrite

List of usage examples for org.eclipse.jdt.internal.core.search.indexing ReadWriteMonitor enterWrite

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core.search.indexing ReadWriteMonitor enterWrite.

Prototype

public synchronized void enterWrite() 

Source Link

Document

Only one writer at a time is allowed to perform Blocking only when already writing or reading.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.search.indexing.AddJarFileToIndex.java

License:Open Source License

public boolean execute(IProgressMonitor progressMonitor) {

    if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled())
        return true;

    if (hasPreBuiltIndex()) {
        boolean added = this.manager.addIndex(this.containerPath, this.indexFileURL);
        if (added)
            return true;
        this.indexFileURL = null;
    }/*w  w w  .  ja v  a 2s. c om*/

    try {
        // if index is already cached, then do not perform any check
        // MUST reset the IndexManager if a jar file is changed
        Index index = this.manager.getIndexForUpdate(this.containerPath, false,
                /*do not reuse index file*/ false /*do not create if none*/);
        if (index != null) {
            if (JobManager.VERBOSE)
                org.eclipse.jdt.internal.core.util.Util
                        .verbose("-> no indexing required (index already exists) for " + this.containerPath); //$NON-NLS-1$
            return true;
        }

        index = this.manager.getIndexForUpdate(this.containerPath, true,
                /*reuse index file*/ true /*create if none*/);
        if (index == null) {
            if (JobManager.VERBOSE)
                org.eclipse.jdt.internal.core.util.Util
                        .verbose("-> index could not be created for " + this.containerPath); //$NON-NLS-1$
            return true;
        }
        ReadWriteMonitor monitor = index.monitor;
        if (monitor == null) {
            if (JobManager.VERBOSE)
                org.eclipse.jdt.internal.core.util.Util
                        .verbose("-> index for " + this.containerPath + " just got deleted"); //$NON-NLS-1$//$NON-NLS-2$
            return true; // index got deleted since acquired
        }
        index.separator = JAR_SEPARATOR;
        ZipFile zip = null;
        try {
            // 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
            // local file system
            Path zipFilePath = null;

            monitor.enterWrite(); // ask permission to write
            if (this.resource != null) {
                URI location = this.resource.getLocationURI();
                if (location == null)
                    return false;
                if (JavaModelManager.ZIP_ACCESS_VERBOSE)
                    System.out.println("(" + Thread.currentThread() //$NON-NLS-1$
                            + ") [AddJarFileToIndex.execute()] Creating ZipFile on " + location.getPath()); //$NON-NLS-1$
                File file = null;
                try {
                    file = org.eclipse.jdt.internal.core.util.Util.toLocalFile(location, progressMonitor);
                } catch (CoreException e) {
                    if (JobManager.VERBOSE) {
                        org.eclipse.jdt.internal.core.util.Util.verbose("-> failed to index " //$NON-NLS-1$
                                + location.getPath() + " because of the following exception:"); //$NON-NLS-1$
                        e.printStackTrace();
                    }
                }
                if (file == null) {
                    if (JobManager.VERBOSE)
                        org.eclipse.jdt.internal.core.util.Util.verbose("-> failed to index " //$NON-NLS-1$
                                + location.getPath() + " because the file could not be fetched"); //$NON-NLS-1$
                    return false;
                }
                zip = new ZipFile(file);
                zipFilePath = (Path) this.resource.getFullPath().makeRelative();
                // absolute path relative to the workspace
            } else {
                if (JavaModelManager.ZIP_ACCESS_VERBOSE)
                    System.out.println("(" + Thread.currentThread() //$NON-NLS-1$
                            + ") [AddJarFileToIndex.execute()] Creating ZipFile on " + this.containerPath); //$NON-NLS-1$
                // external file -> it is ok to use toFile()
                zip = new ZipFile(this.containerPath.toFile());
                zipFilePath = (Path) this.containerPath;
                // path is already canonical since coming from a library classpath entry
            }

            if (this.isCancelled) {
                if (JobManager.VERBOSE)
                    org.eclipse.jdt.internal.core.util.Util
                            .verbose("-> indexing of " + zip.getName() + " has been cancelled"); //$NON-NLS-1$ //$NON-NLS-2$
                return false;
            }

            if (JobManager.VERBOSE)
                org.eclipse.jdt.internal.core.util.Util.verbose("-> indexing " + zip.getName()); //$NON-NLS-1$
            long initialTime = System.currentTimeMillis();

            String[] paths = index.queryDocumentNames(""); // all file names //$NON-NLS-1$
            if (paths != null) {
                int max = paths.length;
                /* check integrity of the existing index file
                 * if the length is equal to 0, we want to index the whole jar again
                 * If not, then we want to check that there is no missing entry, if
                 * one entry is missing then we recreate the index
                 */
                String EXISTS = "OK"; //$NON-NLS-1$
                String DELETED = "DELETED"; //$NON-NLS-1$
                SimpleLookupTable indexedFileNames = new SimpleLookupTable(max == 0 ? 33 : max + 11);
                for (int i = 0; i < max; i++)
                    indexedFileNames.put(paths[i], DELETED);
                for (Enumeration e = zip.entries(); e.hasMoreElements();) {
                    // iterate each entry to index it
                    ZipEntry ze = (ZipEntry) e.nextElement();
                    String zipEntryName = ze.getName();
                    if (Util.isClassFileName(zipEntryName) && isValidPackageNameForClass(zipEntryName))
                        // the class file may not be there if the package name is not valid
                        indexedFileNames.put(zipEntryName, EXISTS);
                }
                boolean needToReindex = indexedFileNames.elementSize != max; // a new file was added
                if (!needToReindex) {
                    Object[] valueTable = indexedFileNames.valueTable;
                    for (int i = 0, l = valueTable.length; i < l; i++) {
                        if (valueTable[i] == DELETED) {
                            needToReindex = true; // a file was deleted so re-index
                            break;
                        }
                    }
                    if (!needToReindex) {
                        if (JobManager.VERBOSE)
                            org.eclipse.jdt.internal.core.util.Util
                                    .verbose("-> no indexing required (index is consistent with library) for " //$NON-NLS-1$
                                            + zip.getName() + " (" //$NON-NLS-1$
                                            + (System.currentTimeMillis() - initialTime) + "ms)"); //$NON-NLS-1$
                        this.manager.saveIndex(index); // to ensure its placed into the saved state
                        return true;
                    }
                }
            }

            // Index the jar for the first time or reindex the jar in case the previous index file has been corrupted
            // index already existed: recreate it so that we forget about previous entries
            SearchParticipant participant = SearchEngine.getDefaultSearchParticipant(manager);
            if (!this.manager.resetIndex(this.containerPath)) {
                // failed to recreate index, see 73330
                this.manager.removeIndex(this.containerPath);
                return false;
            }
            index.separator = JAR_SEPARATOR;
            IPath indexPath = null;
            IndexLocation indexLocation;
            if ((indexLocation = index.getIndexLocation()) != null) {
                indexPath = new Path(indexLocation.getCanonicalFilePath());
            }
            for (Enumeration e = zip.entries(); e.hasMoreElements();) {
                if (this.isCancelled) {
                    if (JobManager.VERBOSE)
                        org.eclipse.jdt.internal.core.util.Util
                                .verbose("-> indexing of " + zip.getName() + " has been cancelled"); //$NON-NLS-1$ //$NON-NLS-2$
                    return false;
                }

                // iterate each entry to index it
                ZipEntry ze = (ZipEntry) e.nextElement();
                String zipEntryName = ze.getName();
                if (Util.isClassFileName(zipEntryName) && isValidPackageNameForClass(zipEntryName)) {
                    // index only classes coming from valid packages - https://bugs.eclipse.org/bugs/show_bug.cgi?id=293861
                    final byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util
                            .getZipEntryByteContent(ze, zip);
                    JavaSearchDocument entryDocument = new JavaSearchDocument(ze, zipFilePath, classFileBytes,
                            participant);
                    this.manager.indexDocument(entryDocument, participant, index, indexPath);
                }
            }
            this.manager.saveIndex(index);
            if (JobManager.VERBOSE)
                org.eclipse.jdt.internal.core.util.Util.verbose("-> done indexing of " //$NON-NLS-1$
                        + zip.getName() + " (" //$NON-NLS-1$
                        + (System.currentTimeMillis() - initialTime) + "ms)"); //$NON-NLS-1$
        } finally {
            if (zip != null) {
                if (JavaModelManager.ZIP_ACCESS_VERBOSE)
                    System.out.println("(" + Thread.currentThread() //$NON-NLS-1$
                            + ") [AddJarFileToIndex.execute()] Closing ZipFile " + zip); //$NON-NLS-1$
                zip.close();
            }
            monitor.exitWrite(); // free write lock
        }
    } catch (IOException e) {
        if (JobManager.VERBOSE) {
            org.eclipse.jdt.internal.core.util.Util.verbose(
                    "-> failed to index " + this.containerPath + " because of the following exception:"); //$NON-NLS-1$ //$NON-NLS-2$
            e.printStackTrace();
        }
        this.manager.removeIndex(this.containerPath);
        return false;
    }
    return true;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.indexing.IndexManager.java

License:Open Source License

public void scheduleDocumentIndexing(final SearchDocument searchDocument, IPath container,
        final IndexLocation indexLocation, final SearchParticipant searchParticipant) {
    request(new IndexRequest(container, this) {
        public boolean execute(IProgressMonitor progressMonitor) {
            if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled())
                return true;

            /* ensure no concurrent write access to index */
            Index index = getIndex(this.containerPath, indexLocation, true,
                    /*reuse index file*/ true /*create if none*/);
            if (index == null)
                return true;
            ReadWriteMonitor monitor = index.monitor;
            if (monitor == null)
                return true; // index got deleted since acquired

            try {
                monitor.enterWrite(); // ask permission to write
                indexDocument(searchDocument, searchParticipant, index,
                        new Path(indexLocation.getCanonicalFilePath()));
            } finally {
                monitor.exitWrite(); // free write lock
            }/* w w w .  j a  v a 2 s .  c  o m*/
            return true;
        }

        public String toString() {
            return "indexing " + searchDocument.getPath(); //$NON-NLS-1$
        }
    });
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.indexing.RemoveFromIndex.java

License:Open Source License

public boolean execute(IProgressMonitor progressMonitor) {

    if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled())
        return true;

    /* ensure no concurrent write access to index */
    Index index = this.manager.getIndex(this.containerPath, true,
            /*reuse index file*/ false /*create if none*/);
    if (index == null)
        return true;
    ReadWriteMonitor monitor = index.monitor;
    if (monitor == null)
        return true; // index got deleted since acquired

    try {/*ww  w  . j a  v  a2 s . com*/
        monitor.enterWrite(); // ask permission to write
        index.remove(this.resourceName);
    } finally {
        monitor.exitWrite(); // free write lock
    }
    return true;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.indexing.SaveIndex.java

License:Open Source License

public boolean execute(IProgressMonitor progressMonitor) {

    if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled())
        return true;

    /* ensure no concurrent write access to index */
    Index index = this.manager.getIndex(this.containerPath, true /*reuse index file*/,
            false /*don't create if none*/);
    if (index == null)
        return true;
    ReadWriteMonitor monitor = index.monitor;
    if (monitor == null)
        return true; // index got deleted since acquired

    try {//ww w.  ja  va2 s.com
        monitor.enterWrite(); // ask permission to write
        this.manager.saveIndex(index);
    } catch (IOException e) {
        if (JobManager.VERBOSE) {
            Util.verbose(
                    "-> failed to save index " + this.containerPath + " because of the following exception:", //$NON-NLS-1$//$NON-NLS-2$
                    System.err);
            e.printStackTrace();
        }
        return false;
    } finally {
        monitor.exitWrite(); // free write lock
    }
    return true;
}

From source file:org.eclipse.che.jdt.internal.core.search.indexing.AddJarFileToIndex.java

License:Open Source License

public boolean execute(IProgressMonitor progressMonitor) {

    if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled())
        return true;

    if (hasPreBuiltIndex()) {
        boolean added = this.manager.addIndex(this.containerPath, this.indexFileURL);
        if (added)
            return true;
        this.indexFileURL = null;
    }//from   w  w  w . j a v a2 s.c om

    try {
        // if index is already cached, then do not perform any check
        // MUST reset the IndexManager if a jar file is changed
        Index index = this.manager.getIndexForUpdate(this.containerPath, false,
                /*do not reuse index file*/ false /*do not create if
                                                  none*/);
        if (index != null) {
            if (JobManager.VERBOSE)
                org.eclipse.jdt.internal.core.util.Util
                        .verbose("-> no indexing required (index already exists) for " + this.containerPath); //$NON-NLS-1$
            return true;
        }

        index = this.manager.getIndexForUpdate(this.containerPath, true,
                /*reuse index file*/ true /*create if none*/);
        if (index == null) {
            if (JobManager.VERBOSE)
                org.eclipse.jdt.internal.core.util.Util
                        .verbose("-> index could not be created for " + this.containerPath); //$NON-NLS-1$
            return true;
        }
        ReadWriteMonitor monitor = index.monitor;
        if (monitor == null) {
            if (JobManager.VERBOSE)
                org.eclipse.jdt.internal.core.util.Util
                        .verbose("-> index for " + this.containerPath + " just got deleted"); //$NON-NLS-1$//$NON-NLS-2$
            return true; // index got deleted since acquired
        }
        index.separator = JAR_SEPARATOR;
        ZipFile zip = null;
        try {
            // 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
            // local file system
            Path zipFilePath = null;

            monitor.enterWrite(); // ask permission to write
            if (this.resource != null) {
                URI location = this.resource.getLocationURI();
                if (location == null)
                    return false;
                if (JavaModelManager.ZIP_ACCESS_VERBOSE)
                    System.out.println("(" + Thread.currentThread() //$NON-NLS-1$
                            + ") [AddJarFileToIndex.execute()] Creating ZipFile on " + location.getPath()); //$NON-NLS-1$
                File file = null;
                try {
                    file = org.eclipse.jdt.internal.core.util.Util.toLocalFile(location, progressMonitor);
                } catch (CoreException e) {
                    if (JobManager.VERBOSE) {
                        org.eclipse.jdt.internal.core.util.Util.verbose("-> failed to index " //$NON-NLS-1$
                                + location.getPath() + " because of the following exception:"); //$NON-NLS-1$
                        e.printStackTrace();
                    }
                }
                if (file == null) {
                    if (JobManager.VERBOSE)
                        org.eclipse.jdt.internal.core.util.Util.verbose("-> failed to index " //$NON-NLS-1$
                                + location.getPath() + " because the file could not be fetched"); //$NON-NLS-1$
                    return false;
                }
                zip = new ZipFile(file);
                zipFilePath = (Path) this.resource.getFullPath().makeRelative();
                // absolute path relative to the workspace
            } else {
                if (JavaModelManager.ZIP_ACCESS_VERBOSE)
                    System.out.println("(" + Thread.currentThread() //$NON-NLS-1$
                            + ") [AddJarFileToIndex.execute()] Creating ZipFile on " + this.containerPath); //$NON-NLS-1$
                // external file -> it is ok to use toFile()
                zip = new ZipFile(this.containerPath.toFile());
                zipFilePath = (Path) this.containerPath;
                // path is already canonical since coming from a library classpath entry
            }

            if (this.isCancelled) {
                if (JobManager.VERBOSE)
                    org.eclipse.jdt.internal.core.util.Util
                            .verbose("-> indexing of " + zip.getName() + " has been cancelled"); //$NON-NLS-1$ //$NON-NLS-2$
                return false;
            }

            if (JobManager.VERBOSE)
                org.eclipse.jdt.internal.core.util.Util.verbose("-> indexing " + zip.getName()); //$NON-NLS-1$
            long initialTime = System.currentTimeMillis();

            String[] paths = index.queryDocumentNames(""); // all file names //$NON-NLS-1$
            if (paths != null) {
                int max = paths.length;
                /* check integrity of the existing index file
                 * if the length is equal to 0, we want to index the whole jar again
                 * If not, then we want to check that there is no missing entry, if
                 * one entry is missing then we recreate the index
                 */
                String EXISTS = "OK"; //$NON-NLS-1$
                String DELETED = "DELETED"; //$NON-NLS-1$
                SimpleLookupTable indexedFileNames = new SimpleLookupTable(max == 0 ? 33 : max + 11);
                for (int i = 0; i < max; i++)
                    indexedFileNames.put(paths[i], DELETED);
                for (Enumeration e = zip.entries(); e.hasMoreElements();) {
                    // iterate each entry to index it
                    ZipEntry ze = (ZipEntry) e.nextElement();
                    String zipEntryName = ze.getName();
                    if (Util.isClassFileName(zipEntryName) && isValidPackageNameForClass(zipEntryName))
                        // the class file may not be there if the package name is not valid
                        indexedFileNames.put(zipEntryName, EXISTS);
                }
                boolean needToReindex = indexedFileNames.elementSize != max; // a new file was added
                if (!needToReindex) {
                    Object[] valueTable = indexedFileNames.valueTable;
                    for (int i = 0, l = valueTable.length; i < l; i++) {
                        if (valueTable[i] == DELETED) {
                            needToReindex = true; // a file was deleted so re-index
                            break;
                        }
                    }
                    if (!needToReindex) {
                        if (JobManager.VERBOSE)
                            org.eclipse.jdt.internal.core.util.Util
                                    .verbose("-> no indexing required (index is consistent with library) for " //$NON-NLS-1$
                                            + zip.getName() + " (" //$NON-NLS-1$
                                            + (System.currentTimeMillis() - initialTime) + "ms)"); //$NON-NLS-1$
                        this.manager.saveIndex(index); // to ensure its placed into the saved state
                        return true;
                    }
                }
            }

            // Index the jar for the first time or reindex the jar in case the previous index file has been corrupted
            // index already existed: recreate it so that we forget about previous entries
            SearchParticipant participant = SearchEngine.getDefaultSearchParticipant(manager, javaProject);
            if (!this.manager.resetIndex(this.containerPath)) {
                // failed to recreate index, see 73330
                this.manager.removeIndex(this.containerPath);
                return false;
            }
            index.separator = JAR_SEPARATOR;
            IPath indexPath = null;
            IndexLocation indexLocation;
            if ((indexLocation = index.getIndexLocation()) != null) {
                indexPath = new Path(indexLocation.getCanonicalFilePath());
            }
            for (Enumeration e = zip.entries(); e.hasMoreElements();) {
                if (this.isCancelled) {
                    if (JobManager.VERBOSE)
                        org.eclipse.jdt.internal.core.util.Util
                                .verbose("-> indexing of " + zip.getName() + " has been cancelled"); //$NON-NLS-1$ //$NON-NLS-2$
                    return false;
                }

                // iterate each entry to index it
                ZipEntry ze = (ZipEntry) e.nextElement();
                String zipEntryName = ze.getName();
                if (Util.isClassFileName(zipEntryName) && isValidPackageNameForClass(zipEntryName)) {
                    // index only classes coming from valid packages - https://bugs.eclipse.org/bugs/show_bug.cgi?id=293861
                    final byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util
                            .getZipEntryByteContent(ze, zip);
                    JavaSearchDocument entryDocument = new JavaSearchDocument(ze, zipFilePath, classFileBytes,
                            participant);
                    this.manager.indexDocument(entryDocument, participant, index, indexPath);
                }
            }
            this.manager.saveIndex(index);
            if (JobManager.VERBOSE)
                org.eclipse.jdt.internal.core.util.Util.verbose("-> done indexing of " //$NON-NLS-1$
                        + zip.getName() + " (" //$NON-NLS-1$
                        + (System.currentTimeMillis() - initialTime) + "ms)"); //$NON-NLS-1$
        } finally {
            if (zip != null) {
                if (JavaModelManager.ZIP_ACCESS_VERBOSE)
                    System.out.println("(" + Thread.currentThread() //$NON-NLS-1$
                            + ") [AddJarFileToIndex.execute()] Closing ZipFile " + zip); //$NON-NLS-1$
                zip.close();
            }
            monitor.exitWrite(); // free write lock
        }
    } catch (IOException e) {
        if (JobManager.VERBOSE) {
            org.eclipse.jdt.internal.core.util.Util.verbose(
                    "-> failed to index " + this.containerPath + " because of the following exception:"); //$NON-NLS-1$ //$NON-NLS-2$
            e.printStackTrace();
        }
        this.manager.removeIndex(this.containerPath);
        return false;
    }
    return true;
}

From source file:org.eclipse.jdt.internal.core.search.indexing.IndexManager.java

License:Open Source License

public void scheduleDocumentIndexing(final SearchDocument searchDocument, IPath container,
        final IPath indexLocation, final SearchParticipant searchParticipant) {
    request(new IndexRequest(container, this) {
        public boolean execute(IProgressMonitor progressMonitor) {
            if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled())
                return true;

            /* ensure no concurrent write access to index */
            Index index = getIndex(this.containerPath, indexLocation, true,
                    /*reuse index file*/ true /*create if none*/);
            if (index == null)
                return true;
            ReadWriteMonitor monitor = index.monitor;
            if (monitor == null)
                return true; // index got deleted since acquired

            try {
                monitor.enterWrite(); // ask permission to write
                indexDocument(searchDocument, searchParticipant, index, indexLocation);
            } finally {
                monitor.exitWrite(); // free write lock
            }/*from   w w w . j  a  v a  2  s  . co  m*/
            return true;
        }

        public String toString() {
            return "indexing " + searchDocument.getPath(); //$NON-NLS-1$
        }
    });
}