use of com.sun.enterprise.admin.util.CommandModelData.ParamModelData in project Payara by payara.
the class ChangeNodeMasterPasswordCommand method executeCommand.
@Override
protected int executeCommand() throws CommandException {
try {
nodeDir = nodeDir0;
node = node0;
File serverDir = new File(nodeDir, node);
if (!serverDir.isDirectory()) {
throw new CommandException(strings.get("bad.node.dir", serverDir));
}
ArrayList<String> serverNames = getInstanceDirs(serverDir);
for (String serverName : serverNames) if (isRunning(serverDir, serverName))
throw new CommandException(strings.get("instance.is.running", serverName));
oldPassword = passwords.get("AS_ADMIN_MASTERPASSWORD");
if (oldPassword == null) {
char[] opArr = super.readPassword(strings.get("old.mp"));
oldPassword = opArr != null ? new String(opArr) : null;
}
if (oldPassword == null)
throw new CommandException(strings.get("no.console"));
// for each instance iterate through the instances first,
// read each keystore with the old password,
// only then should it save the new master password.
boolean valid = true;
for (String instanceDir0 : getInstanceDirs(nodeDirChild)) {
valid &= verifyInstancePassword(new File(nodeDirChild, instanceDir0));
}
if (!valid) {
throw new CommandException(strings.get("incorrect.old.mp"));
}
ParamModelData nmpo = new ParamModelData("AS_ADMIN_NEWMASTERPASSWORD", String.class, false, null);
nmpo.prompt = strings.get("new.mp");
nmpo.promptAgain = strings.get("new.mp.again");
nmpo.param._password = true;
char[] npArr = super.getPassword(nmpo, null, true);
newPassword = npArr != null ? new String(npArr) : null;
// for each instance encrypt the keystore
for (String instanceDir2 : getInstanceDirs(nodeDirChild)) {
encryptKeystore(instanceDir2);
}
if (savemp) {
createMasterPasswordFile();
}
return 0;
} catch (Exception e) {
throw new CommandException(e.getMessage(), e);
}
}
use of com.sun.enterprise.admin.util.CommandModelData.ParamModelData in project Payara by payara.
the class RemoteRestAdminCommand method parseMetadata.
/**
* Parse the JSon metadata for the command.
*
* @param str the string
* @return the etag to compare the command cache model
*/
private CachedCommandModel parseMetadata(String str, String etag) {
if (logger.isLoggable(Level.FINER)) {
// XXX - assume "debug" == "FINER"
logger.finer("------- RAW METADATA RESPONSE ---------");
logger.log(Level.FINER, "ETag: {0}", etag);
logger.finer(str);
logger.finer("------- RAW METADATA RESPONSE ---------");
}
if (str == null) {
return null;
}
try {
boolean sawFile = false;
JsonParser parser = Json.createParser(new StringReader(str));
parser.next();
JsonObject obj = parser.getObject();
obj = obj.getJsonObject("command");
CachedCommandModel cm = new CachedCommandModel(obj.getString("@name"), etag);
cm.dashOk = parseBoolean(obj, "@unknown-options-are-operands", false);
cm.managedJob = parseBoolean(obj, "@managed-job", false);
cm.setUsage(obj.getString("usage", null));
JsonValue optns = obj.get("option");
if (!JsonValue.NULL.equals(optns) && optns != null) {
JsonArray jsonOptions;
if (optns instanceof JsonArray) {
jsonOptions = (JsonArray) optns;
} else {
JsonArrayBuilder optBuilder = Json.createArrayBuilder();
optBuilder.add(optns);
jsonOptions = optBuilder.build();
}
for (int i = 0; i < jsonOptions.size(); i++) {
JsonObject jsOpt = jsonOptions.getJsonObject(i);
String type = jsOpt.getString("@type");
ParamModelData opt = new ParamModelData(jsOpt.getString("@name"), typeOf(type), parseBoolean(jsOpt, "@optional", false), jsOpt.getString("@default", null), jsOpt.getString("@short", null), parseBoolean(jsOpt, "@obsolete", false), jsOpt.getString("@alias", null));
opt.param._acceptableValues = jsOpt.getString("@acceptable-values", "");
if ("PASSWORD".equals(type)) {
opt.param._password = true;
opt.prompt = jsOpt.getString("@prompt", null);
opt.promptAgain = jsOpt.getString("@prompt-again", null);
} else if ("FILE".equals(type)) {
sawFile = true;
}
if (parseBoolean(jsOpt, "@primary", false)) {
opt.param._primary = true;
}
if (parseBoolean(jsOpt, "@multiple", false)) {
if (opt.type == File.class) {
opt.type = File[].class;
} else {
opt.type = List.class;
}
opt.param._multiple = true;
}
cm.add(opt);
}
}
if (sawFile) {
cm.add(new ParamModelData("upload", Boolean.class, true, null));
addedUploadOption = true;
cm.setAddedUploadOption(true);
}
if (notify) {
cm.add(new ParamModelData("notify", Boolean.class, false, "false"));
}
this.usage = cm.getUsage();
return cm;
} catch (Exception ex) {
logger.log(Level.SEVERE, "Can not parse command metadata", ex);
return null;
}
}
use of com.sun.enterprise.admin.util.CommandModelData.ParamModelData in project Payara by payara.
the class CommandModelDataProvider method writeToStreamSimpleFormat.
/**
* Super simple format possible because there can't be any problematic
* symbol like EOL in attributes.
*
* @throws IOException
*/
public void writeToStreamSimpleFormat(CommandModel cm, OutputStream stream) throws IOException {
if (cm == null) {
return;
}
// @todo Java SE 7: Managed source
BufferedWriter bw = null;
OutputStreamWriter writer = null;
try {
writer = new OutputStreamWriter(stream, charset);
bw = new BufferedWriter(writer);
// command name
String str = cm.getCommandName();
if (str != null && !str.isEmpty()) {
bw.write(ROOT_ELEMENT);
bw.write(": ");
bw.write(str);
bw.newLine();
}
// ETag
bw.write(ETAG_ELEMENT);
bw.write(": ");
bw.write(CachedCommandModel.computeETag(cm));
bw.newLine();
// unknown are operands
if (cm.unknownOptionsAreOperands()) {
bw.write(UNKNOWN_ARE_OPERANDS_ELEMENT);
bw.write(": true");
bw.newLine();
}
// CachedCommandModel specific staff
if (cm instanceof CachedCommandModel) {
CachedCommandModel ccm = (CachedCommandModel) cm;
// unknown are operands
if (ccm.isAddedUploadOption()) {
bw.write(ADDEDUPLOADOPTIONS_ELEMENT);
bw.write(": true");
bw.newLine();
}
// usage
str = ccm.getUsage();
if (str != null && !str.isEmpty()) {
bw.write(USAGE_ELEMENT);
bw.write(": ");
bw.write(escapeEndLines(str));
bw.newLine();
}
}
// Parameters
for (CommandModel.ParamModel paramModel : cm.getParameters()) {
bw.newLine();
// parameter / name
bw.write(NAME_ELEMENT);
bw.write(": ");
bw.write(paramModel.getName());
bw.newLine();
// parameter / class
if (paramModel.getType() != null) {
bw.write(CLASS_ELEMENT);
bw.write(": ");
bw.write(paramModel.getType().getName());
bw.newLine();
}
Param param = paramModel.getParam();
// parameter / shortName
str = param.shortName();
if (str != null && !str.isEmpty()) {
bw.write(SHORTNAME_ELEMENT);
bw.write(": ");
bw.write(str);
bw.newLine();
}
// parameter / alias
str = param.alias();
if (str != null && !str.isEmpty()) {
bw.write(ALIAS_ELEMENT);
bw.write(": ");
bw.write(str);
bw.newLine();
}
// parameter / optional
if (param.optional()) {
bw.write(OPTIONAL_ELEMENT);
bw.write(": true");
bw.newLine();
}
// parameter / obsolete
if (param.obsolete()) {
bw.write(OBSOLETE_ELEMENT);
bw.write(": true");
bw.newLine();
}
// parameter / defaultValue
str = param.defaultValue();
if (str != null && !str.isEmpty()) {
bw.write(DEFAULT_VALUE_ELEMENT);
bw.write(": ");
bw.write(str);
bw.newLine();
}
// parameter / primary
if (param.primary()) {
bw.write(PRIMARY_ELEMENT);
bw.write(": true");
bw.newLine();
}
// parameter / multiple
if (param.multiple()) {
bw.write(MULTIPLE_ELEMENT);
bw.write(": true");
bw.newLine();
}
// parameter / password
if (param.password()) {
bw.write(PASSWORD_ELEMENT);
bw.write(": true");
bw.newLine();
}
// parameter / prompt
if (paramModel instanceof ParamModelData) {
str = ((ParamModelData) paramModel).getPrompt();
if (str != null && !str.isEmpty()) {
bw.write(PROMPT_ELEMENT);
bw.write(": ");
bw.write(escapeEndLines(str));
bw.newLine();
}
str = ((ParamModelData) paramModel).getPromptAgain();
if (str != null && !str.isEmpty()) {
bw.write(PROMPT_AGAIN_ELEMENT);
bw.write(": ");
bw.write(escapeEndLines(str));
bw.newLine();
}
}
}
} finally {
try {
bw.close();
} catch (Exception ex) {
}
try {
writer.close();
} catch (Exception ex) {
}
}
}
use of com.sun.enterprise.admin.util.CommandModelData.ParamModelData in project Payara by payara.
the class CLICommand method getPassword.
protected char[] getPassword(String paramname, String localizedPrompt, String localizedPromptConfirm, boolean create) throws CommandValidationException {
ParamModelData po = new ParamModelData(paramname, String.class, false, null);
po.prompt = localizedPrompt;
po.promptAgain = localizedPromptConfirm;
po.param._password = true;
return getPassword(po, null, create);
}
use of com.sun.enterprise.admin.util.CommandModelData.ParamModelData in project Payara by payara.
the class Parser method parseCommandLine.
/**
* Parse the command line arguments according to CLIP.
*
* @param argv command line arguments
* @throws CommandValidationException if command line is invalid
*/
private void parseCommandLine(final String[] argv, final int start) throws CommandValidationException {
for (int si = start; si < argv.length; si++) {
String arg = argv[si];
if (arg.equals("--")) {
// when we process all remaining options
if (!ignoreUnknown)
si++;
while (si < argv.length) operands.add(argv[si++]);
break;
}
// is it an operand or option value?
if (!arg.startsWith("-") || arg.length() <= 1) {
operands.add(arg);
if (ignoreUnknown)
continue;
si++;
while (si < argv.length) operands.add(argv[si++]);
break;
}
// at this point it's got to be an option of some sort
ParamModel opt = null;
String name = null;
String value = null;
if (arg.charAt(1) == '-') {
// long option
int ns = 2;
boolean sawno = false;
if (arg.startsWith("--no-")) {
sawno = true;
value = "false";
// skip prefix
ns = 5;
}
// if of the form "--option=value", extract value
int ne = arg.indexOf('=');
if (ne < 0)
name = arg.substring(ns);
else {
if (value != null)
throw new CommandValidationException(strings.get("parser.noValueAllowed", arg));
name = arg.substring(ns, ne);
value = arg.substring(ne + 1);
}
opt = lookupLongOption(name);
if (sawno && optionRequiresOperand(opt))
throw new CommandValidationException(strings.get("parser.illegalNo", opt.getName()));
} else {
/*
* possibilities are:
* -f
* -f value
* -f=value
* -fxyz (multiple single letter boolean options
* with no arguments)
*/
if (arg.length() <= 2) {
// one of the first two cases
opt = lookupShortOption(arg.charAt(1));
name = arg.substring(1);
} else {
// one of the last two cases
if (arg.charAt(2) == '=') {
// -f=value case
opt = lookupShortOption(arg.charAt(1));
value = arg.substring(3);
} else {
// -fxyz case
for (int i = 1; i < arg.length(); i++) {
opt = lookupShortOption(arg.charAt(i));
if (opt == null) {
if (!ignoreUnknown)
throw new CommandValidationException(strings.get("parser.invalidOption", Character.toString(arg.charAt(i))));
// unknown option, skip all the rest
operands.add(arg);
break;
}
if (opt.getType() == Boolean.class || opt.getType() == boolean.class)
setOption(opt, "true");
else {
if (!ignoreUnknown)
throw new CommandValidationException(strings.get("parser.nonbooleanNotAllowed", Character.toString(arg.charAt(i)), arg));
// unknown option, skip all the rest
operands.add(arg);
break;
}
}
continue;
}
}
}
// is it a known option?
if (opt == null) {
if (!ignoreUnknown)
throw new CommandValidationException(strings.get("parser.invalidOption", arg));
// unknown option, skip it
operands.add(arg);
continue;
}
// find option value, if needed
if (value == null) {
// as an option as long as it doesn't look like an option
if (options == null) {
if (si + 1 < argv.length && !argv[si + 1].startsWith("-"))
value = argv[++si];
else
// fake it
((ParamModelData) opt).type = Boolean.class;
} else if (optionRequiresOperand(opt)) {
if (++si >= argv.length)
throw new CommandValidationException(strings.get("parser.missingValue", name));
value = argv[si];
} else if (opt.getType() == Boolean.class || opt.getType() == boolean.class) {
/*
* If it's a boolean option, the following parameter
* might be the value for the option; peek ahead to
* see if it looks like a boolean value.
*/
if (si + 1 < argv.length) {
String val = argv[si + 1];
if (val.equalsIgnoreCase("true") || val.equalsIgnoreCase("false")) {
// yup, it's a boolean value, consume it
si++;
value = val;
}
}
}
}
setOption(opt, value);
}
}
Aggregations