1 11 package org.eclipse.team.core.variants; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 16 import org.eclipse.core.resources.*; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.osgi.util.NLS; 19 import org.eclipse.team.core.TeamException; 20 import org.eclipse.team.internal.core.*; 21 22 49 public abstract class CachedResourceVariant extends PlatformObject implements IResourceVariant { 50 51 private IStorage storage; 53 54 58 class ResourceVariantStorage implements IEncodedStorage { 59 public InputStream getContents() throws CoreException { 60 if (!isContentsCached()) { 61 throw new TeamException(NLS.bind(Messages.CachedResourceVariant_0, new String [] { getCachePath() })); 64 } 65 return getCachedContents(); 66 } 67 public IPath getFullPath() { 68 return getDisplayPath(); 69 } 70 public String getName() { 71 return CachedResourceVariant.this.getName(); 72 } 73 public boolean isReadOnly() { 74 return true; 75 } 76 public Object getAdapter(Class adapter) { 77 return CachedResourceVariant.this.getAdapter(adapter); 78 } 79 public String getCharset() throws CoreException { 80 InputStream contents = getContents(); 81 try { 82 String charSet = TeamPlugin.getCharset(getName(), contents); 83 return charSet; 84 } catch (IOException e) { 85 throw new TeamException(new Status(IStatus.ERROR, TeamPlugin.ID, IResourceStatus.FAILED_DESCRIBING_CONTENTS, NLS.bind(Messages.CachedResourceVariant_1, new String [] { getFullPath().toString() }), e)); 86 } finally { 87 try { 88 contents.close(); 89 } catch (IOException e1) { 90 } 92 } 93 } 94 } 95 96 99 public IStorage getStorage(IProgressMonitor monitor) throws TeamException { 100 if (isContainer()) return null; 101 ensureContentsCached(monitor); 102 if (storage == null) { 103 storage = new ResourceVariantStorage(); 104 } 105 return storage; 106 } 107 108 private void ensureContentsCached(IProgressMonitor monitor) throws TeamException { 109 if (!isContentsCached()) { 111 fetchContents(monitor); 112 } 113 } 114 115 123 protected abstract void fetchContents(IProgressMonitor monitor) throws TeamException; 124 125 134 protected void setContents(InputStream stream, IProgressMonitor monitor) throws TeamException { 135 Assert.isTrue(!isContainer()); 137 if (!isHandleCached()) cacheHandle(); 138 getCacheEntry().setContents(stream, monitor); 139 } 140 141 private ResourceVariantCacheEntry getCacheEntry() { 142 return getCache().getCacheEntry(this.getCachePath()); 143 } 144 145 155 public boolean isContentsCached() { 156 if (isContainer() || !isHandleCached()) { 157 return false; 158 } 159 ResourceVariantCacheEntry entry = getCache().getCacheEntry(getCachePath()); 160 return entry.getState() == ResourceVariantCacheEntry.READY; 161 } 162 163 172 protected InputStream getCachedContents() throws TeamException { 173 if (isContainer() || !isContentsCached()) return null; 174 return getCache().getCacheEntry(getCachePath()).getContents(); 175 } 176 177 188 protected boolean isHandleCached() { 189 return (getCache().hasEntry(getCachePath())); 190 } 191 192 203 protected abstract String getCachePath(); 204 205 212 public long getSize() { 213 if (isContainer() || !isContentsCached()) return 0; 214 ResourceVariantCacheEntry entry = getCacheEntry(); 215 if (entry == null || entry.getState() != ResourceVariantCacheEntry.READY) { 216 return 0; 217 } 218 return entry.getSize(); 219 } 220 221 225 private ResourceVariantCache getCache() { 226 ResourceVariantCache.enableCaching(getCacheId()); 227 return ResourceVariantCache.getCache(getCacheId()); 228 } 229 230 237 protected abstract String getCacheId(); 238 239 248 protected CachedResourceVariant getCachedHandle() { 249 ResourceVariantCacheEntry entry = getCacheEntry(); 250 if (entry == null) return null; 251 return entry.getResourceVariant(); 252 } 253 254 266 protected void cacheHandle() { 267 getCache().add(getCachePath(), this); 268 } 269 270 280 public IPath getDisplayPath() { 281 return new Path(null, getCachePath()); 282 } 283 284 } 285 | Popular Tags |