Search in sources :

Example 1 with Command

use of org.apache.commons.chain.Command in project sonarqube by SonarSource.

the class ExceptionCatcher method postprocess.

/**
     * <p>If an exception was thrown by a subsequent <code>Command</code>,
     * pass it on to the specified exception handling chain.  Otherwise, do
     * nothing.</p>
     *
     * @param context   The {@link Context} to be processed by this {@link
     *                  Filter}
     * @param exception The <code>Exception</code> (if any) that was thrown by
     *                  the last {@link Command} that was executed; otherwise
     *                  <code>null</code>
     * @return TRUE if post processing an exception occurred and the exception
     *         processing chain invoked
     * @throws IllegalStateException If exception throws exception
     */
public boolean postprocess(Context context, Exception exception) {
    // Do nothing if there was no exception thrown
    if (exception == null) {
        return (false);
    }
    // Stash the exception in the specified context attribute
    if (LOG.isDebugEnabled()) {
        LOG.debug("Attempting to handle a thrown exception");
    }
    ActionContext actionCtx = (ActionContext) context;
    actionCtx.setException(exception);
    // Execute the specified command
    try {
        Command command = lookupExceptionCommand();
        if (command == null) {
            LOG.error("Cannot find exceptionCommand '" + exceptionCommand + "'");
            throw new IllegalStateException("Cannot find exceptionCommand '" + exceptionCommand + "'");
        }
        if (LOG.isTraceEnabled()) {
            LOG.trace("Calling exceptionCommand '" + exceptionCommand + "'");
        }
        command.execute(context);
    } catch (Exception e) {
        LOG.warn("Exception from exceptionCommand '" + exceptionCommand + "'", e);
        throw new IllegalStateException("Exception chain threw exception");
    }
    return (true);
}
Also used : Command(org.apache.commons.chain.Command) ActionContext(org.apache.struts.chain.contexts.ActionContext)

Example 2 with Command

use of org.apache.commons.chain.Command in project sonarqube by SonarSource.

the class WrappingLookupCommand method getCommand.

/**
     * <p>Return the Command to process for this Context.</p>
     *
     * @param context The Context we are processing
     * @return The Command to process for this Context
     */
protected Command getCommand(Context context) {
    CatalogFactory catalogFactory = CatalogFactory.getInstance();
    String catalogName = getCatalogName();
    Catalog catalog;
    if (catalogName == null) {
        catalog = catalogFactory.getCatalog();
        // for debugging purposes
        catalogName = "{default}";
    } else {
        catalog = catalogFactory.getCatalog(catalogName);
    }
    if (catalog == null) {
        throw new IllegalArgumentException("Cannot find catalog '" + catalogName + "'");
    }
    Command command;
    String name = getName();
    if (name == null) {
        name = (String) context.get(getNameKey());
    }
    if (name != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Lookup command " + name + " in catalog " + catalogName);
        }
        command = catalog.getCommand(name);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Found command " + command + ";" + " optional: " + isOptional());
        }
        if ((command == null) && !isOptional()) {
            throw new IllegalArgumentException("Cannot find command " + "'" + name + "' in catalog '" + catalogName + "'");
        } else {
            return command;
        }
    } else {
        throw new IllegalArgumentException("No command name");
    }
}
Also used : Command(org.apache.commons.chain.Command) CatalogFactory(org.apache.commons.chain.CatalogFactory) Catalog(org.apache.commons.chain.Catalog)

Aggregations

Command (org.apache.commons.chain.Command)2 Catalog (org.apache.commons.chain.Catalog)1 CatalogFactory (org.apache.commons.chain.CatalogFactory)1 ActionContext (org.apache.struts.chain.contexts.ActionContext)1