1 package org.mortbay.util; 16 17 import java.io.File ; 18 import java.io.IOException ; 19 import java.net.JarURLConnection ; 20 import java.net.URL ; 21 import java.util.ArrayList ; 22 import java.util.Enumeration ; 23 import java.util.jar.JarEntry ; 24 import java.util.jar.JarFile ; 25 26 import org.apache.commons.logging.Log; 27 import org.mortbay.log.LogFactory; 28 29 30 class JarFileResource extends JarResource 31 { 32 private static Log log = LogFactory.getLog(JarFileResource.class); 33 transient JarFile _jarFile; 34 transient File _file; 35 transient String [] _list; 36 transient JarEntry _entry; 37 transient boolean _directory; 38 transient String _jarUrl; 39 transient String _path; 40 transient boolean _exists; 41 42 43 JarFileResource(URL url) 44 { 45 super(url); 46 } 47 48 49 public synchronized void release() 50 { 51 _list=null; 52 _entry=null; 53 _file=null; 54 _jarFile=null; 55 super.release(); 56 } 57 58 59 protected boolean checkConnection() 60 { 61 try{ 62 super.checkConnection(); 63 } 64 finally 65 { 66 if (_jarConnection==null) 67 { 68 _entry=null; 69 _file=null; 70 _jarFile=null; 71 _list=null; 72 } 73 } 74 return _jarFile!=null; 75 } 76 77 78 79 protected void newConnection() 80 throws IOException 81 { 82 super.newConnection(); 83 84 _entry=null; 85 _file=null; 86 _jarFile=null; 87 _list=null; 88 89 int sep = _urlString.indexOf("!/"); 90 _jarUrl=_urlString.substring(0,sep+2); 91 _path=_urlString.substring(sep+2); 92 if (_path.length()==0) 93 _path=null; 94 _jarFile=_jarConnection.getJarFile(); 95 _file=new File (_jarFile.getName()); 96 } 97 98 99 100 103 public boolean exists() 104 { 105 if (_exists) 106 return true; 107 108 if (_urlString.endsWith("!/")) 109 { 110 String file_url=_urlString.substring(4,_urlString.length()-2); 111 try{return newResource(file_url).exists();} 112 catch(Exception e) {LogSupport.ignore(log,e); return false;} 113 } 114 115 boolean check=checkConnection(); 116 117 if (_jarUrl!=null && _path==null) 119 { 120 _directory=check; 122 return true; 123 } 124 else 125 { 126 JarFile jarFile=null; 128 if (check) 129 jarFile=_jarFile; 131 else 132 { 133 try 135 { 136 jarFile= 137 ((JarURLConnection ) 138 ((new URL (_jarUrl)).openConnection())).getJarFile(); 139 } 140 catch(Exception e) 141 { 142 LogSupport.ignore(log,e); 143 } 144 } 145 146 if (jarFile!=null && _entry==null && !_directory) 148 { 149 Enumeration e=jarFile.entries(); 151 while(e.hasMoreElements()) 152 { 153 JarEntry entry = (JarEntry ) e.nextElement(); 154 String name=entry.getName().replace('\\','/'); 155 156 if (name.equals(_path)) 158 { 159 _entry=entry; 160 _directory=_path.endsWith("/"); 162 break; 163 } 164 else if (_path.endsWith("/")) 165 { 166 if (name.startsWith(_path)) 167 { 168 _directory=true; 169 break; 170 } 171 } 172 else if (name.startsWith(_path) && name.length()>_path.length() && name.charAt(_path.length())=='/') 173 { 174 _directory=true; 175 break; 176 } 177 } 178 } 179 } 180 181 _exists= ( _directory || _entry!=null); 182 return _exists; 183 } 184 185 186 191 public boolean isDirectory() 192 { 193 return _urlString.endsWith("/") || exists() && _directory; 194 } 195 196 197 200 public long lastModified() 201 { 202 if (checkConnection() && _file!=null) 203 return _file.lastModified(); 204 return -1; 205 } 206 207 208 public synchronized String [] list() 209 { 210 if(isDirectory() && _list==null) 211 { 212 ArrayList list = new ArrayList (32); 213 214 checkConnection(); 215 216 JarFile jarFile=_jarFile; 217 if(jarFile==null) 218 { 219 try 220 { 221 jarFile=((JarURLConnection ) 222 ((new URL (_jarUrl)).openConnection())).getJarFile(); 223 } 224 catch(Exception e) 225 { 226 LogSupport.ignore(log,e); 227 } 228 } 229 230 Enumeration e=jarFile.entries(); 231 String dir=_urlString.substring(_urlString.indexOf("!/")+2); 232 while(e.hasMoreElements()) 233 { 234 JarEntry entry = (JarEntry ) e.nextElement(); 235 String name=entry.getName().replace('\\','/'); 236 if(!name.startsWith(dir) || name.length()==dir.length()) 237 continue; 238 String listName=name.substring(dir.length()); 239 int dash=listName.indexOf('/'); 240 if (dash>=0) 241 { 242 listName=listName.substring(0,dash+1); 243 if (list.contains(listName)) 244 continue; 245 } 246 247 list.add(listName); 248 } 249 250 _list=new String [list.size()]; 251 list.toArray(_list); 252 } 253 return _list; 254 } 255 256 257 260 public long length() 261 { 262 if (isDirectory()) 263 return -1; 264 265 if (_entry!=null) 266 return _entry.getSize(); 267 268 return -1; 269 } 270 271 272 277 public String encode(String uri) 278 { 279 return uri; 280 } 281 } 282 283 284 285 286 287 288 289 290 | Popular Tags |