public Processing execute()

in base/src/main/java/org/apache/commons/chain2/impl/ChainBase.java [153:214]


    public Processing execute(C context) {
        // Verify our parameters
        if (context == null) {
            throw new IllegalArgumentException("Can't execute a null context");
        }

        // Freeze the configuration of the command list
        frozen = true;

        // Execute the commands in this list until one returns something else 
        // than Processing.CONTINUE or throws an exception
        Processing saveResult = Processing.CONTINUE;
        Exception saveException = null;
        int i = 0;
        int n = commands.size();
        Command<K, V, C> lastCommand = null;
        for (i = 0; i < n; i++) {
            try {
                lastCommand = commands.get(i);
                saveResult = lastCommand.execute(context);
                if(saveResult == null) {
                    String format = String.format("The command '%s' returned an invalid processing value: '%s'",
                            lastCommand.getClass().getName(), saveResult);
                    throw new ChainException(format);
                } else if (saveResult != Processing.CONTINUE) {
                    break;
                }
            } catch (Exception e) {
                saveException = e;
                break;
            }
        }

        // Call postprocess methods on Filters in reverse order
        if (i >= n) { // Fell off the end of the chain
            i--;
        }
        boolean handled = false;
        boolean result;
        for (int j = i; j >= 0; j--) {
            if (commands.get(j) instanceof Filter) {
                try {
                    result =
                        ((Filter<K, V, C>) commands.get(j)).postprocess(context,
                                                           saveException);
                    if (result) {
                        handled = true;
                    }
                } catch (Exception e) {
                      // Silently ignore
                }
            }
        }

        // Return the exception or result state from the last execute()
        if (saveException != null && !handled) {
            // Wrap and rethrow exception
            throw wrapUnhandledExceptions(saveException, context,
                    lastCommand);
        }
        return saveResult;
    }