use of org.eclipse.kapua.app.console.shared.model.GwtDeviceCommandOutput in project kapua by eclipse.
the class GwtDeviceManagementServiceImpl method executeCommand.
//
// Command
//
@Override
public GwtDeviceCommandOutput executeCommand(GwtXSRFToken xsrfToken, GwtDevice gwtDevice, GwtDeviceCommandInput gwtCommandInput) throws GwtKapuaException {
//
// Checking validity of the given XSRF Token
checkXSRFToken(xsrfToken);
GwtDeviceCommandOutput gwtCommandOutput = new GwtDeviceCommandOutput();
try {
// execute the command
KapuaLocator locator = KapuaLocator.getInstance();
DeviceCommandManagementService deviceCommandManagementService = locator.getService(DeviceCommandManagementService.class);
DeviceCommandFactory deviceCommandFactory = locator.getFactory(DeviceCommandFactory.class);
StringTokenizer st = new StringTokenizer(gwtCommandInput.getCommand());
int count = st.countTokens();
String command = count > 0 ? st.nextToken() : null;
String[] args = count > 1 ? new String[count - 1] : null;
int i = 0;
while (st.hasMoreTokens()) {
args[i++] = st.nextToken();
}
DeviceCommandInput commandInput = deviceCommandFactory.newCommandInput();
// commandInput.setArguments(gwtCommandInput.getArguments());
commandInput.setArguments(args);
// commandInput.setCommand(gwtCommandInput.getCommand());
commandInput.setCommand(command);
commandInput.setEnvironment(gwtCommandInput.getEnvironment());
commandInput.setRunAsynch(gwtCommandInput.isRunAsynch() != null ? gwtCommandInput.isRunAsynch().booleanValue() : false);
commandInput.setStdin(gwtCommandInput.getStdin());
commandInput.setTimeout(gwtCommandInput.getTimeout() != null ? gwtCommandInput.getTimeout().intValue() : 0);
commandInput.setWorkingDir(gwtCommandInput.getWorkingDir());
commandInput.setBody(gwtCommandInput.getZipBytes());
KapuaId scopeId = KapuaEid.parseShortId(gwtDevice.getScopeId());
KapuaId deviceId = KapuaEid.parseShortId(gwtDevice.getId());
DeviceCommandOutput commandOutput = deviceCommandManagementService.exec(scopeId, deviceId, commandInput, null);
if (commandOutput.getExceptionMessage() != null) {
gwtCommandOutput.setExceptionMessage(commandOutput.getExceptionMessage().replace("\n", "<br>"));
}
if (commandOutput.getExceptionStack() != null) {
gwtCommandOutput.setExceptionStack(commandOutput.getExceptionStack().replace("\n", "<br>"));
}
gwtCommandOutput.setExitCode(commandOutput.getExitCode());
if (commandOutput.getStderr() != null) {
gwtCommandOutput.setStderr(commandOutput.getStderr().replace("\n", "<br>"));
}
gwtCommandOutput.setStdout(commandOutput.getStdout());
gwtCommandOutput.setTimedout(commandOutput.getHasTimedout());
} catch (Throwable t) {
KapuaExceptionHandler.handle(t);
}
return gwtCommandOutput;
}
Aggregations