KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > deployment > DefaultJarArchive


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 JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.jar.JarInputStream JavaDoc;
31 import java.util.zip.ZipEntry JavaDoc;
32
33 /**
34  * Provide convenient methods to read information from a Jar archive.
35  *
36  * @since Cactus 1.5
37  * @version $Id: DefaultJarArchive.java,v 1.4 2004/02/29 10:15:47 vmassol Exp $
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     public DefaultJarArchive(File JavaDoc theFile)
57         throws IOException JavaDoc
58     {
59         this(new FileInputStream JavaDoc(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     public DefaultJarArchive(InputStream JavaDoc theInputStream)
70         throws IOException JavaDoc
71     {
72         try
73         {
74             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
75             byte[] buffer = new byte[2048];
76             int bytesRead = -1;
77             while ((bytesRead = theInputStream.read(buffer)) != -1)
78             {
79                 baos.write(buffer, 0, bytesRead);
80             }
81             this.content = baos.toByteArray();
82         }
83         finally
84         {
85             if (theInputStream != null)
86             {
87                 theInputStream.close();
88             }
89         }
90     }
91
92     // Public Methods ----------------------------------------------------------
93

94     /**
95      * @see JarArchive#containsClass(String)
96      */

97     public boolean containsClass(String JavaDoc theClassName)
98         throws IOException JavaDoc
99     {
100         String JavaDoc resourceName = theClassName.replace('.', '/') + ".class";
101         return (getResource(resourceName) != null);
102     }
103
104     /**
105      * @see JarArchive#findResource(String)
106      */

107     public final String JavaDoc findResource(String JavaDoc theName)
108         throws IOException JavaDoc
109     {
110         JarInputStream JavaDoc in = null;
111         try
112         {
113             in = new JarInputStream JavaDoc(getContentAsStream());
114             ZipEntry JavaDoc entry = null;
115             while ((entry = in.getNextEntry()) != null)
116             {
117                 String JavaDoc entryName = entry.getName();
118                 int lastSlashIndex = entryName.lastIndexOf('/');
119                 if (lastSlashIndex >= 0)
120                 {
121                     entryName = entryName.substring(lastSlashIndex + 1);
122                 }
123                 if (entryName.equals(theName))
124                 {
125                     return entry.getName();
126                 }
127             }
128         }
129         finally
130         {
131             if (in != null)
132             {
133                 in.close();
134             }
135         }
136         return null;
137     }
138
139     /**
140      * @see JarArchive#getResource(String)
141      */

142     public final InputStream JavaDoc getResource(String JavaDoc thePath)
143         throws IOException JavaDoc
144     {
145         JarInputStream JavaDoc in = null;
146         try
147         {
148             in = getContentAsStream();
149             ZipEntry JavaDoc zipEntry = null;
150             while ((zipEntry = in.getNextEntry()) != null)
151             {
152                 if (thePath.equals(zipEntry.getName()))
153                 {
154                     ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
155                     byte bytes[] = new byte[2048];
156                     int bytesRead = -1;
157                     while ((bytesRead = in.read(bytes)) != -1)
158                     {
159                         buffer.write(bytes, 0, bytesRead);
160                     }
161                     return new ByteArrayInputStream JavaDoc(buffer.toByteArray());
162                 }
163             }
164         }
165         finally
166         {
167             if (in != null)
168             {
169                 in.close();
170             }
171         }
172         return null;
173     }
174
175     /**
176      * @see JarArchive#getResources(String)
177      */

178     public final List JavaDoc getResources(String JavaDoc thePath)
179         throws IOException JavaDoc
180     {
181         List JavaDoc resources = new ArrayList JavaDoc();
182         JarInputStream JavaDoc in = null;
183         try
184         {
185             in = getContentAsStream();
186             ZipEntry JavaDoc zipEntry = null;
187             while ((zipEntry = in.getNextEntry()) != null)
188             {
189                 if ((zipEntry.getName().startsWith(thePath)
190                  && !zipEntry.getName().equals(thePath)))
191                 {
192                     resources.add(zipEntry.getName());
193                 }
194             }
195         }
196         finally
197         {
198             if (in != null)
199             {
200                 in.close();
201             }
202         }
203         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     protected final JarInputStream JavaDoc getContentAsStream()
215         throws IOException JavaDoc
216     {
217         return new JarInputStream JavaDoc(new ByteArrayInputStream JavaDoc(this.content));
218     }
219 }
220
Popular Tags