Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.util.resource;
 
 import java.io.File;
 import java.net.URL;
 import java.util.List;
 
 
 /* ------------------------------------------------------------ */
 class JarFileResource extends JarResource
 {
     private static final Logger LOG = Log.getLogger(JarFileResource.class);
     private JarFile _jarFile;
     private File _file;
     private String[] _list;
     private JarEntry _entry;
     private boolean _directory;
     private String _jarUrl;
     private String _path;
     private boolean _exists;
     
     /* -------------------------------------------------------- */
     JarFileResource(URL url)
     {
         super(url);
     }
     
     /* ------------------------------------------------------------ */
     JarFileResource(URL urlboolean useCaches)
     {
         super(urluseCaches);
     }
    
 
     /* ------------------------------------------------------------ */
     @Override
     public synchronized void release()
     {
         =null;
         =null;
         =null;
         //if the jvm is not doing url caching, then the JarFiles will not be cached either,
         //and so they are safe to close
         if (!getUseCaches())
         {
             if (  != null )
             {
                 try
                 {
                     .debug("Closing JarFile "+.getName());
                     .close();
                 }
                 catch ( IOException ioe )
                 {
                     .ignore(ioe);
                 }
             }
         }
         =null;
         super.release();
     }
     
     /* ------------------------------------------------------------ */
     @Override
     protected boolean checkConnection()
     {
         try
         {
             super.checkConnection();
         }
         finally
         {
             if (==null)
            {
                =null;
                =null;
                =null;
                =null;
            }
        }
        return !=null;
    }
    /* ------------------------------------------------------------ */
    @Override
    protected synchronized void newConnection()
        throws IOException
    {
        super.newConnection();
        
        =null;
        =null;
        =null;
        =null;
        
        int sep = .indexOf("!/");
        =.substring(0,sep+2);
        =.substring(sep+2);
        if (.length()==0)
            =null;   
        =new File(.getName());
    }
    
    
    /* ------------------------------------------------------------ */
    
Returns true if the represented resource exists.
    @Override
    public boolean exists()
    {
        if ()
            return true;
        if (.endsWith("!/"))
        {
            
            String file_url=.substring(4,.length()-2);
            try{return newResource(file_url).exists();}
            catch(Exception e) {.ignore(e); return false;}
        }
        
        boolean check=checkConnection();
        
        // Is this a root URL?
        if (!=null && ==null)
        {
            // Then if it exists it is a directory
            =check;
            return true;
        }
        else 
        {
            // Can we find a file for it?
            JarFile jarFile=null;
            if (check)
                // Yes
                jarFile=;
            else
            {
                // No - so lets look if the root entry exists.
                try
                {
                    JarURLConnection c=(JarURLConnection)((new URL()).openConnection());
                    c.setUseCaches(getUseCaches());
                    jarFile=c.getJarFile();
                }
                catch(Exception e)
                {
                       .ignore(e);
                }
            }
            // Do we need to look more closely?
            if (jarFile!=null && ==null && !)
            {
                // OK - we have a JarFile, lets look at the entries for our path
                Enumeration<JarEntrye=jarFile.entries();
                while(e.hasMoreElements())
                {
                    JarEntry entry = (JarEntrye.nextElement();
                    String name=entry.getName().replace('\\','/');
                    
                    // Do we have a match
                    if (name.equals())
                    {
                        =entry;
                        // Is the match a directory
                        =.endsWith("/");
                        break;
                    }
                    else if (.endsWith("/"))
                    {
                        if (name.startsWith())
                        {
                            =true;
                            break;
                        }
                    }
                    else if (name.startsWith() && name.length()>.length() && name.charAt(.length())=='/')
                    {
                        =true;
                        break;
                    }
                }
                if ( && !.endsWith("/"))
                {
                    +="/";
                    try
                    {
                        =new URL();
                    }
                    catch(MalformedURLException ex)
                    {
                        .warn(ex);
                    }
                }
            }
        }    
        
        = (  || !=null);
        return ;
    }
    
    /* ------------------------------------------------------------ */
    
Returns true if the represented resource is a container/directory. If the resource is not a file, resources ending with "/" are considered directories.
    @Override
    public boolean isDirectory()
    {
        return .endsWith("/") || exists() && ;
    }
    
    /* ------------------------------------------------------------ */
    
Returns the last modified time
    @Override
    public long lastModified()
    {
        if (checkConnection() && !=null)
        {
            if (exists() && !=null)
                return .getTime();
            return .lastModified();
        }
        return -1;
    }
    /* ------------------------------------------------------------ */
    @Override
    public synchronized String[] list()
    {
        if (isDirectory() && ==null)
        {
            List<Stringlist = null;
            try
            {
                list = listEntries();
            }
            catch (Exception e)
            {
                //Sun's JarURLConnection impl for jar: protocol will close a JarFile in its connect() method if
                //useCaches == false (eg someone called URLConnection with defaultUseCaches==true).
                //As their sun.net.www.protocol.jar package caches JarFiles and/or connections, we can wind up in 
                //the situation where the JarFile we have remembered in our _jarFile member has actually been closed
                //by other code.
                //So, do one retry to drop a connection and get a fresh JarFile
                .warn("Retrying list:"+e);
                .debug(e);
                release();
                list = listEntries();
            }
            if (list != null)
            {
                =new String[list.size()];
                list.toArray();
            }  
        }
        return ;
    }
    
    
    /* ------------------------------------------------------------ */
    private List<StringlistEntries ()
    {
        checkConnection();
        
        ArrayList<Stringlist = new ArrayList<String>(32);
        JarFile jarFile=;
        if(jarFile==null)
        {
            try
            {
                JarURLConnection jc=(JarURLConnection)((new URL()).openConnection());
                jc.setUseCaches(getUseCaches());
                jarFile=jc.getJarFile();
            }
            catch(Exception e)
            {
                e.printStackTrace();
                 .ignore(e);
            }
        }
        
        Enumeration<JarEntrye=jarFile.entries();
        String dir=.substring(.indexOf("!/")+2);
        while(e.hasMoreElements())
        {
            JarEntry entry = e.nextElement();               
            String name=entry.getName().replace('\\','/');               
            if(!name.startsWith(dir) || name.length()==dir.length())
            {
                continue;
            }
            String listName=name.substring(dir.length());               
            int dash=listName.indexOf('/');
            if (dash>=0)
            {
                //when listing jar:file urls, you get back one
                //entry for the dir itself, which we ignore
                if (dash==0 && listName.length()==1)
                    continue;
                //when listing jar:file urls, all files and
                //subdirs have a leading /, which we remove
                if (dash==0)
                    listName=listName.substring(dash+1, listName.length());
                else
                    listName=listName.substring(0,dash+1);
                
                if (list.contains(listName))
                    continue;
            }
            
            list.add(listName);
        }
        
        return list;
    }
    
    
    
    
    
    /* ------------------------------------------------------------ */
    
Return the length of the resource
    @Override
    public long length()
    {
        if (isDirectory())
            return -1;
        if (!=null)
            return .getSize();
        
        return -1;
    }
    
    /* ------------------------------------------------------------ */
    
Encode according to this resource type. File URIs are not encoded.

Parameters:
uri URI to encode.
Returns:
The uri unchanged.
    @Override
    public String encode(String uri)
    {
        return uri;
    }

    
    
Take a Resource that possibly might use URLConnection caching and turn it into one that doesn't.

Parameters:
resource
Returns:
the non-caching resource
    public static Resource getNonCachingResource (Resource resource)
    {
        if (!(resource instanceof JarFileResource))
            return resource;
        
        JarFileResource oldResource = (JarFileResource)resource;
        
        JarFileResource newResource = new JarFileResource(oldResource.getURL(), false);
        return newResource;
        
    }
    
    
Check if this jar:file: resource is contained in the named resource. Eg jar:file:///a/b/c/foo.jar!/x.html isContainedIn file:///a/b/c/foo.jar

Parameters:
resource
Returns:
true if resource is contained in the named resource
Throws:
java.net.MalformedURLException
    @Override
    public boolean isContainedIn (Resource resource
    throws MalformedURLException
    {
        String string = ;
        int index = string.indexOf("!/");
        if (index > 0)
            string = string.substring(0,index);
        if (string.startsWith("jar:"))
            string = string.substring(4);
        URL url = new URL(string);
        return url.sameFile(resource.getURL());     
    }
New to GrepCode? Check out our FAQ X