FileDocCategorySizeDatePackage
AppServerAdmin.javaAPI DocGlassfish v2 API32029Mon Jul 30 17:43:46 BST 2007org.apache.tools.ant.taskdefs.optional.sun.appserv

AppServerAdmin

public abstract class AppServerAdmin extends org.apache.tools.ant.Task
Abstract class which includes base functionality for Tasks targeting the Sun ONE Application Server 8. This includes support for the following attributes:
  • user -- The username used when logging into the application server administration instance. This attribute is inherited by nested <server> elements. Defaults to "admin"
  • password -- The password used when logging into the application server administration instance. This attribute is inherited by nested <server> elements
  • host -- Target server for the command(s). When deploying to a remote server, the fully qualified hostname should be used. This attribute is inherited by nested <server> elements. Defaults to "localhost"
  • port -- Admin port on the target server. This attribute is inherited by nested <server> elements. Defaults to "4848"
  • secure -- This attribute uses SSL/TLS to communicate with the domain application server.
  • instance -- Target application server instance for the command. This attribute is inherited by nested <server%gt elements. If not specified, the default instance name will be used
  • sunonehome -- This attribute has been deprecated, use asinstall.dir instead. The installation directory for the local Sun ONE Application Server 8 installation -- this is used to find the correct administrative classes. If not specified, the task will check to see if the "sunone.home" parameter has been set. Otherwise, the Sun ONE Application Server 8 admin classes must be on the system classpath
  • asinstalldir -- The installation directory for the local Sun ONE Application Server 8 installation -- this is used to find the correct administrative classes. If not specified, the task will check to see if the "asinstall.dir" parameter has been set. Otherwise, the Sun ONE Application Server 8 admin classes must be on the system classpath

In addition, this abstract class provides support for nested <server> elements. These nested elements enable commands to be executed against multiple Sun ONE Application Servers in a single Ant command. Attributes may be specified in the "root" command element and those values will be used ("inherited") by all of the nested <server> components. If an attribute is specified in both elements, the value specified in the <server> element will be used.

author
Greg Nelson gn@sun.com

Fields Summary
static Method
invokeCLI
static Class
adminMain
static Class
inputsAndOutputs
static Class
systemPropertyConstants
protected Server
server
This Server instance will store user settings from the "root" command element.
protected List
servers
The list of servers will store user settings from nested <server> elements.
private File
asinstalldir
Sun ONE Application Server 8 installation directory
private boolean
executeCommand
This attribute is used only for debugging and is not included in the task documentation. When set to "true" the task behaves normally. When set to "false", each admin command is logged but not actually executed.
private static final String
CLASS_INPUTS_AND_OUTPUTS
Constants used to invoke the admin utilies using Java reflection
private static final String
CLASS_ADMIN_MAIN
private static final String
METHOD_INVOKE_CLI
LocalStringsManager
lsm
private static final String
CLASS_SYSTEM_PROPERTY_CONSTANTS
Constructors Summary
public AppServerAdmin()
Creates a new instance with the infrastructure for running admin commands.


	           	 
	  
		server = getNewServer();
	
Methods Summary
protected voidcheckConfiguration()
Verifies that the options and parameters selected by the user are valid and consistent.

throws
BuildException If the user selections are invalid.


		log(servers.size() + " servers were found.", Project.MSG_DEBUG);

		// At least one target server must be specified
		if (servers.size() == 0) {
            final String msg = lsm.getString("SpecifyOneServer");
			throw new BuildException(msg, getLocation());
		}

		// Check the configuration of each target server specified
		Iterator it = servers.iterator();
		while (it.hasNext()) {
			Server aServer = (Server)it.next();
			checkConfiguration(aServer);
		}
	
protected abstract voidcheckConfiguration(org.apache.tools.ant.taskdefs.optional.sun.appserv.AppServerAdmin$Server aServer)
Verifies that the options and parameters selected by the user are valid and consistent for a given server.

param
aServer The server whose configuration is to be validated.
throws
BuildException If the user selections are invalid.

public org.apache.tools.ant.taskdefs.optional.sun.appserv.AppServerAdmin$ServercreateServer()
Creates a nested server element.

return
Server which has been added

		log("createServer", Project.MSG_DEBUG);
		Server aNestedServer = getNewServer();
		servers.add(aNestedServer);
		return aNestedServer;
	
protected voidexecAdminCommand(java.lang.String command)
Executes a command-string using the Sun ONE Application Server administrative CLI infrastructure.

param
command The administrative command String to be executed.
throws
BuildException If any error occurs while executing the command.

		log("Executing: " + command, Project.MSG_INFO);

		try {
            
            if (invokeCLI == null) {
                java.lang.ClassLoader antClassLoader = new AntClassLoader(
                    AppservClassLoader.getClassLoader(), getProject(), null, false);
                log("class = " + ((AntClassLoader)antClassLoader).getClasspath(), Project.MSG_DEBUG);
                inputsAndOutputs = Class.forName(CLASS_INPUTS_AND_OUTPUTS, true, antClassLoader);
                adminMain = Class.forName(CLASS_ADMIN_MAIN, true, antClassLoader);
                systemPropertyConstants = Class.forName(CLASS_SYSTEM_PROPERTY_CONSTANTS, true, antClassLoader);
            }

            
            log("***** INSTALL_ROOT_PROPERTY = " + (String)systemPropertyConstants.getField("INSTALL_ROOT_PROPERTY").get(null), Project.MSG_DEBUG);

            final String installRootConstant = (String)systemPropertyConstants.getField("INSTALL_ROOT_PROPERTY").get(null);
            final String configRootConstant = (String)systemPropertyConstants.getField("CONFIG_ROOT_PROPERTY").get(null);

            final String installRoot =  getInstallRoot(installRootConstant);
            
            log("installRoot: " + installRoot, Project.MSG_DEBUG);
            final String libraryPath = (String) System.getProperty("java.library.path");

            System.setProperty("java.library.path", installRoot+"/lib"+File.pathSeparator+libraryPath);
            System.setProperty(installRootConstant, installRoot);
            System.setProperty(configRootConstant, installRoot+"/config");
            System.setProperty("java.endorsed.dirs", installRoot+"/lib/endorsed");

            //debug display all system properties
            /*
            for (java.util.Enumeration en = System.getProperties().propertyNames(); en.hasMoreElements() ;) 
            {
                String name = (String) en.nextElement();
                String value = (String) System.getProperties().getProperty(name);
                log("System.property = " + name + " " + value, Project.MSG_DEBUG);
            }
            */
            
			Class[] parameterClasses = {String.class, inputsAndOutputs};
			invokeCLI = adminMain.getDeclaredMethod(METHOD_INVOKE_CLI, parameterClasses);

			Object[] parameters = {command, null};

			if (executeCommand) {
				invokeCLI.invoke(adminMain, parameters);
			}
		} catch (ClassNotFoundException e) {
            final String msg = lsm.getString("ClassCouldNotBeFound", new Object[] {e.getMessage()});
			throw new BuildException(msg, getLocation());
        } catch (NoSuchMethodException e) {
            final String msg = lsm.getString("CouldNotFindInvokeCLI", new Object[] {e.getMessage()});
			throw new BuildException(msg, getLocation());
		} catch (InvocationTargetException e) {
            final String msg = lsm.getString("ExceptionOccuredRunningTheCommand", new Object[] {e.getTargetException().getMessage()});
			throw new BuildException(msg, getLocation());
		} catch (IllegalAccessException e) {
            final String msg = lsm.getString("ExceptionOccuredInvokeingCLI", new Object[] {e.getMessage()});
			throw new BuildException(msg, getLocation());
		} catch (Exception e) {
            throw new BuildException(e.getMessage(), getLocation());
        }
        
        
	
public voidexecute()
Does the work.

throws
BuildException If the user selections are invalid or an exception occurs while executing the app server admin commands.

		prepareToExecute();
		checkConfiguration();

		Iterator it = servers.iterator();
		while (it.hasNext()) {
			Server aServer = (Server)it.next();
			execute(aServer);
		}
	
protected abstract voidexecute(org.apache.tools.ant.taskdefs.optional.sun.appserv.AppServerAdmin$Server server)
Builds and executes the admin command on the server specified.

param
aServer The target server for the administrative command.
throws
BuildException If any error occurs while executing the command.

protected java.io.FilegetAsinstalldir()
Returns the asinstalldir attribute specify by in the build script. If asinstalldir hasn't been explicitly set (using the setAsinstalldir method), the value stored in the sunone.home property will be returned.

return
File representing the app server installation directory. Returns null if the installation directory hasn't been explictly set and the sunone.home property isn't set.
throws
ClassNotFoundException if asinstalldir is an invalid directory

		if (asinstalldir == null) {
			String home = getProject().getProperty("asinstall.dir");
			if (home != null) {
                asinstalldir = new File(home);
			}
            else {
                home = getProject().getProperty("sunone.home");
                if (home != null)
                {
                    final String msg = lsm.getString("DeprecatedProperty", new Object[] {"sunone.home", "asinstall.dir"});
                    log(msg, Project.MSG_WARN);
                    asinstalldir = new File(home);
                }
                
            }
		}
        if (asinstalldir!=null) verifyAsinstalldir(asinstalldir);
		return asinstalldir;
	
private java.lang.StringgetInstallRoot(java.lang.String installRootConstant)
returns the appserver server installation directory first it will get asinstalldir from attribute of ant task. if user did not specify asinstalldir, then the retrieve the install directory from java system property using installrootConstant. if all else fails, an ClassNotFoundException is thrown and the appropraite msg will be displayed to bhe user.

param
installRootconstant - the install root constant to retrieve from java system property.
return
install directory
throws
ClassNotFoundException if install directory can not be determined.

        final File installDir = getAsinstalldir();
        String installRoot;
        if (installDir == null) {
            installRoot = System.getProperty(installRootConstant);
            if (installRoot == null)
                throw new ClassNotFoundException("ClassCouldNotBeFound");
        } else {
            installRoot = installDir.getPath();
            System.setProperty(installRootConstant, installRoot);
        }
        return installRoot;
    
protected org.apache.tools.ant.taskdefs.optional.sun.appserv.AppServerAdmin$ServergetNewServer()
Builds a new server instance. This method is intended to be overridden by subclasses which implement their own subclass of Server.

return
new Server instance.

		return new Server(server);
	
protected voidprepareToExecute()
Does any clean-up work required before the user configuration is checked and the command(s) executed.

		if (servers.size() == 0) {
			servers.add(server);
		}
	
public voidsetAsinstalldir(java.io.File asinstalldir)
Specifies the installation directory for the Sun ONE Application Server 8. This may be used if the application server is installed on the local machine.

param
asinstalldir The home directory for the user's app server installation.

		this.asinstalldir = asinstalldir;
	
public voidsetExecuteCommand(boolean executeCommand)
This attribute is used only for debugging and is not included in the task documentation. When set to "true" the task behaves normally. When set to "false", each admin command is logged but not actually executed.

param
executeCommand Whether or not to execute the admin commands generated by this task

		this.executeCommand = executeCommand;
	
public voidsetHost(java.lang.String host)
Sets the hostname where the application server administration instance is running.

param
host The host name where the administration instance is running

		server.setHost(host);  // Delegates to server object
	
public voidsetInstance(java.lang.String instance)
Sets the application server instance name. Typically, this is the "target" for administrative commands.

param
instance The name of the application server instance

		server.setInstance(instance);  // Delegates to server object
	
public voidsetPassword(java.lang.String password)
Sets the password used when logging into the application server administration instance.

param
password The password used to logon

       final String msg = lsm.getString("DeprecatedAttribute", new Object[] {"password",
         "passwordfile"});
        log(msg, Project.MSG_WARN);
		server.setPassword(password);  // Delegates to server object
	
public voidsetPasswordfile(java.lang.String passwordfile)
Sets the passwordfile used when logging into the application server administration instance.

param
password The password used to logon

		server.setPasswordfile(passwordfile);  // Delegates to server object
	
public voidsetPort(int port)
Sets the port where the application server administration instance is running.

param
port The port number of the administration instance

		server.setPort(port);  // Delegates to server object
	
public voidsetSecure(java.lang.String secure)
Sets secure option

param
secure. The secure if true, uses SSL/TLS to communicate with the domain application server.

		server.setSecure(secure);  // Delegates to server object
	
public voidsetSunonehome(java.io.File sunonehome)
Specifies the installation directory for the Sun ONE Application Server 8. This may be used if the application server is installed on the local machine.

param
sunonehome The home directory for the user's app server installation.

        final String msg = lsm.getString("DeprecatedAttribute", new Object[] {"sunonehome",
         "asinstalldir"});
        log(msg, Project.MSG_WARN);
        this.asinstalldir = sunonehome;
	
public voidsetUser(java.lang.String user)
Sets the username used when logging into the application server administration instance.

param
user The username used to logon

		server.setUser(user);  // Delegates to server object
	
private booleanverifyAsinstalldir(java.io.File home)
verify if asinsatlldir attribute is valid. asinstalldir must be a valid directory and must contain the config directory.

return
true if asinstalldir is valid
throws
ClassNotFoundException if asinstalldir is an invalid directory

        if (home!= null && home.isDirectory()) {
            if ( new File(home, "config").isDirectory() ) {
                return true;
            } 
        }
        throw new ClassNotFoundException("ClassCouldNotBeFound");