|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DefaultJarArchive.java | 34.6% | 88.7% | 100% | 73.3% |
|
1 |
/*
|
|
2 |
* ====================================================================
|
|
3 |
*
|
|
4 |
* The Apache Software License, Version 1.1
|
|
5 |
*
|
|
6 |
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
|
7 |
* reserved.
|
|
8 |
*
|
|
9 |
* Redistribution and use in source and binary forms, with or without
|
|
10 |
* modification, are permitted provided that the following conditions
|
|
11 |
* are met:
|
|
12 |
*
|
|
13 |
* 1. Redistributions of source code must retain the above copyright
|
|
14 |
* notice, this list of conditions and the following disclaimer.
|
|
15 |
*
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
17 |
* notice, this list of conditions and the following disclaimer in
|
|
18 |
* the documentation and/or other materials provided with the
|
|
19 |
* distribution.
|
|
20 |
*
|
|
21 |
* 3. The end-user documentation included with the redistribution, if
|
|
22 |
* any, must include the following acknowlegement:
|
|
23 |
* "This product includes software developed by the
|
|
24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
27 |
*
|
|
28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
29 |
* Foundation" must not be used to endorse or promote products
|
|
30 |
* derived from this software without prior written permission. For
|
|
31 |
* written permission, please contact apache@apache.org.
|
|
32 |
*
|
|
33 |
* 5. Products derived from this software may not be called "Apache"
|
|
34 |
* nor may "Apache" appear in their names without prior written
|
|
35 |
* permission of the Apache Group.
|
|
36 |
*
|
|
37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
48 |
* SUCH DAMAGE.
|
|
49 |
* ====================================================================
|
|
50 |
*
|
|
51 |
* This software consists of voluntary contributions made by many
|
|
52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
53 |
* information on the Apache Software Foundation, please see
|
|
54 |
* <http://www.apache.org/>.
|
|
55 |
*
|
|
56 |
*/
|
|
57 |
package org.apache.cactus.integration.ant.deployment;
|
|
58 |
|
|
59 |
import java.io.ByteArrayInputStream;
|
|
60 |
import java.io.ByteArrayOutputStream;
|
|
61 |
import java.io.File;
|
|
62 |
import java.io.FileInputStream;
|
|
63 |
import java.io.IOException;
|
|
64 |
import java.io.InputStream;
|
|
65 |
import java.util.ArrayList;
|
|
66 |
import java.util.List;
|
|
67 |
import java.util.jar.JarInputStream;
|
|
68 |
import java.util.zip.ZipEntry;
|
|
69 |
|
|
70 |
/**
|
|
71 |
* Provide convenient methods to read information from a Jar archive.
|
|
72 |
*
|
|
73 |
* @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
|
|
74 |
*
|
|
75 |
* @since Cactus 1.5
|
|
76 |
* @version $Id: DefaultJarArchive.java,v 1.1.2.1 2003/10/25 17:22:05 vmassol Exp $
|
|
77 |
*/
|
|
78 |
public class DefaultJarArchive implements JarArchive |
|
79 |
{ |
|
80 |
// Instance Variables ------------------------------------------------------
|
|
81 |
|
|
82 |
/**
|
|
83 |
* The content of the archive as an input stream.
|
|
84 |
*/
|
|
85 |
private byte content[]; |
|
86 |
|
|
87 |
// Constructors ------------------------------------------------------------
|
|
88 |
|
|
89 |
/**
|
|
90 |
* Constructor.
|
|
91 |
*
|
|
92 |
* @param theFile The archive file
|
|
93 |
* @throws IOException If there was a problem reading the WAR
|
|
94 |
*/
|
|
95 | 111 |
public DefaultJarArchive(File theFile)
|
96 |
throws IOException
|
|
97 |
{ |
|
98 | 111 |
this(new FileInputStream(theFile)); |
99 |
} |
|
100 |
|
|
101 |
/**
|
|
102 |
* Constructor.
|
|
103 |
*
|
|
104 |
* @param theInputStream The input stream for the archive (it will be closed
|
|
105 |
* after the constructor returns)
|
|
106 |
* @throws IOException If there was a problem reading the WAR
|
|
107 |
*/
|
|
108 | 133 |
public DefaultJarArchive(InputStream theInputStream)
|
109 |
throws IOException
|
|
110 |
{ |
|
111 | 133 |
try
|
112 |
{ |
|
113 | 133 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
114 | 133 |
byte[] buffer = new byte[2048]; |
115 | 133 |
int bytesRead = -1;
|
116 | ? |
while ((bytesRead = theInputStream.read(buffer)) != -1)
|
117 |
{ |
|
118 | 1490 |
baos.write(buffer, 0, bytesRead); |
119 |
} |
|
120 | 132 |
this.content = baos.toByteArray();
|
121 |
} |
|
122 |
finally
|
|
123 |
{ |
|
124 | 133 |
if (theInputStream != null) |
125 |
{ |
|
126 | 132 |
theInputStream.close(); |
127 |
} |
|
128 |
} |
|
129 |
} |
|
130 |
|
|
131 |
// Public Methods ----------------------------------------------------------
|
|
132 |
|
|
133 |
/**
|
|
134 |
* @see JarArchive#containsClass(String)
|
|
135 |
*/
|
|
136 | 18 |
public boolean containsClass(String theClassName) |
137 |
throws IOException
|
|
138 |
{ |
|
139 | 18 |
String resourceName = theClassName.replace('.', '/') + ".class";
|
140 | 18 |
return (getResource(resourceName) != null); |
141 |
} |
|
142 |
|
|
143 |
/**
|
|
144 |
* @see JarArchive#findResource(String)
|
|
145 |
*/
|
|
146 | 4 |
public final String findResource(String theName)
|
147 |
throws IOException
|
|
148 |
{ |
|
149 | 4 |
JarInputStream in = null;
|
150 | 4 |
try
|
151 |
{ |
|
152 | 4 |
in = new JarInputStream(getContentAsStream());
|
153 | 4 |
ZipEntry entry = null;
|
154 | ? |
while ((entry = in.getNextEntry()) != null) |
155 |
{ |
|
156 | 0 |
String entryName = entry.getName(); |
157 | 0 |
int lastSlashIndex = entryName.lastIndexOf('/');
|
158 | 0 |
if (lastSlashIndex >= 0)
|
159 |
{ |
|
160 | 0 |
entryName = entryName.substring(lastSlashIndex + 1); |
161 |
} |
|
162 | 0 |
if (entryName.equals(theName))
|
163 |
{ |
|
164 | 0 |
return entry.getName();
|
165 |
} |
|
166 |
} |
|
167 |
} |
|
168 |
finally
|
|
169 |
{ |
|
170 | 4 |
if (in != null) |
171 |
{ |
|
172 | 4 |
in.close(); |
173 |
} |
|
174 |
} |
|
175 | 4 |
return null; |
176 |
} |
|
177 |
|
|
178 |
/**
|
|
179 |
* @see JarArchive#getResource(String)
|
|
180 |
*/
|
|
181 | 156 |
public final InputStream getResource(String thePath)
|
182 |
throws IOException
|
|
183 |
{ |
|
184 | 156 |
JarInputStream in = null;
|
185 | 156 |
try
|
186 |
{ |
|
187 | 156 |
in = getContentAsStream(); |
188 | 156 |
ZipEntry zipEntry = null;
|
189 | ? |
while ((zipEntry = in.getNextEntry()) != null) |
190 |
{ |
|
191 | 371 |
if (thePath.equals(zipEntry.getName()))
|
192 |
{ |
|
193 | 72 |
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
194 | 72 |
byte bytes[] = new byte[2048]; |
195 | 72 |
int bytesRead = -1;
|
196 | ? |
while ((bytesRead = in.read(bytes)) != -1)
|
197 |
{ |
|
198 | 65 |
buffer.write(bytes, 0, bytesRead); |
199 |
} |
|
200 | 72 |
return new ByteArrayInputStream(buffer.toByteArray()); |
201 |
} |
|
202 |
} |
|
203 |
} |
|
204 |
finally
|
|
205 |
{ |
|
206 | 156 |
if (in != null) |
207 |
{ |
|
208 | 156 |
in.close(); |
209 |
} |
|
210 |
} |
|
211 | 84 |
return null; |
212 |
} |
|
213 |
|
|
214 |
/**
|
|
215 |
* @see JarArchive#getResources(String)
|
|
216 |
*/
|
|
217 | 72 |
public final List getResources(String thePath)
|
218 |
throws IOException
|
|
219 |
{ |
|
220 | 72 |
List resources = new ArrayList();
|
221 | 72 |
JarInputStream in = null;
|
222 | 72 |
try
|
223 |
{ |
|
224 | 72 |
in = getContentAsStream(); |
225 | 72 |
ZipEntry zipEntry = null;
|
226 | ? |
while ((zipEntry = in.getNextEntry()) != null) |
227 |
{ |
|
228 | 173 |
if ((zipEntry.getName().startsWith(thePath)
|
229 |
&& !zipEntry.getName().equals(thePath))) |
|
230 |
{ |
|
231 | 26 |
resources.add(zipEntry.getName()); |
232 |
} |
|
233 |
} |
|
234 |
} |
|
235 |
finally
|
|
236 |
{ |
|
237 | 72 |
if (in != null) |
238 |
{ |
|
239 | 72 |
in.close(); |
240 |
} |
|
241 |
} |
|
242 | 72 |
return resources;
|
243 |
} |
|
244 |
|
|
245 |
// Protected Methods -------------------------------------------------------
|
|
246 |
|
|
247 |
/**
|
|
248 |
* Returns the content of the archive as <code>JarInputStream</code>.
|
|
249 |
*
|
|
250 |
* @return The input stream
|
|
251 |
* @throws IOException If an exception occurred reading the archive
|
|
252 |
*/
|
|
253 | 232 |
protected final JarInputStream getContentAsStream()
|
254 |
throws IOException
|
|
255 |
{ |
|
256 | 232 |
return new JarInputStream(new ByteArrayInputStream(this.content)); |
257 |
} |
|
258 |
} |
|
259 |
|
|