updated code for kona libs, not quite working yet.
[feisty_meow.git] / kona / src / org / feistymeow / utility / Extemporizer.java
1 package org.feistymeow.utility;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 //////////////
10 // Name   : Extemporizer
11 // Author : Chris Koeritz
12 // Rights : Copyright (c) 2012-$now By University of Virginia
13 //////////////
14 // This file is free software; you can modify/redistribute it under the terms
15 // of the Apache License v2.0: http://www.apache.org/licenses/LICENSE-2.0
16 // Feel free to send updates to: [ koeritz@virginia.edu ]
17 //////////////
18
19 /**
20  * A set of useful utilities for dealing with temporary items.
21  */
22 public class Extemporizer
23 {
24     static private Log _logger = LogFactory.getLog(Extemporizer.class);
25
26     /**
27      * creates a uniquely named temporary directory. thanks for guidance to article at:
28      * http://stackoverflow.com/questions/617414/create-a-temporary-directory-in-java
29      * 
30      * @return a File object pointing at the new temporary directory.
31      * @throws IOException
32      */
33     public static File createTempDirectory(String prefix, String suffix)
34     {
35         if ((prefix == null) || (suffix == null))
36             return null;
37         try {
38             final File temp = File.createTempFile(prefix, suffix);
39             if (!temp.delete())
40                 throw new IOException("failed to delete temporary file: " + temp.getAbsolutePath());
41             if (!temp.mkdir())
42                 throw new IOException("failed to create temporary directory: "
43                         + temp.getAbsolutePath());
44             temp.deleteOnExit(); // set for cleanup.
45             return temp;
46         } catch (Throwable cause) {
47             _logger.error("caught exception while creating temporary directory", cause);
48             return null;
49         }
50     }
51 }