FileDocCategorySizeDatePackage
CLIParser.javaAPI DocGlassfish v2 API16035Tue Aug 28 14:41:58 BST 2007com.sun.enterprise.tools.upgrade.cli

CLIParser

public class CLIParser extends ArgsParser implements InteractiveInput
author : Gautam Borah

Fields Summary
private boolean
interactiveInput
private int
index
private Map
interactiveInputMap
private String
currentDomain
private com.sun.enterprise.util.i18n.StringManager
sm
private CommonInfoModel
commonInfo
private ArgsParser
parser
Constructors Summary
public CLIParser()
CLIParser constructor


           
     
        this(new CommonInfoModel(), new String [] {});
    
public CLIParser(CommonInfoModel cim, String[] args)
CLIParser constructor with arguments

        super(args, cim);
        commonInfo = cim;
        sm = StringManager.getManager(LogService.UPGRADE_CLI_LOGGER);
        
        //Parse the inputs and collect any missing arguments
        interactiveInputMap = parse(this);
        
        //Verify if all inputs have been provided
        verifyCommonInfo(commonInfo);
        
        //Build source domains mapping if not already done so.
        commonInfo.enlistDomainsFromSource();
        
        //Check if this is a valid upgrade path.
        if(!commonInfo.isUpgradeSupported()) {
            getLogger().info(sm.getString(
                    "enterprise.tools.upgrade.cli.upgrade_not_supported"));
            cim.recover();	    
            System.exit(1);
        }
    
Methods Summary
private voidcheckValidSourceDir(java.lang.String sourceDir)

        if(!UpgradeUtils.getUpgradeUtils(commonInfo).isValidSourcePath(sourceDir)) {
            getLogger().severe(
                    sm.getString("enterprise.tools.upgrade.cli.not_valid_source_install"));
            commonInfo.recover();	    
            helpUsage(1);
        }
    
public voidcollectMissingArguments(java.util.Map inputMap)
Method to collect missing arguments from the user in CLI mode.

        //If no other instructions are to be received, return.    
        if(inputMap.containsKey(UpgradeConstants.CLI_OPTION_NOPROMPT))
            return;
        
        List domainList = commonInfo.getDomainList();
        
        // Following are Invalid Inputs for Interaction
        //Source is null but domain name is not null
        //Domain name is null but nsspwd, jkdpwd, capwd not null
        Iterator itr =  domainList.iterator();
        while(itr.hasNext()) {
            String domainName = (String)itr.next();
            String suffix = "-" + domainName;
            if( inputMap.get(SOURCE) == null && 
                    inputMap.get(SOURCE_SHORT) == null &&
                    inputMap.get(DOMAIN+suffix) != null)  {
                commonInfo.recover();		
                helpUsage(1);
            }
            if( (inputMap.get(NSSPWD+suffix) != null ||
                    inputMap.get(JKSPWD+suffix) != null || 
                    inputMap.get(CAPWD+suffix) != null) &&
                    inputMap.get(DOMAIN+suffix) == null) {
                commonInfo.recover();		
                helpUsage(1);
            }
        }
       
        System.out.println(UpgradeConstants.CLI_USER_INSTRUCTIONS);
        if(inputMap.get(SOURCE) == null && inputMap.get(SOURCE_SHORT) == null) {
            System.out.print(
                    sm.getString("enterprise.tools.upgrade.cli.Source_input"));
            byte b[] = new byte[1024];
            try {
                int c = System.in.read(b);
                if (c == -1) { // input stream closed, maybe by ^C
                    System.exit(1);
                }
                String sourceDir = new String(b,0,c);
                String source = sourceDir.trim();
                
                //Check if input is a valid source directory input
                checkValidSourceDir(source);
                
                //Set the source input in source install dir
                source = normalizePath(source); // temp fix for 6567417
                commonInfo.setSourceInstallDir(source);
                
                //Create any backups needed and build the source domain mapping
                commonInfo.enlistDomainsFromSource();
                inputMap.put(SOURCE,source);
                interactiveInput = true;
            }catch(Exception e) {
                getLogger().log(Level.SEVERE, 
                        sm.getString("enterprise.tools.upgrade.cli.unexpectedException"), 
                        e);
            }
        }
        if(inputMap.get(TARGET) == null && inputMap.get(TARGET_SHORT) == null) {
            System.out.print(
                    sm.getString("enterprise.tools.upgrade.cli.Target_input"));
            byte b[] = new byte[1024];
            try {
                int c = System.in.read(b);
                if (c == -1) { // input stream closed, maybe by ^C
                    System.exit(1);
                }
                String targetDir = new String(b,0,c);
                String target = targetDir.trim();
                
                //Check if input is a valid target directory input
                if (!UpgradeUtils.getUpgradeUtils(commonInfo).
                        isValidTargetPath(target)) {
                    getLogger().severe(sm.getString("" +
                            "enterprise.tools.upgrade.cli.not_valid_target_install"));
                    commonInfo.recover();		    
                    helpUsage(1);
                }
                
                //Input can only be domains root. Set it in commonInfo
                target = normalizePath(target); // temp fix for 6567417
                commonInfo.setTargetDomainRoot(target);
                
                //Build domains mapping of source
                commonInfo.enlistDomainsFromSource();
                
                inputMap.put(TARGET,target);
                interactiveInput = true;
            }catch(Exception e) {
                getLogger().log(Level.SEVERE, 
                        sm.getString("enterprise.tools.upgrade.cli.unexpectedException"), 
                        e);
            }
        }
        if(inputMap.get(ADMINUSER) == null && inputMap.get(ADMINUSER_SHORT) == null) {
            System.out.print(
                    sm.getString("enterprise.tools.upgrade.cli.adminuser_input"));
            byte b[] = new byte[1024];
            try {
                int c = System.in.read(b);
                if (c == -1) { // input stream closed, maybe by ^C
                    System.exit(1);
                }
                String adminUser = new String(b,0,c);
                String admiuser = adminUser.trim();
                
                //Set admin user in commonInfo
                commonInfo.setAdminUserName(admiuser);
                
                inputMap.put(ADMINUSER,admiuser);
                interactiveInput = true;
            }catch(Exception e) {
                getLogger().log(Level.SEVERE, 
                        sm.getString("enterprise.tools.upgrade.cli.unexpectedException"), 
                        e);
            }
        }
	
        //Getting source edition since target domain's profile will be based on this.	
	String sourceEdition = commonInfo.getSourceEdition();
			
        if(inputMap.get(ADMINPASSWORD) == null && 
                inputMap.get(ADMINPASSWORD_SHORT) == null) {
            System.out.print(
                    sm.getString("enterprise.tools.upgrade.cli.adminpassword_input"));
            byte b[] = new byte[1024];
            try {
                String adminPassword =  new CliUtil().getPassword();
                
                //Set admin password in commonInfo
                commonInfo.setAdminPassword(adminPassword);
                
                inputMap.put(ADMINPASSWORD,adminPassword);
                interactiveInput = true;
                
                //Validate admin credentials if source is PE
                if(UpgradeConstants.EDITION_PE.equals(sourceEdition))
                    verifyUserAndPasswords(commonInfo.getAdminUserName(),
                            adminPassword, null);
            }catch(Exception e) {
                getLogger().log(Level.SEVERE, sm.getString(
                        "enterprise.tools.upgrade.cli.unexpectedException"), e);
            }
        }
        
        //Collect Master Password if source is not PE
        if(!(UpgradeConstants.EDITION_PE.equals(sourceEdition))){
            if(inputMap.get(MASTERPASSWORD) == null && 
                    inputMap.get(MASTERPASSWORD_SHORT) == null) {
                System.out.print(
                        sm.getString("enterprise.tools.upgrade.cli.MasterPW_input"));
                String password =  new CliUtil().getPassword();
                
                //Set master password in commonInfo
                commonInfo.setMasterPassword(password);
                
                inputMap.put(MASTERPASSWORD, password);
                
                //Validate admin credentials and master password
                verifyUserAndPasswords(commonInfo.getAdminUserName(),
                        commonInfo.getAdminPassword(),
                        password);
                interactiveInput = true;
            }
        } else {
			commonInfo.setMasterPassword(commonInfo.getDefaultMasterPassword());
		}
    
public CommonInfoModelgetCommonInfoModel(CommonInfoModel commonInfo)

        return commonInfo;
    
public java.util.logging.LoggergetLogger()

        return LogService.getLogger(LogService.UPGRADE_LOGGER);
    
public static voidmain(java.lang.String[] args)

        CLIParser parser = new CLIParser();
        parser.setCommonInfoModel(new CommonInfoModel());
        parser.parseComandLineArguments(args);
        parser.printInfo();
    
private java.lang.StringnormalizePath(java.lang.String path)

        try {
            return (new File(path)).getCanonicalPath();
        } catch (IOException e) {
            // can't happen if preconditions described above returned true...
            return path;
        }
    
public voidparseComandLineArguments(java.lang.String[] args)

        parser = new ArgsParser(args, commonInfo);
        interactiveInputMap = parser.parse(this);
        verifyCommonInfo(commonInfo);
        if(!commonInfo.isUpgradeSupported()) {
            helpUsage(sm.getString("enterprise.tools.upgrade.cli.upgrade_not_supported"));
            getLogger().info(sm.getString("enterprise.tools.upgrade.cli.upgrade_not_supported"));
            commonInfo.recover();	    
            System.exit(1);
        } else {
            String src = (String)interactiveInputMap.get(SOURCE);
	    String targ = (String)commonInfo.getTargetInstallDir();
	    if(commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_7X)) {
		if(targ != null && !targ.equals("") && src!= null && !src.equals("")) {
		    if(targ.equals(src)) {
		        helpUsage(sm.getString("enterprise.tools.upgrade.cli.upgrade_not_supported"));
                        commonInfo.recover();			
		        System.exit(1);
	            }
		}
	    }
	}
        commonInfo.enlistDomainsFromSource();
    
public voidprintInfo()

        commonInfo.printInfo();
    
public voidsetCommonInfoModel(CommonInfoModel commonInfo)

        this.commonInfo=commonInfo;
    
public voidverifyCommonInfo(CommonInfoModel commonInfo)
Method to verfiy if all inputs have been provided by the user.

        //Check not valid since source and target will be validated for
        //domain directory and domains root already and they will never be equal
        /*if(commonInfo.getSourceInstallDir().equals(
                commonInfo.getTargetInstallDir())) {
            String msg = sm.getString(
                    "enterprise.tools.upgrade.cli.Invalid_input_directory");
            helpUsage(msg);
            commonInfo.recover();	    
            System.exit(1);
        }*/
        if(!( (interactiveInputMap.containsKey(SOURCE) || interactiveInputMap.containsKey(SOURCE_SHORT)) &&
              (interactiveInputMap.containsKey(TARGET) || interactiveInputMap.containsKey(TARGET_SHORT)) &&
              (interactiveInputMap.containsKey(ADMINUSER) || interactiveInputMap.containsKey(ADMINUSER_SHORT)) &&
              (interactiveInputMap.containsKey(ADMINPASSWORD) || interactiveInputMap.containsKey(ADMINPASSWORD_SHORT))))
        {
            commonInfo.recover();
            helpUsage(1);
        }
    
private voidverifyUserAndPasswords(java.lang.String adminUser, java.lang.String adminPassword, java.lang.String masterPassword)

        
        if(!UpgradeUtils.getUpgradeUtils(commonInfo).
                validateUserDetails(adminUser,adminPassword,masterPassword)) {
            if(!UpgradeConstants.EDITION_PE.equals(commonInfo.getSourceEdition())){
                getLogger().severe(sm.getString(
                        "enterprise.tools.upgrade.cli.wrong_adminuser_or_adminpassword_or_masterpassword"));
            } else {
                getLogger().severe(sm.getString(
                        "enterprise.tools.upgrade.cli.wrong_adminuser_or_adminpassword"));
            }
            commonInfo.recover();	    
            System.exit(1);
        }