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