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

CreateServiceCommand

public class CreateServiceCommand extends S1ASCommand

Fields Summary
private static final String
TYPE
private static final String
NAME
private static final String
SERVICE_PROPERTIES
private static final String
VALID_TYPES
private static final String
DAS_TYPE
Constructors Summary
Methods Summary
private java.lang.StringgetName(java.lang.String typeDir, java.lang.StringBuilder absolutePath)
Retrieves the domain/nodeagent name from the given directory

return
domain/nodeagent name
throws
CommandValidationException

        String path = "";
        try
        {
            //Already checked for the valid directory in validateOptions()
           final File f = new File(typeDir);
           String name = f.getName();
           absolutePath.append(f.getAbsolutePath());
           final String nameFromOption = getOption(NAME);
           if (nameFromOption != null)
               name = nameFromOption;
           CLILogger.getInstance().printDebugMessage("service name = " + name);
           return ( name );
        }
        catch (Exception e)
        {
            throw new CommandException(e.getLocalizedMessage());
        }
    
private voidprintSuccess(com.sun.enterprise.admin.servermgmt.Service service)

        final String[] params = new String[] {service.getName(), service.getType().toString(), service.getLocation(), service.getManifestFilePath()};
        final String msg = getLocalizedString("ServiceCreated", params);
        CLILogger.getInstance().printMessage(msg);
    
public voidrunCommand()
Method that Executes the command

throws
CommandException, CommandValidationException

        try {
            validateOptions();

            validateType();

            String passwordFile = getOption(PASSWORDFILE);
            String type = getOption(TYPE);
            String typeDir = (String) getOperands().get(0);
            final Service service = ServiceFactory.getService();
                //configure service
            service.setDate(new Date().toString());
            final StringBuilder ap = new StringBuilder();
            service.setName(getName(typeDir, ap));
            service.setLocation(ap.toString());
            service.setType(type.equals("das") ? 
                            AppserverServiceType.Domain
                            : AppserverServiceType.NodeAgent);
            service.setFQSN();
            service.setOSUser();
            service.setAsadminPath(SystemPropertyConstants.getAsAdminScriptLocation());
            service.setPasswordFilePath(passwordFile);
            service.setServiceProperties(getOption(SERVICE_PROPERTIES));
            service.isConfigValid(); 
            service.setTrace(CLILogger.isDebug());
            service.createService(service.tokensAndValues());
            printSuccess(service);
            CLILogger.getInstance().printDetailMessage(getLocalizedString(
                                                           "CommandSuccessful",
                                                           new Object[] {name}));
        }
        catch (Exception e) {
            displayExceptionMessage(e);
        }
    
protected voidvalidateNodeAgent(java.io.File typeDir)

        throw new CommandException(getLocalizedString( "TypeNotSupported"));
    
public booleanvalidateOptions()
A method that validates the options/operands

return
boolean returns true if success else returns false
throws
CommandValidationException


                              
        
    
        super.validateOptions();
        String passwordFile = getOption(PASSWORDFILE);
        //check final the passwordfile is valid
        if (! new File(passwordFile).exists()) 
        {
            final String msg = getLocalizedString("FileDoesNotExist", 
                                                   new Object[] {passwordFile});
            throw new CommandValidationException(msg);
        }
        String typedirOperand = (String) getOperands().get(0);
        File typeDir = new File(typedirOperand);        
        String type = getOption(TYPE);
        if (!type.matches(VALID_TYPES))
        {
            throw new CommandValidationException(
                            getLocalizedString("InvalideServiceType"));
        }
        if (!typeDir.exists() || !typeDir.canWrite() || !typeDir.isDirectory()) 
        {
            final String msg = getLocalizedString("InvalidDirectory", 
                                                    new Object[] {typeDir});
            throw new CommandValidationException(msg);
        }
    	return true;
    
protected voidvalidateType()

        String typedirOperand = (String) getOperands().get(0);
        File typeDir = new File(typedirOperand);        
        String type = getOption(TYPE);
        //Validate the domain directory
        if (type.equals(DAS_TYPE))
        {
            String domainName = typeDir.getName();
            String domainRoot = typeDir.getParent();
            try {
                DomainConfig dc = new DomainConfig(domainName, domainRoot);
                RepositoryManager rm = new RepositoryManager();
                rm.checkRepository(dc, true);
            }catch (RepositoryException re){
                throw new CommandException(re.getLocalizedMessage());
            }
        }
        else // validate the node-agent directory
        {
            validateNodeAgent(typeDir);
        }