1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.util;
18
19 import java.io.File;
20 import java.io.FileFilter;
21 import java.io.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.util.Enumeration;
27 import java.util.jar.JarEntry;
28 import java.util.jar.JarFile;
29
30 /***
31 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
32 *
33 * Creates a a temp directory to which a JarFile is expanded and can be
34 * manipulated. All operations are performed by an internal instance of
35 * {@link DirectoryHelper}.
36 *
37 */
38 public class JarHelper
39 extends
40 AbstractFileSystemHelper
41 implements
42 FileSystemHelper
43 {
44 protected JarFile jarFile;
45 protected DirectoryHelper dirHelper;
46 protected File file;
47 private boolean deleteOnClose;
48 protected File jarRoot;
49
50 /***
51 *
52 * @param jarFile
53 * @throws IOException
54 */
55 public JarHelper( File file, boolean deleteOnClose ) throws IOException
56 {
57 this.jarFile = new JarFile(file);
58 this.deleteOnClose = deleteOnClose;
59 this.file = file;
60 String tmpDirPath = System.getProperty("java.io.tmpdir");
61 File tmpDir = new File(tmpDirPath);
62 jarRoot = null;
63
64 jarRoot = new File(tmpDir, "jetspeed-jar-tmp/" + file.getName());
65
66 if (!jarRoot.exists())
67 {
68 jarRoot.mkdirs();
69 }
70 jarRoot.deleteOnExit();
71
72 Enumeration entries = this.jarFile.entries();
73 while (entries.hasMoreElements())
74 {
75 JarEntry jarEntry = (JarEntry) entries.nextElement();
76 String name = jarEntry.getName();
77 if (jarEntry.isDirectory())
78 {
79 File newDir = new File(jarRoot, name);
80 newDir.mkdir();
81 newDir.deleteOnExit();
82 }
83 else
84 {
85 copyEntryToFile(jarFile, jarRoot, jarEntry);
86 }
87 }
88
89 dirHelper = new DirectoryHelper(jarRoot);
90 }
91
92 /***
93 * <p>
94 * copyEntryToFile
95 * </p>
96 *
97 * @param jarFile
98 * @param jarRoot
99 * @param jarEntry
100 * @throws IOException
101 * @throws FileNotFoundException
102 */
103 protected void copyEntryToFile( JarFile jarFile, File jarRoot, JarEntry jarEntry ) throws IOException,
104 FileNotFoundException
105 {
106 String name = jarEntry.getName();
107 File file = new File(jarRoot, name);
108 if (!file.getParentFile().exists())
109 {
110 file.getParentFile().mkdirs();
111 file.getParentFile().deleteOnExit();
112 }
113 file.createNewFile();
114 file.deleteOnExit();
115
116 InputStream is = null;
117 OutputStream os = null;
118 try
119 {
120 is = jarFile.getInputStream(jarEntry);
121 os = new FileOutputStream(file);
122
123 byte[] buf = new byte[1024];
124 int len;
125 while ((len = is.read(buf)) > 0)
126 {
127 os.write(buf, 0, len);
128 }
129
130 }
131 finally
132 {
133 if (is != null)
134 {
135 is.close();
136 }
137
138 if (os != null)
139 {
140 os.close();
141 }
142 }
143 }
144
145 /***
146 * <p>
147 * copyFrom
148 * </p>
149 *
150 * @param directory
151 * @param fileFilter
152 * @throws IOException
153 */
154 public void copyFrom( File directory, FileFilter fileFilter ) throws IOException
155 {
156 dirHelper.copyFrom(directory, fileFilter);
157 }
158 /***
159 * <p>
160 * copyFrom
161 * </p>
162 *
163 * @see org.apache.jetspeed.util.FileSystemHelper#copyFrom(java.io.File)
164 * @param directory
165 * @throws IOException
166 */
167 public void copyFrom( File directory ) throws IOException
168 {
169 dirHelper.copyFrom(directory);
170 }
171
172 /***
173 * <p>
174 * getRootDirectory
175 * </p>
176 *
177 * @see org.apache.jetspeed.util.FileSystemHelper#getRootDirectory()
178 * @return
179 */
180 public File getRootDirectory()
181 {
182 return dirHelper.getRootDirectory();
183 }
184
185 /***
186 * <p>
187 * remove
188 * </p>
189 *
190 * @see org.apache.jetspeed.util.FileSystemHelper#remove()
191 * @return
192 */
193 public boolean remove()
194 {
195 return dirHelper.remove();
196 }
197
198 /***
199 * <p>
200 * close
201 * </p>
202 *
203 * @throws IOException
204 *
205 * @see org.apache.jetspeed.util.FileSystemHelper#close()
206 *
207 */
208 public void close() throws IOException
209 {
210 jarFile.close();
211 if (deleteOnClose)
212 {
213
214 dirHelper.remove();
215 }
216
217 dirHelper.close();
218 }
219
220 /***
221 * <p>
222 * getSourcePath
223 * </p>
224 *
225 * @see org.apache.jetspeed.util.FileSystemHelper#getSourcePath()
226 * @return
227 */
228 public String getSourcePath()
229 {
230 return file.getAbsolutePath();
231 }
232 }