View Javadoc
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  public class JarHolder
48  {
49      private String urlpath = null;
50  
51      private JarFile theJar = null;
52  
53      private JarURLConnection conn = null;
54  
55      public JarHolder( String urlpath )
56      {
57          this.urlpath = urlpath;
58  
59          init();
60      }
61  
62      public void init()
63      {
64          try
65          {
66              URL url = new URL( urlpath );
67  
68              conn = (JarURLConnection) url.openConnection();
69  
70              conn.setAllowUserInteraction( false );
71  
72              conn.setDoInput( true );
73  
74              conn.setDoOutput( false );
75  
76              conn.connect();
77  
78              theJar = conn.getJarFile();
79          }
80          catch ( IOException ioe )
81          {
82          }
83      }
84  
85      public void close()
86      {
87          try
88          {
89              theJar.close();
90          }
91          catch ( Exception e )
92          {
93          }
94  
95          theJar = null;
96  
97          conn = null;
98      }
99  
100     public InputStream getResource( String theentry )
101         throws ResourceNotFoundException
102     {
103         InputStream data = null;
104 
105         try
106         {
107             JarEntry entry = theJar.getJarEntry( theentry );
108 
109             if ( entry != null )
110             {
111                 data = theJar.getInputStream( entry );
112             }
113         }
114         catch ( Exception fnfe )
115         {
116             throw new ResourceNotFoundException( fnfe.getMessage() );
117         }
118 
119         return data;
120     }
121 
122     public Hashtable getEntries()
123     {
124         Hashtable allEntries = new Hashtable( 559 );
125 
126         if ( theJar != null )
127         {
128             Enumeration all = theJar.entries();
129 
130             while ( all.hasMoreElements() )
131             {
132                 JarEntry je = (JarEntry) all.nextElement();
133 
134                 // We don't map plain directory entries
135                 if ( !je.isDirectory() )
136                 {
137                     allEntries.put( je.getName(), this.urlpath );
138                 }
139             }
140         }
141         return allEntries;
142     }
143 
144     public String getUrlPath()
145     {
146         return urlpath;
147     }
148 
149     public PlexusResource getPlexusResource( final String name )
150     {
151         final JarEntry entry = theJar.getJarEntry( name );
152         if ( entry == null )
153         {
154             return null;
155         }
156         return new PlexusResource()
157         {
158             public File getFile()
159                 throws IOException
160             {
161                 return null;
162             }
163 
164             public InputStream getInputStream()
165                 throws IOException
166             {
167                 return theJar.getInputStream( entry );
168             }
169 
170             public String getName()
171             {
172                 return conn.getURL() + name;
173             }
174 
175             public URI getURI()
176                 throws IOException
177             {
178                 return null;
179             }
180 
181             public URL getURL()
182                 throws IOException
183             {
184                 return new URL( conn.getJarFileURL(), name );
185             }
186         };
187     }
188 }