2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util.resource;
22 import java.io.IOException;
23 import java.net.JarURLConnection;
24 import java.net.MalformedURLException;
26 import java.util.ArrayList;
27 import java.util.Enumeration;
28 import java.util.List;
29 import java.util.jar.JarEntry;
30 import java.util.jar.JarFile;
32 import org.eclipse.jetty.util.log.Log;
33 import org.eclipse.jetty.util.log.Logger;
35 /* ------------------------------------------------------------ */
36 class JarFileResource extends JarResource
38 private static final Logger LOG = Log.getLogger(JarFileResource.class);
39 private JarFile _jarFile;
41 private String[] _list;
42 private JarEntry _entry;
43 private boolean _directory;
44 private String _jarUrl;
46 private boolean _exists;
48 /* -------------------------------------------------------- */
49 protected JarFileResource(URL url)
54 /* ------------------------------------------------------------ */
55 protected JarFileResource(URL url, boolean useCaches)
57 super(url, useCaches);
61 /* ------------------------------------------------------------ */
63 public synchronized void close()
68 //if the jvm is not doing url caching, then the JarFiles will not be cached either,
69 //and so they are safe to close
72 if ( _jarFile != null )
76 if (LOG.isDebugEnabled())
77 LOG.debug("Closing JarFile "+_jarFile.getName());
80 catch ( IOException ioe )
90 /* ------------------------------------------------------------ */
92 protected synchronized boolean checkConnection()
96 super.checkConnection();
100 if (_jarConnection==null)
108 return _jarFile!=null;
112 /* ------------------------------------------------------------ */
114 protected synchronized void newConnection()
117 super.newConnection();
124 int sep = _urlString.indexOf("!/");
125 _jarUrl=_urlString.substring(0,sep+2);
126 _path=_urlString.substring(sep+2);
127 if (_path.length()==0)
129 _jarFile=_jarConnection.getJarFile();
130 _file=new File(_jarFile.getName());
134 /* ------------------------------------------------------------ */
136 * Returns true if the represented resource exists.
140 public boolean exists()
145 if (_urlString.endsWith("!/"))
148 String file_url=_urlString.substring(4,_urlString.length()-2);
149 try{return newResource(file_url).exists();}
150 catch(Exception e) {LOG.ignore(e); return false;}
153 boolean check=checkConnection();
155 // Is this a root URL?
156 if (_jarUrl!=null && _path==null)
158 // Then if it exists it is a directory
164 // Can we find a file for it?
165 boolean close_jar_file= false;
166 JarFile jar_file=null;
172 // No - so lets look if the root entry exists.
175 JarURLConnection c=(JarURLConnection)((new URL(_jarUrl)).openConnection());
176 c.setUseCaches(getUseCaches());
177 jar_file=c.getJarFile();
178 close_jar_file = !getUseCaches();
186 // Do we need to look more closely?
187 if (jar_file!=null && _entry==null && !_directory)
189 // OK - we have a JarFile, lets look at the entries for our path
190 Enumeration<JarEntry> e=jar_file.entries();
191 while(e.hasMoreElements())
193 JarEntry entry = e.nextElement();
194 String name=entry.getName().replace('\\','/');
196 // Do we have a match
197 if (name.equals(_path))
200 // Is the match a directory
201 _directory=_path.endsWith("/");
204 else if (_path.endsWith("/"))
206 if (name.startsWith(_path))
212 else if (name.startsWith(_path) && name.length()>_path.length() && name.charAt(_path.length())=='/')
220 if(close_jar_file && jar_file!=null)
226 catch (IOException ioe)
233 _exists= ( _directory || _entry!=null);
238 /* ------------------------------------------------------------ */
240 * Returns true if the represented resource is a container/directory.
241 * If the resource is not a file, resources ending with "/" are
242 * considered directories.
245 public boolean isDirectory()
247 return _urlString.endsWith("/") || exists() && _directory;
250 /* ------------------------------------------------------------ */
252 * Returns the last modified time
255 public long lastModified()
257 if (checkConnection() && _file!=null)
259 if (exists() && _entry!=null)
260 return _entry.getTime();
261 return _file.lastModified();
266 /* ------------------------------------------------------------ */
268 public synchronized String[] list()
270 if (isDirectory() && _list==null)
272 List<String> list = null;
275 list = listEntries();
279 //Sun's JarURLConnection impl for jar: protocol will close a JarFile in its connect() method if
280 //useCaches == false (eg someone called URLConnection with defaultUseCaches==true).
281 //As their sun.net.www.protocol.jar package caches JarFiles and/or connections, we can wind up in
282 //the situation where the JarFile we have remembered in our _jarFile member has actually been closed
284 //So, do one retry to drop a connection and get a fresh JarFile
285 LOG.warn("Retrying list:"+e);
288 list = listEntries();
293 _list=new String[list.size()];
301 /* ------------------------------------------------------------ */
302 private List<String> listEntries ()
306 ArrayList<String> list = new ArrayList<String>(32);
307 JarFile jarFile=_jarFile;
312 JarURLConnection jc=(JarURLConnection)((new URL(_jarUrl)).openConnection());
313 jc.setUseCaches(getUseCaches());
314 jarFile=jc.getJarFile();
323 throw new IllegalStateException();
326 Enumeration<JarEntry> e=jarFile.entries();
327 String dir=_urlString.substring(_urlString.indexOf("!/")+2);
328 while(e.hasMoreElements())
330 JarEntry entry = e.nextElement();
331 String name=entry.getName().replace('\\','/');
332 if(!name.startsWith(dir) || name.length()==dir.length())
336 String listName=name.substring(dir.length());
337 int dash=listName.indexOf('/');
340 //when listing jar:file urls, you get back one
341 //entry for the dir itself, which we ignore
342 if (dash==0 && listName.length()==1)
344 //when listing jar:file urls, all files and
345 //subdirs have a leading /, which we remove
347 listName=listName.substring(dash+1, listName.length());
349 listName=listName.substring(0,dash+1);
351 if (list.contains(listName))
365 /* ------------------------------------------------------------ */
367 * Return the length of the resource
376 return _entry.getSize();
383 * Take a Resource that possibly might use URLConnection caching
384 * and turn it into one that doesn't.
386 * @return the non-caching resource
388 public static Resource getNonCachingResource (Resource resource)
390 if (!(resource instanceof JarFileResource))
393 JarFileResource oldResource = (JarFileResource)resource;
395 JarFileResource newResource = new JarFileResource(oldResource.getURL(), false);
401 * Check if this jar:file: resource is contained in the
402 * named resource. Eg <code>jar:file:///a/b/c/foo.jar!/x.html</code> isContainedIn <code>file:///a/b/c/foo.jar</code>
404 * @return true if resource is contained in the named resource
405 * @throws MalformedURLException
408 public boolean isContainedIn (Resource resource)
409 throws MalformedURLException
411 String string = _urlString;
412 int index = string.indexOf("!/");
414 string = string.substring(0,index);
415 if (string.startsWith("jar:"))
416 string = string.substring(4);
417 URL url = new URL(string);
418 return url.sameFile(resource.getURL());