1 22 package org.jboss.virtual.plugins.context.file; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileNotFoundException ; 27 import java.io.IOException ; 28 import java.net.URI ; 29 import java.net.URISyntaxException ; 30 import java.net.URL ; 31 import java.util.List ; 32 import java.util.Properties ; 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 48 public class FileSystemContext extends AbstractVFSContext 49 { 50 51 private final VirtualFileHandler root; 52 53 54 private final VirtualFile rootFile; 55 56 64 private static File getFile(URI uri) throws IOException 65 { 66 if (uri == null) 67 throw new IllegalArgumentException ("Null uri"); 68 69 return new File (uri); 70 } 71 72 80 private static URI getFileURI(File file) throws IOException 81 { 82 if (file == null) 83 throw new IllegalArgumentException ("Null file"); 84 URI url = file.toURI(); 85 String 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 ("file", null, path, null); 96 } 97 catch(URISyntaxException e) 98 { 99 throw new IllegalStateException ("Failed to convert file.toURI", e); 101 } 102 } 103 104 111 public FileSystemContext(URL rootURL) throws IOException , URISyntaxException 112 { 113 this(VFSUtils.toURI(rootURL)); 114 } 115 116 122 public FileSystemContext(URI rootURI) throws IOException 123 { 124 this(rootURI, getFile(rootURI)); 125 } 126 127 135 public FileSystemContext(File file) throws IOException , URISyntaxException 136 { 137 this(getFileURI(file), file); 138 } 139 140 147 private FileSystemContext(URI rootURL, File file) throws IOException 148 { 149 super(rootURL); 150 root = createVirtualFileHandler(null, file); 151 rootFile = root.getVirtualFile(); 152 } 153 154 public VirtualFileHandler getRoot() throws IOException 155 { 156 return root; 157 } 158 159 168 public VirtualFileHandler createVirtualFileHandler(VirtualFileHandler parent, File file) throws IOException 169 { 170 if (file == null) 171 throw new IllegalArgumentException ("Null file"); 172 173 URI fileURL = getFileURI(file); 174 if (file.isFile() && JarUtils.isArchive(file.getName())) 175 { 176 URL url = JarUtils.createJarURL(file.toURL()); 177 String name = file.getName(); 178 try 179 { 180 return new JarHandler(this, parent, url, name); 181 } 182 catch (IOException e) 183 { 184 log.debug(e.getMessage()); 185 } 186 } 187 return createVirtualFileHandler(parent, file, fileURL); 188 } 189 190 200 public VirtualFileHandler createVirtualFileHandler(VirtualFileHandler parent, File file, URI uri) 201 throws IOException 202 { 203 if (file == null) 204 throw new IllegalArgumentException ("Null file"); 205 if (uri == null) 206 throw new IllegalArgumentException ("Null uri"); 207 208 VirtualFileHandler handler = null; 209 if( VFSUtils.isLink(file.getName()) ) 210 { 211 Properties props = new Properties (); 212 FileInputStream fis = new FileInputStream (file); 213 try 214 { 215 List <LinkInfo> links = VFSUtils.readLinkInfo(fis, file.getName(), props); 216 String name = props.getProperty(VFSUtils.VFS_LINK_NAME, "link"); 217 handler = new LinkHandler(this, parent, uri, name, links); 218 } 219 catch(URISyntaxException e) 220 { 221 IOException ex = new IOException ("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 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 List <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 ("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 260 protected void finalize() throws Throwable 261 { 262 if (rootFile != null) 263 rootFile.close(); 264 super.finalize(); 265 } 266 } 267 | Popular Tags |