1 package org.mortbay.util; 16 17 import java.io.File ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.net.URLConnection ; 24 import java.security.Permission ; 25 26 import org.apache.commons.logging.Log; 27 import org.mortbay.log.LogFactory; 28 29 30 36 public class URLResource extends Resource 37 { 38 private static Log log = LogFactory.getLog(URLResource.class); 39 40 protected URL _url; 41 protected String _urlString; 42 protected transient URLConnection _connection; 43 protected transient InputStream _in=null; 44 45 46 protected URLResource(URL url, URLConnection connection) 47 { 48 _url = url; 49 _urlString=_url.toString(); 50 _connection=connection; 51 } 52 53 54 protected synchronized boolean checkConnection() 55 { 56 if (_connection==null) 57 { 58 try{ 59 _connection=_url.openConnection(); 60 } 61 catch(IOException e) 62 { 63 LogSupport.ignore(log,e); 64 } 65 } 66 return _connection!=null; 67 } 68 69 70 72 public synchronized void release() 73 { 74 if (_in!=null) 75 { 76 try{_in.close();}catch(IOException e){LogSupport.ignore(log,e);} 77 _in=null; 78 } 79 80 if (_connection!=null) 81 _connection=null; 82 } 83 84 85 88 public boolean exists() 89 { 90 try 91 { 92 synchronized(this) 93 { 94 if (checkConnection() && _in==null ) 95 _in = _connection.getInputStream(); 96 } 97 } 98 catch (IOException e) 99 { 100 LogSupport.ignore(log,e); 101 } 102 return _in!=null; 103 } 104 105 106 111 public boolean isDirectory() 112 { 113 return exists() && _url.toString().endsWith("/"); 114 } 115 116 117 118 121 public long lastModified() 122 { 123 if (checkConnection()) 124 return _connection.getLastModified(); 125 return -1; 126 } 127 128 129 130 133 public long length() 134 { 135 if (checkConnection()) 136 return _connection.getContentLength(); 137 return -1; 138 } 139 140 141 144 public URL getURL() 145 { 146 return _url; 147 } 148 149 150 154 public File getFile() 155 throws IOException 156 { 157 if (checkConnection()) 159 { 160 Permission perm = _connection.getPermission(); 161 if (perm instanceof java.io.FilePermission ) 162 return new File (perm.getName()); 163 } 164 165 try {return new File (_url.getFile());} 167 catch(Exception e) {LogSupport.ignore(log,e);} 168 169 return null; 171 } 172 173 174 177 public String getName() 178 { 179 return _url.toExternalForm(); 180 } 181 182 183 186 public synchronized InputStream getInputStream() 187 throws java.io.IOException 188 { 189 if (!checkConnection()) 190 throw new IOException ( "Invalid resource"); 191 192 try 193 { 194 if( _in != null) 195 { 196 InputStream in = _in; 197 _in=null; 198 return in; 199 } 200 return _connection.getInputStream(); 201 } 202 finally 203 { 204 _connection=null; 205 } 206 } 207 208 209 210 213 public OutputStream getOutputStream() 214 throws java.io.IOException , SecurityException 215 { 216 throw new IOException ( "Output not supported"); 217 } 218 219 220 223 public boolean delete() 224 throws SecurityException 225 { 226 throw new SecurityException ( "Delete not supported"); 227 } 228 229 230 233 public boolean renameTo( Resource dest) 234 throws SecurityException 235 { 236 throw new SecurityException ( "RenameTo not supported"); 237 } 238 239 240 243 public String [] list() 244 { 245 return null; 246 } 247 248 249 253 public Resource addPath(String path) 254 throws IOException ,MalformedURLException 255 { 256 if (path==null) 257 return null; 258 259 path = URI.canonicalPath(path); 260 261 return newResource(URI.addPaths(_url.toExternalForm(),path)); 262 } 263 264 265 public String toString() 266 { 267 return _urlString; 268 } 269 270 271 public int hashCode() 272 { 273 return _url.hashCode(); 274 } 275 276 277 public boolean equals( Object o) 278 { 279 return o instanceof URLResource && 280 _url.equals(((URLResource)o)._url); 281 } 282 283 } 284 | Popular Tags |