    001// Copyright 2008, 2010 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007// http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5.internal.services;
    016
    017import java.io.IOException;
    018
    019import org.apache.tapestry5.TrackableComponentEventCallback;
    020import org.apache.tapestry5.services.ComponentEventResultProcessor;
    021
    022/**
    023 * A wrapper around {@link ComponentEventResultProcessor} that encapsulates capturing the exception.
    024 */
    025@SuppressWarnings({ "unchecked", "rawtypes" })
    026public class ComponentResultProcessorWrapper implements TrackableComponentEventCallback
    027{
    028    private boolean aborted;
    029
    030    private IOException exception;
    031
    032    private final ComponentEventResultProcessor processor;
    033
    034    private Object result;
    035
    036    public ComponentResultProcessorWrapper(ComponentEventResultProcessor processor)
    037    {
    038        this.processor = processor;
    039    }
    040
    041    public boolean handleResult(Object result)
    042    {
    043        if (aborted)
    044            throw new IllegalStateException(
    045                    "Event callback has already received and processed a result value and can not do so again.");
    046
    047        this.result = result;
    048
    049        try
    050        {
    051            processor.processResultValue(result);
    052        }
    053        catch (IOException ex)
    054        {
    055            exception = ex;
    056        }
    057
    058        aborted = true;
    059
    060        return true;
    061    }
    062
    063    /**
    064     * Returns true if {@link org.apache.tapestry5.ComponentEventCallback#handleResult(Object)} was invoked, false
    065     * otherwise.
    066     *
    067     * @return true if the event was aborted
    068     */
    069    public boolean isAborted()
    070    {
    071        return aborted;
    072    }
    073
    074    public void rethrow() throws IOException
    075    {
    076        if (exception != null)
    077            throw exception;
    078    }
    079
    080    public Object getResult()
    081    {
    082        return result;
    083    }
    084
    085}