FileDocCategorySizeDatePackage
AdminTask.javaAPI DocGlassfish v2 API12225Fri May 04 22:32:42 BST 2007org.apache.tools.ant.taskdefs.optional.sun.appserv

AdminTask

public class AdminTask extends AppServerAdmin
This task enables arbitrary administrative commands and scripts to be executed on the Sun ONE Application Server 7. This is useful for cases where a specific Ant task hasn't been developed or a set of related commands has been composed in a single script. In addition to the server-based attributes, this task introduces three attribute:
  • command -- The command to execute. If the "user", "password", "passwordfile", "host", "port" or "instance" attributes are also specified, they will automatically be inserted into the command before it is exectuted. If any of these options is specified in the command string, the corresponding attribute value will be ignored
  • commandfile -- The command script to execute. If the "user", "password", "passwordfile", "host", "port" or "instance" attributes are also specified, they will automatically be inserted into the command environment before the script is exectuted
  • explicitcommand -- The exact command to execute. No processing of the command is done, and all other attributes are ignored

see
AppServerAdmin
author
Greg Nelson gn@sun.com

Fields Summary
private String
command
private String
explicitCommand
private File
commandFile
LocalStringsManager
lsm
Constructors Summary
Methods Summary
private voidcheckCommandCount()
Verifies that one and only one of the command attributes (command, commandfile, and explicitcommand) has been set.

throws
BuildException If none of the command attributes has been set or multiple command attributes has been set

		int commandCount = 0;
		
		if (command != null) { commandCount++; }
		if (explicitCommand != null) { commandCount++; }
		if (commandFile != null) { commandCount++; }

		if (commandCount != 1) {
            final String msg = lsm.getString("ExactlyOneCommandAttribute");
			throw new BuildException(msg, getLocation());
		}
	
protected voidcheckConfiguration(Server aServer)

		// No error checking is done for this task
	
private booleancommandIncludes(java.lang.StringBuffer cmd, java.lang.String[] options)
Utility method that determines if command options have already been specified in the command line

param
cmd The command line to search.
param
options The options to search for.
return
true if one of the options is found in the command line

		for (int i = 0; i < options.length; i++) {
			if (cmd.indexOf(options[i]) > 0) {
				return true;
			}
		}
		return false;
	
public voidexecute()

		checkCommandCount();

		if ((command != null) && (servers.size() == 0) && (server == null)) {
			// No other attributes or server elements -- treat as explicitCommand // FIXME -- this will no longer ever happen
			explicitCommand = command;
			command = null;
		}

		if (explicitCommand != null) {
			// Normal execution process is skipped and command is run immediately
			execAdminCommand(explicitCommand);
		} else {
			super.execute();
		}
	
protected voidexecute(Server aServer)

		/*
		 * "command" and "commandfile" attributes are processed in this method.
		 * See the execute() method for "explicitcommand" processing.
		 */

		final String userOption[] = {"--user ", "-u "};
		final String passwordOption[] = {"--password ", "-w ", "--passwordfile "};
		final String hostOption[] = {"--host ", "-H "};
		final String portOption[] = {"--port ", "-p "};
		final String instanceOption[] = {"--instance ", "-i "};
        final String secureOption[] = {"--secure ", "-s "};

		StringBuffer cmd;
		if (command != null) {
			cmd = new StringBuffer(command);
			if (!commandIncludes(cmd, userOption)) {
				insertCommandOption(cmd, " --user " + aServer.getUser());
			}
			if ((aServer.hasPassword()) 
								&& (!commandIncludes(cmd, passwordOption))) {
				insertCommandOption(cmd, aServer.getPasswordCommand());
			}
			if (!commandIncludes(cmd, hostOption)) {
				String theHost = aServer.getHost();
				if (theHost == null) {
					theHost = Server.DEFAULT_HOST;
				}
				insertCommandOption(cmd, " --host " + theHost);
			}
			if (!commandIncludes(cmd, portOption)) {
				String thePort = (aServer.getPort() == 0) ?
									Server.DEFAULT_PORT :
									String.valueOf(aServer.getPort());
				insertCommandOption(cmd, " --port " + thePort);
			}
			if ((aServer.getInstance() != null) 
								&& (!commandIncludes(cmd, instanceOption))) {
				insertCommandOption(cmd, " --instance " + aServer.getInstance());
			}
            if (aServer.getSecure() != null &&
                !commandIncludes(cmd, secureOption)) {
				insertCommandOption(cmd, " --secure=" + aServer.getSecure());
			}

		} else {
			String filename;
			try
			{
				filename = commandFile.getCanonicalPath();
			}
			catch(Exception e)
			{
				filename = commandFile.getAbsolutePath();
			}
			// passing in '\\' for Windows would make CLI choke!
			filename = filename.replace('\\", '/");

			cmd = new StringBuffer("multimode --file " + filename + " "); 
		}

		execAdminCommand(cmd.toString());
	
private voidinsertCommandOption(java.lang.StringBuffer cmdLine, java.lang.String commandOption)
Utility method that inserts a string into the current command line. The string is inserted after the command name.

param
cmdLine The command line.
param
commandOption The string to insert into the command line.

		int index = cmdLine.indexOf(" ");
		index = (index >= 0) ? index : cmdLine.length();
		cmdLine.insert(index, commandOption).append(' ");
	
public voidsetCommand(java.lang.String command)
Sets the command to be executed. The task will automatically add user, password or passwordfile, host, port, and instance parameters if they're not specified in the command.

param
command The command to be executed


	                                   	 
	    
		this.command = command.trim();
	
public voidsetCommandfile(java.io.File commandFile)
Sets the command file which contains zero or more administrative commands which will be read and executed. The user, password or passwordfile, host, port, and instance parameters will automatically be added to the environment when the command is executed

param
commandFile The command file to be executed

            final String msg = lsm.getString("DeprecatedAttribute", 
                                                new Object[] {"commandfile", 
                                                              "- 'multimode --file <commandfile>'"});
            log(msg, Project.MSG_WARN);
            this.commandFile = commandFile;
	
public voidsetExplicitcommand(java.lang.String explicitCommand)
Sets the command to be executed. The task will execute the command as specified and will NOT automatically add user, password or passwordfile, host, port and instance parameters

param
command The command to be executed

		this.explicitCommand = explicitCommand;