Coverage Report - org.codehaus.plexus.resource.loader.JarHolder
 
Classes in this File Line Coverage Branch Coverage Complexity
JarHolder
0 %
0/46
0 %
0/10
1,833
JarHolder$1
0 %
0/6
N/A
1,833
 
 1  
 package org.codehaus.plexus.resource.loader;
 2  
 
 3  
 /*
 4  
  * The MIT License
 5  
  *
 6  
  * Copyright (c) 2004, The Codehaus
 7  
  *
 8  
  * Permission is hereby granted, free of charge, to any person obtaining a copy of
 9  
  * this software and associated documentation files (the "Software"), to deal in
 10  
  * the Software without restriction, including without limitation the rights to
 11  
  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 12  
  * of the Software, and to permit persons to whom the Software is furnished to do
 13  
  * so, subject to the following conditions:
 14  
  *
 15  
  * The above copyright notice and this permission notice shall be included in all
 16  
  * copies or substantial portions of the Software.
 17  
  *
 18  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 24  
  * SOFTWARE.
 25  
  */
 26  
 
 27  
 import java.io.File;
 28  
 import java.io.IOException;
 29  
 import java.io.InputStream;
 30  
 import java.net.JarURLConnection;
 31  
 import java.net.URI;
 32  
 import java.net.URL;
 33  
 import java.util.Enumeration;
 34  
 import java.util.Hashtable;
 35  
 import java.util.jar.JarEntry;
 36  
 import java.util.jar.JarFile;
 37  
 
 38  
 import org.codehaus.plexus.resource.PlexusResource;
 39  
 
 40  
 /**
 41  
  * A small wrapper around a Jar
 42  
  * 
 43  
  * @author <a href="mailto:daveb@miceda-data.com">Dave Bryson</a>
 44  
  * @author Jason van Zyl
 45  
  * @version $Id$
 46  
  */
 47  0
 public class JarHolder
 48  
 {
 49  0
     private String urlpath = null;
 50  
 
 51  0
     private JarFile theJar = null;
 52  
 
 53  0
     private JarURLConnection conn = null;
 54  
 
 55  
     public JarHolder( String urlpath )
 56  0
     {
 57  0
         this.urlpath = urlpath;
 58  
 
 59  0
         init();
 60  0
     }
 61  
 
 62  
     public void init()
 63  
     {
 64  
         try
 65  
         {
 66  0
             URL url = new URL( urlpath );
 67  
 
 68  0
             conn = (JarURLConnection) url.openConnection();
 69  
 
 70  0
             conn.setAllowUserInteraction( false );
 71  
 
 72  0
             conn.setDoInput( true );
 73  
 
 74  0
             conn.setDoOutput( false );
 75  
 
 76  0
             conn.connect();
 77  
 
 78  0
             theJar = conn.getJarFile();
 79  
         }
 80  0
         catch ( IOException ioe )
 81  
         {
 82  0
         }
 83  0
     }
 84  
 
 85  
     public void close()
 86  
     {
 87  
         try
 88  
         {
 89  0
             theJar.close();
 90  
         }
 91  0
         catch ( Exception e )
 92  
         {
 93  0
         }
 94  
 
 95  0
         theJar = null;
 96  
 
 97  0
         conn = null;
 98  0
     }
 99  
 
 100  
     public InputStream getResource( String theentry )
 101  
         throws ResourceNotFoundException
 102  
     {
 103  0
         InputStream data = null;
 104  
 
 105  
         try
 106  
         {
 107  0
             JarEntry entry = theJar.getJarEntry( theentry );
 108  
 
 109  0
             if ( entry != null )
 110  
             {
 111  0
                 data = theJar.getInputStream( entry );
 112  
             }
 113  
         }
 114  0
         catch ( Exception fnfe )
 115  
         {
 116  0
             throw new ResourceNotFoundException( fnfe.getMessage() );
 117  0
         }
 118  
 
 119  0
         return data;
 120  
     }
 121  
 
 122  
     public Hashtable getEntries()
 123  
     {
 124  0
         Hashtable allEntries = new Hashtable( 559 );
 125  
 
 126  0
         if ( theJar != null )
 127  
         {
 128  0
             Enumeration all = theJar.entries();
 129  
 
 130  0
             while ( all.hasMoreElements() )
 131  
             {
 132  0
                 JarEntry je = (JarEntry) all.nextElement();
 133  
 
 134  
                 // We don't map plain directory entries
 135  0
                 if ( !je.isDirectory() )
 136  
                 {
 137  0
                     allEntries.put( je.getName(), this.urlpath );
 138  
                 }
 139  0
             }
 140  
         }
 141  0
         return allEntries;
 142  
     }
 143  
 
 144  
     public String getUrlPath()
 145  
     {
 146  0
         return urlpath;
 147  
     }
 148  
 
 149  
     public PlexusResource getPlexusResource( final String name )
 150  
     {
 151  0
         final JarEntry entry = theJar.getJarEntry( name );
 152  0
         if ( entry == null )
 153  
         {
 154  0
             return null;
 155  
         }
 156  0
         return new PlexusResource()
 157  0
         {
 158  
             public File getFile()
 159  
                 throws IOException
 160  
             {
 161  0
                 return null;
 162  
             }
 163  
 
 164  
             public InputStream getInputStream()
 165  
                 throws IOException
 166  
             {
 167  0
                 return theJar.getInputStream( entry );
 168  
             }
 169  
 
 170  
             public String getName()
 171  
             {
 172  0
                 return conn.getURL() + name;
 173  
             }
 174  
 
 175  
             public URI getURI()
 176  
                 throws IOException
 177  
             {
 178  0
                 return null;
 179  
             }
 180  
 
 181  
             public URL getURL()
 182  
                 throws IOException
 183  
             {
 184  0
                 return new URL( conn.getJarFileURL(), name );
 185  
             }
 186  
         };
 187  
     }
 188  
 }