KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > virtual > VirtualFile


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.virtual ;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.Serializable JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URI JavaDoc;
29 import java.net.URISyntaxException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.concurrent.atomic.AtomicBoolean JavaDoc;
35
36 import org.jboss.util.collection.WeakSet;
37 import org.jboss.virtual .plugins.vfs.helpers.FilterVirtualFileVisitor;
38 import org.jboss.virtual .plugins.vfs.helpers.MatchAllVirtualFileFilter;
39 import org.jboss.virtual .spi.VFSContext;
40 import org.jboss.virtual .spi.VirtualFileHandler;
41
42 /**
43  * A virtual file as seen by the user
44  *
45  * @author Scott.Stark@jboss.org
46  * @author adrian@jboss.org
47  * @version $Revision: 44334 $
48  */

49 public class VirtualFile implements Serializable JavaDoc
50 {
51    private static final long serialVersionUID = 1L;
52
53    /** The virtual file handler */
54    private final VirtualFileHandler handler;
55
56    /** Whether we are closed */
57    private AtomicBoolean JavaDoc closed = new AtomicBoolean JavaDoc(false);
58    
59    /** The open streams */
60    private transient final Set JavaDoc<InputStream JavaDoc> streams = Collections.synchronizedSet(new WeakSet());
61    
62    /**
63     * Create a new VirtualFile.
64     *
65     * @param handler the handler
66     * @throws IllegalArgumentException if the handler is null
67     */

68    public VirtualFile(VirtualFileHandler handler)
69    {
70       if (handler == null)
71          throw new IllegalArgumentException JavaDoc("Null handler");
72       this.handler = handler;
73    }
74
75    /**
76     * Get the virtual file handler
77     *
78     * @return the handler
79     * @throws IllegalStateException if the file is closed
80     */

81    protected VirtualFileHandler getHandler()
82    {
83       if (closed.get())
84          throw new IllegalStateException JavaDoc("The virtual file is closed");
85       return handler;
86    }
87    
88    /**
89     * Get the simple VF name (X.java)
90     *
91     * @return the simple file name
92     * @throws IllegalStateException if the file is closed
93     */

94    public String JavaDoc getName()
95    {
96       return getHandler().getName();
97    }
98
99    /**
100     * Get the VFS relative path name (org/jboss/X.java)
101     *
102     * @return the VFS relative path name
103     * @throws IllegalStateException if the file is closed
104     */

105    public String JavaDoc getPathName()
106    {
107       return getHandler().getPathName();
108    }
109
110    /**
111     * Get the VF URL (file://root/org/jboss/X.java)
112     *
113     * @return the full URL to the VF in the VFS.
114     * @throws MalformedURLException if a url cannot be parsed
115     * @throws URISyntaxException if a uri cannot be parsed
116     * @throws IllegalStateException if the file is closed
117     */

118    public URL JavaDoc toURL() throws MalformedURLException JavaDoc, URISyntaxException JavaDoc
119    {
120       return getHandler().toURL();
121    }
122    
123    /**
124     * Get the VF URI (file://root/org/jboss/X.java)
125     *
126     * @return the full URI to the VF in the VFS.
127     * @throws URISyntaxException if a uri cannot be parsed
128     * @throws IllegalStateException if the file is closed
129     */

130    public URI JavaDoc toURI() throws URISyntaxException JavaDoc
131    {
132       return getHandler().toURI();
133    }
134
135    /**
136     * When the file was last modified
137     *
138     * @return the last modified time
139     * @throws IOException for any problem accessing the virtual file system
140     * @throws IllegalStateException if the file is closed
141     */

142    public long getLastModified() throws IOException JavaDoc
143    {
144       return getHandler().getLastModified();
145    }
146    
147    /**
148     * Get the size
149     *
150     * @return the size
151     * @throws IOException for any problem accessing the virtual file system
152     * @throws IllegalStateException if the file is closed
153     */

154    public long getSize() throws IOException JavaDoc
155    {
156       return getHandler().getSize();
157    }
158
159    /**
160     * Whether it is a simple leaf of the VFS,
161     * i.e. whether it can contain other files
162     *
163     * @return true if a simple file.
164     * @throws IOException for any problem accessing the virtual file system
165     * @throws IllegalStateException if the file is closed
166     */

167    public boolean isLeaf() throws IOException JavaDoc
168    {
169       return getHandler().isLeaf();
170    }
171    
172    /**
173     * Whether it is hidden
174     *
175     * @return true when hidden
176     * @throws IOException for any problem accessing the virtual file system
177     * @throws IllegalStateException if the file is closed
178     */

179    public boolean isHidden() throws IOException JavaDoc
180    {
181       return getHandler().isHidden();
182    }
183
184    /**
185     * Access the file contents.
186     *
187     * @return an InputStream for the file contents.
188     * @throws IOException for any error accessing the file system
189     * @throws IllegalStateException if the file is closed
190     */

191    public InputStream JavaDoc openStream() throws IOException JavaDoc
192    {
193       InputStream JavaDoc result = getHandler().openStream();
194       streams.add(result);
195       return result;
196    }
197
198    /**
199     * Close the streams
200     */

201    public void closeStreams()
202    {
203       // Close the streams
204
for (InputStream JavaDoc stream : streams)
205       {
206          if (stream != null)
207          {
208             try
209             {
210                stream.close();
211             }
212             catch (IOException JavaDoc ignored)
213             {
214             }
215          }
216       }
217       streams.clear();
218    }
219
220    /**
221     * Close the file resources (stream, etc.)
222     */

223    public void close()
224    {
225       if (closed.getAndSet(true) == false)
226       {
227          closeStreams();
228          handler.close();
229       }
230    }
231    
232    /**
233     * Get the VFS instance for this virtual file
234     *
235     * @return the VFS
236     * @throws IllegalStateException if the file is closed
237     */

238    public VFS getVFS()
239    {
240       VFSContext context = getHandler().getVFSContext();
241       return context.getVFS();
242    }
243    
244    /**
245     * Get the parent
246     *
247     * @return the parent or null if there is no parent
248     * @throws IOException for any problem accessing the virtual file system
249     * @throws IllegalStateException if the file is closed
250     */

251    public VirtualFile getParent() throws IOException JavaDoc
252    {
253       VirtualFileHandler parent = getHandler().getParent();
254       if (parent != null)
255          return parent.getVirtualFile();
256       return null;
257    }
258    
259    /**
260     * Get the children
261     *
262     * @return the children
263     * @throws IOException for any problem accessing the virtual file system
264     * @throws IllegalStateException if the file is closed
265     */

266    public List JavaDoc<VirtualFile> getChildren() throws IOException JavaDoc
267    {
268       return getChildren(null);
269    }
270
271    /**
272     * Get the children
273     *
274     * @param filter to filter the children
275     * @return the children
276     * @throws IOException for any problem accessing the virtual file system
277     * @throws IllegalStateException if the file is closed or it is a leaf node
278     */

279    public List JavaDoc<VirtualFile> getChildren(VirtualFileFilter filter) throws IOException JavaDoc
280    {
281       if (isLeaf())
282          throw new IllegalStateException JavaDoc("File cannot contain children: " + this);
283
284       if (filter == null)
285          filter = MatchAllVirtualFileFilter.INSTANCE;
286       FilterVirtualFileVisitor visitor = new FilterVirtualFileVisitor(filter, null);
287       visit(visitor);
288       return visitor.getMatched();
289    }
290    
291    /**
292     * Get all the children recursively<p>
293     *
294     * This always uses {@link VisitorAttributes#RECURSE}
295     *
296     * @return the children
297     * @throws IOException for any problem accessing the virtual file system
298     * @throws IllegalStateException if the file is closed
299     */

300    public List JavaDoc<VirtualFile> getChildrenRecursively() throws IOException JavaDoc
301    {
302       return getChildrenRecursively(null);
303    }
304    
305    /**
306     * Get all the children recursively<p>
307     *
308     * This always uses {@link VisitorAttributes#RECURSE}
309     *
310     * @param filter to filter the children
311     * @return the children
312     * @throws IOException for any problem accessing the virtual file system
313     * @throws IllegalStateException if the file is closed or it is a leaf node
314     */

315    public List JavaDoc<VirtualFile> getChildrenRecursively(VirtualFileFilter filter) throws IOException JavaDoc
316    {
317       if (isLeaf())
318          throw new IllegalStateException JavaDoc("File cannot contain children: " + this);
319
320       if (filter == null)
321          filter = MatchAllVirtualFileFilter.INSTANCE;
322       FilterVirtualFileVisitor visitor = new FilterVirtualFileVisitor(filter, VisitorAttributes.RECURSE);
323       visit(visitor);
324       return visitor.getMatched();
325    }
326    
327    /**
328     * Visit the virtual file system
329     *
330     * @param visitor the visitor
331     * @throws IOException for any problem accessing the virtual file system
332     * @throws IllegalArgumentException if the visitor is null
333     * @throws IllegalStateException if the file is closed or it is a leaf node
334     */

335    public void visit(VirtualFileVisitor visitor) throws IOException JavaDoc
336    {
337       if (isLeaf())
338          throw new IllegalStateException JavaDoc("File cannot contain children: " + this);
339
340       getVFS().visit(this, visitor);
341    }
342
343    /**
344     * Find a child
345     *
346     * @param path the path
347     * @return the child
348     * @throws IOException for any problem accessing the VFS (including the child does not exist)
349     * @throws IllegalArgumentException if the path is null
350     * @throws IllegalStateException if the file is closed or it is a leaf node
351     */

352    public VirtualFile findChild(String JavaDoc path) throws IOException JavaDoc
353    {
354       VirtualFileHandler handler = getHandler();
355       
356       if (handler.isLeaf())
357          throw new IllegalStateException JavaDoc("File cannot contain children: " + this);
358
359       path = VFSUtils.fixName(path);
360       VirtualFileHandler child = handler.findChild(path);
361       return child.getVirtualFile();
362    }
363
364    @Override JavaDoc
365    public String JavaDoc toString()
366    {
367       return handler.toString();
368    }
369
370    @Override JavaDoc
371    public int hashCode()
372    {
373       return handler.hashCode();
374    }
375    
376    @Override JavaDoc
377    public boolean equals(Object JavaDoc obj)
378    {
379       if (obj == this)
380          return true;
381       if (obj == null || obj instanceof VirtualFile == false)
382          return false;
383       VirtualFile other = (VirtualFile) obj;
384       return handler.equals(other.handler);
385    }
386    
387    @Override JavaDoc
388    protected void finalize() throws Throwable JavaDoc
389    {
390       close();
391    }
392 }
393
Popular Tags