%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.jetspeed.util.JarHelper |
|
|
1 | /* |
|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
3 | * contributor license agreements. See the NOTICE file distributed with |
|
4 | * this work for additional information regarding copyright ownership. |
|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
6 | * (the "License"); you may not use this file except in compliance with |
|
7 | * the License. You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
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 | 0 | { |
57 | 0 | this.jarFile = new JarFile(file); |
58 | 0 | this.deleteOnClose = deleteOnClose; |
59 | 0 | this.file = file; |
60 | 0 | String tmpDirPath = System.getProperty("java.io.tmpdir"); |
61 | 0 | File tmpDir = new File(tmpDirPath); |
62 | 0 | jarRoot = null; |
63 | ||
64 | 0 | jarRoot = new File(tmpDir, "jetspeed-jar-tmp/" + file.getName()); |
65 | ||
66 | 0 | if (!jarRoot.exists()) |
67 | { |
|
68 | 0 | jarRoot.mkdirs(); |
69 | } |
|
70 | 0 | jarRoot.deleteOnExit(); |
71 | ||
72 | 0 | Enumeration entries = this.jarFile.entries(); |
73 | 0 | while (entries.hasMoreElements()) |
74 | { |
|
75 | 0 | JarEntry jarEntry = (JarEntry) entries.nextElement(); |
76 | 0 | String name = jarEntry.getName(); |
77 | 0 | if (jarEntry.isDirectory()) |
78 | { |
|
79 | 0 | File newDir = new File(jarRoot, name); |
80 | 0 | newDir.mkdir(); |
81 | 0 | newDir.deleteOnExit(); |
82 | 0 | } |
83 | else |
|
84 | { |
|
85 | 0 | copyEntryToFile(jarFile, jarRoot, jarEntry); |
86 | } |
|
87 | 0 | } |
88 | ||
89 | 0 | dirHelper = new DirectoryHelper(jarRoot); |
90 | 0 | } |
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 | 0 | String name = jarEntry.getName(); |
107 | 0 | File file = new File(jarRoot, name); |
108 | 0 | if (!file.getParentFile().exists()) |
109 | { |
|
110 | 0 | file.getParentFile().mkdirs(); |
111 | 0 | file.getParentFile().deleteOnExit(); |
112 | } |
|
113 | 0 | file.createNewFile(); |
114 | 0 | file.deleteOnExit(); |
115 | ||
116 | 0 | InputStream is = null; |
117 | 0 | OutputStream os = null; |
118 | try |
|
119 | { |
|
120 | 0 | is = jarFile.getInputStream(jarEntry); |
121 | 0 | os = new FileOutputStream(file); |
122 | ||
123 | 0 | byte[] buf = new byte[1024]; |
124 | int len; |
|
125 | 0 | while ((len = is.read(buf)) > 0) |
126 | { |
|
127 | 0 | os.write(buf, 0, len); |
128 | } |
|
129 | ||
130 | } |
|
131 | finally |
|
132 | { |
|
133 | 0 | if (is != null) |
134 | { |
|
135 | 0 | is.close(); |
136 | } |
|
137 | ||
138 | 0 | if (os != null) |
139 | { |
|
140 | 0 | os.close(); |
141 | } |
|
142 | } |
|
143 | 0 | } |
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 | 0 | dirHelper.copyFrom(directory, fileFilter); |
157 | 0 | } |
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 | 0 | dirHelper.copyFrom(directory); |
170 | 0 | } |
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 | 0 | 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 | 0 | 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 | 0 | jarFile.close(); |
211 | 0 | if (deleteOnClose) |
212 | { |
|
213 | // remove all temporary files |
|
214 | 0 | dirHelper.remove(); |
215 | } |
|
216 | ||
217 | 0 | dirHelper.close(); |
218 | 0 | } |
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 | 0 | return file.getAbsolutePath(); |
231 | } |
|
232 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |