1 22 package org.jboss.virtual.plugins.context.file; 23 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.IOException ; 27 import java.io.ObjectInputStream ; 28 import java.net.URI ; 29 import java.net.URL ; 30 import java.util.ArrayList ; 31 import java.util.Collections ; 32 import java.util.List ; 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 46 public class FileHandler extends AbstractURLHandler 47 implements StructuredVirtualFileHandler 48 { 49 private static final long serialVersionUID = 1; 50 51 private transient File file; 52 53 63 public FileHandler(FileSystemContext context, VirtualFileHandler parent, File file, URL url) throws IOException 64 { 65 super(context, parent, url, file.getName()); 66 67 this.file = file; 68 if (file.exists() == false) 69 throw new FileNotFoundException ("File does not exist: " + file.getCanonicalPath()); 70 } 71 81 public FileHandler(FileSystemContext context, VirtualFileHandler parent, File file, URI uri) throws IOException 82 { 83 this(context, parent, file, uri.toURL()); 84 } 85 86 @Override 87 public FileSystemContext getVFSContext() 88 { 89 return (FileSystemContext) super.getVFSContext(); 90 } 91 92 97 protected File getFile() 98 { 99 checkClosed(); 100 return file; 101 } 102 103 @Override 104 public long getLastModified() 105 { 106 return getFile().lastModified(); 107 } 108 109 @Override 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 <VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException 126 { 127 File parent = getFile(); 128 File [] files = parent.listFiles(); 129 if (files == null) 130 throw new IOException ("Error listing files: " + parent.getCanonicalPath()); 131 if (files.length == 0) 132 return Collections.emptyList(); 133 134 FileSystemContext context = getVFSContext(); 135 136 List <VirtualFileHandler> result = new ArrayList <VirtualFileHandler>(); 137 for (File file : files) 138 { 139 try 140 { 141 VirtualFileHandler handler = context.createVirtualFileHandler(this, file); 142 result.add(handler); 143 } 144 catch (IOException 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 path) throws IOException 156 { 157 return structuredFindChild(path); 158 } 159 160 public VirtualFileHandler createChildHandler(String name) throws IOException 161 { 162 FileSystemContext context = getVFSContext(); 163 File parentFile = getFile(); 164 File child = new File (parentFile, name); 165 return context.createVirtualFileHandler(this, child); 166 } 167 168 private void readObject(ObjectInputStream in) 169 throws IOException , ClassNotFoundException 170 { 171 in.defaultReadObject(); 172 this.file = new File (getURL().getPath()); 174 } 175 176 } 177 | Popular Tags |