001: /*
002: Copyright (c) 2003 eInnovation Inc. All rights reserved
003:
004: This library is free software; you can redistribute it and/or modify it under the terms
005: of the GNU Lesser General Public License as published by the Free Software Foundation;
006: either version 2.1 of the License, or (at your option) any later version.
007:
008: This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
009: without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: See the GNU Lesser General Public License for more details.
011: */
012:
013: package com.openedit.util;
014:
015: import java.io.File;
016: import java.io.FileOutputStream;
017: import java.io.FileReader;
018: import java.io.FileWriter;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.io.Reader;
023: import java.io.StringReader;
024: import java.io.StringWriter;
025: import java.io.Writer;
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: /**
033: * Some file utility methods such as recursive delete.
034: */
035: public class FileUtils {
036: OutputFiller fieldFiller = new OutputFiller();
037: private static final Log log = LogFactory.getLog(FileUtils.class);
038:
039: /**
040: * Create a temporary directory with a unique name beginning with the given
041: * prefix.
042: *
043: * @param inPrefix
044: * The prefix for the created directory
045: *
046: * @return DOCME
047: *
048: * @throws IOException
049: * DOCME
050: */
051: public File createTempDir(String inPrefix) throws IOException {
052: File tempDir = File.createTempFile(inPrefix, null);
053: tempDir.delete();
054: tempDir.mkdir();
055:
056: return tempDir;
057: }
058:
059: /**
060: * DOCME
061: *
062: * @param fname
063: * DOCME
064: */
065: public void deleteAll(String fname) {
066: File f = new File(fname);
067: deleteAll(f);
068: }
069:
070: /**
071: * Deletes any files in this directory that match the wildcard
072: * @param inMatch
073: */
074: public void deleteMatch(String inMatch) {
075: //get the parent, list the children, find the match, delete
076: File search = new File(inMatch);
077: File dir = search.getParentFile();
078:
079: File[] all = dir.listFiles();
080: if (all != null) {
081: for (int i = 0; i < all.length; i++) {
082: File f = all[i];
083: if (PathUtilities.match(f.getName(), search.getName())) {
084: log.info("deleted " + f.getName());
085: f.delete(); //should this do dirs?
086: }
087: }
088: }
089: }
090:
091: public boolean deleteOlderVersions(String inDir) {
092: System.gc();
093: boolean requiresRestart = false;
094: Map map = new HashMap();
095: //get the parent, list the children, find the match, delete
096: File dir = new File(inDir);
097:
098: File[] all = dir.listFiles();
099: if (all != null) {
100: for (int i = 0; i < all.length; i++) {
101: File f = all[i];
102: String fileName = f.getName();
103: int dashIndex = fileName.lastIndexOf('-');
104: if (dashIndex >= 0) {
105: String base = fileName.substring(0, dashIndex);
106: String version = fileName.substring(dashIndex + 1);
107: String highestVersion = (String) map.get(base);
108: if (highestVersion == null
109: || highestVersion.compareTo(version) < 0) {
110: map.put(base, version);
111: }
112: }
113: }
114:
115: for (int i = 0; i < all.length; i++) {
116: File f = all[i];
117: String fileName = f.getName();
118: int dashIndex = fileName.lastIndexOf('-');
119: if (dashIndex >= 0) {
120: String base = fileName.substring(0, dashIndex);
121: String version = fileName.substring(dashIndex + 1);
122: String highestVersion = (String) map.get(base);
123: if (!version.equals(highestVersion)) {
124: if (f.delete()) {
125: log.info("deleting " + f.getName());
126: } else {
127: log.info("deleting " + f.getName()
128: + " on exit");
129: f.deleteOnExit();
130: requiresRestart = true;
131: }
132: }
133: }
134: }
135: }
136: return requiresRestart;
137: }
138:
139: public void deleteAll(File file) {
140: if (file.isDirectory()) {
141: // If it's a dir, then delete everything in it.
142: File[] fileList = file.listFiles();
143:
144: if (fileList != null) {
145: for (int idx = 0; idx < fileList.length; idx++)
146: deleteAll(fileList[idx]);
147: }
148: }
149:
150: // Now delete ourselves, whether a file or a dir.
151: file.delete();
152: }
153:
154: public void copyFileByMatch(String inMatch, String outDir)
155: throws IOException {
156: File out = new File(outDir);
157: out.mkdirs();
158: copyFileByMatch(inMatch, out);
159: }
160:
161: public void copyFileByMatch(String inMatch, File outDir)
162: throws IOException {
163: File file = new File(inMatch);
164: File dir = file.getParentFile();
165:
166: File[] all = dir.listFiles();
167: if (all != null) {
168: for (int i = 0; i < all.length; i++) {
169: File f = all[i];
170: if (PathUtilities.match(f.getName(), file.getName())) {
171: copyFiles(f, outDir);
172: }
173: }
174: }
175: }
176:
177: /**
178: * DOCME
179: *
180: * @param inSource
181: * DOCME
182: * @param inDest
183: * DOCME
184: * @param inBuffer
185: * DOCME
186: *
187: * @throws IOException
188: * DOCME
189: */
190: public void dirCopy(File inSource, File inDest) throws IOException {
191: if (inSource.isDirectory()) {
192: inDest.mkdirs();
193:
194: File[] files = inSource.listFiles();
195: if (files != null) {
196: for (int i = 0; i < files.length; i++) {
197: // The child directory
198: File newDest = new File(inDest, files[i].getName());
199:
200: if (files[i].isDirectory()) {
201: dirCopy(files[i], newDest);
202: } else {
203: fileCopy(files[i], newDest);
204: }
205: }
206: }
207: }
208: }
209:
210: /**
211: * Does a move even if the directory already exist
212: * Also creates a directory for file to be moved into
213: * @param inSource
214: * @param inDest
215: * @throws IOException
216: */
217: public void move(String inSource, String inDest) throws IOException {
218: move(new File(inSource), new File(inDest));
219: }
220:
221: public void move(File inSource, File inDest) throws IOException {
222: move(inSource, inDest, false);
223: }
224:
225: public void move(File inSource, File inDest, boolean inForce)
226: throws IOException {
227: if (inSource.isDirectory()) {
228: inDest.mkdirs();
229:
230: File[] files = inSource.listFiles();
231: if (files != null) {
232: for (int i = 0; i < files.length; i++) {
233: // The child directory
234: File newDest = new File(inDest, files[i].getName());
235:
236: if (files[i].isDirectory()) {
237: move(files[i], newDest, inForce);
238: } else {
239: renameFile(files[i], newDest, inForce);
240: }
241: }
242: }
243: inSource.delete();
244: } else {
245: if (inDest.isDirectory()) {
246: inDest = new File(inDest, inSource.getName());
247: } else {
248: inDest.getParentFile().mkdirs();
249: }
250: renameFile(inSource, inDest, inForce);
251: }
252: }
253:
254: protected void renameFile(File inSource, File inDest,
255: boolean inForce) throws IOException {
256: if (!inSource.renameTo(inDest)) {
257: if (inForce) {
258: copyFiles(inSource, inDest);
259: inSource.delete();
260: return;
261: }
262: throw new IOException("Could not move "
263: + inSource.getPath() + " to " + inDest.getPath()
264: + " file may already exist. Use forced=true");
265: }
266:
267: }
268:
269: /**
270: * DOCME
271: *
272: * @param src
273: * DOCME
274: * @param dst
275: * DOCME
276: * @param buffer
277: * DOCME
278: *
279: * @throws IOException
280: * DOCME
281: */
282: public void fileCopy(File src, File dst) throws IOException {
283: fieldFiller.fill(src, dst);
284:
285: // Preserve the modification date of the original file.
286: dst.setLastModified(src.lastModified());
287: }
288:
289: public void copyFiles(String source, String destination)
290: throws IOException {
291: copyFiles(new File(source), new File(destination));
292: }
293:
294: public void copyFiles(File source, File destination)
295: throws IOException {
296: if (source.isDirectory()) {
297: if (destination.exists() && !destination.isDirectory()) {
298: destination.delete();
299: }
300: destination.mkdirs();
301:
302: //loop over all the sub files
303: File[] children = source.listFiles();
304:
305: for (int i = 0; i < children.length; i++) {
306: copyFiles(children[i], new File(destination,
307: children[i].getName()));
308: }
309: } else {
310: if (destination.isDirectory()) {
311: copyFiles(source, new File(destination, source
312: .getName()));
313: } else {
314: destination.getParentFile().mkdirs();
315: fieldFiller.fill(source, destination);
316: destination.setLastModified(source.lastModified());
317: }
318: }
319: }
320:
321: /**
322: * This closes the input stream
323: * @param inInput
324: * @param inOutputFile
325: * @throws IOException
326: */
327: public void writeFile(InputStream inInput, File inOutputFile)
328: throws IOException {
329: //TODO: Could probably start using java.nio classes
330: // Write the content
331:
332: // Create any parent directories, if necessary.
333: inOutputFile.getParentFile().mkdirs();
334: OutputStream out = new FileOutputStream(inOutputFile);
335: try {
336: fieldFiller.fill(inInput, out);
337: out.flush();
338: } finally {
339: if (inInput != null) {
340: inInput.close();
341: }
342: out.close();
343: }
344: }
345:
346: /**
347: * @param inIn
348: */
349: public static void safeClose(Reader inIn) {
350: if (inIn != null) {
351: try {
352: inIn.close();
353: } catch (IOException ex) {
354: log.error(ex);
355: }
356: }
357: }
358:
359: public static void safeClose(InputStream inIn) {
360: if (inIn != null) {
361: try {
362: inIn.close();
363: } catch (IOException ex) {
364: log.error(ex);
365: }
366: }
367: }
368:
369: public static void safeClose(OutputStream inIn) {
370: if (inIn != null) {
371: try {
372: inIn.close();
373: } catch (IOException ex) {
374: log.error(ex);
375: }
376: }
377: }
378:
379: public static void safeClose(Writer inIn) {
380: if (inIn != null) {
381: try {
382: inIn.close();
383: } catch (IOException ex) {
384: log.error(ex);
385: }
386: }
387: }
388:
389: public void replace(File inFile, String inKey, String inNewKey)
390: throws Exception {
391: FileReader filereader = new FileReader(inFile);
392: StringWriter out = new StringWriter();
393: new OutputFiller().fill(filereader, out);
394: filereader.close();
395: String readstring = out.toString();
396: readstring = readstring.replace(inKey, inNewKey);
397: FileWriter filewriter = new FileWriter(inFile);
398: new OutputFiller().fill(new StringReader(readstring),
399: filewriter);
400: filewriter.close();
401: }
402:
403: }
|