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")
026public class ComponentResultProcessorWrapper implements TrackableComponentEventCallback
027{
028    private boolean aborted;
029
030    private IOException exception;
031
032    private final ComponentEventResultProcessor processor;
033
034    public ComponentResultProcessorWrapper(ComponentEventResultProcessor processor)
035    {
036        this.processor = processor;
037    }
038
039    public boolean handleResult(Object result)
040    {
041        if (aborted)
042            throw new IllegalStateException(
043                    "Event callback has already received and processed a result value and can not do so again.");
044
045        try
046        {
047            processor.processResultValue(result);
048        }
049        catch (IOException ex)
050        {
051            exception = ex;
052        }
053
054        aborted = true;
055
056        return true;
057    }
058
059    /**
060     * Returns true if {@link org.apache.tapestry5.ComponentEventCallback#handleResult(Object)} was invoked, false
061     * otherwise.
062     * 
063     * @return true if the event was aborted
064     */
065    public boolean isAborted()
066    {
067        return aborted;
068    }
069
070    public void rethrow() throws IOException
071    {
072        if (exception != null)
073            throw exception;
074    }
075
076}