use of com.sun.enterprise.admin.cli.remote.RemoteCLICommand 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 com.sun.enterprise.admin.cli.remote.RemoteCLICommand in project Payara by payara.
the class CLIUtil method getRemoteCommands.
/**
* Get the list of commands from the remote server.
*
* @param container
* @param po Options to get the command i.e. admin port
* @param env
* @return the commands as a String array, sorted
* @throws CommandException
* @throws CommandValidationException
*/
public static String[] getRemoteCommands(CLIContainer container, ProgramOptions po, Environment env) throws CommandException, CommandValidationException {
/*
* In order to eliminate all local command names from the list
* of remote commands, we collect the local command names into
* a Set that we check later when collecting remote command
* names.
*/
Set<String> localnames = container.getLocalCommandsNames();
/*
* Now get the list of remote commands.
*/
po.removeDetach();
RemoteCLICommand cmd = new RemoteCLICommand("list-commands", po, env);
ActionReport report = cmd.executeAndReturnActionReport("list-commands");
List<MessagePart> children = report.getTopMessagePart().getChildren();
List<String> rcmds = new ArrayList<String>(children.size());
for (ActionReport.MessagePart msg : children) {
if (!localnames.contains(msg.getMessage())) {
rcmds.add(msg.getMessage());
}
}
Collections.sort(rcmds);
String[] remoteCommands = rcmds.toArray(new String[rcmds.size()]);
Arrays.sort(remoteCommands);
return remoteCommands;
}
use of com.sun.enterprise.admin.cli.remote.RemoteCLICommand in project Payara by payara.
the class ListDomainsCommand method getStatus.
private DomainInfo getStatus(String dn) throws IOException, CommandException {
setDomainName(dn);
initDomain();
DomainInfo di = new DomainInfo();
di.adminAddr = getAdminAddress();
programOpts.setHostAndPort(di.adminAddr);
di.status = isThisDAS(getDomainRootDir());
if (di.status) {
di.statusMsg = STRINGS.get("list.domains.StatusRunning", dn);
try {
RemoteCLICommand cmd = new RemoteCLICommand("_get-restart-required", programOpts, env);
String restartRequired = cmd.executeAndReturnOutput("_get-restart-required");
di.restartRequired = Boolean.parseBoolean(restartRequired.trim());
if (di.restartRequired) {
di.statusMsg = STRINGS.get("list.domains.StatusRestartRequired", dn);
}
} catch (Exception ex) {
}
} else {
di.statusMsg = STRINGS.get("list.domains.StatusNotRunning", dn);
}
return di;
}
use of com.sun.enterprise.admin.cli.remote.RemoteCLICommand in project Payara by payara.
the class LocalServerCommand method isThisServer.
/**
* See if the server is alive and is the one at the specified directory.
*
* @param ourDir the directory to check if the server is alive agains
* @param directoryKey the key for the directory
* @return true if it's the DAS at this domain directory
*/
protected final boolean isThisServer(File ourDir, String directoryKey) {
if (!ok(directoryKey))
throw new NullPointerException();
ourDir = getUniquePath(ourDir);
logger.log(Level.FINER, "Check if server is at location {0}", ourDir);
try {
programOpts.setHostAndPort(getAdminAddress());
RemoteCLICommand cmd = new RemoteCLICommand("__locations", programOpts, env);
ActionReport report = cmd.executeAndReturnActionReport("__locations");
String theirDirPath = report.findProperty(directoryKey);
logger.log(Level.FINER, "Remote server has root directory {0}", theirDirPath);
if (ok(theirDirPath)) {
File theirDir = getUniquePath(new File(theirDirPath));
return theirDir.equals(ourDir);
}
return false;
} catch (Exception ex) {
return false;
}
}
use of com.sun.enterprise.admin.cli.remote.RemoteCLICommand in project Payara by payara.
the class LocalServerCommand method getUptime.
/**
* Get uptime from the server.
*
* @return uptime in milliseconds
* @throws CommandException if the server is not running
*/
protected final long getUptime() throws CommandException {
RemoteCLICommand cmd = new RemoteCLICommand("uptime", programOpts, env);
String up = cmd.executeAndReturnOutput("uptime", "--milliseconds").trim();
long uptimeMillis = parseUptime(up);
if (uptimeMillis <= 0) {
throw new CommandException(STRINGS.get("restart.dasNotRunning"));
}
logger.log(Level.FINER, "server uptime: {0}", uptimeMillis);
return uptimeMillis;
}
Aggregations