Search in sources :

Example 1 with GwtDeviceCommandOutput

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;
}
Also used : GwtDeviceCommandOutput(org.eclipse.kapua.app.console.shared.model.GwtDeviceCommandOutput) DeviceCommandOutput(org.eclipse.kapua.service.device.management.command.DeviceCommandOutput) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) StringTokenizer(java.util.StringTokenizer) GwtDeviceCommandInput(org.eclipse.kapua.app.console.shared.model.GwtDeviceCommandInput) DeviceCommandInput(org.eclipse.kapua.service.device.management.command.DeviceCommandInput) DeviceCommandFactory(org.eclipse.kapua.service.device.management.command.DeviceCommandFactory) DeviceCommandManagementService(org.eclipse.kapua.service.device.management.command.DeviceCommandManagementService) KapuaId(org.eclipse.kapua.model.id.KapuaId) GwtDeviceCommandOutput(org.eclipse.kapua.app.console.shared.model.GwtDeviceCommandOutput)

Aggregations

StringTokenizer (java.util.StringTokenizer)1 GwtDeviceCommandInput (org.eclipse.kapua.app.console.shared.model.GwtDeviceCommandInput)1 GwtDeviceCommandOutput (org.eclipse.kapua.app.console.shared.model.GwtDeviceCommandOutput)1 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)1 KapuaId (org.eclipse.kapua.model.id.KapuaId)1 DeviceCommandFactory (org.eclipse.kapua.service.device.management.command.DeviceCommandFactory)1 DeviceCommandInput (org.eclipse.kapua.service.device.management.command.DeviceCommandInput)1 DeviceCommandManagementService (org.eclipse.kapua.service.device.management.command.DeviceCommandManagementService)1 DeviceCommandOutput (org.eclipse.kapua.service.device.management.command.DeviceCommandOutput)1