KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > filebasedfs > fileobjects > FileObj


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.masterfs.filebasedfs.fileobjects;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Enumeration JavaDoc;
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 /**
43  * @author rm111737
44  */

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 JavaDoc file, final FileNaming name) {
52         super(file, name);
53         setLastModified(System.currentTimeMillis());
54     }
55
56     public OutputStream JavaDoc getOutputStream(final FileLock lock) throws IOException JavaDoc {
57         return getOutputStream(lock, null, null);
58     }
59     
60     public OutputStream JavaDoc getOutputStream(final FileLock lock, ProvidedExtensions extensions, FileObject mfo) throws IOException JavaDoc {
61         final File JavaDoc 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 JavaDoc retVal = null;
69         try {
70             retVal = new FileOutputStream JavaDoc(f) {
71                                 public void close() throws IOException JavaDoc {
72                                     if (!closable.isClosed()) {
73                                         super.close();
74                                         closable.close();
75                                         setLastModified(f.lastModified());
76                                         fireFileChangedEvent(false);
77                                     }
78                                 }
79                             };
80         } catch (FileNotFoundException JavaDoc e) {
81             if (closable != null) {
82                 closable.close();
83             }
84             FileNotFoundException JavaDoc fex = e;
85             if (!f.exists()) {
86                 fex = (FileNotFoundException JavaDoc)new FileNotFoundException JavaDoc(e.getLocalizedMessage()).initCause(e);
87             } else if (!f.canWrite()) {
88                 fex = (FileNotFoundException JavaDoc)new FileNotFoundException JavaDoc(e.getLocalizedMessage()).initCause(e);
89             } else if (f.getParentFile() == null) {
90                 fex = (FileNotFoundException JavaDoc)new FileNotFoundException JavaDoc(e.getLocalizedMessage()).initCause(e);
91             } else if (!f.getParentFile().exists()) {
92                 fex = (FileNotFoundException JavaDoc)new FileNotFoundException JavaDoc(e.getLocalizedMessage()).initCause(e);
93             }
94             FSException.annotateException(fex);
95             throw fex;
96         }
97         return retVal;
98     }
99
100     public InputStream JavaDoc getInputStream() throws FileNotFoundException JavaDoc {
101         final File JavaDoc f = getFileName().getFile();
102                         
103         InputStream JavaDoc 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 JavaDoc(f) {
110                 public void close() throws IOException JavaDoc {
111                     super.close();
112                     closable.close();
113                 }
114             };
115         } catch (IOException JavaDoc e) {
116             if (closeableReference != null) {
117                 closeableReference.close();
118             }
119             
120             FileNotFoundException JavaDoc fex = null;
121             if (!f.exists()) {
122                 fex = (FileNotFoundException JavaDoc)new FileNotFoundException JavaDoc(e.getLocalizedMessage()).initCause(e);
123             } else if (!f.canRead()) {
124                 fex = (FileNotFoundException JavaDoc)new FileNotFoundException JavaDoc(e.getLocalizedMessage()).initCause(e);
125             } else if (f.getParentFile() == null) {
126                 fex = (FileNotFoundException JavaDoc)new FileNotFoundException JavaDoc(e.getLocalizedMessage()).initCause(e);
127             } else if (!f.getParentFile().exists()) {
128                 fex = (FileNotFoundException JavaDoc)new FileNotFoundException JavaDoc(e.getLocalizedMessage()).initCause(e);
129             } else if ((new FileInfo(f)).isUnixSpecialFile()) {
130                 fex = (FileNotFoundException JavaDoc) new FileNotFoundException JavaDoc(e.toString()).initCause(e);
131             } else {
132                 fex = (FileNotFoundException JavaDoc) new FileNotFoundException JavaDoc(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 JavaDoc lastModified() {
142         final File JavaDoc f = getFileName().getFile();
143         return new Date JavaDoc(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 JavaDoc name) throws IOException JavaDoc {
155         throw new IOException JavaDoc(getPath());//isn't directory - cannot create neither file nor folder
156
}
157
158     public final FileObject createData(final String JavaDoc name, final String JavaDoc ext) throws IOException JavaDoc {
159         throw new IOException JavaDoc(getPath());//isn't directory - cannot create neither file nor folder
160
}
161
162
163     public final FileObject[] getChildren() {
164         return new FileObject[]{};//isn't directory - no children
165
}
166
167     public final FileObject getFileObject(final String JavaDoc name, final String JavaDoc ext) {
168         return null;
169     }
170
171     public boolean isValid() {
172         //0 - because java.io.File.lastModififed returns 0 for not existing files
173
return lastModified != 0;
174     }
175
176     protected void setValid(boolean valid) {
177         if (valid) {
178             //I can't make valid fileobject when it was one invalidated
179
assert isValid() : this.toString();
180         } else {
181             //0 - because java.io.File.lastModififed returns 0 for not existing files
182
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                 //fileobject is invalidated
206
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 JavaDoc getChildren(final boolean rec) {
235         return Enumerations.empty();
236     }
237
238     public final Enumeration JavaDoc getFolders(final boolean rec) {
239         return Enumerations.empty();
240     }
241
242     public final Enumeration JavaDoc getData(final boolean rec) {
243         return Enumerations.empty();
244     }
245
246
247     public final FileLock lock() throws IOException JavaDoc {
248         final File JavaDoc 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 JavaDoc ex) {
257             FileNotFoundException JavaDoc fex = ex;
258             if (!me.exists()) {
259                 fex = (FileNotFoundException JavaDoc)new FileNotFoundException JavaDoc(ex.getLocalizedMessage()).initCause(ex);
260             } else if (!me.canRead()) {
261                 fex = (FileNotFoundException JavaDoc)new FileNotFoundException JavaDoc(ex.getLocalizedMessage()).initCause(ex);
262             } else if (!me.canWrite()) {
263                 fex = (FileNotFoundException JavaDoc)new FileNotFoundException JavaDoc(ex.getLocalizedMessage()).initCause(ex);
264             } else if (me.getParentFile() == null) {
265                 fex = (FileNotFoundException JavaDoc)new FileNotFoundException JavaDoc(ex.getLocalizedMessage()).initCause(ex);
266             } else if (!me.getParentFile().exists()) {
267                 fex = (FileNotFoundException JavaDoc)new FileNotFoundException JavaDoc(ex.getLocalizedMessage()).initCause(ex);
268             }
269             FSException.annotateException(fex);
270             throw fex;
271         }
272     }
273
274     final boolean checkLock(final FileLock lock) throws IOException JavaDoc {
275         final File JavaDoc f = getFileName().getFile();
276         return ((lock instanceof WriteLock) && (((WriteLock) lock).isValid(f)));
277     }
278
279     public void rename(final FileLock lock, final String JavaDoc name, final String JavaDoc ext, ProvidedExtensions.IOHandler handler) throws IOException JavaDoc {
280         super.rename(lock, name, ext, handler);
281         setLastModified(getFileName().getFile().lastModified());
282     }
283 }
284
Popular Tags