FileDocCategorySizeDatePackage
BackupFilenameManager.javaAPI DocGlassfish v2 API6129Fri May 04 22:23:14 BST 2007com.sun.enterprise.config.backup

BackupFilenameManager

public class BackupFilenameManager extends Object
Manage filenames to select for backups.
author
bnevins

Fields Summary
private File
dir
private ZipFileAndNumber[]
zipsAndNumbers
private int[]
numbers
private ZipFileAndNumber
latestVersion
Constructors Summary
BackupFilenameManager(File backupDir)

		this.dir = backupDir;
		findZips();
		findLatest();
	
Methods Summary
private voidfindLatest()
Looks through the list of zips and sets the one with the biggest version number

		int biggest = 0;
		
		for(int i = 0; i < zipsAndNumbers.length; i++)
		{
			int curr = zipsAndNumbers[i].num;
			
			if(curr > biggest)
			{
				biggest = curr;
				latestVersion = zipsAndNumbers[i];
				LoggerHelper.fine(zipsAndNumbers[i].zip.toString() + " newest backup so far...");
			}
		}
	
private voidfindZips()
Looks through the backups directory and assembles a list of all backup files found.

throws
BackupWarningException if there are no backup zip files

		File[] zips = dir.listFiles(new ZipFilenameFilter());
		int len = 0;
		
		if(zips != null)
			len = zips.length;
		
		zipsAndNumbers = new ZipFileAndNumber[len];
		
		for(int i = 0; i < len; i++)
		{
			zipsAndNumbers[i] = new ZipFileAndNumber(zips[i]);
		}
	
java.io.Filelatest()

		if(latestVersion == null)
			throw new BackupWarningException("backup-res.NoBackupFiles", dir);
		
		return latestVersion.zip;
	
public static voidmain(java.lang.String[] args)

	
	///////////////////////////////////////////////////////////////////////////////


	    
	
		try
		{
			File f = new File("c:/tmp/test");
			BackupFilenameManager mgr = new BackupFilenameManager(f);
			File fnew = mgr.next();
			System.out.println("Next backup file: " + fnew);
			File fold = mgr.latest();
			System.out.println("Latest backup file: " + fold);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	
java.io.Filenext()

		int newVersionNum = 1;
		
		if(latestVersion != null)
			newVersionNum = latestVersion.num + 1;
	
		String suffix = padWithLeadingZeroes(newVersionNum);
		return new File(dir, Constants.BACKUP_FILENAME_ROOT + suffix + ".zip");
	
private java.lang.StringpadWithLeadingZeroes(int num)
Convert the array of zip filenames into an array of the number suffixes.

		if(num < 10)
			return "0000" + num;

		if(num < 100)
			return "000" + num;
		
		if(num < 1000)
			return "00" + num;
		
		if(num < 10000)
			return "0" + num;

		if(num < 100000)
			return "" + num;
		
		throw new BackupException("Latest version >= 100,000.  Delete some backup files.");