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