You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@oodt.apache.org by bf...@apache.org on 2011/10/14 00:29:24 UTC
svn commit: r1183144 - in /oodt/branches/cas-cl/src:
main/java/org/apache/oodt/cas/cl/
main/java/org/apache/oodt/cas/cl/help/presenter/
main/java/org/apache/oodt/cas/cl/help/printer/
main/java/org/apache/oodt/cas/cl/option/ main/java/org/apache/oodt/ca...
Author: bfoster
Date: Thu Oct 13 22:29:24 2011
New Revision: 1183144
URL: http://svn.apache.org/viewvc?rev=1183144&view=rev
Log:
- introduced PrintSupportedActions option
- created a standard Handler which allows action setters to be called without any deps on Spring
- added yet another working unit-test
Added:
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineActionsHelpPrinter.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineActionsHelpPrinter.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/PrintSupportedActionsCmdLineOption.java (with props)
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/StdCmdLineOptionHandler.java (with props)
Modified:
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineArgs.java
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineUtility.java
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/CmdLineOptionHelpPresenter.java
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/StdCmdLineOptionHelpPresenter.java
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java
oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/CmdLineOptionUtils.java
oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/option/util/TestCmdLineOptionUtils.java
Modified: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineArgs.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineArgs.java?rev=1183144&r1=1183143&r2=1183144&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineArgs.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineArgs.java Thu Oct 13 22:29:24 2011
@@ -3,46 +3,62 @@ package org.apache.oodt.cas.cl;
import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.findAction;
import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.findActionOption;
import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.findHelpOption;
+import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.findPrintSupportedActionsOption;
import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.findSpecifiedOption;
import java.util.HashSet;
import java.util.Set;
import org.apache.oodt.cas.cl.action.CmdLineAction;
+import org.apache.oodt.cas.cl.option.ActionCmdLineOption;
import org.apache.oodt.cas.cl.option.CmdLineOption;
import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+import org.apache.oodt.cas.cl.option.HelpCmdLineOption;
+import org.apache.oodt.cas.cl.option.PrintSupportedActionsCmdLineOption;
public class CmdLineArgs {
private CmdLineAction specifiedAction;
private Set<CmdLineAction> supportedActions;
- private CmdLineOption helpOption;
+ private HelpCmdLineOption helpOption;
private CmdLineOptionInstance helpOptionInst;
- private CmdLineOption actionOption;
+ private ActionCmdLineOption actionOption;
private CmdLineOptionInstance actionOptionInst;
+ private PrintSupportedActionsCmdLineOption psaOption;
+ private CmdLineOptionInstance psaOptionInst;
private Set<CmdLineOption> supportedOptions;
private Set<CmdLineOption> customSupportedOptions;
private Set<CmdLineOptionInstance> specifiedOptions;
private Set<CmdLineOptionInstance> customSpecifiedOptions;
- public CmdLineArgs(Set<CmdLineAction> supportedActions, Set<CmdLineOption> supportedOptions, Set<CmdLineOptionInstance> specifiedOptions) {
+ CmdLineArgs(Set<CmdLineAction> supportedActions, Set<CmdLineOption> supportedOptions, Set<CmdLineOptionInstance> specifiedOptions) {
helpOption = findHelpOption(supportedOptions);
helpOptionInst = findSpecifiedOption(helpOption, specifiedOptions);
actionOption = findActionOption(supportedOptions);
actionOptionInst = findSpecifiedOption(actionOption, specifiedOptions);
+ psaOption = findPrintSupportedActionsOption(supportedOptions);
+ psaOptionInst = findSpecifiedOption(psaOption, specifiedOptions);
this.supportedOptions = new HashSet<CmdLineOption>(supportedOptions);
customSupportedOptions = new HashSet<CmdLineOption>(supportedOptions);
customSupportedOptions.remove(helpOption);
customSupportedOptions.remove(actionOption);
+ customSupportedOptions.remove(psaOption);
this.specifiedOptions = new HashSet<CmdLineOptionInstance>(specifiedOptions);
customSpecifiedOptions = new HashSet<CmdLineOptionInstance>(specifiedOptions);
- customSpecifiedOptions.remove(helpOptionInst);
- customSpecifiedOptions.remove(actionOptionInst);
+ if (helpOptionInst != null) {
+ customSpecifiedOptions.remove(helpOptionInst);
+ }
+ if (actionOptionInst != null) {
+ customSpecifiedOptions.remove(actionOptionInst);
+ }
+ if (psaOptionInst != null) {
+ customSpecifiedOptions.remove(psaOptionInst);
+ }
this.supportedActions = supportedActions;
if(actionOptionInst != null) {
@@ -50,7 +66,7 @@ public class CmdLineArgs {
}
}
- public CmdLineOption getHelpOption() {
+ public HelpCmdLineOption getHelpOption() {
return helpOption;
}
@@ -58,7 +74,7 @@ public class CmdLineArgs {
return helpOptionInst;
}
- public CmdLineOption getActionOption() {
+ public ActionCmdLineOption getActionOption() {
return actionOption;
}
@@ -66,6 +82,14 @@ public class CmdLineArgs {
return actionOptionInst;
}
+ public PrintSupportedActionsCmdLineOption getPrintSupportedActionsOption() {
+ return psaOption;
+ }
+
+ public CmdLineOptionInstance getPrintSupportedActionsOptionInst() {
+ return psaOptionInst;
+ }
+
public Set<CmdLineOption> getSupportedOptions() {
return supportedOptions;
}
Modified: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineUtility.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineUtility.java?rev=1183144&r1=1183143&r2=1183144&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineUtility.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/CmdLineUtility.java Thu Oct 13 22:29:24 2011
@@ -9,22 +9,21 @@ import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang.Validate;
-import org.apache.oodt.cas.cl.action.CmdLineAction;
import org.apache.oodt.cas.cl.help.presenter.CmdLineOptionHelpPresenter;
import org.apache.oodt.cas.cl.help.presenter.StdCmdLineOptionHelpPresenter;
import org.apache.oodt.cas.cl.help.printer.CmdLineActionHelpPrinter;
+import org.apache.oodt.cas.cl.help.printer.CmdLineActionsHelpPrinter;
import org.apache.oodt.cas.cl.help.printer.CmdLineOptionHelpPrinter;
import org.apache.oodt.cas.cl.help.printer.StdCmdLineActionHelpPrinter;
import org.apache.oodt.cas.cl.help.printer.StdCmdLineOptionHelpPrinter;
import org.apache.oodt.cas.cl.option.ActionCmdLineOption;
import org.apache.oodt.cas.cl.option.CmdLineOption;
import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
-import org.apache.oodt.cas.cl.option.HandleableCmdLineOption;
import org.apache.oodt.cas.cl.option.HelpCmdLineOption;
-import org.apache.oodt.cas.cl.option.ValidatableCmdLineOption;
+import org.apache.oodt.cas.cl.option.PrintSupportedActionsCmdLineOption;
import org.apache.oodt.cas.cl.option.store.CmdLineOptionStore;
import org.apache.oodt.cas.cl.option.store.spring.SpringCmdLineOptionStoreFactory;
-import org.apache.oodt.cas.cl.option.validator.CmdLineOptionValidator;
+import org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils;
import org.apache.oodt.cas.cl.parser.CmdLineOptionParser;
import org.apache.oodt.cas.cl.parser.StdCmdLineOptionParser;
@@ -34,6 +33,7 @@ public class CmdLineUtility {
private CmdLineOptionStore optionStore;
private CmdLineOptionHelpPrinter optionHelpPrinter;
private CmdLineActionHelpPrinter actionHelpPrinter;
+ private CmdLineActionsHelpPrinter actionsHelpPrinter;
private CmdLineOptionHelpPresenter helpPresenter;
public CmdLineUtility() {
@@ -68,6 +68,14 @@ public class CmdLineUtility {
this.actionHelpPrinter = actionHelpPrinter;
}
+ public CmdLineActionsHelpPrinter getActionsHelpPrinter() {
+ return actionsHelpPrinter;
+ }
+
+ public void setActionsHelpPrinter(CmdLineActionsHelpPrinter actionsHelpPrinter) {
+ this.actionsHelpPrinter = actionsHelpPrinter;
+ }
+
public CmdLineOptionHelpPresenter getHelpPresenter() {
return helpPresenter;
}
@@ -84,9 +92,13 @@ public class CmdLineUtility {
helpPresenter.presentActionHelp(actionHelpPrinter.printHelp(cmdLineArgs));
}
+ public void printActionsHelp(CmdLineArgs cmdLineArgs) {
+ helpPresenter.presentActionsHelp(actionsHelpPrinter.printHelp(cmdLineArgs));
+ }
+
public void run(String[] args) throws IOException {
CmdLineArgs cmdLineArgs = parse(args);
- if (!handleHelp(cmdLineArgs)) {
+ if (!handleHelp(cmdLineArgs) && !handlePrintSupportedActions(cmdLineArgs)) {
execute(cmdLineArgs);
}
}
@@ -110,7 +122,13 @@ public class CmdLineUtility {
validOptions.add(actionOption = new ActionCmdLineOption());
}
- // Parse command line arguments.
+ // Insure print supported actions option is present if required.
+ PrintSupportedActionsCmdLineOption psaOption = CmdLineOptionUtils.findPrintSupportedActionsOption(validOptions);
+ if (psaOption == null) {
+ validOptions.add(psaOption = new PrintSupportedActionsCmdLineOption());
+ }
+
+ // Parse command line arguments.
return new CmdLineArgs(optionStore.loadSupportedActions(), validOptions, parser.parse(args, validOptions));
}
@@ -126,18 +144,26 @@ public class CmdLineUtility {
return false;
}
- public void execute(CmdLineArgs cmdLineArgs) throws IOException {
+ public boolean handlePrintSupportedActions(CmdLineArgs cmdLineArgs) throws IOException {
+ if (cmdLineArgs.getPrintSupportedActionsOptionInst() != null) {
+ printActionsHelp(cmdLineArgs);
+ return true;
+ }
+ return false;
+ }
+
+ public static void execute(CmdLineArgs cmdLineArgs) throws IOException {
Set<CmdLineOption> requiredOptionsNotSet = check(cmdLineArgs);
if (!requiredOptionsNotSet.isEmpty()) {
throw new IOException("Required options are not set: '" + requiredOptionsNotSet + "'");
}
- Set<CmdLineOptionInstance> optionsFailedValidation = validate(cmdLineArgs.getSpecifiedOptions());
+ Set<CmdLineOptionInstance> optionsFailedValidation = validate(cmdLineArgs);
if (!optionsFailedValidation.isEmpty()) {
throw new IOException("Options failed validation: '" + optionsFailedValidation + "'");
}
- handle(cmdLineArgs.getSpecifiedAction(), cmdLineArgs.getSpecifiedOptions());
+ handle(cmdLineArgs);
cmdLineArgs.getSpecifiedAction().execute();
}
@@ -151,38 +177,21 @@ public class CmdLineUtility {
return requiredOptionsNotSet;
}
- public static Set<CmdLineOptionInstance> validate(Set<CmdLineOptionInstance> options) {
- Validate.notNull(options);
+ public static Set<CmdLineOptionInstance> validate(CmdLineArgs cmdLineArgs) {
+ Validate.notNull(cmdLineArgs);
HashSet<CmdLineOptionInstance> optionsFailed = new HashSet<CmdLineOptionInstance>();
- for (CmdLineOptionInstance optionInst : options) {
- if (!validate(optionInst)) {
+ for (CmdLineOptionInstance optionInst : cmdLineArgs.getCustomSpecifiedOptions()) {
+ if (!CmdLineOptionUtils.validate(optionInst)) {
optionsFailed.add(optionInst);
}
}
return optionsFailed;
}
- public static boolean validate(CmdLineOptionInstance option) {
- if (option.isValidatable()) {
- for (CmdLineOptionValidator validator : ((ValidatableCmdLineOption) option.getOption()).getValidators()) {
- if (!validator.validate(option)) {
- return false;
- }
- }
- }
- return true;
- }
-
- public static void handle(CmdLineAction action, Set<CmdLineOptionInstance> options) {
- for (CmdLineOptionInstance option : options) {
- handle(action, option);
- }
- }
-
- public static void handle(CmdLineAction action, CmdLineOptionInstance option) {
- if (option.isHandleable()) {
- ((HandleableCmdLineOption) option.getOption()).getHandler().handleOption(action, option);
+ public static void handle(CmdLineArgs cmdLineArgs) {
+ for (CmdLineOptionInstance option : cmdLineArgs.getCustomSpecifiedOptions()) {
+ CmdLineOptionUtils.handle(cmdLineArgs.getSpecifiedAction(), option);
}
}
}
Modified: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/CmdLineOptionHelpPresenter.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/CmdLineOptionHelpPresenter.java?rev=1183144&r1=1183143&r2=1183144&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/CmdLineOptionHelpPresenter.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/CmdLineOptionHelpPresenter.java Thu Oct 13 22:29:24 2011
@@ -6,4 +6,6 @@ public interface CmdLineOptionHelpPresen
public void presentActionHelp(String actionHelpMessage);
+ public void presentActionsHelp(String actionsHelpMessage);
+
}
Modified: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/StdCmdLineOptionHelpPresenter.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/StdCmdLineOptionHelpPresenter.java?rev=1183144&r1=1183143&r2=1183144&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/StdCmdLineOptionHelpPresenter.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/presenter/StdCmdLineOptionHelpPresenter.java Thu Oct 13 22:29:24 2011
@@ -18,4 +18,7 @@ public class StdCmdLineOptionHelpPresent
ps.println(actionHelpMessage);
}
+ public void presentActionsHelp(String actionsHelpMessage) {
+ ps.println(actionsHelpMessage);
+ }
}
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineActionsHelpPrinter.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineActionsHelpPrinter.java?rev=1183144&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineActionsHelpPrinter.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineActionsHelpPrinter.java Thu Oct 13 22:29:24 2011
@@ -0,0 +1,9 @@
+package org.apache.oodt.cas.cl.help.printer;
+
+import org.apache.oodt.cas.cl.CmdLineArgs;
+
+public interface CmdLineActionsHelpPrinter {
+
+ public String printHelp(CmdLineArgs cmdLineArgs);
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/CmdLineActionsHelpPrinter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineActionsHelpPrinter.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineActionsHelpPrinter.java?rev=1183144&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineActionsHelpPrinter.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineActionsHelpPrinter.java Thu Oct 13 22:29:24 2011
@@ -0,0 +1,16 @@
+package org.apache.oodt.cas.cl.help.printer;
+
+import org.apache.oodt.cas.cl.CmdLineArgs;
+import org.apache.oodt.cas.cl.action.CmdLineAction;
+
+public class StdCmdLineActionsHelpPrinter implements CmdLineActionsHelpPrinter {
+
+ public String printHelp(CmdLineArgs cmdLineArgs) {
+ CmdLineAction action = cmdLineArgs.getSpecifiedAction();
+ return
+ "Actions:\n" +
+ " Action:\n Name: " + action.getName() + "\n" +
+ " Description: " + action.getDescription() + "\n";
+ }
+
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/help/printer/StdCmdLineActionsHelpPrinter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/PrintSupportedActionsCmdLineOption.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/PrintSupportedActionsCmdLineOption.java?rev=1183144&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/PrintSupportedActionsCmdLineOption.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/PrintSupportedActionsCmdLineOption.java Thu Oct 13 22:29:24 2011
@@ -0,0 +1,13 @@
+package org.apache.oodt.cas.cl.option;
+
+public class PrintSupportedActionsCmdLineOption extends SimpleCmdLineOption {
+
+ public PrintSupportedActionsCmdLineOption() {
+ super("psa", "printSupportedActions", "Print Supported Actions", false);
+ }
+
+ public PrintSupportedActionsCmdLineOption(String shortOption, String longOption,
+ String description, boolean hasArgs) {
+ super(shortOption, longOption, description, hasArgs);
+ }
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/PrintSupportedActionsCmdLineOption.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java?rev=1183144&r1=1183143&r2=1183144&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/CmdLineOptionBeanHandler.java Thu Oct 13 22:29:24 2011
@@ -17,6 +17,8 @@
package org.apache.oodt.cas.cl.option.handler;
//JDK imports
+import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.convertToType;
+
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
@@ -95,53 +97,4 @@ public class CmdLineOptionBeanHandler im
}
return "Affects: " + affectedClasses.toString();
}
-
- private Object[] convertToType(List<String> values, Class<?> type)
- throws MalformedURLException, ClassNotFoundException {
- if (type.equals(File.class)) {
- List<Object> files = new LinkedList<Object>();
- for (String value : values)
- files.add(new File(value));
- return files.toArray(new Object[files.size()]);
- } else if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) {
- List<Object> booleans = new LinkedList<Object>();
- for (String value : values)
- booleans.add(value.toLowerCase().trim().equals("true"));
- return booleans.toArray(new Object[booleans.size()]);
- } else if (type.equals(URL.class)) {
- List<Object> urls = new LinkedList<Object>();
- for (String value : values)
- urls.add(new URL(value));
- return urls.toArray(new Object[urls.size()]);
- } else if (type.equals(Class.class)) {
- List<Object> classes = new LinkedList<Object>();
- for (String value : values)
- classes.add(Class.forName(value));
- return classes.toArray(new Object[classes.size()]);
- } else if (type.equals(List.class)) {
- return new Object[] { values };
- } else if (type.equals(Integer.class) || type.equals(Integer.TYPE)) {
- List<Object> ints = new LinkedList<Object>();
- for (String value : values)
- ints.add(new Integer(value));
- return ints.toArray(new Object[ints.size()]);
- } else if (type.equals(Long.class) || type.equals(Long.TYPE)) {
- List<Object> longs = new LinkedList<Object>();
- for (String value : values)
- longs.add(new Long(value));
- return longs.toArray(new Object[longs.size()]);
- } else if (type.equals(Double.class) || type.equals(Double.TYPE)) {
- List<Object> doubles = new LinkedList<Object>();
- for (String value : values)
- doubles.add(new Double(value));
- return doubles.toArray(new Object[doubles.size()]);
- } else if (type.equals(String.class)) {
- StringBuffer combinedString = new StringBuffer("");
- for (String value : values)
- combinedString.append(value + " ");
- return new String[] { combinedString.toString().trim() };
- } else {
- return values.toArray(new Object[values.size()]);
- }
- }
}
Added: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/StdCmdLineOptionHandler.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/StdCmdLineOptionHandler.java?rev=1183144&view=auto
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/StdCmdLineOptionHandler.java (added)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/StdCmdLineOptionHandler.java Thu Oct 13 22:29:24 2011
@@ -0,0 +1,46 @@
+package org.apache.oodt.cas.cl.option.handler;
+
+import static org.apache.oodt.cas.cl.option.util.CmdLineOptionUtils.convertToType;
+
+import java.util.Arrays;
+import java.util.Map;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.oodt.cas.cl.action.CmdLineAction;
+import org.apache.oodt.cas.cl.option.CmdLineOption;
+import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
+
+public class StdCmdLineOptionHandler {
+
+ private Map<String, String> actionToMethodMap;
+
+ public void setActionToMethodMap(Map<String, String> actionToMethodMap) {
+ this.actionToMethodMap = actionToMethodMap;
+ }
+
+ public void handleOption(CmdLineAction action, CmdLineOptionInstance optionInstance) {
+ try {
+ Class<?> type = optionInstance.getOption().getType();
+ Object[] vals = (optionInstance.getValues().isEmpty()) ? convertToType(
+ Arrays.asList(new String[] { "true" }), type = Boolean.TYPE)
+ : convertToType(optionInstance.getValues(), type);
+ if (actionToMethodMap != null && actionToMethodMap.containsKey(action.getName())) {
+ action.getClass()
+ .getMethod(actionToMethodMap.get(action.getName()), type)
+ .invoke(action, vals);
+ } else {
+ action
+ .getClass()
+ .getMethod(
+ "set" + StringUtils.capitalize(optionInstance.getOption().getLongOption()), type)
+ .invoke(action, vals);
+ }
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public String getHelp(CmdLineOption option) {
+ return "Will invoke 'set" + option.getLongOption() + "' on action selected";
+ }
+}
Propchange: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/handler/StdCmdLineOptionHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/CmdLineOptionUtils.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/CmdLineOptionUtils.java?rev=1183144&r1=1183143&r2=1183144&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/CmdLineOptionUtils.java (original)
+++ oodt/branches/cas-cl/src/main/java/org/apache/oodt/cas/cl/option/util/CmdLineOptionUtils.java Thu Oct 13 22:29:24 2011
@@ -18,10 +18,14 @@
package org.apache.oodt.cas.cl.option.util;
//JDK imports
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
+import java.util.LinkedList;
import java.util.List;
import java.util.Set;
@@ -33,9 +37,13 @@ import org.apache.oodt.cas.cl.option.Adv
import org.apache.oodt.cas.cl.option.CmdLineOption;
import org.apache.oodt.cas.cl.option.CmdLineOptionInstance;
import org.apache.oodt.cas.cl.option.GroupCmdLineOption;
+import org.apache.oodt.cas.cl.option.HandleableCmdLineOption;
import org.apache.oodt.cas.cl.option.HelpCmdLineOption;
+import org.apache.oodt.cas.cl.option.PrintSupportedActionsCmdLineOption;
+import org.apache.oodt.cas.cl.option.ValidatableCmdLineOption;
import org.apache.oodt.cas.cl.option.require.RequirementRule;
import org.apache.oodt.cas.cl.option.require.RequirementRule.Relation;
+import org.apache.oodt.cas.cl.option.validator.CmdLineOptionValidator;
import org.apache.oodt.cas.cl.action.CmdLineAction;
/**
@@ -102,7 +110,7 @@ public class CmdLineOptionUtils {
public static Set<CmdLineOption> getRequiredOptions(Set<CmdLineOption> options) {
HashSet<CmdLineOption> requiredOptions = new HashSet<CmdLineOption>();
for (CmdLineOption option : options) {
- if (option.isRequired()) {
+ if (!(option instanceof ActionCmdLineOption) && option.isRequired()) {
requiredOptions.add(option);
}
}
@@ -183,6 +191,28 @@ public class CmdLineOptionUtils {
return null;
}
+ public static boolean isPrintSupportedActionsOption(CmdLineOption option) {
+ return option instanceof PrintSupportedActionsCmdLineOption;
+ }
+
+ public static PrintSupportedActionsCmdLineOption findPrintSupportedActionsOption(Set<CmdLineOption> options) {
+ for (CmdLineOption option : options) {
+ if (isPrintSupportedActionsOption(option)) {
+ return (PrintSupportedActionsCmdLineOption) option;
+ }
+ }
+ return null;
+ }
+
+ public static CmdLineOptionInstance findSpecifiedPrintSupportedActionsOption(Set<CmdLineOptionInstance> options) {
+ for (CmdLineOptionInstance option : options) {
+ if (isPrintSupportedActionsOption(option.getOption())) {
+ return option;
+ }
+ }
+ return null;
+ }
+
public static boolean isActionOption(CmdLineOption option) {
return option instanceof ActionCmdLineOption;
}
@@ -242,6 +272,23 @@ public class CmdLineOptionUtils {
return null;
}
+ public static boolean validate(CmdLineOptionInstance option) {
+ if (option.isValidatable()) {
+ for (CmdLineOptionValidator validator : ((ValidatableCmdLineOption) option.getOption()).getValidators()) {
+ if (!validator.validate(option)) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ public static void handle(CmdLineAction action, CmdLineOptionInstance option) {
+ if (option.isHandleable()) {
+ ((HandleableCmdLineOption) option.getOption()).getHandler().handleOption(action, option);
+ }
+ }
+
public static String getFormattedString(String string, int startIndex,
int endIndex) {
StringBuffer outputString = new StringBuffer("");
@@ -260,4 +307,53 @@ public class CmdLineOptionUtils {
}
return outputString.toString();
}
+
+ public static Object[] convertToType(List<String> values, Class<?> type)
+ throws MalformedURLException, ClassNotFoundException {
+ if (type.equals(File.class)) {
+ List<Object> files = new LinkedList<Object>();
+ for (String value : values)
+ files.add(new File(value));
+ return files.toArray(new Object[files.size()]);
+ } else if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) {
+ List<Object> booleans = new LinkedList<Object>();
+ for (String value : values)
+ booleans.add(value.toLowerCase().trim().equals("true"));
+ return booleans.toArray(new Object[booleans.size()]);
+ } else if (type.equals(URL.class)) {
+ List<Object> urls = new LinkedList<Object>();
+ for (String value : values)
+ urls.add(new URL(value));
+ return urls.toArray(new Object[urls.size()]);
+ } else if (type.equals(Class.class)) {
+ List<Object> classes = new LinkedList<Object>();
+ for (String value : values)
+ classes.add(Class.forName(value));
+ return classes.toArray(new Object[classes.size()]);
+ } else if (type.equals(List.class)) {
+ return new Object[] { values };
+ } else if (type.equals(Integer.class) || type.equals(Integer.TYPE)) {
+ List<Object> ints = new LinkedList<Object>();
+ for (String value : values)
+ ints.add(new Integer(value));
+ return ints.toArray(new Object[ints.size()]);
+ } else if (type.equals(Long.class) || type.equals(Long.TYPE)) {
+ List<Object> longs = new LinkedList<Object>();
+ for (String value : values)
+ longs.add(new Long(value));
+ return longs.toArray(new Object[longs.size()]);
+ } else if (type.equals(Double.class) || type.equals(Double.TYPE)) {
+ List<Object> doubles = new LinkedList<Object>();
+ for (String value : values)
+ doubles.add(new Double(value));
+ return doubles.toArray(new Object[doubles.size()]);
+ } else if (type.equals(String.class)) {
+ StringBuffer combinedString = new StringBuffer("");
+ for (String value : values)
+ combinedString.append(value + " ");
+ return new String[] { combinedString.toString().trim() };
+ } else {
+ return values.toArray(new Object[values.size()]);
+ }
+ }
}
Modified: oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/option/util/TestCmdLineOptionUtils.java
URL: http://svn.apache.org/viewvc/oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/option/util/TestCmdLineOptionUtils.java?rev=1183144&r1=1183143&r2=1183144&view=diff
==============================================================================
--- oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/option/util/TestCmdLineOptionUtils.java (original)
+++ oodt/branches/cas-cl/src/test/org/apache/oodt/cas/cl/option/util/TestCmdLineOptionUtils.java Thu Oct 13 22:29:24 2011
@@ -18,19 +18,62 @@ import org.apache.oodt.cas.cl.option.req
public class TestCmdLineOptionUtils extends TestCase {
+ public void testDetermineRequired() {
+ CmdLineAction action = createAction("TestAction");
+ ActionCmdLineOption actionOption = createActionOption("operation");
+
+ HashSet<CmdLineOption> options = new HashSet<CmdLineOption>();
+ options.add(createSimpleOption("url", createRequiredRequirementRule(action)));
+ options.add(createSimpleOption("pass", false));
+ options.add(createSimpleOption("user", false));
+ options.add(actionOption);
+
+ Set<CmdLineOption> requiredOptions = CmdLineOptionUtils.determineRequired(action, options);
+ assertEquals(1, requiredOptions.size());
+ assertNotNull(CmdLineOptionUtils.getOptionByName("url", requiredOptions));
+
+ options = new HashSet<CmdLineOption>();
+ options.add(createSimpleOption("url", createRequiredRequirementRule(action)));
+ options.add(createSimpleOption("pass", true));
+ options.add(createSimpleOption("user", false));
+ options.add(actionOption);
+
+ requiredOptions = CmdLineOptionUtils.determineRequired(action, options);
+ assertEquals(2, requiredOptions.size());
+ assertNotNull(CmdLineOptionUtils.getOptionByName("url", requiredOptions));
+ assertNotNull(CmdLineOptionUtils.getOptionByName("pass", requiredOptions));
+ }
+
public void testDetermineOptional() {
CmdLineAction action = createAction("TestAction");
- SimpleCmdLineOption urlOption = createSimpleOption("url", createRequiredRequirementRule(action));
- ActionCmdLineOption operationOption = createActionOption("operation");
+ ActionCmdLineOption actionOption = new ActionCmdLineOption();
+
HashSet<CmdLineOption> options = new HashSet<CmdLineOption>();
- options.add(urlOption);
+ options.add(createSimpleOption("url", createRequiredRequirementRule(action)));
options.add(createSimpleOption("pass", false));
options.add(createSimpleOption("user", false));
- options.add(operationOption);
+ options.add(actionOption);
Set<CmdLineOption> optionalOptions = CmdLineOptionUtils.determineOptional(action, options);
- System.out.println(optionalOptions);
assertEquals(0, optionalOptions.size());
+
+ options = new HashSet<CmdLineOption>();
+ options.add(createSimpleOption("pass", true));
+ options.add(createSimpleOption("user", true));
+ options.add(actionOption);
+
+ optionalOptions = CmdLineOptionUtils.determineOptional(action, options);
+ assertEquals(0, optionalOptions.size());
+
+ options = new HashSet<CmdLineOption>();
+ options.add(createSimpleOption("pass", createOptionalRequirementRule(action)));
+ options.add(createSimpleOption("user", createOptionalRequirementRule(action)));
+ options.add(actionOption);
+
+ optionalOptions = CmdLineOptionUtils.determineOptional(action, options);
+ assertEquals(2, optionalOptions.size());
+ assertNotNull(CmdLineOptionUtils.getOptionByName("pass", optionalOptions));
+ assertNotNull(CmdLineOptionUtils.getOptionByName("user", optionalOptions));
}
private static CmdLineAction createAction(String name) {
@@ -43,6 +86,7 @@ public class TestCmdLineOptionUtils exte
};
}
+
private static GroupCmdLineOption createGroupOption(String longName, boolean required) {
GroupCmdLineOption option = new GroupCmdLineOption();
option.setLongOption(longName);
@@ -79,4 +123,10 @@ public class TestCmdLineOptionUtils exte
rule.addActionDependency(new ActionDependency(action.getName(), Relation.REQUIRED));
return rule;
}
+
+ private static RequirementRule createOptionalRequirementRule(CmdLineAction action) {
+ StdRequirementRule rule = new StdRequirementRule();
+ rule.addActionDependency(new ActionDependency(action.getName(), Relation.OPTIONAL));
+ return rule;
+ }
}