FileDocCategorySizeDatePackage
BackupCommands.javaAPI DocGlassfish v2 API11173Fri May 04 22:25:08 BST 2007com.sun.enterprise.cli.commands

BackupCommands

public class BackupCommands extends BaseLifeCycleCommand
This is a local command for backing-up domains.
version
$Revision: 1.4 $ The Options:
  • domaindir
The Operand:
  • domain

Fields Summary
private static final String
DOMAINSDIR
private static final String
FILENAME
private static final String
DESCRIPTION
private com.sun.enterprise.config.backup.BackupRequest
request
private String
domainName
private String
domainsDir
private String
backupFilename
private String
description
private CmdType
command
private boolean
terse
private boolean
verbose
Constructors Summary
Methods Summary
private voidcheckOptions()
A method that checks the options and operand that the user supplied. These tests are slightly different for different CLI commands

		// disallow backup & restore if server is running.  list-backups is OK anytime...
		if(command == CmdType.BACKUP || command == CmdType.RESTORE)
		{
			if(!isNotRunning())
			{
				throw new CommandValidationException(getLocalizedString("DomainIsNotStopped",
					new String[] {command.name} ));
			}
		}
		// make sure we have a domainsDir
		if(domainsDir == null || domainsDir.length() <= 0)
		{
			throw new CommandValidationException(getLocalizedString("InvalidDomainPath",
				new String[] {domainsDir}) );
		}

		File domainsDirFile = new File(domainsDir);

		// make sure domainsDir exists and is a directory
		if(!domainsDirFile.isDirectory())
		{
			throw new CommandValidationException(getLocalizedString("InvalidDomainPath",
				new String[] {domainsDir}) );
		}

		File domainFile = new File(domainsDirFile, domainName);

		// BACKUP, LIST: make sure the domain dir exists and is 
		//              a directory and is writable
		// RESTORE: It must exist if backupFilename isn't set.
		boolean domainDirDoesNotHaveToExist = 
			(command == CmdType.RESTORE) && backupFilename != null;
							
		if(!domainDirDoesNotHaveToExist)
		{
			if(!domainFile.isDirectory() || !domainFile.canWrite())
			{
				throw new CommandValidationException(getLocalizedString("InvalidDirectory",
					new String[] {domainFile.getPath()}) );
			}
		}
		
		if(backupFilename != null)
		{
			File f = new File(backupFilename);
			
			if(!f.exists() || !f.canRead())
			{
				throw new CommandValidationException(getLocalizedString("FileDoesNotExist",
					new String[] { backupFilename } ));
			}
		}
	
private booleanisNotRunning()

		try
		{
			ClientPluggableFeatureFactory	cpff	= getFeatureFactory();
			DomainsManager					dm		= cpff.getDomainsManager();
			DomainConfig					dc		= getDomainConfig(domainName);
			InstancesManager				im		= dm.getInstancesManager(dc);
			final int						state	= im.getInstanceStatus();

			return state == Status.kInstanceNotRunningCode;
		}
		catch(Exception e)
		{
			throw new CommandValidationException(e);
		}
	
private voidprepareRequest()

		if(backupFilename == null)
			request = new BackupRequest(domainsDir, domainName, description);
		else
			request = new BackupRequest(domainsDir, domainName, description, backupFilename);
		
		request.setTerse(terse);
		request.setVerbose(verbose);
	
public voidrunCommand()
An abstract method that executes the command

throws
CommandException

		validateOptions();
		
		try
		{
			if(command == CmdType.BACKUP)
			{
				BackupManager mgr = new BackupManager(request);
				CLILogger.getInstance().printMessage(mgr.backup());
			}
			else if(command == CmdType.RESTORE)
			{
				RestoreManager mgr = new RestoreManager(request);
				CLILogger.getInstance().printMessage(mgr.restore());
			}
			else if(command == CmdType.LIST)
			{
				ListManager mgr = new ListManager(request);
				CLILogger.getInstance().printMessage(mgr.list());
			}
			else
			{
				// IMPOSSIBLE!!!
				throw new CommandException("Internal Error");
			}
		}
		catch(BackupWarningException bwe)
		{
			CLILogger.getInstance().printMessage(bwe.getMessage());
		}
		catch(BackupException be)
		{
			throw new CommandException(be);
		}
	
private voidsetBackupFilename()

		// this option is only used for restore operations
		backupFilename = getOption(FILENAME);
	
private voidsetCommand()

		String cmd = getName();
		command = CmdType.valueOf(cmd);
		
		if(command == null)
		{
			// This shouldn't happen unless somebody erred editing CLIDescriptor.xml
			throw new CommandValidationException(
				getLocalizedString("NoUsageText", new String[] {cmd}) );
		}
	
private voidsetDescription()

		description = getOption(DESCRIPTION);
	
private voidsetDomainName()

		try
		{
			domainName = getDomainName();
		}
		catch(CommandException ce)
		{
			throw new CommandValidationException(ce);
		}
		//domainName = (String)operands.firstElement();
	
private voidsetDomainsDir()

		domainsDir = getOption(DOMAINSDIR);

		if(domainsDir == null || domainsDir.length() <= 0)
			domainsDir = System.getProperty(SystemPropertyConstants.DOMAINS_ROOT_PROPERTY);
	
private voidsetOptions()
A method that sets the options and operand that the user supplied.

		setCommand();
		setDomainsDir();
		setDomainName();
		setBackupFilename();
		setDescription();
		setVerbosity();
	
private voidsetVerbosity()

		if(getBooleanOption("terse"))
			terse = true;

		if(getBooleanOption("verbose"))
			verbose = true;
		
		// it is an error for both to be true (duh!)
		
		if(verbose && terse)
			throw new CommandValidationException(getLocalizedString("NoVerboseAndTerseAtTheSameTime"));
	
public java.lang.StringtoString()

		return super.toString() + "\n" + ObjectAnalyzer.toString(this);
	
public booleanvalidateOptions()
An abstract method that validates the options on the specification in the xml properties file

return
true if successfull

		super.validateOptions();
		setOptions();
		checkOptions();
		prepareRequest();
		
		// if anything went wrong, an Exception would have been thrown 
		// and we'd never get to this return statement...
		return true;