use of org.apache.commons.cli.Options in project zm-mailbox by Zimbra.
the class WaitSetValidator method main.
/**
* @param args
*/
public static void main(String[] args) {
CliUtil.toolSetup();
WaitSetValidator t = new WaitSetValidator();
CommandLineParser parser = new PosixParser();
Options options = new Options();
options.addOption("i", "id", true, "Wait Set ID");
options.addOption("h", "host", true, "Hostname");
options.addOption("u", "user", true, "Username");
options.addOption("p", "pw", true, "Password");
options.addOption("?", "help", false, "Help");
options.addOption("v", "verbose", false, "Verbose");
CommandLine cl = null;
boolean err = false;
String[] hosts;
String[] ids;
try {
cl = parser.parse(options, args, true);
} catch (ParseException pe) {
printError("error: " + pe.getMessage());
err = true;
}
if (err || cl.hasOption('?')) {
t.usage();
}
String id = null, host = null, user = null, pw = null;
if (!cl.hasOption('i'))
t.usage();
id = cl.getOptionValue('i');
if (cl.hasOption('h'))
host = cl.getOptionValue('h');
else
host = "http://localhost:7071/service/admin";
if (cl.hasOption('u'))
user = cl.getOptionValue('u');
else
user = "admin";
if (cl.hasOption('p'))
pw = cl.getOptionValue('p');
else
pw = "test123";
if (cl.hasOption('v'))
t.setVerbose(true);
hosts = host.split(",");
ids = id.split(",");
if (hosts.length != ids.length) {
System.err.println("If multiple hosts or ids are specified, the same number is required of each");
System.exit(3);
}
for (int i = 0; i < hosts.length; i++) {
if (i > 0)
System.out.println("\n\n");
System.out.println("Checking server " + hosts[i] + " waitsetId=" + ids[i]);
t.run(ids[i], hosts[i], user, pw);
}
}
use of org.apache.commons.cli.Options in project zm-mailbox by Zimbra.
the class ZMailboxUtil method main.
public static void main(String[] args) throws IOException, ServiceException {
CliUtil.toolSetup();
SoapTransport.setDefaultUserAgent("zmmailbox", BuildInfo.VERSION);
ZMailboxUtil pu = new ZMailboxUtil();
CommandLineParser parser = new GnuParser();
Options options = new Options();
options.addOption("a", "admin", true, "admin account name to auth as");
options.addOption("z", "zadmin", false, "use zimbra admin name/password from localconfig for admin/password");
options.addOption("h", "help", false, "display usage");
options.addOption("f", "file", true, "use file as input stream");
options.addOption("u", "url", true, "http[s]://host[:port] of server to connect to");
options.addOption("r", "protocol", true, "protocol to use for request/response [soap11, soap12, json]");
options.addOption("m", "mailbox", true, "mailbox to open");
options.addOption(null, "auth", true, "account name to auth as; defaults to -m unless -A is used");
options.addOption("A", "admin-priv", false, "execute requests with admin privileges");
options.addOption("p", "password", true, "auth password");
options.addOption("P", "passfile", true, "filename with password in it");
options.addOption("t", "timeout", true, "timeout (in seconds)");
options.addOption("v", "verbose", false, "verbose mode");
options.addOption("d", "debug", false, "debug mode");
options.addOption(SoapCLI.OPT_AUTHTOKEN);
options.addOption(SoapCLI.OPT_AUTHTOKENFILE);
CommandLine cl = null;
boolean err = false;
try {
cl = parser.parse(options, args, true);
} catch (ParseException pe) {
stderr.println("error: " + pe.getMessage());
err = true;
}
if (err || cl.hasOption('h')) {
pu.usage();
}
try {
boolean isAdmin = false;
pu.setVerbose(cl.hasOption('v'));
if (cl.hasOption('a')) {
pu.setAdminAccountName(cl.getOptionValue('a'));
pu.setUrl(DEFAULT_ADMIN_URL, true);
isAdmin = true;
}
if (cl.hasOption('z')) {
pu.setAdminAccountName(LC.zimbra_ldap_user.value());
pu.setPassword(LC.zimbra_ldap_password.value());
pu.setUrl(DEFAULT_ADMIN_URL, true);
isAdmin = true;
}
if (cl.hasOption(SoapCLI.O_AUTHTOKEN) && cl.hasOption(SoapCLI.O_AUTHTOKENFILE))
pu.usage();
if (cl.hasOption(SoapCLI.O_AUTHTOKEN)) {
pu.setAdminAuthToken(ZAuthToken.fromJSONString(cl.getOptionValue(SoapCLI.O_AUTHTOKEN)));
pu.setUrl(DEFAULT_ADMIN_URL, true);
isAdmin = true;
}
if (cl.hasOption(SoapCLI.O_AUTHTOKENFILE)) {
String authToken = StringUtil.readSingleLineFromFile(cl.getOptionValue(SoapCLI.O_AUTHTOKENFILE));
pu.setAdminAuthToken(ZAuthToken.fromJSONString(authToken));
pu.setUrl(DEFAULT_ADMIN_URL, true);
isAdmin = true;
}
String authAccount, targetAccount;
if (cl.hasOption('m')) {
if (!cl.hasOption('p') && !cl.hasOption('P') && !cl.hasOption('y') && !cl.hasOption('Y') && !cl.hasOption('z')) {
throw ZClientException.CLIENT_ERROR("-m requires one of the -p/-P/-y/-Y/-z options", null);
}
targetAccount = cl.getOptionValue('m');
} else {
targetAccount = null;
}
if ((cl.hasOption("A") || cl.hasOption("auth")) && !cl.hasOption('m')) {
throw ZClientException.CLIENT_ERROR("-A/--auth requires -m", null);
}
if (cl.hasOption("A")) {
if (!isAdmin) {
throw ZClientException.CLIENT_ERROR("-A requires admin auth", null);
}
if (cl.hasOption("auth")) {
throw ZClientException.CLIENT_ERROR("-A cannot be combined with --auth", null);
}
authAccount = null;
} else if (cl.hasOption("auth")) {
authAccount = cl.getOptionValue("auth");
} else {
// default case
authAccount = targetAccount;
}
if (!StringUtil.isNullOrEmpty(authAccount))
pu.setAuthAccountName(authAccount);
if (!StringUtil.isNullOrEmpty(targetAccount))
pu.setTargetAccountName(targetAccount);
if (cl.hasOption('u'))
pu.setUrl(cl.getOptionValue('u'), isAdmin);
if (cl.hasOption('p'))
pu.setPassword(cl.getOptionValue('p'));
if (cl.hasOption('P')) {
pu.setPassword(StringUtil.readSingleLineFromFile(cl.getOptionValue('P')));
}
if (cl.hasOption('d'))
pu.setDebug(true);
if (cl.hasOption('t'))
pu.setTimeout(cl.getOptionValue('t'));
args = cl.getArgs();
pu.setInteractive(args.length < 1);
pu.initMailbox();
if (args.length < 1) {
InputStream is = null;
if (cl.hasOption('f')) {
is = new FileInputStream(cl.getOptionValue('f'));
} else {
if (LC.command_line_editing_enabled.booleanValue()) {
try {
CliUtil.enableCommandLineEditing(LC.zimbra_home.value() + "/.zmmailbox_history");
} catch (IOException e) {
System.err.println("Command line editing will be disabled: " + e);
if (pu.mGlobalVerbose) {
e.printStackTrace(System.err);
}
}
}
// This has to happen last because JLine modifies System.in.
is = System.in;
}
pu.interactive(new BufferedReader(new InputStreamReader(is, "UTF-8")));
} else {
pu.execute(args);
}
} catch (ServiceException e) {
Throwable cause = e.getCause();
stderr.println("ERROR: " + e.getCode() + " (" + e.getMessage() + ")" + (cause == null ? "" : " (cause: " + cause.getClass().getName() + " " + cause.getMessage() + ")"));
if (pu.mGlobalVerbose)
e.printStackTrace(stderr);
System.exit(2);
}
}
use of org.apache.commons.cli.Options in project zm-mailbox by Zimbra.
the class VolumeCLI method printOpt.
private void printOpt(String optStr, int leftPad) {
Options options = getOptions();
Option opt = options.getOption(optStr);
StringBuilder buf = new StringBuilder();
buf.append(Strings.repeat(" ", leftPad));
buf.append('-').append(opt.getOpt()).append(",--").append(opt.getLongOpt());
if (opt.hasArg()) {
buf.append(" <arg>");
}
buf.append(Strings.repeat(" ", 35 - buf.length()));
buf.append(opt.getDescription());
System.err.println(buf.toString());
}
use of org.apache.commons.cli.Options in project zm-mailbox by Zimbra.
the class VolumeCLI method setupCommandLineOptions.
@Override
protected void setupCommandLineOptions() {
super.setupCommandLineOptions();
Options options = getOptions();
OptionGroup og = new OptionGroup();
og.addOption(new Option(O_A, "add", false, "Adds a volume."));
og.addOption(new Option(O_D, "delete", false, "Deletes a volume."));
og.addOption(new Option(O_L, "list", false, "Lists volumes."));
og.addOption(new Option(O_E, "edit", false, "Edits a volume."));
og.addOption(new Option(O_DC, "displayCurrent", false, "Displays the current volumes."));
og.addOption(new Option(O_SC, "setCurrent", false, "Sets the current volume."));
og.addOption(new Option(O_TS, "turnOffSecondary", false, "Turns off the current secondary message volume"));
og.setRequired(true);
options.addOptionGroup(og);
options.addOption(O_ID, "id", true, "Volume ID");
options.addOption(O_T, "type", true, "Volume type (primaryMessage, secondaryMessage, or index)");
options.addOption(O_N, "name", true, "volume name");
options.addOption(O_P, "path", true, "Root path");
options.addOption(O_C, "compress", true, "Compress blobs; \"true\" or \"false\"");
options.addOption(O_CT, "compressionThreshold", true, "Compression threshold; default 4KB");
options.addOption(SoapCLI.OPT_AUTHTOKEN);
options.addOption(SoapCLI.OPT_AUTHTOKENFILE);
}
use of org.apache.commons.cli.Options in project zm-mailbox by Zimbra.
the class TestLdapReadTimeout method main.
/**
* /Users/pshao/dev/workspace/sandbox/sandbox/bin>/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin/java LdapReadTimeout
* /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin/java LdapReadTimeout
*
* zmjava com.zimbra.qa.unittest.TestLdapReadTimeout -s 5
* zmjava com.zimbra.qa.unittest.TestLdapReadTimeout -H ldap://localhost:389 -D uid=zimbra,cn=admins,cn=zimbra -w zimbra -s 5
*
* @param args
*/
public static void main(String[] args) throws Exception {
Options options = new Options();
options.addOption(O_HELP, false, "print usage");
options.addOption(O_SLEEP, true, "upon hitting an Exception, minutes to wait until exiting the program. " + "If not specified, will exit immediately.");
options.addOption(O_URI, true, "URI. e.g. ldap://localhost:389");
options.addOption(O_BINDDN, true, "bind DN");
options.addOption(O_PASSWORD, true, "password");
CommandLine cl = null;
try {
CommandLineParser parser = new GnuParser();
cl = parser.parse(options, args);
if (cl == null) {
throw new ParseException("");
}
} catch (ParseException e) {
usage(options);
e.printStackTrace();
System.exit(1);
}
if (cl.hasOption(O_HELP)) {
usage(options);
System.exit(0);
}
String uri = null;
String bindDN = null;
String password = null;
Integer minutesToWait = null;
if (cl.hasOption(O_URI)) {
uri = cl.getOptionValue(O_URI);
} else {
uri = "ldap://localhost:389";
}
if (cl.hasOption(O_BINDDN)) {
bindDN = cl.getOptionValue(O_BINDDN);
} else {
bindDN = "uid=zimbra,cn=admins,cn=zimbra";
}
if (cl.hasOption(O_PASSWORD)) {
password = cl.getOptionValue(O_PASSWORD);
} else {
password = "zimbra";
}
if (cl.hasOption(O_SLEEP)) {
String wait = cl.getOptionValue(O_SLEEP);
try {
minutesToWait = Integer.valueOf(wait);
} catch (NumberFormatException e) {
usage(options);
e.printStackTrace();
System.exit(1);
}
}
LdapReadTimeoutTester tester = null;
try {
// tester = new JNDITest(uri, bindDN, password); // fails
// works
tester = new UnboundIDTest(uri, bindDN, password);
System.out.println("=============");
System.out.println(tester.getClass().getCanonicalName());
System.out.println("LDAP server URI: " + uri);
System.out.println("bind DN: " + bindDN);
System.out.println("=============");
System.out.println();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
long startTime = System.currentTimeMillis();
test(tester);
long endTime = System.currentTimeMillis();
long elapsed = endTime - startTime;
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println(tester.getClass().getCanonicalName());
System.out.println("Started at: " + fmt.format(new Date(startTime)));
System.out.println("Ended at: " + fmt.format(new Date(endTime)));
System.out.println("Elapsed = " + (elapsed / 1000) + " seconds");
System.out.println();
if (minutesToWait != null) {
System.out.println("Sleeping for " + minutesToWait + " minutes before exiting...");
Thread.sleep(minutesToWait * 60 * 1000);
}
}
Aggregations