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

RestoreManager

public class RestoreManager extends BackupRestoreManager
author
Byron Nevins

Fields Summary
private static final String
OLD_DOMAIN_SUFFIX
private File
tempRestoreDir
private File
backupDir
Constructors Summary
public RestoreManager(BackupRequest req)

		super(req);
	
Methods Summary
private voidatomicSwap()

		// 1 -- rename original domain dir
		// 2 -- rename new restored dir to domain dir
		// 3 -- delete original domain dir

		// tempRestoreDir
		File oldDomain = new File(request.domainsDir, 
			request.domainName + OLD_DOMAIN_SUFFIX + System.currentTimeMillis());
		
		// On Error -- just fail and delete the new files
		if(!request.domainDir.renameTo(oldDomain))
		{
			FileUtils.whack(tempRestoreDir);
			throw new BackupException("backup-res.CantRenameOriginalDomain", request.domainDir);
		}
		
		// On Error -- Delete the new files and undo the rename that was done
		//successfully above
		if(!tempRestoreDir.renameTo(request.domainDir))
		{
			oldDomain.renameTo(request.domainDir);
			FileUtils.whack(tempRestoreDir);
			throw new BackupException("backup-res.CantRenameRestoredDomain");
		}	
		
		FileUtils.whack(oldDomain);
	
private voidcheckDomainName()

		Status status = new Status();
		status.read(request.backupFile);
		String buDomainName = status.getDomainName();
		
		if(buDomainName == null)
        {
            //this means the backup zip is bad...
			throw new BackupException(StringHelper.get("backup-res.CorruptBackupFile", request.backupFile));
        }
        else if(!request.domainName.equals(buDomainName))
        {
            System.out.println(StringHelper.get("backup-res.DomainNameDifferentWarning", buDomainName, request.domainName));

            //LoggerHelper.warning(StringHelper.get("backup-res.DomainNameDifferentWarning", buDomainName, request.domainName));
            //throw new BackupException(StringHelper.get("backup-res.DomainNameDifferent", buDomainName, request.domainName));
        }
	
private voidcopyBackups()

 
		if(backupDir != null)
		{
			File tempRestoreDirBackups = new File(tempRestoreDir, Constants.BACKUP_DIR);
			FileUtils.copyTree(backupDir, tempRestoreDirBackups);
		}
	
voidinit()

		super.init();

		if(request.backupFile == null)
			initWithNoSpecifiedBackupFile();
		else
			initWithSpecifiedBackupFile();
		
		tempRestoreDir = new File(request.domainsDir, request.domainName +
			"_" + System.currentTimeMillis());
	
private voidinitWithNoSpecifiedBackupFile()

		// if they did NOT specify a backupFile, then we *must* have a pre-existing
		// backups directory in a pre-existing domain directory.
		
		if(!FileUtils.safeIsDirectory(request.domainDir))
			throw new BackupException("backup-res.NoDomainDir", request.domainDir);

		backupDir = new File(request.domainDir, Constants.BACKUP_DIR);
		
		// It's an error to not exist...
		if(!FileUtils.safeIsDirectory(backupDir))
			throw new BackupException("backup-res.NoBackupDir", backupDir);

		BackupFilenameManager bfmgr = new BackupFilenameManager(backupDir);
		request.backupFile = bfmgr.latest();
		
		//request.backupFile = getNewestZip(backupDir);
	
private voidinitWithSpecifiedBackupFile()

		if(request.backupFile.length() <= 0)
			throw new BackupException("backup-res.CorruptBackupFile", request.backupFile);

		if(!FileUtils.safeIsDirectory(request.domainDir))
			request.domainDir.mkdirs();

		backupDir = new File(request.domainDir, Constants.BACKUP_DIR);

		// It's NOT an error to not exist.  The domain may not exist currently and, besides,
		// they are specifying the backup-file from anywhere potentially...
		if(!FileUtils.safeIsDirectory(backupDir))
			backupDir = null;

		//throw new BackupException("NOT YET IMPLEMENTED");
		
	
private java.lang.StringreadAndDeletePropsFile()

		// The "backup.properties" file from the restored zip should be
		// in the domain dir now.
		File propsFile = new File(request.domainDir, Constants.PROPS_FILENAME);
		Status status = new Status();

		String mesg = StringHelper.get("backup-res.SuccessfulRestore", 
			request.domainName, request.domainDir );

		if(request.terse == false)
			mesg += "\n" + status.read(propsFile, false);
		
		if(!propsFile.delete())
			propsFile.deleteOnExit();
		
		return mesg;
	
public java.lang.Stringrestore()

		try
		{
			checkDomainName();
			ZipFile zf = new ZipFile(request.backupFile, tempRestoreDir);
			zf.explode();
			sanityCheckExplodedFiles();
			copyBackups();
			atomicSwap();
			setPermissions();
			String mesg = readAndDeletePropsFile();
			return mesg;
		}
		catch(BackupException be)
		{
			throw be;
		}
		catch(Exception e)
		{
			throw new BackupException("Restore Error", e);
		}
	
private voidsanityCheckExplodedFiles()

		// Is the "magic" properties file where it is supposed to be?
		
		File statusFile = new File(tempRestoreDir, Constants.PROPS_FILENAME);
		
		if(!statusFile.exists())
		{
			// cleanup -- we are officially failing the restore!
			FileUtils.whack(tempRestoreDir);
			throw new BackupException("backup-res.RestoreError.CorruptBackupFile.NoStatusFile", request.domainName);
		}
	
private voidsetPermissions()
zip is platform dependent -- so non-default permissions are gone!

		File backups		= new File(request.domainDir, "backups");
		File bin			= new File(request.domainDir, "bin");
		File config			= new File(request.domainDir, "config");
		File webtmp			= new File(request.domainDir, "generated/tmp");
		File masterPassword	= new File(request.domainDir, "master-password");

		// note that makeExecutable(File f) will make all the files under f
		// executable if f happens to be a directory.
		FileUtils.makeExecutable(bin);
		
		FileUtils.protect(backups);
		FileUtils.protect(config);
		FileUtils.protect(masterPassword);
		FileUtils.protect(webtmp);
		
 		// Jan 19, 2005 -- rolled back the fix for 6206176.  It has been decided
		// that this is not a bug but rather a security feature.
		//FileUtils.whack(webtmp);

		// fix: 6206176 -- instead of setting permissions for the tmp dir, 
		// we just delete it.   This will allow, say, user 'A' to do the restore,
		// and then allow user 'B' (including root) to start the domain without
		// getting a web-container error.
		// see: bug 6194504 for the tmp dir details
		//old:  
		//new: