diff options
author | Omair Majid <[email protected]> | 2010-12-20 09:26:16 -0500 |
---|---|---|
committer | Omair Majid <[email protected]> | 2010-12-20 09:26:16 -0500 |
commit | 3c92b8a089bdbe50943c1fdfcc8eee46f2948054 (patch) | |
tree | 03fa8ca4911d45b5873f98ce6de972c4663ef07d /netx/net/sourceforge | |
parent | ef9a3f269781353eaf8b2cfb74a68b5f33265ad7 (diff) |
add command line interface to the control panel
2010-12-20 Omair Majid <[email protected]>
* Makefile.am
($(NETX_DIR)/launcher/controlpanel/%.o): Set program name, and launch
net.sourceforge.jnlp.controlpanel.CommandLine.
* netx/net/sourceforge/jnlp/config/Defaults.java
(getDefaults): Set descriptions to Unknown rather than the name.
Set source to localized form of internal.
* netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java
(getProperty): Check for nulls.
(setProperty): Add unknown as description and source for new
properties.
(checkAndFixConfiguration): Fix translation constants.
(parsePropertiesFile): Use unknown as description.
* netx/net/sourceforge/jnlp/controlpanel/CommandLine.java: New file
(CommandLine): New method.
(handleHelpCommand): Likewise.
(printListHelp): Likewise.
(handleListCommand): Likewise.
(printGetHelp): Likewise.
(handleGetCommand): Likewise.
(printSetHelp): Likewise.
(handleSetCommand): Likewise.
(printResetHelp): Likewise.
(handleResetCommand): Likewise.
(printInfoHelp): Likewise.
(handleInfoCommand): Likewise.
(printCheckHelp): Likewise.
(handleCheckCommand): Likewise.
(handle): Likewise.
(main): Likewise.
* netx/net/sourceforge/jnlp/resources/Messages.properties: Add
Usage, Unknown, RConfigurationFatal, DCIncorrectValue,
DCSourceInternal, DCUnknownSettingWithName, VVPossibleValues,
CLNoInfo, CLValue, CLValueSource, CLDescription, CLUnknownCommand
CLUnknownProperty, CLNoIssuesFound, CLIncorrectValue,
CLListDescription, CLGetDescription, CLSetDescription,
CLResetDescription, CLInfoDescription, CLCheckDescription and
CLHelpDescription. Remove DCErrorInSetting and
DCUnknownSettingWithVal.
Diffstat (limited to 'netx/net/sourceforge')
4 files changed, 493 insertions, 9 deletions
diff --git a/netx/net/sourceforge/jnlp/config/Defaults.java b/netx/net/sourceforge/jnlp/config/Defaults.java index 78e203d..8ab8347 100644 --- a/netx/net/sourceforge/jnlp/config/Defaults.java +++ b/netx/net/sourceforge/jnlp/config/Defaults.java @@ -37,6 +37,8 @@ exception statement from your version. package net.sourceforge.jnlp.config; +import static net.sourceforge.jnlp.runtime.Translator.R; + import java.io.File; import java.util.HashMap; import java.util.Map; @@ -385,7 +387,7 @@ public class Defaults { ValueValidator checker = (ValueValidator) defaults[i][1]; String actualValue = (String) defaults[i][2]; boolean locked = false; - Setting<String> value = new Setting<String>(name, name, locked, checker, actualValue, actualValue, "<internal>"); + Setting<String> value = new Setting<String>(name, R("Unknown"), locked, checker, actualValue, actualValue, R("DCSourceInternal")); result.put(name, value); } diff --git a/netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java b/netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java index dd329aa..a07df1f 100644 --- a/netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java +++ b/netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java @@ -267,7 +267,11 @@ public final class DeploymentConfiguration { } } - return currentConfiguration.get(key).getValue(); + String value = null; + if (currentConfiguration.get(key) != null) { + value = currentConfiguration.get(key).getValue(); + } + return value; } /** @@ -319,7 +323,7 @@ public final class DeploymentConfiguration { currentValue.setValue(value); } } else { - currentValue = new Setting<String>(key, key, false, null, null, value, "<unknown>"); + currentValue = new Setting<String>(key, R("Unknown"), false, null, null, value, R("Unknown")); currentConfiguration.put(key, currentValue); } } @@ -341,7 +345,7 @@ public final class DeploymentConfiguration { if (!(s.getName().equals(key))) { System.out.println(R("DCInternal", "key " + key + " does not match setting name " + s.getName())); } else if (!defaults.containsKey(key)) { - System.out.println(R("DCUnknownSettingWithVal", key)); + System.out.println(R("DCUnknownSettingWithName", key)); } else { ValueValidator checker = defaults.get(key).getValidator(); if (checker == null) { @@ -351,7 +355,7 @@ public final class DeploymentConfiguration { try { checker.validate(s.getValue()); } catch (IllegalArgumentException e) { - System.out.println(R("DCErrorInSetting", key, s.getValue(), s.getDefaultValue(), checker.getPossibleValues())); + System.out.println(R("DCIncorrectValue", key, s.getValue(), checker.getPossibleValues())); s.setValue(s.getDefaultValue()); } } @@ -546,7 +550,7 @@ public final class DeploymentConfiguration { String realKey = key.substring(0, key.length() - ".locked".length()); Setting<String> configValue = result.get(realKey); if (configValue == null) { - configValue = new Setting<String>(realKey, realKey, true, null, null, null, propertiesFile.toString()); + configValue = new Setting<String>(realKey, R("Unknown"), true, null, null, null, propertiesFile.toString()); result.put(realKey, configValue); } else { configValue.setLocked(true); @@ -556,7 +560,7 @@ public final class DeploymentConfiguration { String newValue = properties.getProperty(key); Setting<String> configValue = result.get(key); if (configValue == null) { - configValue = new Setting<String>(key, key, false, null, null, newValue, propertiesFile.toString()); + configValue = new Setting<String>(key, R("Unknown"), false, null, null, newValue, propertiesFile.toString()); result.put(key, configValue); } else { configValue.setValue(newValue); diff --git a/netx/net/sourceforge/jnlp/controlpanel/CommandLine.java b/netx/net/sourceforge/jnlp/controlpanel/CommandLine.java new file mode 100644 index 0000000..0e4f1d0 --- /dev/null +++ b/netx/net/sourceforge/jnlp/controlpanel/CommandLine.java @@ -0,0 +1,456 @@ +/* CommandLine.java -- command line interface to icedtea-web's deployment settings. +Copyright (C) 2010 Red Hat + +This program 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 of the License, or +(at your option) any later version. + +This program 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 this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +package net.sourceforge.jnlp.controlpanel; + +import static net.sourceforge.jnlp.runtime.Translator.R; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import javax.naming.ConfigurationException; + +import net.sourceforge.jnlp.config.ConfiguratonValidator; +import net.sourceforge.jnlp.config.DeploymentConfiguration; +import net.sourceforge.jnlp.config.Setting; + +/** + * Encapsulates a command line interface to the deployment configuration. + * <p> + * The central method is {@link #handle(String[])}, which calls one of the + * various 'handle' methods. The commands listed in {@link #allCommands} are + * supported. For each supported command, a method handleCOMMANDCommand exists. + * This method actually takes action based on the command. Generally, a + * printCOMMANDHelp method also exists, and prints out the help message for + * that specific command. For example, see {@link #handleListCommand(List)} + * and {@link #printListHelp()}. + * <p> + * Sample usage: + * <pre> + * CommandLine cli = new CommandLine(); + * // the string array represents input using the command line + * int retVal = cli.handle(new String[] { "help" }); + * if (retVal == CommandLine.SUCCESS) { + * // good! + * } else { + * // bad! + * } + * </pre> + * + * @author Omair Majid ([email protected]) + */ +public class CommandLine { + + public static final int ERROR = 1; + public static final int SUCCESS = 0; + + public final String PROGRAM_NAME; + + private static final List<String> allCommands = Arrays.asList(new String[] { + "list", "get", "set", "reset", "info", "check" + }); + + DeploymentConfiguration config = null; + + /** + * Creates a new instance + */ + public CommandLine() { + PROGRAM_NAME = System.getProperty("program.name"); + + config = new DeploymentConfiguration(); + try { + config.load(false); + } catch (ConfigurationException e) { + System.out.println(R("RConfigurationFatal")); + } + } + + /** + * Handle the 'help' command + * + * @param args optional + * @return the result of handling the help command. SUCCESS if no errors occurred. + */ + public int handleHelpCommand(List<String> args) { + System.out.println(R("Usage")); + System.out.println(" " + PROGRAM_NAME + " " + + allCommands.toString().replace(',', '|').replaceAll(" ", "") + " [help]"); + System.out.println(R("CLHelpDescription", PROGRAM_NAME)); + return SUCCESS; + } + + /** + * Prints help message for the list command + */ + public void printListHelp() { + System.out.println(R("Usage")); + System.out.println(" " + PROGRAM_NAME + " list [--details]"); + System.out.println(R("CLListDescription")); + } + + /** + * Handles the 'list' command + * + * @param args the arguments to the list command + * @return result of handling the command. SUCCESS if no errors occurred. + */ + public int handleListCommand(List<String> args) { + if (args.contains("help")) { + printListHelp(); + return SUCCESS; + } + + boolean verbose = false; + + if (args.contains("--details")) { + verbose = true; + args.remove("--details"); + } + + if (args.size() != 0) { + printListHelp(); + return ERROR; + } + + Map<String, Setting<String>> all = config.getRaw(); + for (String key : all.keySet()) { + Setting<String> value = all.get(key); + System.out.println(key + ": " + value.getValue()); + if (verbose) { + System.out.println("\t" + R("CLDescription", value.getDescription())); + } + } + return SUCCESS; + } + + /** + * Prints help message for the get command + */ + public void printGetHelp() { + System.out.println(R("Usage")); + System.out.println(" " + PROGRAM_NAME + " get property-name"); + System.out.println(R("CLGetDescription")); + } + + /** + * Handles the 'get' command. + * + * @param args the arguments to the get command + * @return an integer representing success (SUCCESS) or error handling the + * get command. + */ + public int handleGetCommand(List<String> args) { + if (args.contains("help")) { + printGetHelp(); + return SUCCESS; + } + + if (args.size() != 1) { + printGetHelp(); + return ERROR; + } + + Map<String, Setting<String>> all = config.getRaw(); + + String key = args.get(0); + String value = null; + if (all.containsKey(key)) { + value = all.get(key).getValue(); + System.out.println(value); + return SUCCESS; + } else { + System.out.println(R("CLUnknownProperty", key)); + return ERROR; + } + } + + /** + * Prints the help message for the 'set' command + */ + public void printSetHelp() { + System.out.println(R("Usage")); + System.out.println(" " + PROGRAM_NAME + " set property-name value"); + System.out.println(R("CLSetDescription")); + } + + /** + * Handles the 'set' command + * + * @param args the arguments to the set command + * @return an integer indicating success (SUCCESS) or error in handling + * the command + */ + public int handleSetCommand(List<String> args) { + if (args.contains("help")) { + printSetHelp(); + return SUCCESS; + } + + if (args.size() != 2) { + printSetHelp(); + return ERROR; + } + + String key = args.get(0); + String value = args.get(1); + + if (config.getRaw().containsKey(key)) { + Setting<String> old = config.getRaw().get(key); + if (old.getValidator() != null) { + try { + old.getValidator().validate(value); + } catch (IllegalArgumentException e) { + System.out.println(R("CLIncorrectValue", old.getName(), value, old.getValidator().getPossibleValues())); + return ERROR; + } + } + config.setProperty(key, value); + } else { + System.out.println(R("CLUnknownProperty", value)); + config.setProperty(key, value); + } + + try { + config.save(); + } catch (IOException e) { + e.printStackTrace(); + return ERROR; + } + + return SUCCESS; + } + + /** + * Prints a help message for the reset command + */ + public void printResetHelp() { + System.out.println(R("Usage")); + System.out.println(" " + PROGRAM_NAME + " reset property-name"); + System.out.println(R("CLResetDescription")); + } + + /** + * Handles the 'reset' command + * + * @param args the arguments to the reset command + * @return an integer indicating success (SUCCESS) or error in handling + * the command + */ + public int handleResetCommand(List<String> args) { + if (args.contains("help")) { + printResetHelp(); + return SUCCESS; + } + + if (args.size() != 1) { + printResetHelp(); + return ERROR; + } + + String key = args.get(0); + + Map<String, Setting<String>> all = config.getRaw(); + if (!all.containsKey(key)) { + System.out.println(R("CLUnknownProperty", key)); + return ERROR; + } + + Setting<String> setting = all.get(key); + setting.setValue(setting.getDefaultValue()); + + try { + config.save(); + } catch (IOException e) { + e.printStackTrace(); + return ERROR; + } + + return SUCCESS; + } + + /** + * Print a help message for the 'info' command + */ + public void printInfoHelp() { + System.out.println(R("Usage")); + System.out.println(" " + PROGRAM_NAME + " info property-name"); + System.out.println(R("CLInfoDescription")); + } + + /** + * Handles the 'info' command + * + * @param args the arguments to the info command + * @return an integer indicating success (SUCCESS) or error in handling + * the command + */ + public int handleInfoCommand(List<String> args) { + if (args.contains("help")) { + printInfoHelp(); + return SUCCESS; + } + + if (args.size() != 1) { + printInfoHelp(); + return ERROR; + } + + Map<String, Setting<String>> all = config.getRaw(); + + String key = args.get(0); + Setting<String> value = all.get(key); + if (value == null) { + System.out.println(R("CLNoInfo")); + return ERROR; + } else { + System.out.println(R("CLDescription", value.getDescription())); + System.out.println(R("CLValue", value.getValue())); + if (value.getValidator() != null) { + System.out.println("\t" + R("VVPossibleValues", value.getValidator().getPossibleValues())); + } + System.out.println(R("CLValueSource", value.getSource())); + return SUCCESS; + } + } + + /** + * Prints a help message for the 'check' command + */ + public void printCheckHelp() { + System.out.println(R("Usage")); + System.out.println(" " + PROGRAM_NAME + " check"); + System.out.println(R("CLCheckDescription")); + } + + /** + * Handles the 'check' command + * + * @param args the arguments to the check command. + * @return an integer indicating success (SUCCESS) or error in handling + * the command + */ + public int handleCheckCommand(List<String> args) { + if (args.contains("help")) { + printCheckHelp(); + return SUCCESS; + } + + if (args.size() != 0) { + printCheckHelp(); + return ERROR; + } + + Map<String, Setting<String>> all = config.getRaw(); + + ConfiguratonValidator validator = new ConfiguratonValidator(all); + validator.validate(); + + boolean allValid = true; + for (Setting<String> setting : validator.getIncorrectSetting()) { + System.out.println(R("CLIncorrectValue", setting.getName(), setting.getValue(), setting.getValidator().getPossibleValues())); + allValid = false; + } + + for (Setting<String> setting : validator.getUnrecognizedSetting()) { + System.out.println(R("CLUnknownProperty", setting.getName())); + allValid = false; + } + + if (allValid) { + System.out.println(R("CLNoIssuesFound")); + return SUCCESS; + } else { + return ERROR; + } + } + + /** + * Handles overall command line arguments. The argument array is split + * into two pieces: the first element is assumend to be the command, and + * everything after is taken to be the argument to the command. + * + * @param commandAndArgs A string array representing the command and + * arguments to take action on + * @return an integer representing an error code or SUCCESS if no problems + * occurred. + */ + public int handle(String[] commandAndArgs) { + + if (commandAndArgs == null) { + throw new NullPointerException("command is null"); + } + + if (commandAndArgs.length == 0) { + handleHelpCommand(new ArrayList<String>()); + return ERROR; + } + + String command = commandAndArgs[0]; + String[] argsArray = new String[commandAndArgs.length - 1]; + System.arraycopy(commandAndArgs, 1, argsArray, 0, commandAndArgs.length - 1); + List<String> arguments = new ArrayList<String>(Arrays.asList(argsArray)); + + int val; + if (command.equals("help")) { + val = handleHelpCommand(arguments); + } else if (command.equals("list")) { + val = handleListCommand(arguments); + } else if (command.equals("set")) { + val = handleSetCommand(arguments); + } else if (command.equals("reset")) { + val = handleResetCommand(arguments); + } else if (command.equals("get")) { + val = handleGetCommand(arguments); + } else if (command.equals("info")) { + val = handleInfoCommand(arguments); + } else if (command.equals("check")) { + val = handleCheckCommand(arguments); + } else if (allCommands.contains(command)) { + System.out.println("INTERNAL ERROR: " + command + " should have been implemented"); + val = ERROR; + } else { + System.out.println(R("CLUnknownCommand", command)); + handleHelpCommand(new ArrayList<String>()); + val = ERROR; + } + + return val; + } + + /** + * The starting point of the program + * @param args the command line arguments to this program + */ + public static void main(String[] args) throws Exception { + if (args.length == 0) { + ControlPanel.main(new String[] {}); + } else { + CommandLine cli = new CommandLine(); + int result = cli.handle(args); + + // instead of returning, use System.exit() so we can pass back + // error codes indicating success or failure. Otherwise using + // this program for scripting will become much more challenging + System.exit(result); + } + } +} diff --git a/netx/net/sourceforge/jnlp/resources/Messages.properties b/netx/net/sourceforge/jnlp/resources/Messages.properties index bd021af..2c376ff 100644 --- a/netx/net/sourceforge/jnlp/resources/Messages.properties +++ b/netx/net/sourceforge/jnlp/resources/Messages.properties @@ -14,12 +14,14 @@ ButProceed=Proceed ButRun=Run
AFileOnTheMachine=a file on the machine
AlwaysAllowAction=Always allow this action
+Usage=Usage:
Continue=Do you want to continue?
Field=Field
From=From
Name=Name
Publisher=Publisher
+Unknown=<Unknown>
Value=Value
Version=Version
@@ -136,6 +138,7 @@ RNoLockDir=Unable to create locks directory ({0}) RNestedJarExtration=Unable to extract nested jar.
RUnexpected=Unexpected {0} at {1}
RConfigurationError=Fatal error while reading the configuration
+RConfigurationFatal=ERROR: a fatal error has occurred while loading configuration. Perhaps a global configuration was required but could not be found
# Boot options, message should be shorter than this ---------------->
BOUsage=javaws [-run-options] <jnlp file>
@@ -234,11 +237,13 @@ KSJsseCaCerts=Trusted JSSE Root CA Certificates, KSClientCerts=Client Authentication Certificates
# Deployment Configuration messages
-DCErrorInSetting=Error: setting "{0}" has incorrect value "{1}". Using the default value "{2}" instead. Possible values {3}.
+DCIncorrectValue=Property "{0}" has incorrect value "{1}". Possible values {2}.
DCInternal=Internal error: {0}
-DCUnknownSettingWithVal=Unknown setting "{0}"
+DCSourceInternal=<internal> +DCUnknownSettingWithName=Property "{0}" is unknown.
# Value Validator messages. Messages should follow "Possible values ..."
+VVPossibleValues=Possible values {0}
VVPossibleBooleanValues=are {0} or {1}
VVPossibleFileValues=include the absolute location of a file - it must begin with a /
VVPossibleRangedIntegerValues=are in range {0} to {1} (inclusive)
@@ -371,5 +376,22 @@ TIFPRestoreDefaults=Restore defaults # Control Panel - Misc. CPJRESupport=IcedTea-Web currently does not support the use of multiple JREs. +# command line control panel
+CLNoInfo=No information avaiable (is this a valid option?).
+CLValue=Value: {0}
+CLValueSource=Source: {0}
+CLDescription=Description: {0}
+CLUnknownCommand=Unknown command {0}
+CLUnknownProperty=Unknown property-name "{0}"
+CLNoIssuesFound=No issues found.
+CLIncorrectValue=Property "{0}" has incorrect value "{1}". Possible values {2}.
+CLListDescription=Shows a list of all property names and values that are in use by IcedTea-Web
+CLGetDescription=Shows the value for property-name
+CLSetDescription=Sets the property-name to value if possible. The value is checked for being valid. If the administrator has locked the property, this will have no effect
+CLResetDescription=Resets the value for property-name to it\'s default value
+CLInfoDescription=Shows more information about the given property
+CLCheckDescription=Shows any properties that have been defined but are not recognized by IcedTea Web
+CLHelpDescription=The itweb-settings tool allows a user to modify, view and check configuration. \nTo use the GUI, do not pass any arguments. To use the CLI mode, pass in the approrpiate command and parameters. For help with a particular command, try: {0} command help
+ # Buttons ButApply=Apply |