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