001// Copyright 2005 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.tapestry.error;
016
017import java.io.IOException;
018
019import org.apache.hivemind.util.PropertyUtils;
020import org.apache.tapestry.IPage;
021import org.apache.tapestry.IRequestCycle;
022import org.apache.tapestry.StaleLinkException;
023import org.apache.tapestry.services.ResponseRenderer;
024
025/**
026 * Implementation of {@link org.apache.tapestry.error.StaleLinkExceptionPresenter} that uses a page
027 * to present the exception. The page must implement a property named "message" of type String and
028 * should present that message to the user.
029 * 
030 * @author Howard M. Lewis Ship
031 * @since 4.0
032 */
033public class StaleLinkExceptionPresenterImpl implements StaleLinkExceptionPresenter
034{
035    private ResponseRenderer _responseRenderer;
036
037    private String _pageName;
038
039    public void presentStaleLinkException(IRequestCycle cycle, StaleLinkException cause)
040            throws IOException
041    {
042        IPage exceptionPage = cycle.getPage(_pageName);
043
044        PropertyUtils.write(exceptionPage, "message", cause.getMessage());
045
046        cycle.activate(exceptionPage);
047
048        _responseRenderer.renderResponse(cycle);
049    }
050
051    public void setPageName(String pageName)
052    {
053        _pageName = pageName;
054    }
055
056    public void setResponseRenderer(ResponseRenderer responseRenderer)
057    {
058        _responseRenderer = responseRenderer;
059    }
060
061}