Search in sources :

Example 61 with Reference

use of java.lang.ref.Reference in project webtools.sourceediting by eclipse.

the class FileContentCache method getContents.

public String getContents(IPath filePath) {
    if (DEBUG)
        // $NON-NLS-1$
        System.out.println(getClass().getName() + "#getContents: " + filePath);
    /*
		 * Use an open file buffer if one exists for contents in dirty
		 * editors. LocationKind.IFILE will only apply to workspace paths, but
		 * they're far more likely to be open in an editor and modified than
		 * files outside the workspace (LocationKind.LOCATION). We'll use
		 * LocationKind.NORMALIZE and let the framework sort things out,
		 * unless it proves to be a performance problem.
		 * 
		 * Do not cause a file buffer to be opened by calling connect()
		 */
    ITextFileBuffer existingBuffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(filePath, LocationKind.NORMALIZE);
    if (existingBuffer != null) {
        IDocument document = existingBuffer.getDocument();
        if (document != null) {
            return document.get();
        }
    }
    // check the cache
    CacheEntry entry = null;
    Object o = fContentMap.get(filePath);
    if (o instanceof Reference) {
        entry = (CacheEntry) ((Reference) o).get();
    }
    if (entry == null || entry.isStale()) {
        if (DEBUG && entry != null && entry.isStale())
            // $NON-NLS-1$
            System.out.println(getClass().getName() + " stale contents: " + filePath);
        entry = new CacheEntry(filePath);
        synchronized (fContentMap) {
            fContentMap.put(filePath, new SoftReference(entry));
        }
    }
    cleanup();
    return entry.contents;
}
Also used : SoftReference(java.lang.ref.SoftReference) SoftReference(java.lang.ref.SoftReference) Reference(java.lang.ref.Reference) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IDocument(org.eclipse.jface.text.IDocument)

Example 62 with Reference

use of java.lang.ref.Reference in project webtools.sourceediting by eclipse.

the class ModuleCoreSupportDelegate method resolve.

/**
 * @param basePath
 *            - the full path to a resource within the workspace
 * @param reference
 *            - the reference string to resolve
 * @return - the full path within the workspace that corresponds to the
 *         given reference according to the virtual pathing support
 */
static IPath resolve(IPath basePath, String reference) {
    if (reference == null || basePath == null || basePath.segmentCount() == 0)
        return null;
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(basePath.segment(0));
    if (!ModuleCoreNature.isFlexibleProject(project))
        return null;
    if (basePath.segmentCount() > 1) {
        /*
			 * It can take the better part of a full second to do this, so
			 * cache the result.
			 */
        IPath resolved = null;
        Map mapForBaseResource = null;
        mapForBaseResource = (Map) fResolvedMap.get(basePath);
        if (mapForBaseResource != null) {
            Reference resolvedReference = (Reference) mapForBaseResource.get(reference);
            if (resolvedReference != null)
                resolved = (IPath) resolvedReference.get();
        } else {
            mapForBaseResource = new HashMap();
            fResolvedMap.put(basePath, mapForBaseResource);
        }
        if (resolved == null) {
            IFile baseFile = ResourcesPlugin.getWorkspace().getRoot().getFile(basePath);
            IVirtualResource[] virtualResources = ComponentCore.createResources(baseFile);
            for (int i = 0; i < virtualResources.length; i++) {
                IPath baseRuntimePath = virtualResources[i].getRuntimePath();
                IPath referenceRuntimePath = null;
                if (reference.startsWith(SLASH)) {
                    referenceRuntimePath = new Path(reference);
                } else {
                    referenceRuntimePath = baseRuntimePath.removeLastSegments(1).append(reference);
                }
                IVirtualFile virtualFile = ComponentCore.createFile(project, referenceRuntimePath);
                if (virtualFile != null && virtualFile.exists()) {
                    IFile[] underlyingFiles = virtualFile.getUnderlyingFiles();
                    for (int j = 0; j < underlyingFiles.length; j++) {
                        if (underlyingFiles[j].getProject().equals(project) && underlyingFiles[j].exists()) {
                            mapForBaseResource.put(reference, new SoftReference(underlyingFiles[j].getFullPath()));
                            resolved = underlyingFiles[j].getFullPath();
                        }
                    }
                } else {
                    // http://bugs.eclipse.org/338751
                    IVirtualFolder virtualFolder = ComponentCore.createFolder(project, referenceRuntimePath);
                    if (virtualFolder != null && virtualFolder.exists()) {
                        IContainer[] underlyingFolders = virtualFolder.getUnderlyingFolders();
                        for (int j = 0; j < underlyingFolders.length; j++) {
                            if (underlyingFolders[j].getProject().equals(project) && underlyingFolders[j].isAccessible()) {
                                return underlyingFolders[j].getFullPath();
                            }
                        }
                    } else {
                        // check assembled projects
                        return resolveInReferenced(project, referenceRuntimePath);
                    }
                }
            }
        }
        return resolved;
    } else {
        IVirtualFile virtualFile = ComponentCore.createFile(project, new Path(reference));
        if (virtualFile != null && virtualFile.exists()) {
            return virtualFile.getUnderlyingFile().getFullPath();
        }
    }
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) IVirtualReference(org.eclipse.wst.common.componentcore.resources.IVirtualReference) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) IVirtualFolder(org.eclipse.wst.common.componentcore.resources.IVirtualFolder) IVirtualResource(org.eclipse.wst.common.componentcore.resources.IVirtualResource) IProject(org.eclipse.core.resources.IProject) IVirtualFile(org.eclipse.wst.common.componentcore.resources.IVirtualFile) SoftReference(java.lang.ref.SoftReference) IContainer(org.eclipse.core.resources.IContainer) HashMap(java.util.HashMap) Map(java.util.Map)

Example 63 with Reference

use of java.lang.ref.Reference in project webtools.sourceediting by eclipse.

the class TLDCMDocumentManager method getCMDocument.

/**
 * Return the CMDocument at the uri (cached)
 */
protected CMDocument getCMDocument(String uri) {
    if (uri == null || uri.length() == 0)
        return null;
    String reference = uri;
    Object cacheKey = getCacheKey(reference);
    if (cacheKey == null)
        return null;
    CMDocument doc = (CMDocument) getDocuments().get(cacheKey);
    if (doc == null) {
        long lastModified = getModificationStamp(reference);
        /*
			 * If hasn't been moved into the local table, do so and increment
			 * the count. A local URI reference can be different depending on
			 * the file from which it was referenced. Use a computed key to
			 * keep them straight.
			 */
        Object o = getSharedDocumentCache().get(cacheKey);
        if (o != null) {
            if (o instanceof TLDCacheEntry) {
                TLDCacheEntry entry = (TLDCacheEntry) o;
                if (_debugCache) {
                    System.out.println("TLDCMDocument cache hit on " + cacheKey);
                }
                if (entry != null && entry.modificationStamp != IResource.NULL_STAMP && entry.modificationStamp >= lastModified) {
                    doc = entry.document;
                    entry.referenceCount++;
                } else {
                    getSharedDocumentCache().remove(cacheKey);
                }
            } else if (o instanceof Reference) {
                TLDCacheEntry entry = (TLDCacheEntry) ((Reference) o).get();
                if (entry != null) {
                    if (entry.modificationStamp != IResource.NULL_STAMP && entry.modificationStamp >= lastModified) {
                        doc = entry.document;
                        entry.referenceCount = 1;
                        getSharedDocumentCache().put(cacheKey, entry);
                    }
                } else {
                    getSharedDocumentCache().remove(cacheKey);
                }
            }
        }
        /* No document was found cached, create a new one and share it */
        if (doc == null) {
            if (_debugCache) {
                System.out.println("TLDCMDocument cache miss on " + cacheKey);
            }
            CMDocument document = loadTaglib(reference);
            if (document != null) {
                TLDCacheEntry entry = new TLDCacheEntry();
                doc = entry.document = document;
                entry.referenceCount = 1;
                entry.modificationStamp = lastModified;
                getSharedDocumentCache().put(cacheKey, entry);
            }
        }
        if (doc != null) {
            getDocuments().put(cacheKey, doc);
        }
    }
    return doc;
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference)

Example 64 with Reference

use of java.lang.ref.Reference in project rxlib by RockyLOMO.

the class WeakCache method remove.

public void remove(TK key, boolean destroy) {
    require(key);
    Reference ref = container.remove(key);
    if (ref == null) {
        return;
    }
    AutoCloseable ac;
    if (destroy && (ac = as(ref.get(), AutoCloseable.class)) != null) {
        try {
            ac.close();
        } catch (Exception ex) {
            Logger.error(ex, "Auto close error");
        }
    }
    ref.clear();
}
Also used : Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference)

Example 65 with Reference

use of java.lang.ref.Reference in project freemarker by apache.

the class BeansWrapperSingletonsTest method clearClassIntrospectorInstanceCacheReferences.

static void clearClassIntrospectorInstanceCacheReferences(boolean enqueue) {
    Map instanceCache = ClassIntrospectorBuilder.getInstanceCache();
    synchronized (instanceCache) {
        for (Iterator it = instanceCache.values().iterator(); it.hasNext(); ) {
            Reference ref = ((Reference) it.next());
            ref.clear();
            if (enqueue) {
                ref.enqueue();
            }
        }
    }
}
Also used : Reference(java.lang.ref.Reference) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Reference (java.lang.ref.Reference)74 WeakReference (java.lang.ref.WeakReference)34 SoftReference (java.lang.ref.SoftReference)33 ReferenceQueue (java.lang.ref.ReferenceQueue)13 Field (java.lang.reflect.Field)13 Map (java.util.Map)13 HashMap (java.util.HashMap)11 PhantomReference (java.lang.ref.PhantomReference)10 ArrayList (java.util.ArrayList)7 List (java.util.List)5 Tracing (brave.Tracing)3 Platform (brave.internal.Platform)3 TraceContext (brave.propagation.TraceContext)3 BufferedImage (java.awt.image.BufferedImage)3 VolatileImage (java.awt.image.VolatileImage)3 IOException (java.io.IOException)3 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)3 ConcurrentModificationException (java.util.ConcurrentModificationException)3 Iterator (java.util.Iterator)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3