KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > DelegateFileObject


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.provider;
17
18 import org.apache.commons.vfs.FileChangeEvent;
19 import org.apache.commons.vfs.FileListener;
20 import org.apache.commons.vfs.FileName;
21 import org.apache.commons.vfs.FileObject;
22 import org.apache.commons.vfs.FileSystemException;
23 import org.apache.commons.vfs.FileType;
24
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.security.cert.Certificate JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31
32 /**
33  * A file backed by another file.
34  *
35  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
36  * @author Gary D. Gregory
37  * @todo Extract subclass that overlays the children
38  */

39 public class DelegateFileObject
40     extends AbstractFileObject
41     implements FileListener
42 {
43     private FileObject file;
44     private final Set JavaDoc children = new HashSet JavaDoc();
45     private boolean ignoreEvent;
46
47     public DelegateFileObject(final FileName name,
48                               final AbstractFileSystem fileSystem,
49                               final FileObject file) throws FileSystemException
50     {
51         super(name, fileSystem);
52         this.file = file;
53         if (file != null)
54         {
55             file.getFileSystem().addListener(file, this);
56         }
57     }
58
59     /**
60      * Adds a child to this file.
61      */

62     public void attachChild(final FileName baseName, final FileType type) throws Exception JavaDoc
63     {
64         final FileType oldType = doGetType();
65         if (children.add(baseName))
66         {
67             childrenChanged(baseName, type);
68         }
69         maybeTypeChanged(oldType);
70     }
71
72     /**
73      * Attaches or detaches the target file.
74      */

75     public void setFile(final FileObject file) throws Exception JavaDoc
76     {
77         final FileType oldType = doGetType();
78
79         if (file != null)
80         {
81             file.getFileSystem().addListener(file, this);
82         }
83         this.file = file;
84         maybeTypeChanged(oldType);
85     }
86
87     /**
88      * Checks whether the file's type has changed, and fires the appropriate
89      * events.
90      */

91     private void maybeTypeChanged(final FileType oldType) throws Exception JavaDoc
92     {
93         final FileType newType = doGetType();
94         if (oldType == FileType.IMAGINARY && newType != FileType.IMAGINARY)
95         {
96             handleCreate(newType);
97         }
98         else if (oldType != FileType.IMAGINARY && newType == FileType.IMAGINARY)
99         {
100             handleDelete();
101         }
102     }
103
104     /**
105      * Determines the type of the file, returns null if the file does not
106      * exist.
107      */

108     protected FileType doGetType() throws FileSystemException
109     {
110         if (file != null)
111         {
112             return file.getType();
113         }
114         else if (children.size() > 0)
115         {
116             return FileType.FOLDER;
117         }
118         else
119         {
120             return FileType.IMAGINARY;
121         }
122     }
123
124     /**
125      * Determines if this file can be read.
126      */

127     protected boolean doIsReadable() throws FileSystemException
128     {
129         if (file != null)
130         {
131             return file.isReadable();
132         }
133         else
134         {
135             return true;
136         }
137     }
138
139     /**
140      * Determines if this file can be written to.
141      */

142     protected boolean doIsWriteable() throws FileSystemException
143     {
144         if (file != null)
145         {
146             return file.isWriteable();
147         }
148         else
149         {
150             return false;
151         }
152     }
153
154     /**
155      * Determines if this file is hidden.
156      */

157     protected boolean doIsHidden() throws FileSystemException
158     {
159         if (file != null)
160         {
161             return file.isHidden();
162         }
163         else
164         {
165             return false;
166         }
167     }
168
169     /**
170      * Lists the children of the file.
171      */

172     protected String JavaDoc[] doListChildren() throws Exception JavaDoc
173     {
174         if (file != null)
175         {
176             final FileObject[] children = file.getChildren();
177             final String JavaDoc[] childNames = new String JavaDoc[children.length];
178             for (int i = 0; i < children.length; i++)
179             {
180                 childNames[i] = children[i].getName().getBaseName();
181             }
182             return childNames;
183         }
184         else
185         {
186             return (String JavaDoc[]) children.toArray(new String JavaDoc[children.size()]);
187         }
188     }
189
190     /**
191      * Creates this file as a folder.
192      */

193     protected void doCreateFolder() throws Exception JavaDoc
194     {
195         ignoreEvent = true;
196         try
197         {
198             file.createFolder();
199         }
200         finally
201         {
202             ignoreEvent = false;
203         }
204     }
205
206     /**
207      * Deletes the file.
208      */

209     protected void doDelete() throws Exception JavaDoc
210     {
211         ignoreEvent = true;
212         try
213         {
214             file.delete();
215         }
216         finally
217         {
218             ignoreEvent = false;
219         }
220     }
221
222     /**
223      * Returns the size of the file content (in bytes). Is only called if
224      * {@link #doGetType} returns {@link FileType#FILE}.
225      */

226     protected long doGetContentSize() throws Exception JavaDoc
227     {
228         return file.getContent().getSize();
229     }
230
231     /**
232      * Returns the attributes of this file.
233      */

234     protected Map JavaDoc doGetAttributes()
235         throws Exception JavaDoc
236     {
237         return file.getContent().getAttributes();
238     }
239
240     /**
241      * Sets an attribute of this file.
242      */

243     protected void doSetAttribute(final String JavaDoc atttrName,
244                                   final Object JavaDoc value)
245         throws Exception JavaDoc
246     {
247         file.getContent().setAttribute(atttrName, value);
248     }
249
250     /**
251      * Returns the certificates of this file.
252      */

253     protected Certificate JavaDoc[] doGetCertificates() throws Exception JavaDoc
254     {
255         return file.getContent().getCertificates();
256     }
257
258     /**
259      * Returns the last-modified time of this file.
260      */

261     protected long doGetLastModifiedTime() throws Exception JavaDoc
262     {
263         return file.getContent().getLastModifiedTime();
264     }
265
266     /**
267      * Sets the last-modified time of this file.
268      */

269     protected void doSetLastModifiedTime(final long modtime)
270         throws Exception JavaDoc
271     {
272         file.getContent().setLastModifiedTime(modtime);
273     }
274
275     /**
276      * Creates an input stream to read the file content from.
277      */

278     protected InputStream JavaDoc doGetInputStream() throws Exception JavaDoc
279     {
280         return file.getContent().getInputStream();
281     }
282
283     /**
284      * Creates an output stream to write the file content to.
285      */

286     protected OutputStream JavaDoc doGetOutputStream(boolean bAppend) throws Exception JavaDoc
287     {
288         return file.getContent().getOutputStream(bAppend);
289     }
290
291     /**
292      * Called when a file is created.
293      */

294     public void fileCreated(final FileChangeEvent event) throws Exception JavaDoc
295     {
296         if (!ignoreEvent)
297         {
298             handleCreate(file.getType());
299         }
300     }
301
302     /**
303      * Called when a file is deleted.
304      */

305     public void fileDeleted(final FileChangeEvent event) throws Exception JavaDoc
306     {
307         if (!ignoreEvent)
308         {
309             handleDelete();
310         }
311     }
312
313     /**
314      * Called when a file is changed.
315      * <p/>
316      * This will only happen if you monitor the file using {@link org.apache.commons.vfs.FileMonitor}.
317      */

318     public void fileChanged(FileChangeEvent event) throws Exception JavaDoc
319     {
320         if (!ignoreEvent)
321         {
322             handleChanged();
323         }
324     }
325
326     /**
327      * Close the delegated file
328      */

329     public void close() throws FileSystemException
330     {
331         super.close();
332
333         if (file != null)
334         {
335             file.close();
336         }
337     }
338 }
339
Popular Tags