KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > virtual > plugins > context > file > FileHandler


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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.plugins.context.file;
23
24 import java.io.File JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.net.URI JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.List JavaDoc;
33
34 import org.jboss.virtual.plugins.context.AbstractURLHandler;
35 import org.jboss.virtual.plugins.context.StructuredVirtualFileHandler;
36 import org.jboss.virtual.plugins.context.jar.JarUtils;
37 import org.jboss.virtual.spi.VirtualFileHandler;
38
39 /**
40  * FileHandler.
41  *
42  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
43  * @author Scott.Stark@jboss.org
44  * @version $Revision: 1.1 $
45  */

46 public class FileHandler extends AbstractURLHandler
47    implements StructuredVirtualFileHandler
48 {
49    private static final long serialVersionUID = 1;
50    /** The file */
51    private transient File JavaDoc file;
52    
53    /**
54     * Create a new FileHandler.
55     *
56     * @param context the context
57     * @param parent the parent
58     * @param file the file
59     * @param url the url
60     * @throws IOException for an error accessing the file system
61     * @throws IllegalArgumentException for a null context, url
62     */

63    public FileHandler(FileSystemContext context, VirtualFileHandler parent, File JavaDoc file, URL JavaDoc url) throws IOException JavaDoc
64    {
65       super(context, parent, url, file.getName());
66
67       this.file = file;
68       if (file.exists() == false)
69          throw new FileNotFoundException JavaDoc("File does not exist: " + file.getCanonicalPath());
70    }
71    /**
72     * Create a new FileHandler
73     *
74     * @param context the context
75     * @param parent the parent
76     * @param file the file
77     * @param uri the uri
78     * @throws IOException for an error accessing the file system
79     * @throws IllegalArgumentException for a null context, uri
80     */

81    public FileHandler(FileSystemContext context, VirtualFileHandler parent, File JavaDoc file, URI JavaDoc uri) throws IOException JavaDoc
82    {
83       this(context, parent, file, uri.toURL());
84    }
85
86    @Override JavaDoc
87    public FileSystemContext getVFSContext()
88    {
89       return (FileSystemContext) super.getVFSContext();
90    }
91    
92    /**
93     * Get the file for this file handler
94     *
95     * @return the file
96     */

97    protected File JavaDoc getFile()
98    {
99       checkClosed();
100       return file;
101    }
102    
103    @Override JavaDoc
104    public long getLastModified()
105    {
106       return getFile().lastModified();
107    }
108
109    @Override JavaDoc
110    public long getSize()
111    {
112       return getFile().length();
113    }
114
115    public boolean isLeaf()
116    {
117       return getFile().isFile();
118    }
119
120    public boolean isHidden()
121    {
122       return getFile().isHidden();
123    }
124
125    public List JavaDoc<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException JavaDoc
126    {
127       File JavaDoc parent = getFile();
128       File JavaDoc[] files = parent.listFiles();
129       if (files == null)
130          throw new IOException JavaDoc("Error listing files: " + parent.getCanonicalPath());
131       if (files.length == 0)
132          return Collections.emptyList();
133
134       FileSystemContext context = getVFSContext();
135       
136       List JavaDoc<VirtualFileHandler> result = new ArrayList JavaDoc<VirtualFileHandler>();
137       for (File JavaDoc file : files)
138       {
139          try
140          {
141             VirtualFileHandler handler = context.createVirtualFileHandler(this, file);
142             result.add(handler);
143          }
144          catch (IOException JavaDoc e)
145          {
146             if (ignoreErrors)
147                log.trace("Ignored: " + e);
148             else
149                throw e;
150          }
151       }
152       return result;
153    }
154
155    public VirtualFileHandler findChild(String JavaDoc path) throws IOException JavaDoc
156    {
157       return structuredFindChild(path);
158    }
159
160    public VirtualFileHandler createChildHandler(String JavaDoc name) throws IOException JavaDoc
161    {
162       FileSystemContext context = getVFSContext();
163       File JavaDoc parentFile = getFile();
164       File JavaDoc child = new File JavaDoc(parentFile, name);
165       return context.createVirtualFileHandler(this, child);
166    }
167
168    private void readObject(ObjectInputStream JavaDoc in)
169       throws IOException JavaDoc, ClassNotFoundException JavaDoc
170    {
171       in.defaultReadObject();
172       // Initialize the transient values
173
this.file = new File JavaDoc(getURL().getPath());
174    }
175
176 }
177
Popular Tags