KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileInputStream JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.net.URI JavaDoc;
29 import java.net.URISyntaxException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 import org.jboss.virtual.VFSUtils;
35 import org.jboss.virtual.VirtualFile;
36 import org.jboss.virtual.plugins.context.AbstractVFSContext;
37 import org.jboss.virtual.plugins.context.jar.JarHandler;
38 import org.jboss.virtual.plugins.context.jar.JarUtils;
39 import org.jboss.virtual.spi.LinkInfo;
40 import org.jboss.virtual.spi.VirtualFileHandler;
41
42 /**
43  * FileSystemContext.
44  *
45  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
46  * @version $Revision: 1.1 $
47  */

48 public class FileSystemContext extends AbstractVFSContext
49 {
50    /** The root file */
51    private final VirtualFileHandler root;
52    
53    /** A reference to the virtual file of the root to stop it getting closed */
54    private final VirtualFile rootFile;
55    
56    /**
57     * Get the file for a url
58     *
59     * @param url the url
60     * @return the file
61     * @throws IOException for any error accessing the file system
62     * @throws IllegalArgumentException for a null url
63     */

64    private static File JavaDoc getFile(URI JavaDoc uri) throws IOException JavaDoc
65    {
66       if (uri == null)
67          throw new IllegalArgumentException JavaDoc("Null uri");
68       
69       return new File JavaDoc(uri);
70    }
71    
72    /**
73     * Get the url for a file
74     *
75     * @param file the file
76     * @return the url
77     * @throws IOException for any error accessing the file system
78     * @throws IllegalArgumentException for a null file
79     */

80    private static URI JavaDoc getFileURI(File JavaDoc file) throws IOException JavaDoc
81    {
82       if (file == null)
83          throw new IllegalArgumentException JavaDoc("Null file");
84       URI JavaDoc url = file.toURI();
85       String JavaDoc path = url.getPath();
86       if (file.isDirectory() == false)
87          path = VFSUtils.fixName(path);
88       else
89       {
90          if (path.endsWith("/") == false)
91             path = path + '/';
92       }
93       try
94       {
95          return new URI JavaDoc("file", null, path, null);
96       }
97       catch(URISyntaxException JavaDoc e)
98       {
99          // Should not be possible
100
throw new IllegalStateException JavaDoc("Failed to convert file.toURI", e);
101       }
102    }
103    
104    /**
105     * Create a new FileSystemContext.
106     *
107     * @param rootURL the root url
108     * @throws IOException for an error accessing the file system
109     * @throws URISyntaxException for an error parsing the uri
110     */

111    public FileSystemContext(URL JavaDoc rootURL) throws IOException JavaDoc, URISyntaxException JavaDoc
112    {
113       this(VFSUtils.toURI(rootURL));
114    }
115
116    /**
117     * Create a new FileSystemContext.
118     *
119     * @param rootURI the root uri
120     * @throws IOException for an error accessing the file system
121     */

122    public FileSystemContext(URI JavaDoc rootURI) throws IOException JavaDoc
123    {
124       this(rootURI, getFile(rootURI));
125    }
126    
127    /**
128     * Create a new FileSystemContext.
129     *
130     * @param file the root file
131     * @throws IOException for an error accessing the file system
132     * @throws IllegalArgumentException for a null file
133     * @throws URISyntaxException for an error parsing the uri
134     */

135    public FileSystemContext(File JavaDoc file) throws IOException JavaDoc, URISyntaxException JavaDoc
136    {
137       this(getFileURI(file), file);
138    }
139
140    /**
141     * Create a new FileSystemContext.
142     *
143     * @param rootURL the root url
144     * @param file the file
145     * @throws IOException for an error accessing the file system
146     */

147    private FileSystemContext(URI JavaDoc rootURL, File JavaDoc file) throws IOException JavaDoc
148    {
149       super(rootURL);
150       root = createVirtualFileHandler(null, file);
151       rootFile = root.getVirtualFile();
152    }
153
154    public VirtualFileHandler getRoot() throws IOException JavaDoc
155    {
156       return root;
157    }
158
159    /**
160     * Create a new virtual file handler
161     *
162     * @param parent the parent
163     * @param file the file
164     * @return the handler
165     * @throws IOException for any error accessing the file system
166     * @throws IllegalArgumentException for a null file
167     */

168    public VirtualFileHandler createVirtualFileHandler(VirtualFileHandler parent, File JavaDoc file) throws IOException JavaDoc
169    {
170       if (file == null)
171          throw new IllegalArgumentException JavaDoc("Null file");
172       
173       URI JavaDoc fileURL = getFileURI(file);
174       if (file.isFile() && JarUtils.isArchive(file.getName()))
175       {
176          URL JavaDoc url = JarUtils.createJarURL(file.toURL());
177          String JavaDoc name = file.getName();
178          try
179          {
180             return new JarHandler(this, parent, url, name);
181          }
182          catch (IOException JavaDoc e)
183          {
184             log.debug(e.getMessage());
185          }
186       }
187       return createVirtualFileHandler(parent, file, fileURL);
188    }
189
190    /**
191     * Create a new virtual file handler
192     *
193     * @param parent the parent
194     * @param file the file
195     * @param uri the uri
196     * @return the handler
197     * @throws IOException for any error accessing the file system
198     * @throws IllegalArgumentException for a null file
199     */

200    public VirtualFileHandler createVirtualFileHandler(VirtualFileHandler parent, File JavaDoc file, URI JavaDoc uri)
201       throws IOException JavaDoc
202    {
203       if (file == null)
204          throw new IllegalArgumentException JavaDoc("Null file");
205       if (uri == null)
206          throw new IllegalArgumentException JavaDoc("Null uri");
207
208       VirtualFileHandler handler = null;
209       if( VFSUtils.isLink(file.getName()) )
210       {
211          Properties JavaDoc props = new Properties JavaDoc();
212          FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
213          try
214          {
215             List JavaDoc<LinkInfo> links = VFSUtils.readLinkInfo(fis, file.getName(), props);
216             String JavaDoc name = props.getProperty(VFSUtils.VFS_LINK_NAME, "link");
217             handler = new LinkHandler(this, parent, uri, name, links);
218          }
219          catch(URISyntaxException JavaDoc e)
220          {
221             IOException JavaDoc ex = new IOException JavaDoc("Failed to parse link URIs");
222             ex.initCause(e);
223             throw ex;
224          }
225          finally
226          {
227             try
228             {
229                fis.close();
230             }
231             catch(IOException JavaDoc e)
232             {
233                log.debug("Exception closing file input stream: " + fis, e);
234             }
235          }
236       }
237       else if (file.exists() == false && parent != null)
238       {
239          // See if we can resolve this to a link in the parent
240
List JavaDoc<VirtualFileHandler> children = parent.getChildren(true);
241          for(VirtualFileHandler vfh : children)
242          {
243             if( vfh.getName().equals(file.getName()) )
244             {
245                handler = vfh;
246                break;
247             }
248          }
249          if( handler == null )
250             throw new FileNotFoundException JavaDoc("File does not exist: " + file.getCanonicalPath());
251       }
252       else
253       {
254          handler = new FileHandler(this, parent, file, uri);
255       }
256       return handler;
257    }
258    
259    @Override JavaDoc
260    protected void finalize() throws Throwable JavaDoc
261    {
262       if (rootFile != null)
263          rootFile.close();
264       super.finalize();
265    }
266 }
267
Popular Tags