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;
}
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;
}
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;
}
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();
}
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();
}
}
}
}
Aggregations