KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > connectors > util > JarResourceExtractor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.connectors.util;
25
26 import java.io.*;
27 import java.util.*;
28 import java.util.logging.Logger JavaDoc;
29 import java.util.zip.*;
30
31 import com.sun.logging.LogDomains;
32
33
34 /**
35  * JarResourceExtractor: JarResourceExtractor maps all resources included in a Zip or Jar file.
36  * Additionaly, it provides a method to extract one as a blob.
37  *
38  * @author Sivakumar Thyagarajan
39  */

40
41 public final class JarResourceExtractor {
42     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER);
43
44     //resourceName as String Vs contents as byte[]
45
private Hashtable htJarContents = new Hashtable();
46     
47     /**
48      * creates a JarResourceExtractor. It extracts all resources from a Jar into an
49      * internal hashtable, keyed by resource names.
50      *
51      * @param jarFileName
52      * a jar or zip file
53      */

54     public JarResourceExtractor(String JavaDoc jarFileName) {
55         init(jarFileName);
56     }
57     
58     /**
59      * Extracts a jar resource as a blob.
60      *
61      * @param name
62      * a resource name.
63      */

64     public byte[] getResource(String JavaDoc name) {
65         _logger.finer("getResource: " + name);
66         return (byte[]) htJarContents.get(name);
67     }
68     
69     /** initializes internal hash tables with Jar file resources. */
70     private void init(String JavaDoc jarFileName) {
71         try {
72             //extract resources and put them into the hashtable.
73
FileInputStream fis = new FileInputStream(jarFileName);
74             BufferedInputStream bis = new BufferedInputStream(fis);
75             ZipInputStream zis = new ZipInputStream(bis);
76             extractResources(zis);
77         } catch (Exception JavaDoc ex){
78             ex.printStackTrace();
79         }
80         
81     }
82     
83     /**
84      * @throws FileNotFoundException
85      * @throws IOException
86      */

87     private void extractResources(ZipInputStream zis) throws FileNotFoundException, IOException {
88         ZipEntry ze = null;
89         while ((ze = zis.getNextEntry()) != null) {
90             _logger.finer("ExtractResources : " + ze.getName());
91             extractZipEntryContents(ze, zis);
92         }
93     }
94     
95     /**
96      * @param zis
97      * @throws IOException
98      */

99     private void extractZipEntryContents(ZipEntry ze, ZipInputStream zis) throws IOException {
100             if (ze.isDirectory()) {
101                 return;
102             }
103             
104             _logger.finer("ze.getName()=" + ze.getName() + ","
105                         + "getSize()=" + ze.getSize());
106
107             byte[] b = getZipEntryContents(ze,zis);
108             //If it is a jar go RECURSIVE !!
109
if(ze.getName().trim().endsWith(".jar")){
110                 _logger.finer("JAR - going into it !!");
111                 BufferedInputStream bis = new BufferedInputStream( (new ByteArrayInputStream(b)));
112                 extractResources(new ZipInputStream(bis));
113             } else {
114                 //add to internal resource hashtable
115
htJarContents.put(ze.getName(), b );
116                 if (ze.getName().trim().endsWith("class")){
117                     _logger.finer("CLASS added " + ze.getName());
118                 }
119                 _logger.finer(ze.getName() + ",size="
120                         + b.length + ",csize=" + ze.getCompressedSize());
121             }
122     }
123     
124     private byte[] getZipEntryContents(ZipEntry ze, ZipInputStream zis) throws IOException{
125         int size = (int) ze.getSize();
126         
127         byte[] b = null;
128         // -1 means unknown size.
129
if (size != -1) {
130             //got a proper size, read 'size' bytes
131
b = new byte[(int) size];
132             
133             int rb = 0;
134             int chunk = 0;
135             
136             while (((int) size - rb) > 0) {
137                 chunk = zis.read(b, rb, (int) size - rb);
138                 if (chunk == -1) {
139                     break;
140                 }
141                 rb += chunk;
142             }
143         } else {
144             //size of entry unknown .. keep on reading till we hit a -1
145
ArrayList al = new ArrayList();
146             int c = 0;
147             while( (c = zis.read()) != -1) {
148                 al.add(new Byte JavaDoc((byte)c));
149             }
150             Byte JavaDoc[] btArr = (Byte JavaDoc[])al.toArray(new Byte JavaDoc[]{});
151             b = new byte[btArr.length];
152             _logger.finer("ByteArray length" + btArr.length);
153             for (int i = 0; i < btArr.length; i++) {
154                 b[i] = btArr[i].byteValue();
155             }
156         }
157         
158         return b;
159     }
160 }
161
Popular Tags