/*
* Enhydra Java Application Server Project
*
* The contents of this file are subject to the Enhydra Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License on
* the Enhydra web site ( http://www.enhydra.org/ ).
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific terms governing rights and limitations
* under the License.
*
* The Initial Developer of the Enhydra Application Server is Lutris
* Technologies, Inc. The Enhydra Application Server and portions created
* by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
* All Rights Reserved.
*
* Contributor(s):
*
* $Id: TmpDir.java,v 1.15 2005/06/13 09:26:06 draganr Exp $
*/
package com.lutris.util;
import java.io.*;
/**
* Class for creating and manage a temporary directory.
*
* @version $Revision: 1.15 $
* @author Mark Diekhans
* @since Jolt1.0
*/
class TmpDir {
File dir;
/**
* Create a tmp directory.
*
* @param dirPath Path of directory to generate the temporary file in.
* @param baseName Prefix name of the directory.
* @exception java.io.IOException if unable to create the directory.
*/
public TmpDir (String path,
String baseName)
throws IOException {
int dirNum = 0;
/*
* Generate a unique directory.
*/
while (true) {
dir = new File (path, baseName + "." + System.currentTimeMillis () +
"." + dirNum + ".tmp");
if (dir.mkdirs ()) {
break;
}
if (!dir.exists ()) {
/*
* Didn't fail because it already exists, must be an access error.
*/
throw new IOException ("no permission to create directory " +
dir.getAbsolutePath ());
}
dirNum++;
if (dirNum >= 25) {
throw new IOException ("failed to create tmp directory named in the " +
"form \"" + dir.getAbsolutePath () +
"\" after " + dirNum + " trys");
}
}
}
/**
* Get the File object describing the directory.
*
* @return Reference to the file object.
*/
public File file () {
return dir;
}
/**
* Recursive delete files.
*/
void recursiveDelete (File dirPath) {
String [] ls = dirPath.list ();
for (int idx = 0; idx < ls.length; idx++) {
File file = new File (dirPath, ls [idx]);
if (file.isDirectory ())
recursiveDelete (file);
file.delete ();
}
}
/**
* Delete the directory and its contents
*
* @exception java.io.IOException if unable to remove the directory.
*/
public void delete ()
throws IOException {
recursiveDelete (dir);
if (dir.exists ()) {
throw new IOException ("Unable to delete directory hierarchy \"" +
dir.getAbsolutePath () + "\"");
}
}
/**
* Create the specified directory in the tmp directory if it doesn't exist.
*
* @param path Path of directory relative to the tmp directory.
* @return A File naming the new directory.
* @exception java.io.IOException if unable to create the directory.
*/
public File mkdirs (String path)
throws IOException {
File newDir = new File (dir.getAbsolutePath (), path);
newDir.mkdirs ();
if (!newDir.exists ()) {
throw new IOException ("Unable to create directory \"" +
dir.getAbsolutePath () + "\"");
}
return newDir;
}
}