Revision: 5475 http://jnode.svn.sourceforge.net/jnode/?rev=5475&view=rev Author: chrisboertien Date: 2009-05-11 17:38:32 +0000 (Mon, 11 May 2009)
Log Message: ----------- Bare commands implementation This commit includes the relevant changes to the shell code to support the exeuction and completion of bare commands Signed-off-by: chrisboertien <chris.boert...@gmail.com> Modified Paths: -------------- trunk/shell/src/shell/org/jnode/shell/CommandInfo.java trunk/shell/src/shell/org/jnode/shell/CommandLine.java trunk/shell/src/shell/org/jnode/shell/CommandRunner.java trunk/shell/src/shell/org/jnode/shell/CommandShell.java Modified: trunk/shell/src/shell/org/jnode/shell/CommandInfo.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/CommandInfo.java 2009-05-11 17:37:40 UTC (rev 5474) +++ trunk/shell/src/shell/org/jnode/shell/CommandInfo.java 2009-05-11 17:38:32 UTC (rev 5475) @@ -32,10 +32,11 @@ * @author craw...@jnode.org */ public final class CommandInfo { - + private final Class<?> clazz; private final String commandName; private final SyntaxBundle syntaxBundle; + private final ArgumentBundle argBundle; private final boolean internal; private Command instance; @@ -45,7 +46,27 @@ this.internal = internal; this.commandName = commandName; this.syntaxBundle = syntaxBundle; + this.argBundle = null; } + + /** + * Creates a CommandInfo object for a bare command. + * + * If the bare command has a defined {...@code ArgumentBundle} then + * it will be used when parsing the command line. + * + * @param clazz the designated {...@code Class} for executing the command + * @param commandName the name, or alias, for the command + * @param syntaxBundle the syntax definition to parse the command line against + * @param argBundle the optional {...@code ArgumentBundle} to parse the command line against + */ + public CommandInfo(Class<?> clazz, String commandName, SyntaxBundle syntaxBundle, ArgumentBundle argBundle) { + this.clazz = clazz; + this.internal = false; + this.commandName = commandName; + this.syntaxBundle = syntaxBundle; + this.argBundle = argBundle; + } public final Class<?> getCommandClass() { return clazz; @@ -54,6 +75,19 @@ public final boolean isInternal() { return internal; } + + public final ArgumentBundle getArgumentBundle() { + if (argBundle == null) { + if (Command.class.isAssignableFrom(clazz)) { + try { + return ((Command) clazz.newInstance()).getArgumentBundle(); + } catch (Exception e) { + // fall through + } + } + } + return argBundle; + } /** * Get the Command instance for this CommandInfo, instantiating it if necessary. @@ -90,17 +124,24 @@ * parses against the Syntax, binding the command arguments to Argument objects * in an ArgumentBundle object obtained from the Command object. * - * @param shell the context for resolving command aliases and locating syntaxes + * If this CommandInfo object was created from a bare command, then the argBundle + * will have been previously supplied to parse against. + * + * @param cmdLine the command line containing the tokens to parse against the argument bundle. * @throws CommandSyntaxException if the chosen syntax doesn't match the command * line arguments. * @throws ShellException for problems instantiating the command class. */ public void parseCommandLine(CommandLine cmdLine) throws ShellException { try { - Command command = createCommandInstance(); - - // Get the command's argument bundle, or the default one. - ArgumentBundle bundle = (command == null) ? null : command.getArgumentBundle(); + ArgumentBundle bundle = argBundle; + if (bundle == null) { + Command cmd = createCommandInstance(); + if (cmd != null) { + bundle = cmd.getArgumentBundle(); + } + } + if (bundle != null) { // Do a full parse to bind the command line argument tokens to corresponding // command arguments Modified: trunk/shell/src/shell/org/jnode/shell/CommandLine.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/CommandLine.java 2009-05-11 17:37:40 UTC (rev 5474) +++ trunk/shell/src/shell/org/jnode/shell/CommandLine.java 2009-05-11 17:38:32 UTC (rev 5475) @@ -594,7 +594,7 @@ } catch (ShellException ex) { throw new CompletionException(ex.getMessage(), ex); } - + Command command; try { command = cmdClass.createCommandInstance(); @@ -603,7 +603,9 @@ } // Get the command's argument bundle and syntax - ArgumentBundle bundle = (command == null) ? null : command.getArgumentBundle(); + ArgumentBundle bundle = (command == null) + ? cmdClass.getArgumentBundle() + : command.getArgumentBundle(); SyntaxBundle syntaxes = shell.getSyntaxManager().getSyntaxBundle(cmd); if (bundle == null) { Modified: trunk/shell/src/shell/org/jnode/shell/CommandRunner.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/CommandRunner.java 2009-05-11 17:37:40 UTC (rev 5474) +++ trunk/shell/src/shell/org/jnode/shell/CommandRunner.java 2009-05-11 17:38:32 UTC (rev 5475) @@ -117,6 +117,10 @@ } catch (Exception ex) { throw new ShellInvocationException("Problem while creating command instance", ex); } + // make this happen before setting up the main() method call for + // bare commands. For commands without a bare command definition + // this will return silently having done nothing. + cmdInfo.parseCommandLine(commandLine); if (command == null) { try { method = cmdInfo.getCommandClass().getMethod(MAIN_METHOD, MAIN_ARG_TYPES); @@ -138,8 +142,6 @@ throw new ShellInvocationException( "No entry point method found for " + cmdInfo.getCommandClass()); } - } else { - cmdInfo.parseCommandLine(commandLine); } } Modified: trunk/shell/src/shell/org/jnode/shell/CommandShell.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/CommandShell.java 2009-05-11 17:37:40 UTC (rev 5474) +++ trunk/shell/src/shell/org/jnode/shell/CommandShell.java 2009-05-11 17:38:32 UTC (rev 5475) @@ -666,9 +666,15 @@ SyntaxBundle syntaxBundle = getSyntaxManager().getSyntaxBundle(cmd); try { Class<?> cls = aliasMgr.getAliasClass(cmd); - return new CommandInfo(cls, cmd, syntaxBundle, aliasMgr.isInternal(cmd)); + if (Command.class.isAssignableFrom(cls)) { + return new CommandInfo(cls, cmd, syntaxBundle, aliasMgr.isInternal(cmd)); + } else { + // check if this alias has a bare command definition + ArgumentBundle argBundle = getSyntaxManager().getArgumentBundle(cmd); + return new CommandInfo(cls, cmd, syntaxBundle, argBundle); + } } catch (ClassNotFoundException ex) { - throw new ShellException("Cannot the load command class for alias '" + cmd + "'", ex); + throw new ShellException("Cannot load the command class for alias '" + cmd + "'", ex); } catch (NoSuchAliasException ex) { try { final ClassLoader cl = @@ -682,15 +688,7 @@ } protected ArgumentBundle getCommandArgumentBundle(CommandInfo commandInfo) { - if (Command.class.isAssignableFrom(commandInfo.getCommandClass())) { - try { - Command cmd = (Command) (commandInfo.getCommandClass().newInstance()); - return cmd.getArgumentBundle(); - } catch (Exception ex) { - // drop through - } - } - return null; + return commandInfo.getArgumentBundle(); } boolean isDebugEnabled() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your production scanning environment may not be a perfect world - but thanks to Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700 Series Scanner you'll get full speed at 300 dpi even with all image processing features enabled. http://p.sf.net/sfu/kodak-com _______________________________________________ Jnode-svn-commits mailing list Jnode-svn-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jnode-svn-commits