package org.opennaas.core.resources.shell;
import java.util.List;
import org.apache.felix.gogo.commands.Argument;
import org.apache.felix.gogo.commands.Command;
import org.opennaas.core.resources.IResourceIdentifier;
import org.opennaas.core.resources.IncorrectLifecycleStateException;
import org.opennaas.core.resources.ResourceException;
import org.opennaas.core.resources.ResourceManager;
/**
* Start one or more resources
*
* @author Scott Campbell (CRC)
*
*/
@Command(scope = "resource", name = "start", description = "Start one or more resources")
public class StartResourceCommand extends GenericKarafCommand {
@Argument(index = 0, name = "resourceType:resourceName", description = "A space delimited list of resource type:name to be started", required = true, multiValued = true)
private List<String> resourceIDs;
@Override
protected Object doExecute() throws Exception {
printInitCommand("resource start");
int counter = 0;
try {
ResourceManager manager = (ResourceManager) getResourceManager();
for (String id : resourceIDs) {
String[] argsRouterName = new String[2];
try {
argsRouterName = splitResourceName(id);
} catch (Exception e) {
printError(e.getMessage());
printEndCommand();
return -1;
}
IResourceIdentifier identifier = null;
try {
identifier = manager.getIdentifierFromResourceName(argsRouterName[0], argsRouterName[1]);
if (identifier != null) {
// printInfo("Starting Resource: "+ argsRouterName[1]);
manager.startResource(identifier);
counter++;
printInfo("Resource " + id + " started.");
} else {
printError("Resource " + id +
" not found on repository.");
}
} catch (ResourceException e) {
if (e.getCause() instanceof IncorrectLifecycleStateException)
printError("Cannot start resource " + id + " from state: " + ((IncorrectLifecycleStateException) e.getCause())
.getResourceState());
else
printError("Cannot start resource " + id + ": ", e);
}
// printSymbol(horizontalSeparator);
}
printInfo("Started " + counter + " resource/s of " + resourceIDs.size());
} catch (Exception e) {
printError("An error occurred starting the resource.");
printError(e);
}
printEndCommand();
return null;
}
}