001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.util;
018:
019: import java.io.File;
020: import java.io.FileFilter;
021: import java.io.FileNotFoundException;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.OutputStream;
026: import java.util.Enumeration;
027: import java.util.jar.JarEntry;
028: import java.util.jar.JarFile;
029:
030: /**
031: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
032: *
033: * Creates a a temp directory to which a JarFile is expanded and can be
034: * manipulated. All operations are performed by an internal instance of
035: * {@link DirectoryHelper}.
036: *
037: */
038: public class JarHelper extends AbstractFileSystemHelper implements
039: FileSystemHelper {
040: protected JarFile jarFile;
041: protected DirectoryHelper dirHelper;
042: protected File file;
043: private boolean deleteOnClose;
044: protected File jarRoot;
045:
046: /**
047: *
048: * @param jarFile
049: * @throws IOException
050: */
051: public JarHelper(File file, boolean deleteOnClose)
052: throws IOException {
053: this .jarFile = new JarFile(file);
054: this .deleteOnClose = deleteOnClose;
055: this .file = file;
056: String tmpDirPath = System.getProperty("java.io.tmpdir");
057: File tmpDir = new File(tmpDirPath);
058: jarRoot = null;
059:
060: jarRoot = new File(tmpDir, "jetspeed-jar-tmp/" + file.getName());
061:
062: if (!jarRoot.exists()) {
063: jarRoot.mkdirs();
064: }
065: jarRoot.deleteOnExit();
066:
067: Enumeration entries = this .jarFile.entries();
068: while (entries.hasMoreElements()) {
069: JarEntry jarEntry = (JarEntry) entries.nextElement();
070: String name = jarEntry.getName();
071: if (jarEntry.isDirectory()) {
072: File newDir = new File(jarRoot, name);
073: newDir.mkdir();
074: newDir.deleteOnExit();
075: } else {
076: copyEntryToFile(jarFile, jarRoot, jarEntry);
077: }
078: }
079:
080: dirHelper = new DirectoryHelper(jarRoot);
081: }
082:
083: /**
084: * <p>
085: * copyEntryToFile
086: * </p>
087: *
088: * @param jarFile
089: * @param jarRoot
090: * @param jarEntry
091: * @throws IOException
092: * @throws FileNotFoundException
093: */
094: protected void copyEntryToFile(JarFile jarFile, File jarRoot,
095: JarEntry jarEntry) throws IOException,
096: FileNotFoundException {
097: String name = jarEntry.getName();
098: File file = new File(jarRoot, name);
099: if (!file.getParentFile().exists()) {
100: file.getParentFile().mkdirs();
101: file.getParentFile().deleteOnExit();
102: }
103: file.createNewFile();
104: file.deleteOnExit();
105:
106: InputStream is = null;
107: OutputStream os = null;
108: try {
109: is = jarFile.getInputStream(jarEntry);
110: os = new FileOutputStream(file);
111:
112: byte[] buf = new byte[1024];
113: int len;
114: while ((len = is.read(buf)) > 0) {
115: os.write(buf, 0, len);
116: }
117:
118: } finally {
119: if (is != null) {
120: is.close();
121: }
122:
123: if (os != null) {
124: os.close();
125: }
126: }
127: }
128:
129: /**
130: * <p>
131: * copyFrom
132: * </p>
133: *
134: * @param directory
135: * @param fileFilter
136: * @throws IOException
137: */
138: public void copyFrom(File directory, FileFilter fileFilter)
139: throws IOException {
140: dirHelper.copyFrom(directory, fileFilter);
141: }
142:
143: /**
144: * <p>
145: * copyFrom
146: * </p>
147: *
148: * @see org.apache.jetspeed.util.FileSystemHelper#copyFrom(java.io.File)
149: * @param directory
150: * @throws IOException
151: */
152: public void copyFrom(File directory) throws IOException {
153: dirHelper.copyFrom(directory);
154: }
155:
156: /**
157: * <p>
158: * getRootDirectory
159: * </p>
160: *
161: * @see org.apache.jetspeed.util.FileSystemHelper#getRootDirectory()
162: * @return
163: */
164: public File getRootDirectory() {
165: return dirHelper.getRootDirectory();
166: }
167:
168: /**
169: * <p>
170: * remove
171: * </p>
172: *
173: * @see org.apache.jetspeed.util.FileSystemHelper#remove()
174: * @return
175: */
176: public boolean remove() {
177: return dirHelper.remove();
178: }
179:
180: /**
181: * <p>
182: * close
183: * </p>
184: *
185: * @throws IOException
186: *
187: * @see org.apache.jetspeed.util.FileSystemHelper#close()
188: *
189: */
190: public void close() throws IOException {
191: jarFile.close();
192: if (deleteOnClose) {
193: // remove all temporary files
194: dirHelper.remove();
195: }
196:
197: dirHelper.close();
198: }
199:
200: /**
201: * <p>
202: * getSourcePath
203: * </p>
204: *
205: * @see org.apache.jetspeed.util.FileSystemHelper#getSourcePath()
206: * @return
207: */
208: public String getSourcePath() {
209: return file.getAbsolutePath();
210: }
211: }
|