|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| DefaultJarArchive.java | 34.6% | 88.7% | 100% | 73.3% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ========================================================================
|
|
| 3 |
*
|
|
| 4 |
* Copyright 2003 The Apache Software Foundation.
|
|
| 5 |
*
|
|
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
| 7 |
* you may not use this file except in compliance with the License.
|
|
| 8 |
* You may obtain a copy of the License at
|
|
| 9 |
*
|
|
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
| 11 |
*
|
|
| 12 |
* Unless required by applicable law or agreed to in writing, software
|
|
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
| 15 |
* See the License for the specific language governing permissions and
|
|
| 16 |
* limitations under the License.
|
|
| 17 |
*
|
|
| 18 |
* ========================================================================
|
|
| 19 |
*/
|
|
| 20 |
package org.apache.cactus.integration.ant.deployment;
|
|
| 21 |
|
|
| 22 |
import java.io.ByteArrayInputStream;
|
|
| 23 |
import java.io.ByteArrayOutputStream;
|
|
| 24 |
import java.io.File;
|
|
| 25 |
import java.io.FileInputStream;
|
|
| 26 |
import java.io.IOException;
|
|
| 27 |
import java.io.InputStream;
|
|
| 28 |
import java.util.ArrayList;
|
|
| 29 |
import java.util.List;
|
|
| 30 |
import java.util.jar.JarInputStream;
|
|
| 31 |
import java.util.zip.ZipEntry;
|
|
| 32 |
|
|
| 33 |
/**
|
|
| 34 |
* Provide convenient methods to read information from a Jar archive.
|
|
| 35 |
*
|
|
| 36 |
* @since Cactus 1.5
|
|
| 37 |
* @version $Id: DefaultJarArchive.java 238812 2004-02-29 10:21:34Z vmassol $
|
|
| 38 |
*/
|
|
| 39 |
public class DefaultJarArchive implements JarArchive |
|
| 40 |
{
|
|
| 41 |
// Instance Variables ------------------------------------------------------
|
|
| 42 |
|
|
| 43 |
/**
|
|
| 44 |
* The content of the archive as an input stream.
|
|
| 45 |
*/
|
|
| 46 |
private byte content[]; |
|
| 47 |
|
|
| 48 |
// Constructors ------------------------------------------------------------
|
|
| 49 |
|
|
| 50 |
/**
|
|
| 51 |
* Constructor.
|
|
| 52 |
*
|
|
| 53 |
* @param theFile The archive file
|
|
| 54 |
* @throws IOException If there was a problem reading the WAR
|
|
| 55 |
*/
|
|
| 56 | 12 |
public DefaultJarArchive(File theFile)
|
| 57 |
throws IOException
|
|
| 58 |
{
|
|
| 59 | 12 |
this(new FileInputStream(theFile)); |
| 60 |
} |
|
| 61 |
|
|
| 62 |
/**
|
|
| 63 |
* Constructor.
|
|
| 64 |
*
|
|
| 65 |
* @param theInputStream The input stream for the archive (it will be closed
|
|
| 66 |
* after the constructor returns)
|
|
| 67 |
* @throws IOException If there was a problem reading the WAR
|
|
| 68 |
*/
|
|
| 69 | 19 |
public DefaultJarArchive(InputStream theInputStream)
|
| 70 |
throws IOException
|
|
| 71 |
{
|
|
| 72 | 19 |
try
|
| 73 |
{
|
|
| 74 | 19 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
| 75 | 19 |
byte[] buffer = new byte[2048]; |
| 76 | 19 |
int bytesRead = -1;
|
| 77 | ? |
while ((bytesRead = theInputStream.read(buffer)) != -1)
|
| 78 |
{
|
|
| 79 | 18 |
baos.write(buffer, 0, bytesRead); |
| 80 |
} |
|
| 81 | 18 |
this.content = baos.toByteArray();
|
| 82 |
} |
|
| 83 |
finally
|
|
| 84 |
{
|
|
| 85 | 19 |
if (theInputStream != null) |
| 86 |
{
|
|
| 87 | 18 |
theInputStream.close(); |
| 88 |
} |
|
| 89 |
} |
|
| 90 |
} |
|
| 91 |
|
|
| 92 |
// Public Methods ----------------------------------------------------------
|
|
| 93 |
|
|
| 94 |
/**
|
|
| 95 |
* @see JarArchive#containsClass(String)
|
|
| 96 |
*/
|
|
| 97 | 3 |
public boolean containsClass(String theClassName) |
| 98 |
throws IOException
|
|
| 99 |
{
|
|
| 100 | 3 |
String resourceName = theClassName.replace('.', '/') + ".class";
|
| 101 | 3 |
return (getResource(resourceName) != null); |
| 102 |
} |
|
| 103 |
|
|
| 104 |
/**
|
|
| 105 |
* @see JarArchive#findResource(String)
|
|
| 106 |
*/
|
|
| 107 | 4 |
public final String findResource(String theName)
|
| 108 |
throws IOException
|
|
| 109 |
{
|
|
| 110 | 4 |
JarInputStream in = null;
|
| 111 | 4 |
try
|
| 112 |
{
|
|
| 113 | 4 |
in = new JarInputStream(getContentAsStream());
|
| 114 | 4 |
ZipEntry entry = null;
|
| 115 | ? |
while ((entry = in.getNextEntry()) != null) |
| 116 |
{
|
|
| 117 | 0 |
String entryName = entry.getName(); |
| 118 | 0 |
int lastSlashIndex = entryName.lastIndexOf('/');
|
| 119 | 0 |
if (lastSlashIndex >= 0)
|
| 120 |
{
|
|
| 121 | 0 |
entryName = entryName.substring(lastSlashIndex + 1); |
| 122 |
} |
|
| 123 | 0 |
if (entryName.equals(theName))
|
| 124 |
{
|
|
| 125 | 0 |
return entry.getName();
|
| 126 |
} |
|
| 127 |
} |
|
| 128 |
} |
|
| 129 |
finally
|
|
| 130 |
{
|
|
| 131 | 4 |
if (in != null) |
| 132 |
{
|
|
| 133 | 4 |
in.close(); |
| 134 |
} |
|
| 135 |
} |
|
| 136 | 4 |
return null; |
| 137 |
} |
|
| 138 |
|
|
| 139 |
/**
|
|
| 140 |
* @see JarArchive#getResource(String)
|
|
| 141 |
*/
|
|
| 142 | 27 |
public final InputStream getResource(String thePath)
|
| 143 |
throws IOException
|
|
| 144 |
{
|
|
| 145 | 27 |
JarInputStream in = null;
|
| 146 | 27 |
try
|
| 147 |
{
|
|
| 148 | 27 |
in = getContentAsStream(); |
| 149 | 27 |
ZipEntry zipEntry = null;
|
| 150 | ? |
while ((zipEntry = in.getNextEntry()) != null) |
| 151 |
{
|
|
| 152 | 42 |
if (thePath.equals(zipEntry.getName()))
|
| 153 |
{
|
|
| 154 | 24 |
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
| 155 | 24 |
byte bytes[] = new byte[2048]; |
| 156 | 24 |
int bytesRead = -1;
|
| 157 | ? |
while ((bytesRead = in.read(bytes)) != -1)
|
| 158 |
{
|
|
| 159 | 21 |
buffer.write(bytes, 0, bytesRead); |
| 160 |
} |
|
| 161 | 24 |
return new ByteArrayInputStream(buffer.toByteArray()); |
| 162 |
} |
|
| 163 |
} |
|
| 164 |
} |
|
| 165 |
finally
|
|
| 166 |
{
|
|
| 167 | 27 |
if (in != null) |
| 168 |
{
|
|
| 169 | 27 |
in.close(); |
| 170 |
} |
|
| 171 |
} |
|
| 172 | 3 |
return null; |
| 173 |
} |
|
| 174 |
|
|
| 175 |
/**
|
|
| 176 |
* @see JarArchive#getResources(String)
|
|
| 177 |
*/
|
|
| 178 | 2 |
public final List getResources(String thePath)
|
| 179 |
throws IOException
|
|
| 180 |
{
|
|
| 181 | 2 |
List resources = new ArrayList();
|
| 182 | 2 |
JarInputStream in = null;
|
| 183 | 2 |
try
|
| 184 |
{
|
|
| 185 | 2 |
in = getContentAsStream(); |
| 186 | 2 |
ZipEntry zipEntry = null;
|
| 187 | ? |
while ((zipEntry = in.getNextEntry()) != null) |
| 188 |
{
|
|
| 189 | 3 |
if ((zipEntry.getName().startsWith(thePath)
|
| 190 |
&& !zipEntry.getName().equals(thePath))) |
|
| 191 |
{
|
|
| 192 | 1 |
resources.add(zipEntry.getName()); |
| 193 |
} |
|
| 194 |
} |
|
| 195 |
} |
|
| 196 |
finally
|
|
| 197 |
{
|
|
| 198 | 2 |
if (in != null) |
| 199 |
{
|
|
| 200 | 2 |
in.close(); |
| 201 |
} |
|
| 202 |
} |
|
| 203 | 2 |
return resources;
|
| 204 |
} |
|
| 205 |
|
|
| 206 |
// Protected Methods -------------------------------------------------------
|
|
| 207 |
|
|
| 208 |
/**
|
|
| 209 |
* Returns the content of the archive as <code>JarInputStream</code>.
|
|
| 210 |
*
|
|
| 211 |
* @return The input stream
|
|
| 212 |
* @throws IOException If an exception occurred reading the archive
|
|
| 213 |
*/
|
|
| 214 | 33 |
protected final JarInputStream getContentAsStream()
|
| 215 |
throws IOException
|
|
| 216 |
{
|
|
| 217 | 33 |
return new JarInputStream(new ByteArrayInputStream(this.content)); |
| 218 |
} |
|
| 219 |
} |
|
| 220 |
|
|
||||||||||