package org.opennaas.extensions.queuemanager.shell;
import java.util.List;
import org.apache.felix.gogo.commands.Argument;
import org.apache.felix.gogo.commands.Command;
import org.opennaas.core.resources.IResource;
import org.opennaas.core.resources.IResourceIdentifier;
import org.opennaas.core.resources.IResourceManager;
import org.opennaas.core.resources.action.IAction;
import org.opennaas.core.resources.shell.GenericKarafCommand;
import org.opennaas.extensions.queuemanager.IQueueManagerCapability;
import org.opennaas.extensions.queuemanager.QueueManager;
@Command(scope = "queue", name = "listActions", description = "List all actions included in the queue.")
public class ListActionsCommand extends GenericKarafCommand {
@Argument(index = 0, name = "resourceType:resourceName", description = "Name of the resource owning the queue", required = true, multiValued = false)
private String resourceId;
@Override
protected Object doExecute() throws Exception {
printInitCommand("List actions in the queue");
try {
IResourceManager manager = getResourceManager();
String[] argsRouterName = new String[2];
try {
argsRouterName = splitResourceName(resourceId);
} catch (Exception e) {
printError(e.getMessage());
printEndCommand();
return -1;
}
IResourceIdentifier resourceIdentifier = null;
resourceIdentifier = manager.getIdentifierFromResourceName(argsRouterName[0], argsRouterName[1]);
/* validate resource identifier */
if (resourceIdentifier == null) {
printError("Could not get resource with name: " + argsRouterName[0] + ":" + argsRouterName[1]);
printEndCommand();
return -1;
}
IResource resource = manager.getResource(resourceIdentifier);
validateResource(resource);
IQueueManagerCapability queue = (IQueueManagerCapability) getCapability(resource.getCapabilities(), QueueManager.QUEUE);
if (queue == null) {
printError("Could not found capability " + QueueManager.QUEUE + " in resource " + resourceId);
return -1;
}
List<IAction> listActions = queue.getActions();
String[] titles = { "num", "actionID" };
String[][] matrix = new String[listActions.size()][2];
int num = 0;
for (IAction action : listActions) {
String actionID = action.getActionID();
String params[] = { Integer.toString(num), actionID };
matrix[num] = params;
num++;
}
printSymbol("Actions in queue of resource " + resourceId + ":");
printTable(titles, matrix, -1);
} catch (Exception e) {
printError("Error getting queue.");
printError(e);
printEndCommand();
return -1;
}
printEndCommand();
return null;
}
}