KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > config > IOUtil


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

42 public class IOUtil {
43     public static void recursiveCopy(File JavaDoc srcDir, File JavaDoc destDir) throws IOException JavaDoc {
44         if (srcDir == null) throw new NullPointerException JavaDoc("sourceDir is null");
45         if (destDir == null) throw new NullPointerException JavaDoc("destDir is null");
46         if (!srcDir.isDirectory() || ! srcDir.canRead()) {
47             throw new IllegalArgumentException JavaDoc("Source directory must be a readable directory " + srcDir);
48         }
49         if (destDir.exists()) {
50             throw new IllegalArgumentException JavaDoc("Destination directory already exists " + destDir);
51         }
52         if (srcDir.equals(destDir)) {
53             throw new IllegalArgumentException JavaDoc("Source and destination directory are the same " + srcDir);
54         }
55
56         destDir.mkdirs();
57         if (!destDir.exists()) {
58             throw new IOException JavaDoc("Could not create destination directory " + destDir);
59         }
60
61
62         File JavaDoc[] srcFiles = srcDir.listFiles();
63         if (srcFiles != null) {
64             for (int i = 0; i < srcFiles.length; i++) {
65                 File JavaDoc srcFile = srcFiles[i];
66                 File JavaDoc destFile = new File JavaDoc(destDir, srcFile.getName());
67                 if (srcFile.isDirectory()) {
68                     recursiveCopy(srcFile, destFile);
69                 } else {
70                     copyFile(srcFile, destFile);
71                 }
72             }
73         }
74     }
75
76     public static void copyFile(File JavaDoc source, File JavaDoc destination) throws IOException JavaDoc {
77         File JavaDoc destinationDir = destination.getParentFile();
78         if (!destinationDir.exists() && !destinationDir.mkdirs()) {
79             throw new IOException JavaDoc("Cannot create directory : " + destinationDir);
80         }
81
82         InputStream JavaDoc in = null;
83         OutputStream JavaDoc out = null;
84         try {
85             in = new FileInputStream JavaDoc(source);
86             out = new FileOutputStream JavaDoc(destination);
87             writeAll(in, out);
88         } finally {
89             close(in);
90             close(out);
91         }
92     }
93
94     public static void writeAll(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc {
95         byte[] buffer = new byte[4096];
96         int count;
97         while ((count = in.read(buffer)) > 0) {
98             out.write(buffer, 0, count);
99         }
100         out.flush();
101     }
102
103     public static boolean recursiveDelete(File JavaDoc root) {
104         if (root == null) {
105             return true;
106         }
107
108         if (root.isDirectory()) {
109             File JavaDoc[] files = root.listFiles();
110             if (files != null) {
111                 for (int i = 0; i < files.length; i++) {
112                     File JavaDoc file = files[i];
113                     if (file.isDirectory()) {
114                         recursiveDelete(file);
115                     } else {
116                         file.delete();
117                     }
118                 }
119             }
120         }
121         return root.delete();
122     }
123
124     public static void flush(OutputStream JavaDoc thing) {
125         if (thing != null) {
126             try {
127                 thing.flush();
128             } catch(Exception JavaDoc ignored) {
129             }
130         }
131     }
132
133     public static void flush(Writer JavaDoc thing) {
134         if (thing != null) {
135             try {
136                 thing.flush();
137             } catch(Exception JavaDoc ignored) {
138             }
139         }
140     }
141
142     public static void close(JarFile JavaDoc thing) {
143         if (thing != null) {
144             try {
145                 thing.close();
146             } catch(Exception JavaDoc ignored) {
147             }
148         }
149     }
150
151     public static void close(InputStream JavaDoc thing) {
152         if (thing != null) {
153             try {
154                 thing.close();
155             } catch(Exception JavaDoc ignored) {
156             }
157         }
158     }
159
160     public static void close(OutputStream JavaDoc thing) {
161         if (thing != null) {
162             try {
163                 thing.close();
164             } catch(Exception JavaDoc ignored) {
165             }
166         }
167     }
168
169     public static void close(Reader JavaDoc thing) {
170         if (thing != null) {
171             try {
172                 thing.close();
173             } catch(Exception JavaDoc ignored) {
174             }
175         }
176     }
177
178     public static void close(Writer JavaDoc thing) {
179         if (thing != null) {
180             try {
181                 thing.close();
182             } catch(Exception JavaDoc ignored) {
183             }
184         }
185     }
186
187     public static Set JavaDoc search(File JavaDoc root, String JavaDoc pattern) throws MalformedURLException JavaDoc {
188         if (root.isDirectory()) {
189             if (!SelectorUtils.hasWildcards(pattern)) {
190                 File JavaDoc match = new File JavaDoc(root, pattern);
191                 if (match.exists() && match.canRead()) {
192                     return Collections.singleton(match.toURL());
193                 } else {
194                     return Collections.EMPTY_SET;
195                 }
196             } else {
197                 Set JavaDoc matches = new LinkedHashSet JavaDoc();
198                 Map JavaDoc files = listAllFileNames(root);
199                 for (Iterator JavaDoc iterator = files.entrySet().iterator(); iterator.hasNext();) {
200                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
201                     String JavaDoc fileName = (String JavaDoc) entry.getKey();
202                     if (SelectorUtils.matchPath(pattern, fileName)) {
203                         File JavaDoc file = (File JavaDoc) entry.getValue();
204                         matches.add(file.toURL());
205                     }
206                 }
207                 return matches;
208             }
209         } else {
210             JarFile JavaDoc jarFile = null;
211             try {
212                 jarFile = new JarFile JavaDoc(root);
213                 URL JavaDoc baseURL = new URL JavaDoc("jar:" + root.toURL().toString() + "!/");
214                 if (!SelectorUtils.hasWildcards(pattern)) {
215                     ZipEntry JavaDoc entry = jarFile.getEntry(pattern);
216                     if (entry != null) {
217                         URL JavaDoc match = new URL JavaDoc(baseURL, entry.getName());
218                         return Collections.singleton(match);
219                     } else {
220                         return Collections.EMPTY_SET;
221                     }
222                 } else {
223                     Set JavaDoc matches = new LinkedHashSet JavaDoc();
224                     Enumeration JavaDoc entries = jarFile.entries();
225                     while (entries.hasMoreElements()) {
226                         ZipEntry JavaDoc entry = (ZipEntry JavaDoc) entries.nextElement();
227                         String JavaDoc fileName = entry.getName();
228                         if (SelectorUtils.matchPath(pattern, fileName)) {
229                             URL JavaDoc url = new URL JavaDoc(baseURL, fileName);
230                             matches.add(url);
231                         }
232                     }
233                     return matches;
234                 }
235             } catch (MalformedURLException JavaDoc e) {
236                 throw e;
237             } catch (IOException JavaDoc e) {
238                 return Collections.EMPTY_SET;
239             } finally {
240                 close(jarFile);
241             }
242         }
243     }
244
245     public static Map JavaDoc listAllFileNames(File JavaDoc base) {
246         return listAllFileNames(base, "");
247     }
248
249     private static Map JavaDoc listAllFileNames(File JavaDoc base, String JavaDoc prefix) {
250         if (!base.canRead() || !base.isDirectory()) {
251             throw new IllegalArgumentException JavaDoc(base.getAbsolutePath());
252         }
253         Map JavaDoc map = new LinkedHashMap JavaDoc();
254         File JavaDoc[] hits = base.listFiles();
255         for (int i = 0; i < hits.length; i++) {
256             File JavaDoc hit = hits[i];
257             if (hit.canRead()) {
258                 if (hit.isDirectory()) {
259                     map.putAll(listAllFileNames(hit, prefix.equals("") ? hit.getName() : prefix + "/" + hit.getName()));
260                 } else {
261                     map.put(prefix.equals("") ? hit.getName() : prefix + "/" + hit.getName(), hit);
262                 }
263             }
264         }
265         map.put(prefix, base);
266         return map;
267     }
268 }
269
Popular Tags