001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042: * JarExploder.java
043: *
044: * Created on October 20, 2004, 1:43 PM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb.util;
048:
049: import java.io.BufferedInputStream;
050: import java.io.BufferedOutputStream;
051: import java.io.File;
052: import java.io.FileInputStream;
053: import java.io.FileNotFoundException;
054: import java.io.FileOutputStream;
055: import java.io.IOException;
056: import java.util.*;
057: import java.util.zip.ZipEntry;
058: import java.util.zip.ZipFile;
059: import java.util.zip.ZipInputStream;
060: import org.openide.ErrorManager;
061:
062: /**
063: * A utility class to explode the given jar file
064: *
065: * @author cao
066: */
067: public class JarExploder {
068:
069: private boolean classOnly = false;
070:
071: public JarExploder() {
072: this (false);
073: }
074:
075: public JarExploder(boolean classOnly) {
076: this .classOnly = classOnly;
077: }
078:
079: public static ArrayList getAllClasses(String jarPath)
080: throws FileNotFoundException, IOException {
081: try {
082: ArrayList allClazz = new ArrayList();
083:
084: // First, just extract the entry size only
085: ZipFile zf = new ZipFile(jarPath);
086: Map entrySizes = new HashMap();
087:
088: Enumeration e = zf.entries();
089: while (e.hasMoreElements()) {
090: ZipEntry ze = (ZipEntry) e.nextElement();
091:
092: entrySizes.put(ze.getName(), new Integer((int) ze
093: .getSize()));
094: }
095:
096: zf.close();
097:
098: // Now, extract resources and look for the deployment descriptors.
099:
100: FileInputStream fileInputStream = new FileInputStream(
101: jarPath);
102: BufferedInputStream bufferedInputStream = new BufferedInputStream(
103: fileInputStream);
104: ZipInputStream zipInputStream = new ZipInputStream(
105: bufferedInputStream);
106:
107: String stdXml = null;
108: String vendorXml = null;
109: ZipEntry zipEntry = null;
110: while ((zipEntry = zipInputStream.getNextEntry()) != null) {
111: if (zipEntry.isDirectory()) {
112: continue;
113: }
114:
115: int size = (int) zipEntry.getSize();
116:
117: // -1 means unknown size.
118: if (size == -1) {
119: size = ((Integer) entrySizes
120: .get(zipEntry.getName())).intValue();
121: }
122:
123: // Read the content of this zip entry
124: byte[] b = new byte[(int) size];
125: int rb = 0;
126: int chunk = 0;
127: while (((int) size - rb) > 0) {
128: chunk = zipInputStream.read(b, rb, (int) size - rb);
129: if (chunk == -1) {
130: break;
131: }
132:
133: rb += chunk;
134: }
135:
136: if (zipEntry.getName().endsWith(".class")) {
137: int index = zipEntry.getName().indexOf('.');
138: allClazz.add(zipEntry.getName().substring(0, index)
139: .replace('/', '.'));
140: }
141: }
142:
143: return allClazz;
144:
145: } catch (java.io.FileNotFoundException e) {
146: // Log eror
147: String logMsg = "Error occurred when trying to explode jar file. Cannot find file "
148: + jarPath;
149: ErrorManager
150: .getDefault()
151: .getInstance(
152: "org.netbeans.modules.visualweb.ejb.util.JarExploder")
153: .log(ErrorManager.ERROR, logMsg);
154: e.printStackTrace();
155:
156: throw e;
157: } catch (java.io.IOException e) {
158: // Log eror
159: String logMsg = "Error occurred when trying to explode jar file. Cannot read file "
160: + jarPath;
161: ErrorManager
162: .getDefault()
163: .getInstance(
164: "org.netbeans.modules.visualweb.ejb.util.JarExploder")
165: .log(ErrorManager.ERROR, logMsg);
166: e.printStackTrace();
167:
168: throw e;
169: }
170: }
171:
172: public void explodeJar(String destDir, String jarPath)
173: throws FileNotFoundException, IOException {
174: try {
175: // First, just extract the entry size only
176: ZipFile zf = new ZipFile(jarPath);
177: Map entrySizes = new HashMap();
178:
179: Enumeration e = zf.entries();
180: while (e.hasMoreElements()) {
181: ZipEntry ze = (ZipEntry) e.nextElement();
182:
183: entrySizes.put(ze.getName(), new Integer((int) ze
184: .getSize()));
185: }
186:
187: zf.close();
188:
189: // Now, extract resources and look for the deployment descriptors.
190:
191: FileInputStream fileInputStream = new FileInputStream(
192: jarPath);
193: BufferedInputStream bufferedInputStream = new BufferedInputStream(
194: fileInputStream);
195: ZipInputStream zipInputStream = new ZipInputStream(
196: bufferedInputStream);
197:
198: String stdXml = null;
199: String vendorXml = null;
200: ZipEntry zipEntry = null;
201: while ((zipEntry = zipInputStream.getNextEntry()) != null) {
202: if (zipEntry.isDirectory()) {
203: continue;
204: }
205:
206: int size = (int) zipEntry.getSize();
207:
208: // -1 means unknown size.
209: if (size == -1) {
210: size = ((Integer) entrySizes
211: .get(zipEntry.getName())).intValue();
212: }
213:
214: // Read the content of this zip entry
215: byte[] b = new byte[(int) size];
216: int rb = 0;
217: int chunk = 0;
218: while (((int) size - rb) > 0) {
219: chunk = zipInputStream.read(b, rb, (int) size - rb);
220: if (chunk == -1) {
221: break;
222: }
223:
224: rb += chunk;
225: }
226:
227: if (!this .classOnly
228: || (this .classOnly && zipEntry.getName()
229: .endsWith(".class"))) {
230: saveFile(destDir, zipEntry.getName(), b);
231: }
232: }
233: } catch (java.io.FileNotFoundException e) {
234: // Log eror
235: String logMsg = "Error occurred when trying to explode jar file. Cannot find file "
236: + jarPath;
237: ErrorManager
238: .getDefault()
239: .getInstance(
240: "org.netbeans.modules.visualweb.ejb.util.JarExploder")
241: .log(ErrorManager.ERROR, logMsg);
242: e.printStackTrace();
243:
244: throw e;
245: } catch (java.io.IOException e) {
246: // Log eror
247: String logMsg = "Error occurred when trying to explode jar file. Cannot read file "
248: + jarPath;
249: ErrorManager
250: .getDefault()
251: .getInstance(
252: "org.netbeans.modules.visualweb.ejb.util.JarExploder")
253: .log(ErrorManager.ERROR, logMsg);
254: e.printStackTrace();
255:
256: throw e;
257: }
258: }
259:
260: private String saveFile(String destDir, String fileName,
261: byte[] bytes) throws FileNotFoundException, IOException {
262:
263: File file = new File(destDir, fileName);
264:
265: try {
266: if (!file.exists())
267: file.getParentFile().mkdirs();
268:
269: FileOutputStream fos = new FileOutputStream(file);
270: BufferedOutputStream bos = new BufferedOutputStream(fos);
271: bos.write(bytes);
272: bos.flush();
273: bos.close();
274: fos.close();
275: return file.getAbsolutePath();
276: } catch (java.io.FileNotFoundException e) {
277: // Log eror
278: String logMsg = "Error occurred when trying to save file. Cannot find file "
279: + file.getAbsolutePath();
280: ErrorManager
281: .getDefault()
282: .getInstance(
283: "org.netbeans.modules.visualweb.ejb.load.DeploymentDescriptorExtractor")
284: .log(ErrorManager.ERROR, logMsg);
285: e.printStackTrace();
286:
287: throw e;
288: } catch (java.io.IOException e) {
289: // Log eror
290: String logMsg = "Error occurred when trying to save file. Cannot write to file "
291: + file.getAbsolutePath();
292: ErrorManager
293: .getDefault()
294: .getInstance(
295: "org.netbeans.modules.visualweb.ejb.load.DeploymentDescriptorExtractor")
296: .log(ErrorManager.ERROR, logMsg);
297: e.printStackTrace();
298:
299: throw e;
300: }
301: }
302:
303: }
|