Search in sources :

Example 21 with CommandException

use of org.glassfish.api.admin.CommandException in project Payara by payara.

the class RemoteCLICommand method prepare.

@Override
protected void prepare() throws CommandException, CommandValidationException {
    try {
        processProgramOptions();
        initializeAuth();
        /*
             * Now we have all the information we need to create
             * the remote admin command object.
             */
        initializeRemoteAdminCommand();
        if (responseFormatType != null) {
            rac.setResponseFormatType(responseFormatType);
        }
        if (userOut != null) {
            rac.setUserOut(userOut);
        }
        /*
             * Initialize a CookieManager so that we can retreive
             * any cookies included in the reply.   These cookies
             * (e.g. JSESSIONID, JROUTE) are used for CLI session
             * based routing.
             */
        initializeCookieManager();
        /*
             * If this is a help request, we don't need the command
             * metadata and we throw away all the other options and
             * fake everything else.
             */
        if (programOpts.isHelp()) {
            CommandModelData cm = new CommandModelData(name);
            cm.add(new ParamModelData("help", boolean.class, true, "false", "?"));
            this.commandModel = cm;
            rac.setCommandModel(cm);
            return;
        }
        /*
             * Find the metadata for the command.
             */
        commandModel = rac.getCommandModel();
    } catch (CommandException cex) {
        if (logger.isLoggable(Level.FINER))
            logger.finer("RemoteCommand.prepare throws " + cex);
        throw cex;
    } catch (Exception e) {
        if (logger.isLoggable(Level.FINER))
            logger.finer("RemoteCommand.prepare throws " + e);
        throw new CommandException(e.getMessage());
    }
}
Also used : CommandModelData(com.sun.enterprise.admin.util.CommandModelData) ParamModelData(com.sun.enterprise.admin.util.CommandModelData.ParamModelData) CommandException(org.glassfish.api.admin.CommandException) StoreException(com.sun.appserv.management.client.prefs.StoreException) CommandException(org.glassfish.api.admin.CommandException) CommandValidationException(org.glassfish.api.admin.CommandValidationException)

Example 22 with CommandException

use of org.glassfish.api.admin.CommandException in project Payara by payara.

the class RemoteCLICommand method executeCommand.

// /**
// * We do all our help processing in executeCommand.
// */
// @Override
// protected boolean checkHelp()
// throws CommandException, CommandValidationException {
// return false;
// }
/**
 * Runs the command using the specified arguments.
 */
@Override
protected int executeCommand() throws CommandException, CommandValidationException {
    try {
        if (logger.isLoggable(Level.FINER)) {
            logger.finer("RemoteCLICommand.executeCommand()");
        }
        rac.statusPrinter.reset();
        options.set("DEFAULT", operands);
        if (programOpts.isDetachedCommand()) {
            rac.registerListener(AdminCommandState.EVENT_STATE_CHANGED, new DetachListener(logger, rac, programOpts.isTerse()));
        }
        /*if (programOpts.isNotifyCommand()) {
                rac.registerListener(AdminCommandState.EVENT_STATE_CHANGED,
                        new NotifyListener(logger, rac, programOpts.isTerse()));
            }*/
        try {
            output = rac.executeCommand(options);
        } finally {
            rac.statusPrinter.deleteLastMessage();
        }
        ar = rac.getActionReport();
        if (!returnActionReport && !returnOutput) {
            if (output.length() > 0) {
                logger.info(output);
            }
        }
    } catch (CommandValidationException cve) {
        reExecuteAfterMetadataUpdate();
        throw cve;
    } catch (CommandException ex) {
        // if a --help request failed, try to emulate it locally
        if (programOpts.isHelp()) {
            Reader r = getLocalManPage();
            BufferedReader br = null;
            try {
                if (r != null) {
                    br = new BufferedReader(r);
                    PrintWriter pw = new PrintWriter(System.out);
                    char[] buf = new char[8192];
                    int cnt;
                    while ((cnt = br.read(buf)) > 0) pw.write(buf, 0, cnt);
                    pw.flush();
                    return SUCCESS;
                }
            } catch (IOException ioex2) {
            // ignore it and throw original exception
            } finally {
                try {
                    if (br != null)
                        br.close();
                } catch (IOException ioex3) {
                // ignore it
                }
            }
        }
        throw ex;
    }
    ActionReport ar = rac.getActionReport();
    if (ar != null && ExitCode.WARNING == ar.getActionExitCode()) {
        return WARNING;
    }
    return SUCCESS;
}
Also used : CommandValidationException(org.glassfish.api.admin.CommandValidationException) CommandException(org.glassfish.api.admin.CommandException) ActionReport(org.glassfish.api.ActionReport)

Example 23 with CommandException

use of org.glassfish.api.admin.CommandException in project Payara by payara.

the class ChangeMasterPasswordCommand method executeCommand.

@Override
protected int executeCommand() throws CommandException {
    CLICommand command = null;
    if (domainDirParam != null && nodeDir != null) {
        throw new CommandException(strings.get("both.domaindir.nodedir.not.allowed"));
    }
    try {
        if (isDomain()) {
            // is it domain
            command = CLICommand.getCommand(habitat, CHANGE_MASTER_PASSWORD_DAS);
            return command.execute(argv);
        }
        if (nodeDir != null) {
            command = CLICommand.getCommand(habitat, CHANGE_MASTER_PASSWORD_NODE);
            return command.execute(argv);
        } else {
            // nodeDir is not specified and domainNameOrNodeName is not a domain.
            // It could be a node
            // We add defaultNodeDir parameter to args
            ArrayList arguments = new ArrayList<String>(Arrays.asList(argv));
            arguments.remove(argv.length - 1);
            arguments.add("--nodedir");
            arguments.add(getDefaultNodesDirs().getAbsolutePath());
            arguments.add(domainNameOrNodeName);
            String[] newargs = (String[]) arguments.toArray(new String[arguments.size()]);
            command = CLICommand.getCommand(habitat, CHANGE_MASTER_PASSWORD_NODE);
            return command.execute(newargs);
        }
    } catch (IOException e) {
        throw new CommandException(e.getMessage(), e);
    }
}
Also used : CLICommand(com.sun.enterprise.admin.cli.CLICommand) ArrayList(java.util.ArrayList) CommandException(org.glassfish.api.admin.CommandException) IOException(java.io.IOException)

Example 24 with CommandException

use of org.glassfish.api.admin.CommandException in project Payara by payara.

the class CreateDomainCommand method createTheDomain.

/**
 * Create the domain.
 *
 * @param domainPath domain path to insert in domainConfig
 * @param domainProperties properties to insert in domainConfig
 * @throws CommandException if domain cannot be created
 */
private void createTheDomain(final String domainPath, Properties domainProperties) throws DomainException, CommandValidationException {
    // 
    // fix for bug# 4930684
    // domain name is validated before the ports
    // 
    String domainFilePath = (domainPath + File.separator + domainName);
    if (FileUtils.safeGetCanonicalFile(new File(domainFilePath)).exists()) {
        throw new CommandValidationException(strings.get("DomainExists", domainName));
    }
    DomainConfig domainConfig = null;
    if (template == null || template.endsWith(".jar")) {
        domainConfig = new DomainConfig(domainName, domainPath, adminUser, adminPassword, masterPassword, saveMasterPassword, adminPort, instancePort, domainProperties);
        domainConfig.put(DomainConfig.K_VALIDATE_PORTS, Boolean.valueOf(checkPorts));
        domainConfig.put(DomainConfig.KEYTOOLOPTIONS, keytoolOptions);
        domainConfig.put(DomainConfig.K_TEMPLATE_NAME, template);
        domainConfig.put(DomainConfig.K_PORTBASE, portBase);
        domainConfig.put(DomainConfig.K_INITIAL_ADMIN_USER_GROUPS, Version.getInitialAdminGroups());
        initSecureAdminSettings(domainConfig);
        try {
            DomainBuilder domainBuilder = new DomainBuilder(domainConfig);
            domainBuilder.validateTemplate();
            domainBuilder.run();
        } catch (Exception e) {
            throw new DomainException(e.getMessage());
        }
    } else {
        throw new DomainException(strings.get("InvalidTemplateValue", template));
    }
    logger.info(strings.get("DomainCreated", domainName));
    Integer aPort = (Integer) domainConfig.get(DomainConfig.K_ADMIN_PORT);
    logger.info(strings.get("DomainPort", domainName, Integer.toString(aPort)));
    if (adminPassword != null && adminPassword.equals(SystemPropertyConstants.DEFAULT_ADMIN_PASSWORD)) {
        logger.info(strings.get("DomainAllowsUnauth", domainName, adminUser));
    } else {
        logger.info(strings.get("DomainAdminUser", domainName, adminUser));
    }
    // checkAsadminPrefsFile();
    if (saveLoginOpt) {
        saveLogin(aPort, adminUser, adminPassword != null ? adminPassword.toCharArray() : null, domainName);
    }
}
Also used : DomainBuilder(com.sun.enterprise.admin.servermgmt.domain.DomainBuilder) CommandValidationException(org.glassfish.api.admin.CommandValidationException) File(java.io.File) CommandException(org.glassfish.api.admin.CommandException) CommandValidationException(org.glassfish.api.admin.CommandValidationException)

Example 25 with CommandException

use of org.glassfish.api.admin.CommandException in project Payara by payara.

the class CreateDomainCommand method executeCommand.

/**
 */
@Override
protected int executeCommand() throws CommandException, CommandValidationException {
    // domain validation upfront (i.e. before we prompt)
    try {
        DomainsManager manager = new PEDomainsManager();
        DomainConfig config = new DomainConfig(domainName, domainDir);
        manager.validateDomain(config, false);
        verifyPortBase();
    } catch (DomainException e) {
        logger.fine(e.getLocalizedMessage());
        throw new CommandException(strings.get("CouldNotCreateDomain", domainName), e);
    }
    /*
         * The admin user is specified with the --user program option. If not
         * specified (because the user hit Enter at the prompt), we use the
         * default, which allows unauthenticated login.
         */
    adminUser = programOpts.getUser();
    if (!ok(adminUser)) {
        adminUser = SystemPropertyConstants.DEFAULT_ADMIN_USER;
        adminPassword = SystemPropertyConstants.DEFAULT_ADMIN_PASSWORD;
    } else if (noPassword) {
        adminPassword = SystemPropertyConstants.DEFAULT_ADMIN_PASSWORD;
    } else {
        char[] pwdArr = getAdminPassword();
        adminPassword = pwdArr != null ? new String(pwdArr) : null;
        boolean haveAdminPwd = true;
    }
    if (saveMasterPassword) {
        useMasterPassword = true;
    }
    if (masterPassword == null) {
        if (useMasterPassword) {
            char[] mpArr = getMasterPassword();
            masterPassword = mpArr != null ? new String(mpArr) : null;
        } else {
            masterPassword = DEFAULT_MASTER_PASSWORD;
        }
    }
    try {
        // verify admin port is valid if specified on command line
        if (adminPort != null) {
            verifyPortIsValid(adminPort);
        }
        // instance option is entered then verify instance port is valid
        if (instancePort != null) {
            verifyPortIsValid(instancePort);
        }
        // saving the login information happens inside this method
        createTheDomain(domainDir, domainProperties);
    } catch (CommandException ce) {
        logger.info(ce.getLocalizedMessage());
        throw new CommandException(strings.get("CouldNotCreateDomain", domainName), ce);
    } catch (Exception e) {
        logger.fine(e.getLocalizedMessage());
        throw new CommandException(strings.get("CouldNotCreateDomain", domainName), e);
    }
    return 0;
}
Also used : PEDomainsManager(com.sun.enterprise.admin.servermgmt.pe.PEDomainsManager) CommandException(org.glassfish.api.admin.CommandException) PEDomainsManager(com.sun.enterprise.admin.servermgmt.pe.PEDomainsManager) CommandException(org.glassfish.api.admin.CommandException) CommandValidationException(org.glassfish.api.admin.CommandValidationException)

Aggregations

CommandException (org.glassfish.api.admin.CommandException)61 File (java.io.File)20 CommandValidationException (org.glassfish.api.admin.CommandValidationException)16 IOException (java.io.IOException)13 ParameterMap (org.glassfish.api.admin.ParameterMap)11 InvalidCommandException (org.glassfish.api.admin.InvalidCommandException)10 ActionReport (org.glassfish.api.ActionReport)9 ArrayList (java.util.ArrayList)6 RemoteCLICommand (com.sun.enterprise.admin.cli.remote.RemoteCLICommand)5 MiniXmlParserException (com.sun.enterprise.universal.xml.MiniXmlParserException)5 HostAndPort (com.sun.enterprise.util.HostAndPort)4 BackupException (com.sun.enterprise.backup.BackupException)3 BackupWarningException (com.sun.enterprise.backup.BackupWarningException)3 SmartFile (com.sun.enterprise.universal.io.SmartFile)3 FileNotFoundException (java.io.FileNotFoundException)3 UnknownHostException (java.net.UnknownHostException)3 Logger (java.util.logging.Logger)3 SFTPClient (org.glassfish.cluster.ssh.sftp.SFTPClient)3 CLIProcessExecutor (com.sun.enterprise.admin.cli.CLIProcessExecutor)2 PEDomainsManager (com.sun.enterprise.admin.servermgmt.pe.PEDomainsManager)2