FileDocCategorySizeDatePackage
ServerManager.javaAPI DocGlassfish v2 API20503Fri May 04 22:35:00 BST 2007com.sun.enterprise.instance

ServerManager

public class ServerManager extends Object
A class that acts as a helper when one has to take care of various instances. Such a situation would probably occur in case of administration as it has to administer all the instances in the given install. It is supposed to be a singleton.
author
Jeet Kaul - originially.
author
Kedar Mhaswade.

Fields Summary
public static final String
INSTALL_ROOT
public static final String
INSTANCE_CFG_ROOT
public static final String
DOC_DIR_NAME
public static final String
ADMINSERVER_ID
private static final ServerManager
SHARED_INSTANCE
private static Logger
sLogger
Constructors Summary
private ServerManager()
Private constructor so that nobody else creates the instance


	          

	  
	
Methods Summary
public voidcreateServerInstance(InstanceDefinition instance)
Creates the given ServerInstance. InstanceDefinition may not be null. Gives call to underlying cgi program to actually create the new instance of iAS. The creation of instance consists of creation of directory structure as per the definition of instance. Note that this method does an expensive Runtime.exec() internally.

param
instance the InstanceDefinition pertaining to instance to be created.
throws
ConfigException if there is any error in arguments and/or the underlying program throws an error.
throws
IllegalArgumentException in case of null instance.

            /*
             * Be very careful with this method. All the paths that are
             * required to be put into the various config files for
             * an instance have forward slashes regardless of the
             * underlying platform.
            */
            /* Removing this entire method as it is not applicable to PE */
            throw new UnsupportedOperationException("createServerInstance - not in PE");
	    
public voiddeleteServerInstance(java.lang.String instanceName)
A method to delete the server instance with given name or id. Unlike other methods to create, start and stop the instances, this method does not depend on other lower level API. The file system folder where the instance's configuration resides, is deleted. Running instance should be soped before this call (in ServerController). On NT the Service needs to be deleted - which is not yet implemented (01/26/02).

param
instanceName name of the instance(id) to be deleted - may not be null.
throws
ConfigException if the action encounters some problems.

            /* Removing this entire method as it is not applicable to PE */
        throw new UnsupportedOperationException("deleteServerInstance - not in PE");
	
public java.lang.StringgetDomainName()
Provides the domain name from the domain root

		File domainRoot = new File(INSTANCE_CFG_ROOT);
		return domainRoot.getName();
	
private intgetFreePort(int defaultPort)
This method is a wrapper over the other routine to get the port. It provides logging support and default ports.

param
integer that caller indicates to return in case of a failure.
return
a free port if available, the passed port otherwise.

        int port = NetUtils.getFreePort();
        if (port == 0) {
            /* log the stuff */
            sLogger.log(Level.SEVERE, "general.free_port_failed");
            return defaultPort;
        }
        else {
            Integer portInteger = new Integer(port);
            sLogger.log(Level.INFO, "general.free_port", portInteger);
            return port;
        }
    
public java.lang.String[]getInstanceNames(boolean countAdmin)
The instance names are not registered in any configuration file for iASSE. Hence this method scans the folder on disk where installation is done, each time it is called and returns the instance names. Note that all the instances are denoted by https-id folders in INSTALL_ROOT. This method returns an array of all ids. Note that admin server itself is NOT returned as an Instance.

return
an array of all instance names. An array of zero length, if there isn't any. Never returns a null.
throws
SecurityException, if there is no read permission to INSTALL_ROOT.

        //KE: FIXTHIS. We can get instances from domain.xml only.
        return new String[] {"server"};
	
public java.lang.StringgetInstanceUser(InstanceEnvironment env)

        /*
            Need an alternative. PE conf does'nt include init.conf. 
            Ramakanth 04/23/2003
         */
        /*
        if (env == null)
        {
            throw new IllegalArgumentException("env cant be null");
        }
        InitConfFileBean initConf = new InitConfFileBean();
        initConf.readConfig(env.getInitFilePath());
        String instanceUser = initConf.get_mag_var("User");
        return instanceUser;
        */
        return System.getProperty("user.name");
    
public java.lang.StringgetMimeTypesTemplateFilePath()
Returns the path of the mime.types.template file for this installation. This templates file is stored in the templates directory.

return
path of template file for mime.types

        final String libDirName             = "lib";
        final String installDirName         = "install";
        final String templateDirName        = "templates";
        final String mimeTemplateFileName   = "mime.types.template";

        String [] fileNames = new String[] {INSTALL_ROOT, libDirName,
            installDirName, templateDirName, mimeTemplateFileName};
        return ( StringUtils.makeFilePath(fileNames, false) );
    
public intgetNumInstances(boolean countAdmin)
Returns the number of instances depending on whether the admin instance is to be counted.

param
countAdmin true if the returned list should contain admin
return
integer representing the number of server instances.

		return ( getInstanceNames(countAdmin).length );
	
public java.lang.String[]getSecurityTokensForInstance(InstanceDefinition instance)

		if (instance == null) {
			throw new IllegalArgumentException();
		}
		String[] command = instance.getGetSecurityTokensCommand();
        String[] inputLines = null;
        try {
            sLogger.log(Level.FINE, "general.gettokens_cmd", command[0]);
			ProcessExecutor executor = new ProcessExecutor(command);
			return executor.execute(true);
		}
                catch (ExecException ee) {
                        throw new RuntimeException(Localizer.getValue(ExceptionType.NO_RECEIVE_TOKENS));
                }
		catch (Exception e) {
			throw new ConfigException(e.getMessage());
		}
	
public static com.sun.enterprise.instance.ServerManagerinstance()
Static Method to get a reference to the singleton.

return
instance of ServerManager class.

		return SHARED_INSTANCE;
	
public booleaninstanceExists(java.lang.String instanceID)
A method to find whether a Server Instance with given name exists in the install. The instanceID should be the one designated by the user while creating the instance. An Instance is recognized by the configuration stored in a folder (on file system) named https- id. (This is for historical reasons). So, there are as many instances as there are folders beginning with string https-. No other consideration will be given. This method always ignores admin server.

param
instanceID representing the id of the Server Instance. May not be null.
return
true if the given instance's config directory exists, false otherwise.

        //KE: FIXTHIS: We can only tell if an instance exists by looking 
        //at domain.xml
        if (instanceID.equals("server")) {
            return true;
        }
        return false;
	
private booleanportTakenByHTTP(int port)
Method to check whether the given port has clash with the http ports of other instances. It checks in the server.xml of various instances for this.

        boolean     portTaken   = true;
        String      portString  = "" + port;
        String[]    instances = getInstanceNames(true);
        /* count admin server also for this purpose */
        try {
            for (int i = 0 ; i < instances.length ; i++) {
                String instanceName = instances[i];
                InstanceEnvironment inst =
                        new InstanceEnvironment(instanceName);
                String backURL = inst.getConfigFilePath();
                ConfigContext context = ConfigFactory.
                        createConfigContext(backURL);
                Config rootElement = ServerBeansFactory.getConfigBean(context);

                HttpService httpService = rootElement.getHttpService();
                HttpListener[] httpListeners =
                        httpService.getHttpListener();
                for (int j = 0 ; j < httpListeners.length ; j++) {
                    String aPort = httpListeners[j].getPort();
                    aPort = aPort.trim();
                    sLogger.log(Level.FINE, "port = " + aPort);
                    if (aPort.equals(portString)) {
                        sLogger.log(Level.WARNING,
                                "general.port_occupied", instanceName);
                        return portTaken;
                    }
                }
            }
        }
        catch (Exception e) {
            sLogger.log(Level.WARNING, "general.port_derivation_failed", e);
        }
        return ( false );
    
private booleanportTakenByJMS(int port)
Checks whether this port is taken by jms provider (imq broker).

        boolean     portTaken   = true;
        String      portString  = "" + port;
        String[]    instances = getInstanceNames(true);
        /* count admin server also for this purpose */
        try {
            for (int i = 0 ; i < instances.length ; i++) {
                String instanceName = instances[i];
                InstanceEnvironment inst =
                        new InstanceEnvironment(instanceName);
                String backURL = inst.getConfigFilePath();
                ConfigContext context = ConfigFactory.
                        createConfigContext(backURL);
                JmsHost jmsHost = ServerBeansFactory.getJmsHostBean(context);
                String aPort = jmsHost.getPort();
                aPort = aPort.trim();
                sLogger.log(Level.FINE, "port = " + aPort);
                if (aPort.equals(portString)) {
                    sLogger.log(Level.WARNING,
                            "general.port_occupied", instanceName);
                    return portTaken;
                }
            }
        }
        catch (Exception e) {
            sLogger.log(Level.WARNING, "general.port_derivation_failed", e);
        }
        return ( false );
    
private booleanportTakenByORB(int port)
Checks whether this port is occupied by any of the orb listeners.

        boolean     portTaken   = true;
        String      portString  = "" + port;
        String[]    instances = getInstanceNames(true);
        /* count admin server also for this purpose */
        try {
            for (int i = 0 ; i < instances.length ; i++) {
                String instanceName = instances[i];
                InstanceEnvironment inst =
                        new InstanceEnvironment(instanceName);
                String backURL = inst.getConfigFilePath();
                ConfigContext context = ConfigFactory.
                        createConfigContext(backURL);

                Config rootElement = ServerBeansFactory.getConfigBean(context);

                IiopService iiopService = rootElement.getIiopService();
                IiopListener[] iiopListeners = iiopService.getIiopListener();
                for (int j = 0 ; j < iiopListeners.length ; j++) {
                    String aPort = iiopListeners[j].getPort();
                    aPort = aPort.trim();
                    sLogger.log(Level.FINE, "port = " + aPort);
                    if (aPort.equals(portString)) {
                        sLogger.log(Level.WARNING,
                                "general.port_occupied", instanceName);
                        return portTaken;
                    }
                }
            }
        }
        catch (Exception e) {
            sLogger.log(Level.WARNING, "general.port_derivation_failed", e);
        }
        return ( false );
    
public voidrestartServerInstance(InstanceDefinition instance)

		if (instance == null) {
			throw new IllegalArgumentException();
		}
		String[] restartCommand = instance.getRestartCommand();
		try {
            sLogger.log(Level.FINE, "general.exec_cmd", restartCommand[0]);
			ProcessExecutor executor = new ProcessExecutor(restartCommand);
			executor.execute();
		}
		catch (Exception e) {
			throw new ConfigException(e.getMessage());
		}
	
public voidstartServerInstance(InstanceDefinition instance)
Starts the given instance. The argument may not be null. Gives call to start program inside the proper directory structure of that instance (e.g. /export/iplanet/ias7/https-test/start). The createServerInstance call has to succeed for this call to work. Note that it gives call to an expensive Runtime.exec().

param
instance the InstanceDefinition pertaining to instance to be created.
throws
ConfigException if there is any error in arguments and/or the underlying program throws an error.
throws
IllegalArgumentException in case of null instance.

		startServerInstance(instance, null);
	
public voidstartServerInstance(InstanceDefinition instance, java.lang.String[] passwords)

		if (instance == null) {
			throw new IllegalArgumentException();
		}
		String[] startCommand = instance.getStartCommand();
        String[] inputLines = null;
        if(passwords!=null)
            inputLines = passwords;
        else
            inputLines = new String[]{}; //to provoke stream closing
        //startCommand = startCommand + " " + instance.getID() + " " + INSTALL_ROOT;
        try {
            sLogger.log(Level.FINE, "general.exec_cmd", startCommand[0]);
			ProcessExecutor executor = new ProcessExecutor(startCommand, inputLines);
			executor.execute();
		}
                catch (ExecException ee) {
                    sLogger.log(Level.WARNING, "general.exec_cmd", ee);
                    throw new RuntimeException(Localizer.getValue(ExceptionType.SERVER_NO_START));
                }
		catch (Exception e) {
			throw new ConfigException(e.getMessage());
		}
	
public voidstopServerInstance(InstanceDefinition instance)
Stops the given instance. The argument may not be null. Gives call to stop program inside the proper directory structure of that instance (e.g. /export/iplanet/ias7/https-test/stop). The createServerInstance call has to succeed for this call to work. Note that it gives call to an expensive Runtime.exec().

param
instance the InstanceDefinition pertaining to instance to be created.
throws
ConfigException if there is any error in arguments and/or the underlying program throws an error.
throws
IllegalArgumentException in case of null instance.

		if (instance == null) {
			throw new IllegalArgumentException();
		}
		String stopCommand[] = instance.getStopCommand();
		try {
            sLogger.log(Level.FINE, "general.exec_cmd", stopCommand[0]);
			ProcessExecutor executor = new ProcessExecutor(stopCommand);
			executor.execute();
		}
		catch (Exception e) {
			throw new ConfigException(e.getMessage());
		}