KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > util > DeploymentUtil


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.geronimo.deployment.util;
18
19 import java.io.BufferedOutputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.io.Reader JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.LinkedList JavaDoc;
35 import java.util.jar.JarFile JavaDoc;
36 import java.util.jar.JarOutputStream JavaDoc;
37 import java.util.jar.Manifest JavaDoc;
38 import java.util.zip.ZipEntry JavaDoc;
39 import java.util.zip.ZipFile JavaDoc;
40
41 /**
42  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
43  */

44 public final class DeploymentUtil {
45     private DeploymentUtil() {
46     }
47
48     public static final File JavaDoc DUMMY_JAR_FILE;
49     static {
50         try {
51             DUMMY_JAR_FILE = DeploymentUtil.createTempFile();
52             new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(DeploymentUtil.DUMMY_JAR_FILE), new Manifest JavaDoc()).close();
53         } catch (IOException JavaDoc e) {
54             throw new ExceptionInInitializerError JavaDoc(e);
55         }
56     }
57
58     // be careful to clean up the temp directory
59
public static File JavaDoc createTempDir() throws IOException JavaDoc {
60         File JavaDoc tempDir = File.createTempFile("geronimo-deploymentUtil", ".tmpdir");
61         tempDir.delete();
62         tempDir.mkdirs();
63         return tempDir;
64     }
65
66     // be careful to clean up the temp file... we tell the vm to delete this on exit
67
// but VMs can't be trusted to acutally delete the file
68
public static File JavaDoc createTempFile() throws IOException JavaDoc {
69         File JavaDoc tempFile = File.createTempFile("geronimo-deploymentUtil", ".tmpdir");
70         tempFile.deleteOnExit();
71         return tempFile;
72     }
73
74     public static void copyFile(File JavaDoc source, File JavaDoc destination) throws IOException JavaDoc {
75         File JavaDoc destinationDir = destination.getParentFile();
76         if (false == destinationDir.exists() && false == destinationDir.mkdirs()) {
77             throw new java.io.IOException JavaDoc("Cannot create directory : " + destinationDir);
78         }
79         
80         InputStream JavaDoc in = null;
81         OutputStream JavaDoc out = null;
82         try {
83             in = new FileInputStream JavaDoc(source);
84             out = new FileOutputStream JavaDoc(destination);
85             writeAll(in, out);
86         } finally {
87             close(in);
88             close(out);
89         }
90     }
91
92     private static void writeAll(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc {
93         byte[] buffer = new byte[4096];
94         int count;
95         while ((count = in.read(buffer)) > 0) {
96             out.write(buffer, 0, count);
97         }
98         out.flush();
99     }
100     public static File JavaDoc toTempFile(JarFile JavaDoc jarFile, String JavaDoc path) throws IOException JavaDoc {
101         return toTempFile(createJarURL(jarFile, path));
102     }
103
104     public static File JavaDoc toTempFile(URL JavaDoc url) throws IOException JavaDoc {
105         InputStream JavaDoc in = null;
106         OutputStream JavaDoc out = null;
107         try {
108             in = url.openStream();
109
110             File JavaDoc tempFile = createTempFile();
111             out = new FileOutputStream JavaDoc(tempFile);
112
113             writeAll(in, out);
114             return tempFile;
115         } finally {
116             close(out);
117             close(in);
118         }
119     }
120
121     public static String JavaDoc readAll(URL JavaDoc url) throws IOException JavaDoc {
122         Reader JavaDoc reader = null;
123         try {
124             reader = new InputStreamReader JavaDoc(url.openStream());
125
126             char[] buffer = new char[4000];
127             StringBuffer JavaDoc out = new StringBuffer JavaDoc();
128             for(int count = reader.read(buffer); count >= 0; count = reader.read(buffer)) {
129                 out.append(buffer, 0, count);
130             }
131             return out.toString();
132         } finally {
133             close(reader);
134         }
135     }
136
137     public static File JavaDoc toFile(JarFile JavaDoc jarFile) throws IOException JavaDoc {
138         if (jarFile instanceof UnpackedJarFile) {
139             return ((UnpackedJarFile) jarFile).getBaseDir();
140         } else {
141             throw new IOException JavaDoc("jarFile is not a directory");
142         }
143     }
144
145     // be careful with this method as it can leave a temp lying around
146
public static File JavaDoc toFile(JarFile JavaDoc jarFile, String JavaDoc path) throws IOException JavaDoc {
147         if (jarFile instanceof UnpackedJarFile) {
148             File JavaDoc baseDir = ((UnpackedJarFile) jarFile).getBaseDir();
149             File JavaDoc file = new File JavaDoc(baseDir, path);
150             if (!file.isFile()) {
151                 throw new IOException JavaDoc("No such file: " + file.getAbsolutePath());
152             }
153             return file;
154         } else {
155             String JavaDoc urlString = "jar:" + new File JavaDoc(jarFile.getName()).toURL() + "!/" + path;
156             return toTempFile(new URL JavaDoc(urlString));
157         }
158     }
159
160     public static URL JavaDoc createJarURL(JarFile JavaDoc jarFile, String JavaDoc path) throws MalformedURLException JavaDoc {
161         if (jarFile instanceof NestedJarFile) {
162             NestedJarFile nestedJar = (NestedJarFile) jarFile;
163             if (nestedJar.isUnpacked()) {
164                 JarFile JavaDoc baseJar = nestedJar.getBaseJar();
165                 String JavaDoc basePath = nestedJar.getBasePath();
166                 if (baseJar instanceof UnpackedJarFile) {
167                     File JavaDoc baseDir = ((UnpackedJarFile) baseJar).getBaseDir();
168                     baseDir = new File JavaDoc(baseDir, basePath);
169                     return new File JavaDoc(baseDir, path).toURL();
170                 }
171             }
172         }
173         
174         if (jarFile instanceof UnpackedJarFile) {
175             File JavaDoc baseDir = ((UnpackedJarFile) jarFile).getBaseDir();
176             return new File JavaDoc(baseDir, path).toURL();
177         } else {
178             String JavaDoc urlString = "jar:" + new File JavaDoc(jarFile.getName()).toURL() + "!/" + path;
179             return new URL JavaDoc(urlString);
180         }
181     }
182
183     public static JarFile JavaDoc createJarFile(File JavaDoc jarFile) throws IOException JavaDoc {
184         if (jarFile.isDirectory()) {
185             return new UnpackedJarFile(jarFile);
186         } else {
187             return new JarFile JavaDoc(jarFile);
188         }
189     }
190
191     public static void copyToPackedJar(JarFile JavaDoc inputJar, File JavaDoc outputFile) throws IOException JavaDoc {
192         if (inputJar.getClass() == JarFile JavaDoc.class) {
193             // this is a plain old jar... nothign special
194
copyFile(new File JavaDoc(inputJar.getName()), outputFile);
195         } else if (inputJar instanceof NestedJarFile && ((NestedJarFile)inputJar).isPacked()) {
196             NestedJarFile nestedJarFile = (NestedJarFile)inputJar;
197             JarFile JavaDoc baseJar = nestedJarFile.getBaseJar();
198             String JavaDoc basePath = nestedJarFile.getBasePath();
199             if (baseJar instanceof UnpackedJarFile) {
200                 // our target jar is just a file in upacked jar (a plain old directory)... now
201
// we just need to find where it is and copy it to the outptu
202
copyFile(((UnpackedJarFile)baseJar).getFile(basePath), outputFile);
203             } else {
204                 // out target is just a plain old jar file directly accessabel from the file system
205
copyFile(new File JavaDoc(baseJar.getName()), outputFile);
206             }
207         } else {
208             // copy out the module contents to a standalone jar file (entry by entry)
209
JarOutputStream JavaDoc out = null;
210             try {
211                 out = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(outputFile));
212                 byte[] buffer = new byte[4096];
213                 Enumeration JavaDoc entries = inputJar.entries();
214                 while (entries.hasMoreElements()) {
215                     ZipEntry JavaDoc entry = (ZipEntry JavaDoc) entries.nextElement();
216                     InputStream JavaDoc in = inputJar.getInputStream(entry);
217                     try {
218                         out.putNextEntry(new ZipEntry JavaDoc(entry.getName()));
219                         try {
220                             int count;
221                             while ((count = in.read(buffer)) > 0) {
222                                 out.write(buffer, 0, count);
223                             }
224                         } finally {
225                             out.closeEntry();
226                         }
227                     } finally {
228                         close(in);
229                     }
230                 }
231             } finally {
232                 close(out);
233             }
234         }
235     }
236
237     public static void jarDirectory(File JavaDoc sourceDirecotry, File JavaDoc destinationFile) throws IOException JavaDoc {
238         JarFile JavaDoc inputJar = new UnpackedJarFile(sourceDirecotry);
239         try {
240             copyToPackedJar(inputJar, destinationFile);
241         } finally {
242             close(inputJar);
243         }
244     }
245
246     public static void unzipToDirectory(ZipFile JavaDoc zipFile, File JavaDoc destDir) throws IOException JavaDoc {
247         Enumeration JavaDoc entries = zipFile.entries();
248         try {
249             while (entries.hasMoreElements()) {
250                 ZipEntry JavaDoc entry = (ZipEntry JavaDoc) entries.nextElement();
251                 if (entry.isDirectory()) {
252                     File JavaDoc dir = new File JavaDoc(destDir, entry.getName());
253                     boolean success = dir.mkdirs();
254                     if (!success) {
255                         throw new IOException JavaDoc("Cannot create directory " + dir.getAbsolutePath());
256                     }
257                 } else {
258                     File JavaDoc file = new File JavaDoc(destDir, entry.getName());
259                     OutputStream JavaDoc out = null;
260                     InputStream JavaDoc in = null;
261                     try {
262                         out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(file));
263                         in = zipFile.getInputStream(entry);
264                         writeAll(in, out);
265                     } finally {
266                         if (null != out) {
267                             out.close();
268                         }
269                         if (null != in) {
270                             in.close();
271                         }
272                     }
273                 }
274             }
275         } finally {
276             zipFile.close();
277         }
278     }
279     
280     
281     public static boolean recursiveDelete(File JavaDoc root, Collection JavaDoc unableToDeleteCollection) {
282         if (root == null) {
283             return true;
284         }
285
286         if (root.isDirectory()) {
287             File JavaDoc[] files = root.listFiles();
288             if (files != null) {
289                 for (int i = 0; i < files.length; i++) {
290                     File JavaDoc file = files[i];
291                     if (file.isDirectory()) {
292                         recursiveDelete(file);
293                     } else {
294                         if (!file.delete() && unableToDeleteCollection != null) {
295                             unableToDeleteCollection.add(file);
296                         }
297                     }
298                 }
299             }
300         }
301         return root.delete();
302     }
303     
304     public static boolean recursiveDelete(File JavaDoc root) {
305         return recursiveDelete(root,null);
306     }
307
308     public static Collection JavaDoc listRecursiveFiles(File JavaDoc file) {
309         LinkedList JavaDoc list = new LinkedList JavaDoc();
310         listRecursiveFiles(file, list);
311         return Collections.unmodifiableCollection(list);
312     }
313
314     public static void listRecursiveFiles(File JavaDoc file, Collection JavaDoc collection) {
315         File JavaDoc[] files = file.listFiles();
316         if ( null == files ) {
317             return;
318         }
319         for (int i = 0; i < files.length; i++) {
320             collection.add(files[i]);
321             if (files[i].isDirectory()) {
322                 listRecursiveFiles(files[i], collection);
323             }
324         }
325     }
326
327     public static void flush(OutputStream JavaDoc thing) {
328         if (thing != null) {
329             try {
330                 thing.flush();
331             } catch(Exception JavaDoc ignored) {
332             }
333         }
334     }
335
336     public static void flush(Writer JavaDoc thing) {
337         if (thing != null) {
338             try {
339                 thing.flush();
340             } catch(Exception JavaDoc ignored) {
341             }
342         }
343     }
344
345     public static void close(JarFile JavaDoc thing) {
346         if (thing != null) {
347             try {
348                 thing.close();
349             } catch(Exception JavaDoc ignored) {
350             }
351         }
352     }
353
354     public static void close(InputStream JavaDoc thing) {
355         if (thing != null) {
356             try {
357                 thing.close();
358             } catch(Exception JavaDoc ignored) {
359             }
360         }
361     }
362
363     public static void close(OutputStream JavaDoc thing) {
364         if (thing != null) {
365             try {
366                 thing.close();
367             } catch(Exception JavaDoc ignored) {
368             }
369         }
370     }
371
372     public static void close(Reader JavaDoc thing) {
373         if (thing != null) {
374             try {
375                 thing.close();
376             } catch(Exception JavaDoc ignored) {
377             }
378         }
379     }
380
381     public static void close(Writer JavaDoc thing) {
382         if (thing != null) {
383             try {
384                 thing.close();
385             } catch(Exception JavaDoc ignored) {
386             }
387         }
388     }
389
390     public static final class EmptyInputStream extends InputStream JavaDoc {
391         public int read() {
392             return -1;
393         }
394
395         public int read(byte b[]) {
396             return -1;
397         }
398
399         public int read(byte b[], int off, int len) {
400             return -1;
401         }
402
403         public long skip(long n) {
404             return 0;
405         }
406
407         public int available() {
408             return 0;
409         }
410
411         public void close() {
412         }
413
414         public synchronized void mark(int readlimit) {
415         }
416
417         public synchronized void reset() {
418         }
419
420         public boolean markSupported() {
421             return false;
422         }
423     }
424 }
425
Popular Tags