# HG changeset patch # User Severin Gehwolf # Date 1357928583 -3600 # Node ID 0f4271d4e33453c8b73a9e2ed0cceff8c63cb9a7 # Parent 8af946d24b9dc0be42669d393dffbf667ecd9c6d Change common options implementation. This patch removes the old CommonCommandOptions class. Common options - dbUrl, logLevel, username, password - are now automatically added if the "options" list in command.properties contains "special" option names such as AUTO_DB_OPTIONS (dbUrl, username, password) and AUTO_LOG_OPTION (logLevel). If these are present thermostat adds options as requested automatically. Note that all automatic options are optional. The required property can be changed/overridden by using a .required key in command.properties and adding to the "options" list AFTER the AUTO_* option. I've also cleaned up the command.properties files and help command output. command.properties don't need to maintain "thermostat" any longer. Moreover, help output contains the description of the command as well. Example: $ ./distribution/target/bin/thermostat help agent usage: thermostat agent [-d [-u -p ]] [-s] [--debug] [-l ] starts and stops the thermostat agent thermostat agent -d,--dbUrl connect to the given URL -l,--logLevel sets the log level for this invocation. Possible values for in decreasing severity are: SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST and OFF. -p,--password the password to use for authentication -s,--saveOnExit save the data on exit -u,--username the username to use for authentication -v,--debug launch with debug console enabled Finally, this should be the last patch required in order to drop the storage-core dependency of common-core, since it removes the need for client-cli to depend on the launcher bundle. It fixes PR1200 too. Reviewed-by: ebaron, vanaltj Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-January/005118.html diff -r 8af946d24b9d -r 0f4271d4e334 client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/ConnectCommand.java --- a/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/ConnectCommand.java Tue Jan 15 23:08:35 2013 +0100 +++ b/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/ConnectCommand.java Fri Jan 11 19:23:03 2013 +0100 @@ -44,7 +44,6 @@ import com.redhat.thermostat.common.config.ClientPreferences; import com.redhat.thermostat.common.locale.Translate; import com.redhat.thermostat.common.utils.OSGIUtils; -import com.redhat.thermostat.launcher.CommonCommandOptions; import com.redhat.thermostat.storage.core.ConnectionException; import com.redhat.thermostat.storage.core.StorageException; import com.redhat.thermostat.utils.keyring.Keyring; @@ -59,6 +58,10 @@ */ public class ConnectCommand extends SimpleCommand { + private static final String DB_URL_ARG = "dbUrl"; + private static final String USERNAME_ARG = "username"; + private static final String PASSWORD_ARG = "password"; + private static final Translate translator = LocaleResources.createLocalizer(); private static final String NAME = "connect"; @@ -84,12 +87,12 @@ if (prefs == null) { prefs = new ClientPreferences(OSGIUtils.getInstance().getService(Keyring.class)); } - String dbUrl = ctx.getArguments().getArgument(CommonCommandOptions.DB_URL_ARG); + String dbUrl = ctx.getArguments().getArgument(DB_URL_ARG); if (dbUrl == null) { dbUrl = prefs.getConnectionUrl(); } - String username = ctx.getArguments().getArgument(CommonCommandOptions.USERNAME_ARG); - String password = ctx.getArguments().getArgument(CommonCommandOptions.PASSWORD_ARG); + String username = ctx.getArguments().getArgument(USERNAME_ARG); + String password = ctx.getArguments().getArgument(PASSWORD_ARG); try { // may throw StorageException if storage url is not supported service = dbServiceFactory.createDbService(username, password, dbUrl); diff -r 8af946d24b9d -r 0f4271d4e334 client/cli/src/test/java/com/redhat/thermostat/client/cli/internal/ConnectCommandTest.java --- a/client/cli/src/test/java/com/redhat/thermostat/client/cli/internal/ConnectCommandTest.java Tue Jan 15 23:08:35 2013 +0100 +++ b/client/cli/src/test/java/com/redhat/thermostat/client/cli/internal/ConnectCommandTest.java Fri Jan 11 19:23:03 2013 +0100 @@ -45,11 +45,8 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import org.apache.commons.cli.Option; -import org.apache.commons.cli.Options; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.osgi.framework.Bundle; @@ -65,7 +62,6 @@ import com.redhat.thermostat.common.cli.SimpleArguments; import com.redhat.thermostat.common.locale.Translate; import com.redhat.thermostat.common.utils.OSGIUtils; -import com.redhat.thermostat.launcher.CommonCommandOptions; import com.redhat.thermostat.test.TestCommandContextFactory; @RunWith(PowerMockRunner.class) @@ -168,29 +164,4 @@ assertNotNull(cmd.getUsage()); } - @Ignore - @Test - public void testAcceptedArguments() { - Options options = cmd.getOptions(); - assertNotNull(options); - assertTrue(options.getOptions().size() == 3); - - assertTrue(options.hasOption(CommonCommandOptions.DB_URL_ARG)); - Option db = options.getOption(CommonCommandOptions.DB_URL_ARG); - assertEquals(CommonCommandOptions.DB_URL_DESC, db.getDescription()); - assertTrue(db.isRequired()); - assertTrue(db.hasArg()); - - assertTrue(options.hasOption(CommonCommandOptions.USERNAME_ARG)); - Option user = options.getOption(CommonCommandOptions.USERNAME_ARG); - assertEquals(CommonCommandOptions.USERNAME_DESC, user.getDescription()); - assertFalse(user.isRequired()); - assertTrue(user.hasArg()); - - assertTrue(options.hasOption(CommonCommandOptions.PASSWORD_ARG)); - Option pass = options.getOption(CommonCommandOptions.PASSWORD_ARG); - assertEquals(CommonCommandOptions.PASSWORD_DESC, pass.getDescription()); - assertFalse(pass.isRequired()); - assertTrue(pass.hasArg()); - } } diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/agent.properties --- a/distribution/config/commands/agent.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/agent.properties Fri Jan 11 19:23:03 2013 +0100 @@ -41,31 +41,15 @@ description = starts and stops the thermostat agent -usage = thermostat agent [-d [-u -p ]] [-s] [--debug] +usage = agent -d [-u -p ] [-s] [--debug] [-l ] -options = saveOnExit, dbUrl, username, password, debug +options = AUTO_LOG_OPTION, AUTO_DB_OPTIONS, saveOnExit, dbUrl, debug saveOnExit.short = s saveOnExit.long = saveOnExit saveOnExit.description = save the data on exit -dbUrl.short = d -dbUrl.long = dbUrl -dbUrl.hasarg = true dbUrl.required = true -dbUrl.description = connect to the given URL - -username.short = u -username.long = username -username.hasarg = true -username.required = false -username.description = the username to use for authentication - -password.short = p -password.long = password -password.hasarg = true -password.required = false -password.description = the password to use for authentication debug.short = v debug.long = debug diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/connect.properties --- a/distribution/config/commands/connect.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/connect.properties Fri Jan 11 19:23:03 2013 +0100 @@ -14,24 +14,8 @@ description = persistently connect to storage -usage = connect -d [-u ] [-p ] - -options = dbUrl, username, password - -dbUrl.short = d -dbUrl.long = dbUrl -dbUrl.hasarg = true -dbUrl.required = true -dbUrl.description = the URL of the storage to connect to +usage = connect -d [-u ] [-p ] [-l ] -username.short = u -username.long = username -username.hasarg = true -username.required = false -username.description = the username to use for authentication +options = AUTO_LOG_OPTION, AUTO_DB_OPTIONS, dbUrl -password.short = p -password.long = password -password.hasarg = true -password.required = false -password.description = the password to use for authentication +dbUrl.required = true diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/disconnect.properties --- a/distribution/config/commands/disconnect.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/disconnect.properties Fri Jan 11 19:23:03 2013 +0100 @@ -2,7 +2,6 @@ description = disconnect from the currently used storage -usage = disconnect +usage = disconnect [-l ] -# No options necessary for this command -#options = +options = AUTO_LOG_OPTION diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/dump-heap.properties --- a/distribution/config/commands/dump-heap.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/dump-heap.properties Fri Jan 11 19:23:03 2013 +0100 @@ -19,9 +19,9 @@ description = trigger a heap dump on the VM -usage = thermostat dump-heap --hostId --vmId +usage = dump-heap --hostId --vmId [-d [-u -p ]] [-l ] -options = hostId, vmId +options = hostId, vmId, AUTO_DB_OPTIONS, AUTO_LOG_OPTION hostId.short = a hostId.long = hostId diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/find-objects.properties --- a/distribution/config/commands/find-objects.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/find-objects.properties Fri Jan 11 19:23:03 2013 +0100 @@ -18,9 +18,9 @@ description = Finds objects in a heapdump -usage = thermostat find-objects --heapId --limit +usage = find-objects [-d [-u -p ]] [-l ] --heapId --limit -options = heapId, limit +options = heapId, limit, AUTO_DB_OPTIONS, AUTO_LOG_OPTION heapId.short = h heapId.long = heapId @@ -28,7 +28,7 @@ heapId.required = true heapId.description = the ID of the heapdump to analyze -limit.short = l +limit.short = L limit.long = limit limit.hasarg = true limit.required = false diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/find-root.properties --- a/distribution/config/commands/find-root.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/find-root.properties Fri Jan 11 19:23:03 2013 +0100 @@ -18,9 +18,9 @@ description = finds the shortest path from an object to a GC root -usage = thermostat find-root --heapId --objectId [-a] +usage = find-root --heapId --objectId [-a] [-d [-u -p ]] [-l ] -options = heapId, objectId, all +options = heapId, objectId, all, AUTO_DB_OPTIONS, AUTO_LOG_OPTION heapId.short = h heapId.long = heapId diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/gui.properties --- a/distribution/config/commands/gui.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/gui.properties Fri Jan 11 19:23:03 2013 +0100 @@ -55,7 +55,6 @@ description = launches the GUI client -usage = thermostat gui +usage = gui [-l ] -# This command does not have any options -#options = +options = AUTO_LOG_OPTION diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/help.properties --- a/distribution/config/commands/help.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/help.properties Fri Jan 11 19:23:03 2013 +0100 @@ -1,7 +1,7 @@ # HelpCommand is provided by launcher, and needs no other bundles to be loaded. bundles = description = show help for a given command or help overview -usage = thermostat help [command-name] +usage = help [command-name] # This command does not have any options #options = diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/list-heap-dumps.properties --- a/distribution/config/commands/list-heap-dumps.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/list-heap-dumps.properties Fri Jan 11 19:23:03 2013 +0100 @@ -18,9 +18,9 @@ description = list all heap dumps -usage = thermostat list-heap-dumps --hostId --vmId +usage = list-heap-dumps --hostId --vmId [-d [-u -p ]] [-l ] -options = hostId, vmId +options = hostId, vmId, AUTO_DB_OPTIONS, AUTO_LOG_OPTION hostId.short = a hostId.long = hostId diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/list-vms.properties --- a/distribution/config/commands/list-vms.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/list-vms.properties Fri Jan 11 19:23:03 2013 +0100 @@ -13,7 +13,7 @@ description = lists all currently monitored VMs -usage = thermostat list-vms [-d [-u -p ]] +usage = list-vms [-d [-u -p ]] [-l ] # This command does not have any options -#options = +options = AUTO_DB_OPTIONS, AUTO_LOG_OPTION diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/object-info.properties --- a/distribution/config/commands/object-info.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/object-info.properties Fri Jan 11 19:23:03 2013 +0100 @@ -18,9 +18,9 @@ description = prints information about an object in a heap dump -usage = thermostat object-info --heapId --objectId +usage = object-info --heapId --objectId [-d [-u -p ]] [-l ] -options = heapId, objectId +options = heapId, objectId, AUTO_DB_OPTIONS, AUTO_LOG_OPTION heapId.short = h heapId.long = heapId diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/ping.properties --- a/distribution/config/commands/ping.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/ping.properties Fri Jan 11 19:23:03 2013 +0100 @@ -14,7 +14,6 @@ description = using the Command Channel, send a ping to a running agent -usage = ping +usage = ping [-d [-u -p ]] [-l ] -# This command does not have any options -#options = +options = AUTO_DB_OPTIONS, AUTO_LOG_OPTION diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/save-heap-dump-to-file.properties --- a/distribution/config/commands/save-heap-dump-to-file.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/save-heap-dump-to-file.properties Fri Jan 11 19:23:03 2013 +0100 @@ -18,9 +18,9 @@ description = saves a heap dump to a local file -usage = thermostat save-heap-dump-to-file --heapId --file +usage = save-heap-dump-to-file --heapId --file [-d [-u -p ]] [-l ] -options = heapId, file +options = heapId, file, AUTO_DB_OPTIONS, AUTO_LOG_OPTION heapId.short = h heapId.long = heapId diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/service.properties --- a/distribution/config/commands/service.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/service.properties Fri Jan 11 19:23:03 2013 +0100 @@ -14,7 +14,6 @@ description = starts and stops the thermostat storage and agent -usage = thermostat service +usage = service [-l ] -# This command does not have any options -#options = +options = AUTO_LOG_OPTION diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/shell.properties --- a/distribution/config/commands/shell.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/shell.properties Fri Jan 11 19:23:03 2013 +0100 @@ -3,7 +3,7 @@ description = launches the Thermostat interactive shell -usage = thermostat shell +usage = shell # This command does not have any options #options = diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/show-heap-histogram.properties --- a/distribution/config/commands/show-heap-histogram.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/show-heap-histogram.properties Fri Jan 11 19:23:03 2013 +0100 @@ -18,9 +18,9 @@ description = show the heap histogram -usage = thermostat show-heap-histogram --heapId +usage = show-heap-histogram --heapId [-d [-u -p ]] [-l ] -options = heapId +options = heapId, AUTO_DB_OPTIONS, AUTO_LOG_OPTION heapId.short = h heapId.long = heapId diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/storage.properties --- a/distribution/config/commands/storage.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/storage.properties Fri Jan 11 19:23:03 2013 +0100 @@ -8,9 +8,9 @@ description = starts and stops the thermostat storage -usage = thermostat storage <--start|--stop> +usage = storage <--start|--stop> [--dryRun] [-q] [-l ] -options = dryRun, start|stop, quiet +options = dryRun, start|stop, quiet, AUTO_LOG_OPTION dryRun.short = d dryRun.long = dryRun diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/vm-info.properties --- a/distribution/config/commands/vm-info.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/vm-info.properties Fri Jan 11 19:23:03 2013 +0100 @@ -13,9 +13,9 @@ description = shows basic information about a VM -usage = thermostat vm-info [--vmId ] [--hostId ] +usage = vm-info [--vmId ] [--hostId ] [-d [-u -p ]] [-l ] -options = hostId, vmId +options = hostId, vmId, AUTO_DB_OPTIONS, AUTO_LOG_OPTION hostId.short = a hostId.long = hostId diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/vm-stat.properties --- a/distribution/config/commands/vm-stat.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/vm-stat.properties Fri Jan 11 19:23:03 2013 +0100 @@ -17,9 +17,9 @@ description = show various statistics about a VM -usage = thermostat vm-stat --hostId --vmId +usage = vm-stat --hostId --vmId [-d [-u -p ]] [-l ] -options = hostId, vmId, continuous +options = hostId, vmId, continuous, AUTO_DB_OPTIONS, AUTO_LOG_OPTION hostId.short = a hostId.long = hostId diff -r 8af946d24b9d -r 0f4271d4e334 distribution/config/commands/webservice.properties --- a/distribution/config/commands/webservice.properties Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/config/commands/webservice.properties Fri Jan 11 19:23:03 2013 +0100 @@ -27,27 +27,11 @@ description = starts and stops the thermostat web service -usage = thermostat webservice - -options = storageURL, username, password, bindAddrs - -storageURL.short = d -storageURL.long = storageURL -storageURL.hasarg = true -storageURL.required = true -storageURL.description = connect to the given URL +usage = webservice -d -b [-u -p ] [-l ] -username.short = u -username.long = username -username.hasarg = true -username.required = false -username.description = the username to authenticate against the storage +options = AUTO_DB_OPTIONS, dbUrl, bindAddrs, AUTO_LOG_OPTION -password.short = p -password.long = password -password.hasarg = true -password.required = false -password.description = the password to authenticate against the storage +dbUrl.required = true bindAddrs.long = bindAddrs bindAddrs.short = b diff -r 8af946d24b9d -r 0f4271d4e334 distribution/src/test/java/com/redhat/thermostat/distribution/CliTest.java --- a/distribution/src/test/java/com/redhat/thermostat/distribution/CliTest.java Tue Jan 15 23:08:35 2013 +0100 +++ b/distribution/src/test/java/com/redhat/thermostat/distribution/CliTest.java Fri Jan 11 19:23:03 2013 +0100 @@ -144,13 +144,10 @@ String[] lines = stdOut.split("\n"); String usage = lines[0]; - assertTrue(usage.matches("^usage: shell \\[.*\\]$")); + assertTrue(usage.matches("^usage: thermostat shell$")); String description = lines[1]; - - for (int i = 2; i < lines.length; i++) { - String argLine = lines[i]; - assertTrue(argLine.matches("^\\s+--\\w+\\s.*$")); - } + assertTrue(description.matches("^\\s+launches the Thermostat interactive shell$")); + assertTrue(lines[2].matches("thermostat shell")); } @Test diff -r 8af946d24b9d -r 0f4271d4e334 launcher/src/main/java/com/redhat/thermostat/launcher/CommonCommandOptions.java --- a/launcher/src/main/java/com/redhat/thermostat/launcher/CommonCommandOptions.java Tue Jan 15 23:08:35 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,100 +0,0 @@ -/* - * Copyright 2012 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * . - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.launcher; - -import org.apache.commons.cli.Option; -import org.apache.commons.cli.Options; - -import com.redhat.thermostat.common.cli.Command; -import com.redhat.thermostat.common.cli.CommandInfo; - -public class CommonCommandOptions { - - public static final String DB_URL_ARG = "dbUrl"; - public static final String USERNAME_ARG = "username"; - public static final String PASSWORD_ARG = "password"; - - public static final String DB_URL_DESC = "the URL of the storage to connect to"; - public static final String USERNAME_DESC = "the username to use for authentication"; - public static final String PASSWORD_DESC = "the password to use for authentication"; - - public static final String LOG_LEVEL_ARG = "logLevel"; - private static final String LOG_LEVEL_DESC = "log level"; - - public Options getOptionsFor(Command cmd) { - - Options options = cmd.getOptions(); - addDbUrlOptionForStorageCommand(cmd, options); - addLogLevelOption(options); - addOptionalAuthenticationArguments(options); - return options; - } - - public Options getOptionsFor(CommandInfo info) { - // TODO make storageRequired part of CommandInfo (in command.properties) - Options options = info.getOptions(); - addLogLevelOption(options); - addOptionalAuthenticationArguments(options); - return options; - } - - private void addDbUrlOptionForStorageCommand(Command cmd, Options options) { - if (cmd.isStorageRequired()) { - Option option = new Option("d", DB_URL_ARG, true, DB_URL_DESC); - option.setRequired(false); - options.addOption(option); - } - } - - private void addLogLevelOption(Options options) { - Option option = new Option(null, LOG_LEVEL_ARG, true, LOG_LEVEL_DESC); - option.setRequired(false); - options.addOption(option); - } - - private void addOptionalAuthenticationArguments(Options options) { - - Option userOption = new Option(null, USERNAME_ARG, true, USERNAME_DESC); - userOption.setRequired(false); - options.addOption(userOption); - Option passwordOption = new Option(null, PASSWORD_ARG, true, PASSWORD_DESC); - passwordOption.setRequired(false); - options.addOption(passwordOption); - } - -} - diff -r 8af946d24b9d -r 0f4271d4e334 launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommandInfoImpl.java --- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommandInfoImpl.java Tue Jan 15 23:08:35 2013 +0100 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommandInfoImpl.java Fri Jan 11 19:23:03 2013 +0100 @@ -65,7 +65,7 @@ private static final String PROP_OPTHASARG = ".hasarg"; private static final String PROP_OPTREQUIRED = ".required"; private static final String PROP_OPTDESC = ".description"; - + private String name, description, usage; private Options options; private List dependencies; @@ -122,8 +122,20 @@ } private void learnOption(String name, Properties props) { - Option option = optionFromProperties(name, props); - options.addOption(option); + if (name.equals(CommonOptions.OPTIONS_COMMON_DB_OPTIONS)) { + addDbOptions(); + } else if (name.equals(CommonOptions.OPTIONS_COMMON_LOG_OPTION)) { + options.addOption(CommonOptions.getLogOption()); + } else { + Option option = optionFromProperties(name, props); + options.addOption(option); + } + } + + private void addDbOptions() { + for (Option opt: CommonOptions.getDbOptions()) { + options.addOption(opt); + } } /* TODO currently this assumes that any set of mutually exclusive options will be @@ -153,6 +165,17 @@ String argKey = name + PROP_OPTHASARG; String requiredKey = name + PROP_OPTREQUIRED; String descKey = name + PROP_OPTDESC; + + // required property of common options are allowed to be overridden by + // command.properties files + if (CommonOptions.ALL_COMMON_OPTIONS.contains(name) && options.hasOption(name)) { + if (props.containsKey(requiredKey)) { + Option optionToChange = options.getOption(name); + required = Boolean.parseBoolean((String) props.getProperty(requiredKey)); + optionToChange.setRequired(required); + return optionToChange; + } + } if (props.containsKey(optKey)) { opt = (String) props.getProperty(optKey); diff -r 8af946d24b9d -r 0f4271d4e334 launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommonOptions.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommonOptions.java Fri Jan 11 19:23:03 2013 +0100 @@ -0,0 +1,103 @@ +/* + * Copyright 2013 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * . + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.launcher.internal; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.apache.commons.cli.Option; + +import com.redhat.thermostat.common.locale.Translate; + +/* + * Container class for launcher-added options, such as dbUrl. + */ +final class CommonOptions { + + // The launcher uses username, password and dbUrl options for establishing a + // DB connection before the command is run. These options can be added via + // this special option in the options section of command.properties. + static final String OPTIONS_COMMON_DB_OPTIONS = "AUTO_DB_OPTIONS"; + // The launcher will auto-add a logLevel option if this special option is + // specified in the command.properties option section. + static final String OPTIONS_COMMON_LOG_OPTION = "AUTO_LOG_OPTION"; + + static final String LOG_LEVEL_ARG = "logLevel"; + static final String DB_URL_ARG = "dbUrl"; + static final String USERNAME_ARG = "username"; + static final String PASSWORD_ARG = "password"; + static final Set ALL_COMMON_OPTIONS = new HashSet<>(4); + + static { + ALL_COMMON_OPTIONS.add(LOG_LEVEL_ARG); + ALL_COMMON_OPTIONS.add(DB_URL_ARG); + ALL_COMMON_OPTIONS.add(USERNAME_ARG); + ALL_COMMON_OPTIONS.add(PASSWORD_ARG); + } + + static final Translate t = LocaleResources.createLocalizer(); + + static List