use of org.glassfish.api.admin.CommandException in project Payara by payara.
the class InstanceInfo method pingInstance.
// TODO what about security????
private Future<InstanceCommandResult> pingInstance() {
try {
ActionReport aReport = report.addSubActionsReport();
InstanceCommandResult aResult = new InstanceCommandResult();
ParameterMap map = new ParameterMap();
map.set("type", "terse");
InstanceCommandExecutor ice = new InstanceCommandExecutor(habitat, "__locations", FailurePolicy.Error, FailurePolicy.Error, svr, host, port, logger, map, aReport, aResult);
return stateService.submitJob(svr, ice, aResult);
/*
String ret = rac.executeCommand(map).trim();
if (ret == null || (!ret.endsWith("/" + name)))
return -1;
running = true;
String uptimeStr = rac.getAttributes().get("Uptime_value");
return Long.parseLong(uptimeStr);
*/
} catch (CommandException ex) {
running = false;
return null;
}
}
use of org.glassfish.api.admin.CommandException in project Payara by payara.
the class RemoteAdminCommand method executeCommand.
/**
* Run the command using the specified arguments.
* Return the output of the command.
* @param opts
* @return
* @throws org.glassfish.api.admin.CommandException
*/
public String executeCommand(ParameterMap opts) throws CommandException {
// first, make sure we have the command model
getCommandModel();
// XXX : This is to take care of camel case from ReST calls that
// do not go through usual CLI path
// XXX : This is not clean; this should be handled the same way
// it is handled for incoming CLI commands
options = new ParameterMap();
for (Map.Entry<String, List<String>> o : opts.entrySet()) {
String key = o.getKey();
List<String> value = o.getValue();
options.set(key.toLowerCase(Locale.ENGLISH), value);
}
// "DEFAULT".toLowerCase()
operands = options.get("default");
try {
initializeDoUpload();
// if uploading, we need a payload
if (doUpload) {
outboundPayload = PayloadImpl.Outbound.newInstance();
}
StringBuilder uriString = getCommandURI();
ParamModel operandParam = null;
for (ParamModel opt : commandModel.getParameters()) {
if (opt.getParam().primary()) {
operandParam = opt;
continue;
}
String paramName = opt.getName();
List<String> paramValues = new ArrayList<String>(options.get(paramName.toLowerCase(Locale.ENGLISH)));
if (!opt.getParam().alias().isEmpty() && !paramName.equalsIgnoreCase(opt.getParam().alias())) {
paramValues.addAll(options.get(opt.getParam().alias().toLowerCase(Locale.ENGLISH)));
}
if (!opt.getParam().multiple() && paramValues.size() > 1) {
throw new CommandException(strings.get("tooManyOptions", paramName));
}
if (paramValues.isEmpty()) {
// perhaps it's set in the environment?
String envValue = getFromEnvironment(paramName);
if (envValue != null) {
paramValues.add(envValue);
}
}
if (paramValues.isEmpty()) {
/*
* Option still not set. Note that we ignore the default
* value and don't send it explicitly on the assumption
* that the server will supply the default value itself.
*
* If the missing option is required, that's an error,
* which should never happen here because validate()
* should check it first.
*/
if (!opt.getParam().optional()) {
throw new CommandException(strings.get("missingOption", paramName));
}
// optional param not set, skip it
continue;
}
for (String paramValue : paramValues) {
if (opt.getType() == File.class || opt.getType() == File[].class) {
addFileOption(uriString, paramName, paramValue);
} else if (opt.getParam().password()) {
addPasswordOption(uriString, paramName, paramValue);
} else {
addStringOption(uriString, paramName, paramValue);
}
}
}
// add operands
for (String operand : operands) {
if (operandParam.getType() == File.class || operandParam.getType() == File[].class) {
addFileOption(uriString, "DEFAULT", operand);
} else {
addStringOption(uriString, "DEFAULT", operand);
}
}
// remove the last character, whether it was "?" or "&"
uriString.setLength(uriString.length() - 1);
executeRemoteCommand(uriString.toString());
} catch (IOException ioex) {
// possibly an error caused while reading or writing a file?
throw new CommandException("I/O Error", ioex);
}
return output;
}
use of org.glassfish.api.admin.CommandException in project Payara by payara.
the class RemoteAdminCommand method getCommandModel.
/**
* Get the CommandModel for the command from the server.
* If the CommandModel hasn't been set, it's fetched from
* the server.
*
* @return the model for the command
* @throws CommandException if the server can't be contacted
*/
public CommandModel getCommandModel() throws CommandException {
if (commandModel == null && !omitCache) {
long startNanos = System.nanoTime();
try {
commandModel = AdminCacheUtils.getCache().get(createCommandCacheKey(), CommandModel.class);
if (commandModel != null) {
this.commandModelFromCache = true;
if (commandModel instanceof CachedCommandModel) {
CachedCommandModel ccm = (CachedCommandModel) commandModel;
this.usage = ccm.getUsage();
addedUploadOption = ccm.isAddedUploadOption();
}
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Command model for command {0} was successfully loaded from the cache. [Duration: {1} nanos]", new Object[] { name, System.nanoTime() - startNanos });
}
} else {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Command model for command {0} is not in cache. It must be fatched from server.", name);
}
}
} catch (Exception ex) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Can not get data from cache under key " + createCommandCacheKey(), ex);
}
}
}
if (commandModel == null) {
fetchCommandModel();
}
return commandModel;
}
use of org.glassfish.api.admin.CommandException in project Payara by payara.
the class CommandExecutorImpl method getParameters.
ParameterMap getParameters(String command, String[] args) throws CommandException {
CommandModel commandModel = commandRunner.getModel(command, logger);
if (commandModel == null) {
throw new CommandException("Command lookup failed for command " + command);
}
// Filter out the global options.
// We are interested only in --passwordfile option. No other options are relevant when GlassFish is running in embedded mode.
Parser parser = new Parser(args, 0, ProgramOptions.getValidOptions(), true);
ParameterMap globalOptions = parser.getOptions();
List<String> operands = parser.getOperands();
String[] argv = operands.toArray(new String[operands.size()]);
parser = new Parser(argv, 0, commandModel.getParameters(), false);
ParameterMap options = parser.getOptions();
operands = parser.getOperands();
options.set("DEFAULT", operands);
// if command has a "terse" option, set it in options
if (commandModel.getModelFor("terse") != null)
options.set("terse", Boolean.toString(terse));
// Read the passwords from the password file and set it in command options.
if (globalOptions.size() > 0) {
String pwfile = globalOptions.getOne(ProgramOptions.PASSWORDFILE);
if (pwfile != null && pwfile.length() > 0) {
Map<String, String> passwords = CLIUtil.readPasswordFileOptions(pwfile, false);
for (CommandModel.ParamModel opt : commandModel.getParameters()) {
if (opt.getParam().password()) {
String pwdname = opt.getName();
String pwd = passwords.get(pwdname);
if (pwd != null) {
options.set(pwdname, pwd);
}
}
}
}
}
return options;
}
use of org.glassfish.api.admin.CommandException in project Payara by payara.
the class DeployerImpl method undeploy.
@Override
public void undeploy(String appName, String... params) throws GlassFishException {
String[] newParams = new String[params.length + 1];
System.arraycopy(params, 0, newParams, 0, params.length);
newParams[params.length] = appName;
CommandExecutorImpl executer = habitat.getService(CommandExecutorImpl.class);
try {
ActionReport actionReport = executer.executeCommand("undeploy", newParams);
if (actionReport.hasSuccesses()) {
logger.log(Level.INFO, "{0} was successfully undeployed", appName);
}
} catch (CommandException e) {
throw new GlassFishException(e);
}
}
Aggregations