Skip to content

Commit

Permalink
cells: Document and rename onerror commands
Browse files Browse the repository at this point in the history
Motivation:

Provide docmentation for cell commands.

Modification:

Renames show onexit to show onerror and marks show onexit as deprecated.

Extend command help printer to recognize deprecated commands. Deprecated
commands are not included in the command list, but the full help information
is available. For annotated commands the help output warns that the command
is deprecated.

Refactors the onerror state of the command interpreter to use an enum
instead of a string.

Result:

show onexit is deprecated; use show onerror instead. Better cell command
documentation.

Target: trunk
Require-notes: yes
Require-book: yes
Acked-by: Paul Millar <paul.millar@desy.de>
Patch: https://rb.dcache.org/r/9109/
  • Loading branch information
gbehrmann committed Mar 21, 2016
1 parent c4d4e4f commit d3c4cf0
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 30 deletions.
91 changes: 63 additions & 28 deletions modules/cells/src/main/java/dmg/cells/nucleus/CellShell.java
Expand Up @@ -82,6 +82,11 @@ public class CellShell extends CommandInterpreter
private static final Logger _logNucleus =
LoggerFactory.getLogger(CellNucleus.class);

enum ErrorAction
{
SHUTDOWN, EXIT, CONTINUE
}

private final CellNucleus _nucleus ;
private StringBuilder _contextString;
private String _contextName;
Expand All @@ -92,7 +97,7 @@ public class CellShell extends CommandInterpreter
private int _helpMode = 1 ;
private int _errorCode;
private String _errorMsg;
private String _doOnExit;
private ErrorAction _doOnError = ErrorAction.CONTINUE;
private final Map<String, Object> _environment =
new ConcurrentHashMap<>();
private CommandInterpreter _externalInterpreter;
Expand Down Expand Up @@ -276,15 +281,12 @@ public Object objectCommand( String strin ) throws CommandExitException {
_errorCode = ce.getErrorCode() ;
_errorMsg = ce.getErrorMessage() ;

if( _doOnExit != null ){
if( _doOnExit.equals( "shutdown" ) ) {
throw new CommandExitException(ce.toString(), 666);
} else {
throw new CommandExitException(ce.getErrorMessage(),
ce.getErrorCode());
}

}
switch (_doOnError) {
case SHUTDOWN:
throw new CommandExitException(ce.toString(), 666);
case EXIT:
throw new CommandExitException(ce.getErrorMessage(), ce.getErrorCode());
}
if( ce instanceof CommandSyntaxException ){
CommandSyntaxException cse = (CommandSyntaxException)ce ;

Expand Down Expand Up @@ -998,22 +1000,55 @@ public String call() throws ClassNotFoundException, NoSuchMethodException,
}
}

////////////////////////////////////////////////////////////
//
// this and that
//
public static final String hh_onerror = "shutdown|exit|continue" ;
public String ac_onerror_$_1( Args args ){
if( args.argv(0).equals( "continue" ) ) {
_doOnExit = null;
} else {
_doOnExit = args.argv(0);
}
return "" ;
}
public String ac_show_onexit( Args args ){
return _doOnExit != null ? _doOnExit : "" ;
}
////////////////////////////////////////////////////////////
//
// this and that
//
@Command(name = "onerror", hint = "set error action",
description = "Defines how the command interpreter reacts to processing errors.")
class OnErrorCommand implements Callable<String>
{
@Argument(valueSpec = "shutdown|exit|continue",
usage = "shutdown:\n" +
"\tterminate dCache domain.\n" +
"exit:\n" +
"\tterminate interpreter.\n" +
"continue:\n" +
"\tignore error.")
ErrorAction action;

@Override
public String call()
{
_doOnError = action;
return "";
}
}

@Command(name = "show onexit", hint = "show current error action",
description = "Shows how the command interpreter reacts to errors. The action can " +
"be set using the onerror command.")
@Deprecated
class ShowOnExitCommand implements Callable<String>
{
@Override
public String call()
{
return _doOnError.toString().toLowerCase();
}
}

@Command(name = "show onerror", hint = "show current error action",
description = "Shows how the command interpreter reacts to errors. The action can " +
"be set using the onerror command.")
class ShowOnErrorCommand implements Callable<String>
{
@Override
public String call()
{
return _doOnError.toString().toLowerCase();
}
}


private static final int PRINT_CELL = 1;
Expand Down Expand Up @@ -1631,11 +1666,11 @@ public void execute(String source, Reader in, Writer out, Writer err, Args args)
"information.", error.getCause());
}

if (_doOnExit != null) {
if (_doOnError != ErrorAction.CONTINUE) {
String msg =
String.format("%s: line %d: %s", source, no,
error.getMessage());
if (_doOnExit.equals("shutdown")) {
if (_doOnError == ErrorAction.SHUTDOWN) {
throw new CommandExitException(msg, 666, error);
} else if (error instanceof CommandException) {
int rc = ((CommandException) error).getErrorCode();
Expand Down
Expand Up @@ -29,17 +29,25 @@ class AcCommandExecutor implements CommandExecutor
private Field _fullHelp;
private Field _helpHint;
private Field _acls;
private boolean _isDeprecated;

public AcCommandExecutor(Object listener)
{
_listener = listener;
}

@Override
public boolean isDeprecated()
{
return _isDeprecated;
}

public void setMethod(Method m, int mn, int mx)
{
_method = m;
_minArgs = mn;
_maxArgs = mx;
_isDeprecated = m.getDeclaredAnnotation(Deprecated.class) != null;
}

public void setFullHelpField(Field f) {
Expand Down
Expand Up @@ -262,6 +262,11 @@ public String getHelp(Object instance)
writer.append(Strings.wrap(" ", literal(command.name()) + " " + getSignature(clazz), WIDTH));
writer.println();

if (clazz.getDeclaredAnnotation(Deprecated.class) != null) {
writer.append(Strings.wrap(" ", "This command is deprecated and will be removed in a future release.", WIDTH));
writer.println();
}

if (!command.description().isEmpty()) {
writer.println(heading("DESCRIPTION"));
writer.append(Strings.wrap(" ", command.description(), WIDTH));
Expand Down
Expand Up @@ -68,14 +68,23 @@ public Integer apply(Handler handler)
private final Command _command;
private final Constructor<? extends Callable<? extends Serializable>> _constructor;
private final List<Handler> _handlers;
private final boolean _isDeprecated;

public AnnotatedCommandExecutor(Object parent, Command command,
Constructor<? extends Callable<? extends Serializable>> constructor)
{
_parent = parent;
_command = command;
_constructor = constructor;
_handlers = createFieldHandlers(command, _constructor.getDeclaringClass());
Class<? extends Callable<? extends Serializable>> commandClass = _constructor.getDeclaringClass();
_handlers = createFieldHandlers(command, commandClass);
_isDeprecated = commandClass.getDeclaredAnnotation(Deprecated.class) != null;
}

@Override
public boolean isDeprecated()
{
return _isDeprecated;
}

@Override
Expand Down
Expand Up @@ -13,6 +13,11 @@
*/
public interface CommandExecutor
{
/**
* Returns true if and only if the command is marked as deprecated.
*/
boolean isDeprecated();

/**
* Returns true if and only if the command has any ACLs.
*/
Expand Down
Expand Up @@ -246,7 +246,7 @@ public boolean hasACLs()

public void dumpHelpHint(String top, StringBuilder sb, HelpFormat format)
{
if (_commandExecutor != null) {
if (_commandExecutor != null && !_commandExecutor.isDeprecated()) {
String hint = _commandExecutor.getHelpHint(format);
if (hint != null) {
sb.append(top).append(hint).append("\n");
Expand Down

0 comments on commit d3c4cf0

Please sign in to comment.