[Schmitzm-commits] r1488 - in trunk/schmitzm-core: . src/main/java/de/schmitzm/io

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sat Feb 5 11:25:00 CET 2011


Author: alfonx
Date: 2011-02-05 11:24:59 +0100 (Sat, 05 Feb 2011)
New Revision: 1488

Modified:
   trunk/schmitzm-core/pom.xml
   trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
Log:
Added method cleanupTempDir to IOUtils

Modified: trunk/schmitzm-core/pom.xml
===================================================================
--- trunk/schmitzm-core/pom.xml	2011-02-05 10:00:29 UTC (rev 1487)
+++ trunk/schmitzm-core/pom.xml	2011-02-05 10:24:59 UTC (rev 1488)
@@ -45,7 +45,7 @@
 		<dependency>
 			<groupId>commons-io</groupId>
 			<artifactId>commons-io</artifactId>
-			<version>1.4</version>
+			<version>2.0.1</version>
 			<type>jar</type>
 			<scope>compile</scope>
 		</dependency>

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2011-02-05 10:00:29 UTC (rev 1487)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2011-02-05 10:24:59 UTC (rev 1488)
@@ -70,6 +70,8 @@
 import org.apache.commons.lang.SystemUtils;
 import org.apache.log4j.Logger;
 
+import de.schmitzm.lang.LangUtil;
+
 /**
  * Diese Klasse stellt statische Methoden fuer die Ein/Ausgabe zur Verfuegung.
  * 
@@ -876,6 +878,100 @@
 	}
 
 	/**
+	 * @param deleteDirectlyPrefix
+	 *            Deletes any files+directories in the temp directory starting
+	 *            with this String directly. May be <code>null</code>.
+	 * @param deleteOldPrefix
+	 *            Deltes any files/directories starting with this string, if
+	 *            they have not been modified within 3 days. May be
+	 *            <code>null</code>.
+	 */
+	public static void cleanupTempDir(final String deleteDirectlyPrefix,
+			final String deleteOldPrefix) {
+
+		/**
+		 * Files oder than DAYS days old will be deleted.
+		 */
+		int DAYS = 3;
+
+		int count = 0;
+
+		try {
+			/**
+			 * Deleting temp-files directly
+			 */
+			if (deleteDirectlyPrefix != null) {
+				File[] listFiles = IOUtil.getTempDir().listFiles(
+						new FileFilter() {
+
+							@Override
+							public boolean accept(File pathname) {
+								return pathname != null
+										&& pathname.getName().startsWith(
+												deleteDirectlyPrefix);
+							}
+
+						});
+				for (File ff : listFiles) {
+					// LOGGER.debug("Going to delete temporary instance file/directory created by this Java instance: "
+					// + IOUtil.escapePath(ff));
+
+					if (!FileUtils.deleteQuietly(ff)) {
+						LOGGER.warn("Couldn't delete instance temp file "
+								+ IOUtil.escapePath(ff));
+					} else
+						count++;
+				}
+			}
+
+			/**
+			 * Deleting older temp-files
+			 */
+			if (deleteOldPrefix != null) {
+				File[] listFiles = IOUtil.getTempDir().listFiles(
+						new FileFilter() {
+
+							@Override
+							public boolean accept(File pathname) {
+								return pathname != null
+										&& pathname.getName().startsWith(
+												deleteOldPrefix);
+							}
+
+						});
+				for (File ff : listFiles) {
+
+					long diff = System.currentTimeMillis() - ff.lastModified();
+					if (diff < LangUtil.DAY_MILLIS * DAYS) {
+						// LOGGER.debug("Not deleting " + IOUtil.escapePath(ff)
+						// + " since it is only " + (diff / 1000 / 60)
+						// + " minutes old.");
+						continue;
+					}
+
+					// LOGGER.debug("Going to delete orphaned temporary file/directory "
+					// + IOUtil.escapePath(ff));
+
+					if (!FileUtils.deleteQuietly(ff)) {
+						LOGGER.warn("Couldn't delete orphaned temp file "
+								+ IOUtil.escapePath(ff));
+					} else
+						count++;
+				}
+			}
+
+		} catch (Exception e) {
+			// ExceptionDialog.show(null, e);
+			LOGGER.error("Could not cleanup tempdirectory for deleteDirectly="
+					+ deleteDirectlyPrefix + "*  and deleteOld = "
+					+ deleteOldPrefix + "*", e);
+		} finally {
+			LOGGER.debug(count
+					+ " temporary files and directories have been deleted.");
+		}
+	}
+
+	/**
 	 * Test whether it is possible to access the given URL. Times-out after 3s
 	 */
 	public static boolean urlExists(final URL url) {



More information about the Schmitzm-commits mailing list