1 package org.mortbay.util; 16 17 import java.io.File ; 18 import java.io.FileInputStream ; 19 import java.io.FileOutputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 import java.net.MalformedURLException ; 24 import java.net.URI ; 25 import java.net.URISyntaxException ; 26 import java.net.URL ; 27 import java.net.URLConnection ; 28 import java.security.Permission ; 29 30 import org.apache.commons.logging.Log; 31 import org.mortbay.log.LogFactory; 32 33 34 35 49 public class FileResource extends URLResource 50 { 51 private static Log log = LogFactory.getLog(Credential.class); 52 private static boolean __checkAliases; 53 static 54 { 55 __checkAliases= 56 "true".equalsIgnoreCase 57 (System.getProperty("org.mortbay.util.FileResource.checkAliases","true")); 58 59 if (__checkAliases) 60 log.info("Checking Resource aliases"); 61 } 62 63 64 private File _file; 65 private transient URL _alias=null; 66 private transient boolean _aliasChecked=false; 67 68 69 72 public static void setCheckAliases(boolean checkAliases) 73 { 74 __checkAliases=checkAliases; 75 } 76 77 78 81 public static boolean getCheckAliases() 82 { 83 return __checkAliases; 84 } 85 86 87 FileResource(URL url) 88 throws IOException , URISyntaxException 89 { 90 super(url,null); 91 92 try 93 { 94 _file =new File (new URI(url.toString())); 96 } 97 catch (Exception e) 98 { 99 LogSupport.ignore(log,e); 100 try 101 { 102 String urls= 105 "file:"+org.mortbay.util.URI.encodePath(url.toString().substring(5)); 106 _file =new File (new URI(urls)); 107 } 108 catch (Exception e2) 109 { 110 LogSupport.ignore(log,e2); 111 112 checkConnection(); 114 Permission perm = _connection.getPermission(); 115 _file = new File (perm==null?url.getFile():perm.getName()); 116 } 117 } 118 119 if (_file.isDirectory() && !_urlString.endsWith("/")) 120 _urlString=_urlString+"/"; 121 } 122 123 124 FileResource(URL url, URLConnection connection, File file) 125 { 126 super(url,connection); 127 _file=file; 128 if (_file.isDirectory() && !_urlString.endsWith("/")) 129 _urlString=_urlString+"/"; 130 } 131 132 133 public Resource addPath(String path) 134 throws IOException ,MalformedURLException 135 { 136 FileResource r=null; 137 138 if (!isDirectory()) 139 { 140 r=(FileResource)super.addPath(path); 141 } 142 else 143 { 144 path = org.mortbay.util.URI.canonicalPath(path); 145 146 String rel=path; 148 if (path.startsWith("/")) 149 rel = path.substring(1); 150 151 File newFile = new File (_file,rel.replace('/', File.separatorChar)); 152 r=new FileResource(newFile.toURI().toURL(),null,newFile); 153 } 154 155 String encoded=org.mortbay.util.URI.encodePath(path); 156 int expected=r._urlString.length()-encoded.length(); 157 int index = r._urlString.lastIndexOf(encoded, expected); 158 159 if (expected!=index && ((expected-1)!=index || path.endsWith("/") || !r.isDirectory())) 160 { 161 r._alias=r._url; 162 r._aliasChecked=true; 163 } 164 return r; 165 } 166 167 168 169 public URL getAlias() 170 { 171 if (__checkAliases && !_aliasChecked) 172 { 173 try 174 { 175 String abs=_file.getAbsolutePath(); 176 String can=_file.getCanonicalPath(); 177 178 if (abs.length()!=can.length() || !abs.equals(can)) 179 _alias=new File (can).toURI().toURL(); 180 181 _aliasChecked=true; 182 183 if (_alias!=null && log.isDebugEnabled()) 184 { 185 log.debug("ALIAS abs="+abs); 186 log.debug("ALIAS can="+can); 187 } 188 } 189 catch(Exception e) 190 { 191 log.warn(LogSupport.EXCEPTION,e); 192 return getURL(); 193 } 194 } 195 return _alias; 196 } 197 198 199 202 public boolean exists() 203 { 204 return _file.exists(); 205 } 206 207 208 211 public long lastModified() 212 { 213 return _file.lastModified(); 214 } 215 216 217 220 public boolean isDirectory() 221 { 222 return _file.isDirectory(); 223 } 224 225 226 229 public long length() 230 { 231 return _file.length(); 232 } 233 234 235 236 239 public String getName() 240 { 241 return _file.getAbsolutePath(); 242 } 243 244 245 249 public File getFile() 250 { 251 return _file; 252 } 253 254 255 258 public InputStream getInputStream() throws IOException 259 { 260 return new FileInputStream (_file); 261 } 262 263 264 267 public OutputStream getOutputStream() 268 throws java.io.IOException , SecurityException 269 { 270 return new FileOutputStream (_file); 271 } 272 273 274 277 public boolean delete() 278 throws SecurityException 279 { 280 return _file.delete(); 281 } 282 283 284 287 public boolean renameTo( Resource dest) 288 throws SecurityException 289 { 290 if( dest instanceof FileResource) 291 return _file.renameTo( ((FileResource)dest)._file); 292 else 293 return false; 294 } 295 296 297 300 public String [] list() 301 { 302 String [] list =_file.list(); 303 if (list==null) 304 return null; 305 for (int i=list.length;i-->0;) 306 { 307 if (new File (_file,list[i]).isDirectory() && 308 !list[i].endsWith("/")) 309 list[i]+="/"; 310 } 311 return list; 312 } 313 314 315 320 public String encode(String uri) 321 { 322 return uri; 323 } 324 325 326 330 public boolean equals( Object o) 331 { 332 if (this == o) 333 return true; 334 335 if (null == o || ! (o instanceof FileResource)) 336 return false; 337 338 FileResource f=(FileResource)o; 339 return f._file == _file || (null != _file && _file.equals(f._file)); 340 } 341 342 343 346 public int hashCode() 347 { 348 return null == _file ? super.hashCode() : _file.hashCode(); 349 } 350 } 351 | Popular Tags |