Search in sources :

Example 21 with CLIException

use of org.ow2.proactive_grid_cloud_portal.cli.CLIException in project scheduling by ow2-proactive.

the class ThirdPartyCredentialKeySetCommand method execute.

@Override
public void execute(ApplicationContext currentContext) throws CLIException {
    SchedulerRestInterface scheduler = currentContext.getRestClient().getScheduler();
    try {
        Set<String> credentialsKeySet = scheduler.thirdPartyCredentialsKeySet(currentContext.getSessionId());
        resultStack(currentContext).push(credentialsKeySet);
        if (!currentContext.isSilent()) {
            writeLine(currentContext, "%s", StringUtility.credentialsKeysAsString(credentialsKeySet));
        }
    } catch (Exception e) {
        handleError("An error occurred while removing third-party credential:", e, currentContext);
    }
}
Also used : SchedulerRestInterface(org.ow2.proactive_grid_cloud_portal.common.SchedulerRestInterface) CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException)

Example 22 with CLIException

use of org.ow2.proactive_grid_cloud_portal.cli.CLIException in project scheduling by ow2-proactive.

the class FileUtility method writeStringToFile.

public static void writeStringToFile(File file, String content) {
    try {
        FileUtils.writeStringToFile(file, content);
    } catch (IOException ioe) {
        throw new CLIException(REASON_OTHER, ioe);
    }
    file.setReadable(true, true);
    file.setWritable(true, true);
}
Also used : CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException) IOException(java.io.IOException)

Example 23 with CLIException

use of org.ow2.proactive_grid_cloud_portal.cli.CLIException in project scheduling by ow2-proactive.

the class FileUtility method writeObjectToFile.

public static void writeObjectToFile(Object object, File file) {
    FileOutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(file);
        ObjectOutputStream decorated = new ObjectOutputStream(outputStream);
        decorated.writeObject(object);
        decorated.flush();
    } catch (IOException ioe) {
        throw new CLIException(REASON_IO_ERROR, ioe);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
            // ignore
            }
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 24 with CLIException

use of org.ow2.proactive_grid_cloud_portal.cli.CLIException in project scheduling by ow2-proactive.

the class EntryPoint method run.

protected int run(String... args) {
    CommandFactory commandFactory = null;
    CommandLine cli = null;
    AbstractDevice console;
    ApplicationContext currentContext = new ApplicationContextImpl().currentContext();
    // Cannot rely on AbstractCommand#isDebugModeEnabled
    // because at this step SetDebugModeCommand#execute has not yet been executed
    // Consequently, SetDebugModeCommand.PROP_DEBUG_MODE is not set even if debug mode is enabled.
    boolean isDebugModeEnabled = isDebugModeEnabled(args);
    try {
        commandFactory = getCommandFactory(currentContext);
        Options options = commandFactory.supportedOptions();
        cli = parseArgs(options, args);
    } catch (IOException ioe) {
        System.err.println("An error occurred.");
        ioe.printStackTrace(System.err);
        return 1;
    } catch (ParseException pe) {
        writeError(currentContext, pe.getMessage(), pe, isDebugModeEnabled);
        // print usage
        Command help = commandFactory.commandForOption(new Option("h", null));
        if (help != null) {
            help.execute(currentContext);
        }
        return 1;
    }
    currentContext.setObjectMapper(new ObjectMapper().configure(FAIL_ON_UNKNOWN_PROPERTIES, false));
    currentContext.setRestServerUrl(DFLT_REST_SCHEDULER_URL);
    currentContext.setResourceType(resourceType());
    // retrieve the (ordered) command list corresponding to command-line
    // arguments
    List<Command> commands;
    try {
        commands = commandFactory.getCommandList(cli, currentContext);
    } catch (CLIException e) {
        writeError(currentContext, "An error occurred.", e, isDebugModeEnabled);
        return 1;
    }
    boolean retryLogin = false;
    try {
        executeCommandList(commands, currentContext);
    } catch (CLIException error) {
        if (REASON_UNAUTHORIZED_ACCESS == error.reason() && hasLoginCommand(commands)) {
            retryLogin = true;
        } else {
            writeError(currentContext, "An error occurred.", error, isDebugModeEnabled);
            return 1;
        }
    } catch (Throwable e) {
        writeError(currentContext, "An error occurred.", e, isDebugModeEnabled);
        return 1;
    }
    /*
         * in case of an existing session-id, the REST CLI reuses it without
         * obtaining a new session-id even if a login with credentials
         * specified. However if the REST server responds with an authorization
         * error (e.g. due to session timeout), it re-executes the commands list
         * with AbstractLoginCommand.PROP_RENEW_SESSION property set to 'true'.
         * This will effectively re-execute the user command with a new
         * session-id from server.
         */
    if (retryLogin && currentContext.getProperty(PROP_PERSISTED_SESSION, Boolean.TYPE, false)) {
        try {
            currentContext.setProperty(PROP_RENEW_SESSION, true);
            executeCommandList(commands, currentContext);
        } catch (Throwable error) {
            writeError(currentContext, "An error occurred while execution.", error, isDebugModeEnabled);
            return 1;
        }
    }
    return 0;
}
Also used : Options(org.apache.commons.cli.Options) IOException(java.io.IOException) CommandLine(org.apache.commons.cli.CommandLine) Command(org.ow2.proactive_grid_cloud_portal.cli.cmd.Command) AbstractLoginCommand(org.ow2.proactive_grid_cloud_portal.cli.cmd.AbstractLoginCommand) AbstractDevice(org.ow2.proactive_grid_cloud_portal.cli.console.AbstractDevice) Option(org.apache.commons.cli.Option) ParseException(org.apache.commons.cli.ParseException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 25 with CLIException

use of org.ow2.proactive_grid_cloud_portal.cli.CLIException in project scheduling by ow2-proactive.

the class AbstractCommand method buildCLIException.

protected CLIException buildCLIException(int reason, HttpResponseWrapper response, ApplicationContext currentContext) {
    String responseContent = StringUtility.responseAsString(response);
    ErrorView errorView = errorView(responseContent, currentContext);
    if (errorView != null) {
        throw new CLIException(reason, errorView.getErrorMessage(), errorView.getStackTrace());
    } else {
        HttpErrorView httpErrorView = errorView(responseContent);
        throw new CLIException(reason, httpErrorView.errorMessage, httpErrorView.stackTrace);
    }
}
Also used : CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException) ErrorView(org.ow2.proactive_grid_cloud_portal.cli.json.ErrorView) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString)

Aggregations

CLIException (org.ow2.proactive_grid_cloud_portal.cli.CLIException)49 SchedulerRestInterface (org.ow2.proactive_grid_cloud_portal.common.SchedulerRestInterface)30 HttpResponseWrapper (org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper)22 QueryStringBuilder (org.ow2.proactive_grid_cloud_portal.cli.utils.QueryStringBuilder)12 HttpGet (org.apache.http.client.methods.HttpGet)10 HttpPost (org.apache.http.client.methods.HttpPost)9 IOException (java.io.IOException)8 File (java.io.File)6 List (java.util.List)5 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)4 NSStateView (org.ow2.proactive_grid_cloud_portal.cli.json.NSStateView)4 PluginView (org.ow2.proactive_grid_cloud_portal.cli.json.PluginView)4 ArrayList (java.util.ArrayList)3 RmStateView (org.ow2.proactive_grid_cloud_portal.cli.json.RmStateView)3 FileInputStream (java.io.FileInputStream)2 Stack (java.util.Stack)2 HttpPut (org.apache.http.client.methods.HttpPut)2 ConfigurableFieldView (org.ow2.proactive_grid_cloud_portal.cli.json.ConfigurableFieldView)2 TaskResultData (org.ow2.proactive_grid_cloud_portal.scheduler.dto.TaskResultData)2 NotConnectedRestException (org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException)2