KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > classfile > impl > NestedZipFileCodeBase


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.classfile.impl;
21
22 import java.io.BufferedOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import edu.umd.cs.findbugs.classfile.ICodeBase;
31 import edu.umd.cs.findbugs.classfile.ICodeBaseEntry;
32 import edu.umd.cs.findbugs.classfile.ICodeBaseIterator;
33 import edu.umd.cs.findbugs.classfile.ICodeBaseLocator;
34 import edu.umd.cs.findbugs.classfile.IScannableCodeBase;
35 import edu.umd.cs.findbugs.classfile.ResourceNotFoundException;
36 import edu.umd.cs.findbugs.io.IO;
37
38 /**
39  * A scannable code base class for a zip (or Jar) file nested inside
40  * some other codebase. These are handled by extracting the nested
41  * zip/jar file to a temporary file, and delegating to an
42  * internal ZipFileCodeBase that reads from the temporary file.
43  *
44  * @author David Hovemeyer
45  */

46 public class NestedZipFileCodeBase extends AbstractScannableCodeBase implements IScannableCodeBase {
47     private ICodeBase parentCodeBase;
48     private String JavaDoc resourceName;
49     private File JavaDoc tempFile;
50     private ZipFileCodeBase delegateCodeBase;
51
52     /**
53      * Constructor.
54      *
55      * @param codeBaseLocator the codebase locator for this codebase
56      */

57     public NestedZipFileCodeBase(NestedZipFileCodeBaseLocator codeBaseLocator)
58             throws ResourceNotFoundException, IOException JavaDoc {
59         super(codeBaseLocator);
60         this.parentCodeBase = codeBaseLocator.getParentCodeBase();
61         this.resourceName = codeBaseLocator.getResourceName();
62         
63         InputStream JavaDoc inputStream = null;
64         OutputStream JavaDoc outputStream = null;
65         try {
66             // Create a temp file
67
this.tempFile = File.createTempFile("findbugs", ".zip");
68             tempFile.deleteOnExit(); // just in case we crash before the codebase is closed
69

70             // Copy nested zipfile to the temporary file
71
// FIXME: potentially long blocking operation - should be interruptible
72
inputStream = parentCodeBase.lookupResource(resourceName).openResource();
73             outputStream = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(tempFile));
74             IO.copy(inputStream, outputStream);
75             outputStream.flush();
76             
77             // Create the delegate to read from the temporary file
78
delegateCodeBase = new ZipFileCodeBase(codeBaseLocator, tempFile);
79         } finally {
80             if (inputStream != null) {
81                 IO.close(inputStream);
82             }
83             
84             if (outputStream != null) {
85                 IO.close(outputStream);
86             }
87         }
88     }
89     
90     /* (non-Javadoc)
91      * @see edu.umd.cs.findbugs.classfile.IScannableCodeBase#iterator()
92      */

93     public ICodeBaseIterator iterator() throws InterruptedException JavaDoc {
94         return new DelegatingCodeBaseIterator(this, delegateCodeBase);
95     }
96     
97     /* (non-Javadoc)
98      * @see edu.umd.cs.findbugs.classfile.ICodeBase#lookupResource(java.lang.String)
99      */

100     public ICodeBaseEntry lookupResource(String JavaDoc resourceName) throws ResourceNotFoundException {
101         ICodeBaseEntry delegateCodeBaseEntry = delegateCodeBase.lookupResource(resourceName);
102         return new DelegatingCodeBaseEntry(this, delegateCodeBaseEntry);
103     }
104     
105     /* (non-Javadoc)
106      * @see edu.umd.cs.findbugs.classfile.ICodeBase#getPathName()
107      */

108     public String JavaDoc getPathName() {
109         return null;
110     }
111     
112     /* (non-Javadoc)
113      * @see edu.umd.cs.findbugs.classfile.ICodeBase#close()
114      */

115     public void close() {
116         delegateCodeBase.close();
117         tempFile.delete();
118     }
119 }
120
Popular Tags