1 package org.codehaus.plexus.resource.loader;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
42
43
44
45
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
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 }