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 org.apache.hivemind.ApplicationRuntimeException;
018    import org.apache.tapestry.IPage;
019    import org.apache.tapestry.IRequestCycle;
020    import org.apache.tapestry.StaleLinkException;
021    import org.apache.tapestry.services.ResponseRenderer;
022    
023    /**
024     * Implementation of {@link org.apache.tapestry.error.StaleLinkExceptionPresenter} that uses a page
025     * to present the exception. The page must implement a property named "message" of type String and
026     * should present that message to the user.
027     * 
028     * @author Howard M. Lewis Ship
029     * @since 4.0
030     */
031    public class StaleLinkExceptionPresenterImpl implements StaleLinkExceptionPresenter
032    {
033        private RequestExceptionReporter _requestExceptionReporter;
034    
035        private ResponseRenderer _responseRenderer;
036    
037        private String _pageName;
038    
039        public void presentStaleLinkException(IRequestCycle cycle, StaleLinkException cause)
040        {
041            try
042            {
043                IPage exceptionPage = cycle.getPage(_pageName);
044    
045                exceptionPage.setProperty("message", cause.getMessage());
046    
047                cycle.activate(exceptionPage);
048    
049                _responseRenderer.renderResponse(cycle);
050            }
051            catch (Throwable ex)
052            {
053                // Worst case scenario. The exception page itself is broken, leaving
054                // us with no option but to write the cause to the output.
055    
056                _requestExceptionReporter.reportRequestException(ErrorMessages
057                        .unableToProcessClientRequest(cause), cause);
058    
059                // Also, write the exception thrown when redendering the exception
060                // page, so that can get fixed as well.
061    
062                _requestExceptionReporter.reportRequestException(ErrorMessages
063                        .unableToPresentExceptionPage(ex), ex);
064    
065                // And throw the exception.
066    
067                throw new ApplicationRuntimeException(ex.getMessage(), ex);
068            }
069    
070        }
071    
072        public void setPageName(String pageName)
073        {
074            _pageName = pageName;
075        }
076    
077        public void setRequestExceptionReporter(RequestExceptionReporter requestExceptionReporter)
078        {
079            _requestExceptionReporter = requestExceptionReporter;
080        }
081    
082        public void setResponseRenderer(ResponseRenderer responseRenderer)
083        {
084            _responseRenderer = responseRenderer;
085        }
086    
087    }