1 19 20 package org.netbeans.modules.masterfs.filebasedfs.fileobjects; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.util.Date ; 30 import java.util.Enumeration ; 31 import org.netbeans.modules.masterfs.filebasedfs.Statistics; 32 import org.netbeans.modules.masterfs.filebasedfs.children.ChildrenCache; 33 import org.netbeans.modules.masterfs.filebasedfs.naming.FileNaming; 34 import org.netbeans.modules.masterfs.filebasedfs.utils.FSException; 35 import org.netbeans.modules.masterfs.filebasedfs.utils.FileInfo; 36 import org.netbeans.modules.masterfs.providers.ProvidedExtensions; 37 import org.openide.filesystems.FileLock; 38 import org.openide.filesystems.FileObject; 39 import org.openide.util.Enumerations; 40 import org.openide.util.Mutex; 41 42 45 public class FileObj extends BaseFileObj { 46 static final long serialVersionUID = -1133540210876356809L; 47 private long lastModified = -1; 48 private boolean realLastModifiedCached; 49 50 51 FileObj(final File file, final FileNaming name) { 52 super(file, name); 53 setLastModified(System.currentTimeMillis()); 54 } 55 56 public OutputStream getOutputStream(final FileLock lock) throws IOException { 57 return getOutputStream(lock, null, null); 58 } 59 60 public OutputStream getOutputStream(final FileLock lock, ProvidedExtensions extensions, FileObject mfo) throws IOException { 61 final File f = getFileName().getFile(); 62 63 if (extensions != null) { 64 extensions.beforeChange(mfo); 65 } 66 final MutualExclusionSupport.Closeable closable = MutualExclusionSupport.getDefault().addResource(this, false); 67 68 FileOutputStream retVal = null; 69 try { 70 retVal = new FileOutputStream (f) { 71 public void close() throws IOException { 72 if (!closable.isClosed()) { 73 super.close(); 74 closable.close(); 75 setLastModified(f.lastModified()); 76 fireFileChangedEvent(false); 77 } 78 } 79 }; 80 } catch (FileNotFoundException e) { 81 if (closable != null) { 82 closable.close(); 83 } 84 FileNotFoundException fex = e; 85 if (!f.exists()) { 86 fex = (FileNotFoundException )new FileNotFoundException (e.getLocalizedMessage()).initCause(e); 87 } else if (!f.canWrite()) { 88 fex = (FileNotFoundException )new FileNotFoundException (e.getLocalizedMessage()).initCause(e); 89 } else if (f.getParentFile() == null) { 90 fex = (FileNotFoundException )new FileNotFoundException (e.getLocalizedMessage()).initCause(e); 91 } else if (!f.getParentFile().exists()) { 92 fex = (FileNotFoundException )new FileNotFoundException (e.getLocalizedMessage()).initCause(e); 93 } 94 FSException.annotateException(fex); 95 throw fex; 96 } 97 return retVal; 98 } 99 100 public InputStream getInputStream() throws FileNotFoundException { 101 final File f = getFileName().getFile(); 102 103 InputStream inputStream; 104 MutualExclusionSupport.Closeable closeableReference = null; 105 106 try { 107 final MutualExclusionSupport.Closeable closable = MutualExclusionSupport.getDefault().addResource(this, true); 108 closeableReference = closable; 109 inputStream = new FileInputStream (f) { 110 public void close() throws IOException { 111 super.close(); 112 closable.close(); 113 } 114 }; 115 } catch (IOException e) { 116 if (closeableReference != null) { 117 closeableReference.close(); 118 } 119 120 FileNotFoundException fex = null; 121 if (!f.exists()) { 122 fex = (FileNotFoundException )new FileNotFoundException (e.getLocalizedMessage()).initCause(e); 123 } else if (!f.canRead()) { 124 fex = (FileNotFoundException )new FileNotFoundException (e.getLocalizedMessage()).initCause(e); 125 } else if (f.getParentFile() == null) { 126 fex = (FileNotFoundException )new FileNotFoundException (e.getLocalizedMessage()).initCause(e); 127 } else if (!f.getParentFile().exists()) { 128 fex = (FileNotFoundException )new FileNotFoundException (e.getLocalizedMessage()).initCause(e); 129 } else if ((new FileInfo(f)).isUnixSpecialFile()) { 130 fex = (FileNotFoundException ) new FileNotFoundException (e.toString()).initCause(e); 131 } else { 132 fex = (FileNotFoundException ) new FileNotFoundException (e.toString()).initCause(e); 133 } 134 FSException.annotateException(fex); 135 throw fex; 136 } 137 assert inputStream != null; 138 return inputStream; 139 } 140 141 public final Date lastModified() { 142 final File f = getFileName().getFile(); 143 return new Date (f.lastModified()); 144 } 145 146 private final void setLastModified(long lastModified) { 147 if (this.lastModified != -1 && !realLastModifiedCached) { 148 realLastModifiedCached = true; 149 } 150 this.lastModified = lastModified; 151 } 152 153 154 public final FileObject createFolder(final String name) throws IOException { 155 throw new IOException (getPath()); } 157 158 public final FileObject createData(final String name, final String ext) throws IOException { 159 throw new IOException (getPath()); } 161 162 163 public final FileObject[] getChildren() { 164 return new FileObject[]{}; } 166 167 public final FileObject getFileObject(final String name, final String ext) { 168 return null; 169 } 170 171 public boolean isValid() { 172 return lastModified != 0; 174 } 175 176 protected void setValid(boolean valid) { 177 if (valid) { 178 assert isValid() : this.toString(); 180 } else { 181 lastModified = 0; 183 } 184 } 185 186 public final boolean isFolder() { 187 return false; 188 } 189 190 public void refresh(final boolean expected, boolean fire) { 191 Statistics.StopWatch stopWatch = Statistics.getStopWatch(Statistics.REFRESH_FILE); 192 stopWatch.start(); 193 if (isValid()) { 194 final long oldLastModified = lastModified; 195 boolean isReal = realLastModifiedCached; 196 setLastModified(getFileName().getFile().lastModified()); 197 boolean isModified = (isReal) ? 198 (oldLastModified != lastModified) : (oldLastModified < lastModified); 199 if (fire && oldLastModified != -1 && lastModified != -1 && lastModified != 0 && isModified) { 200 fireFileChangedEvent(expected); 201 } 202 203 boolean validityFlag = getFileName().getFile().exists(); 204 if (!validityFlag) { 205 FolderObj parent = getExistingParent(); 207 if (parent != null) { 208 ChildrenCache childrenCache = parent.getChildrenCache(); 209 final Mutex.Privileged mutexPrivileged = (childrenCache != null) ? childrenCache.getMutexPrivileged() : null; 210 if (mutexPrivileged != null) mutexPrivileged.enterWriteAccess(); 211 try { 212 childrenCache.getChild(getFileName().getFile().getName(),true); 213 } finally { 214 if (mutexPrivileged != null) mutexPrivileged.exitWriteAccess(); 215 } 216 217 } 218 setValid(false); 219 if (fire) { 220 fireFileDeletedEvent(expected); 221 } 222 } 223 } 224 stopWatch.stop(); 225 } 226 227 public final void refresh(final boolean expected) { 228 refresh(expected, true); 229 } 230 231 232 233 234 public final Enumeration getChildren(final boolean rec) { 235 return Enumerations.empty(); 236 } 237 238 public final Enumeration getFolders(final boolean rec) { 239 return Enumerations.empty(); 240 } 241 242 public final Enumeration getData(final boolean rec) { 243 return Enumerations.empty(); 244 } 245 246 247 public final FileLock lock() throws IOException { 248 final File me = getFileName().getFile(); 249 try { 250 boolean lightWeightLock = false; 251 BaseFileObj bfo = getExistingParent(); 252 if (bfo instanceof FolderObj) { 253 lightWeightLock = ((FolderObj)bfo).isLightWeightLockRequired(); 254 } 255 return WriteLockFactory.tryLock(me, lightWeightLock); 256 } catch (FileNotFoundException ex) { 257 FileNotFoundException fex = ex; 258 if (!me.exists()) { 259 fex = (FileNotFoundException )new FileNotFoundException (ex.getLocalizedMessage()).initCause(ex); 260 } else if (!me.canRead()) { 261 fex = (FileNotFoundException )new FileNotFoundException (ex.getLocalizedMessage()).initCause(ex); 262 } else if (!me.canWrite()) { 263 fex = (FileNotFoundException )new FileNotFoundException (ex.getLocalizedMessage()).initCause(ex); 264 } else if (me.getParentFile() == null) { 265 fex = (FileNotFoundException )new FileNotFoundException (ex.getLocalizedMessage()).initCause(ex); 266 } else if (!me.getParentFile().exists()) { 267 fex = (FileNotFoundException )new FileNotFoundException (ex.getLocalizedMessage()).initCause(ex); 268 } 269 FSException.annotateException(fex); 270 throw fex; 271 } 272 } 273 274 final boolean checkLock(final FileLock lock) throws IOException { 275 final File f = getFileName().getFile(); 276 return ((lock instanceof WriteLock) && (((WriteLock) lock).isValid(f))); 277 } 278 279 public void rename(final FileLock lock, final String name, final String ext, ProvidedExtensions.IOHandler handler) throws IOException { 280 super.rename(lock, name, ext, handler); 281 setLastModified(getFileName().getFile().lastModified()); 282 } 283 } 284 | Popular Tags |