use of org.glassfish.api.admin.CommandValidationException in project Payara by payara.
the class CLICommand method inject.
/**
* Inject this instance with the final values of all the command
* parameters.
*
* @throws CommandException if execution of the command fails
* @throws CommandValidationException if there's something wrong
* with the options or arguments
*/
protected void inject() throws CommandException {
// injector expects operands to be in the ParameterMap with the key
// "DEFAULT"
options.set("DEFAULT", operands);
// if command has a "terse" or "extraterse" option, set it from ProgramOptions
if (commandModel.getModelFor("terse") != null) {
options.set("terse", Boolean.toString(programOpts.isTerse()));
}
if (commandModel.getModelFor("extraterse") != null) {
options.set("extraterse", Boolean.toString(programOpts.isExtraTerse()));
}
if (commandModel.getModelFor("autoname") != null) {
options.set("autoname", Boolean.toString(programOpts.isAutoName()));
}
// initialize the injector.
InjectionResolver<Param> injector = new MapInjectionResolver(commandModel, options);
// inject
try {
injectionMgr.inject(this, injector);
} catch (UnsatisfiedDependencyException e) {
throw new CommandValidationException(e.getMessage(), e);
}
}
use of org.glassfish.api.admin.CommandValidationException in project Payara by payara.
the class CLICommand method initializeCommandPassword.
/**
* Initialize all the passwords required by the command.
*
* @throws CommandException
*/
private void initializeCommandPassword() throws CommandException {
/*
* Go through all the valid options and check for required password
* options that weren't specified in the password file. If option
* is missing and we're interactive, prompt for it. Store the
* password as if it was a parameter.
*/
for (ParamModel opt : commandModel.getParameters()) {
if (!opt.getParam().password())
continue;
String pwdname = opt.getName();
char[] pwd = getPassword(opt, null, true);
if (pwd == null) {
if (opt.getParam().optional())
// not required, skip it
continue;
// if not terse, provide more advice about what to do
String msg;
if (programOpts.isTerse()) {
msg = strings.get("missingPassword", name, passwordName(opt));
} else {
msg = strings.get("missingPasswordAdvice", name, passwordName(opt));
}
throw new CommandValidationException(msg);
}
options.set(pwdname, new String(pwd));
}
}
use of org.glassfish.api.admin.CommandValidationException in project Payara by payara.
the class CLICommand method prevalidate.
/**
* The prevalidate method supplies missing options from
* the environment. It also supplies passwords from the password
* file or prompts for them if interactive.
*
* @throws CommandException if execution of the command fails
* @throws CommandValidationException if there's something wrong
* with the options or arguments
*/
protected void prevalidate() throws CommandException {
/*
* First, check that the command has the proper scope.
* (Could check this in getCommand(), but at that point we
* don't have the CommandModel yet.)
* Remote commands are checked on the server.
*/
if (!(this instanceof RemoteCommand) && !(this instanceof RemoteCLICommand)) {
Class<? extends Annotation> myScope = getScope(this.getClass());
if (myScope == null) {
throw new CommandException(strings.get("NoScope", name));
} else if (Singleton.class.equals(myScope)) {
// check that there are no parameters for this command
if (commandModel.getParameters().size() > 0) {
throw new CommandException(strings.get("HasParams", name));
}
}
}
/*
* Check for missing options and operands.
*/
int operandMin = 0;
int operandMax = 0;
ParamModel operandParam = getOperandModel();
if (operandParam != null) {
operandMin = operandParam.getParam().optional() ? 0 : 1;
operandMax = operandParam.getParam().multiple() ? Integer.MAX_VALUE : 1;
}
if (programOpts.isInteractive()) {
try {
buildTerminal();
buildLineReader();
boolean missingOption = false;
for (ParamModel opt : commandModel.getParameters()) {
if (opt.getParam().password()) {
// passwords are handled later
continue;
}
if (opt.getParam().obsolete() && getOption(opt.getName()) != null) {
logger.info(strings.get("ObsoleteOption", opt.getName()));
}
if (opt.getParam().optional()) {
continue;
}
if (opt.getParam().primary()) {
continue;
}
// if option isn't set, prompt for it (if interactive)
if (getOption(opt.getName()) == null && lineReader != null && !missingOption) {
String val = lineReader.readLine(strings.get("optionPrompt", lc(opt.getName())));
if (ok(val)) {
options.set(opt.getName(), val);
}
}
// if it's still not set, that's an error
if (getOption(opt.getName()) == null) {
missingOption = true;
logger.log(Level.INFO, strings.get("missingOption", "--" + opt.getName()));
}
if (opt.getParam().obsolete()) {
// a required obsolete option?
logger.log(Level.INFO, strings.get("ObsoleteOption", opt.getName()));
}
}
if (missingOption) {
throw new CommandValidationException(strings.get("missingOptions", name));
}
if (operands.size() < operandMin && lineReader != null) {
String val = null;
if (programOpts.isAutoName()) {
val = NameGenerator.generateName();
}
if (!ok(val)) {
val = lineReader.readLine(strings.get("operandPrompt", operandParam.getName()));
}
if (ok(val)) {
operands = new ArrayList<>();
operands.add(val);
}
}
} catch (UserInterruptException | EndOfFileException e) {
// Ignore
} finally {
closeTerminal();
}
} else {
// Check if we're missing an operand even if not interactive in case we want to generate it.
if (operands.size() < operandMin) {
String val = null;
if (programOpts.isAutoName()) {
val = NameGenerator.generateName();
}
if (ok(val)) {
operands = new ArrayList<>();
operands.add(val);
}
}
}
// Validate that we have the required operands
if (operands.size() < operandMin) {
throw new CommandValidationException(strings.get("notEnoughOperands", name, operandParam.getType()));
}
if (operands.size() > operandMax) {
switch(operandMax) {
case 0:
throw new CommandValidationException(strings.get("noOperandsAllowed", name));
case 1:
throw new CommandValidationException(strings.get("tooManyOperands1", name));
default:
throw new CommandValidationException(strings.get("tooManyOperands", name, operandMax));
}
}
initializeCommandPassword();
}
use of org.glassfish.api.admin.CommandValidationException in project Payara by payara.
the class MultimodeCommand method executeCommand.
@Override
protected int executeCommand() throws CommandException, CommandValidationException {
LineReader reader = null;
// restore echo flag, saved in validate
programOpts.setEcho(echo);
try {
if (file == null) {
System.out.println(strings.get("multimodeIntro"));
Completer completer = getAllCommandsCompleter();
Terminal asadminTerminal = TerminalBuilder.builder().name(ASADMIN).system(true).encoding(encoding != null ? Charset.forName(encoding) : Charset.defaultCharset()).build();
reader = newLineReaderBuilder().terminal(asadminTerminal).completer(completer).build();
reader.unsetOpt(LineReader.Option.INSERT_TAB);
} else {
printPrompt = false;
if (!file.canRead()) {
throw new CommandException("File: " + file + " can not be read");
}
OutputStream out = new OutputStream() {
@Override
public void write(int b) throws IOException {
return;
}
@Override
public void write(byte[] b) throws IOException {
return;
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
return;
}
};
Terminal asadminTerminal = new ExternalTerminal(ASADMIN, "", new FileInputStream(file), out, encoding != null ? Charset.forName(encoding) : Charset.defaultCharset());
reader = newLineReaderBuilder().terminal(asadminTerminal).build();
}
return executeCommands(reader);
} catch (IOException e) {
throw new CommandException(e);
} finally {
try {
if (file != null && reader != null && reader.getTerminal() != null) {
reader.getTerminal().close();
}
} catch (Exception e) {
// ignore it
}
}
}
use of org.glassfish.api.admin.CommandValidationException 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, hazelcastDasPort, hazelcastStartPort, hazelcastAutoIncrement, domainProperties);
domainConfig.put(K_VALIDATE_PORTS, checkPorts);
domainConfig.put(KEYTOOLOPTIONS, keytoolOptions);
domainConfig.put(K_TEMPLATE_NAME, template);
domainConfig.put(K_PORTBASE, portBase);
domainConfig.put(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(), e);
}
} else {
throw new DomainException(STRINGS.get("InvalidTemplateValue", template));
}
logger.info(STRINGS.get("DomainCreated", domainName));
Integer aPort = (Integer) domainConfig.get(K_ADMIN_PORT);
logger.info(STRINGS.get("DomainPort", domainName, Integer.toString(aPort)));
if (adminPassword != null && adminPassword.equals(DEFAULT_ADMIN_PASSWORD)) {
logger.info(STRINGS.get("DomainAllowsUnauth", domainName, adminUser));
} else {
logger.info(STRINGS.get("DomainAdminUser", domainName, adminUser));
}
if (saveLoginOpt) {
saveLogin(aPort, adminUser, adminPassword != null ? adminPassword.toCharArray() : null, domainName);
}
}
Aggregations