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.hivemind.util.PropertyUtils;
019    import org.apache.tapestry.IPage;
020    import org.apache.tapestry.IRequestCycle;
021    import org.apache.tapestry.services.ResponseRenderer;
022    
023    /**
024     * @author Howard M. Lewis Ship
025     * @since 4.0
026     */
027    public class ExceptionPresenterImpl implements ExceptionPresenter
028    {
029    
030        private RequestExceptionReporter _requestExceptionReporter;
031    
032        private ResponseRenderer _responseRenderer;
033    
034        private String _exceptionPageName;
035    
036        private boolean _verbose;
037    
038        public void presentException(IRequestCycle cycle, Throwable cause)
039        {
040            try
041            {
042                IPage exceptionPage = cycle.getPage(_exceptionPageName);
043                
044                PropertyUtils.write(exceptionPage, "exception", cause);
045                
046                cycle.activate(exceptionPage);
047    
048                _responseRenderer.renderResponse(cycle);
049            }
050            catch (Throwable ex)
051            {
052                // Worst case scenario. The exception page itself is broken, leaving
053                // us with no option but to write the cause to the output.
054    
055                _requestExceptionReporter.reportRequestException(ErrorMessages
056                        .unableToProcessClientRequest(cause), cause);
057    
058                // Also, write the exception thrown when redendering the exception
059                // page, so that can get fixed as well.
060    
061                _requestExceptionReporter.reportRequestException(ErrorMessages
062                        .unableToPresentExceptionPage(ex), ex);
063    
064                // And throw the exception.
065    
066                throw new ApplicationRuntimeException(ex.getMessage(), ex);
067            }
068    
069            if (_verbose)
070                _requestExceptionReporter.reportRequestException(ErrorMessages
071                        .unableToProcessClientRequest(cause), cause);
072        }
073    
074        public void setExceptionPageName(String exceptionPageName)
075        {
076            _exceptionPageName = exceptionPageName;
077        }
078    
079        public void setRequestExceptionReporter(
080                RequestExceptionReporter requestExceptionReporter)
081        {
082            _requestExceptionReporter = requestExceptionReporter;
083        }
084    
085        public void setResponseRenderer(ResponseRenderer responseRenderer)
086        {
087            _responseRenderer = responseRenderer;
088        }
089    
090        public boolean isVerbose()
091        {
092            return _verbose;
093        }
094    
095        public void setVerbose(boolean verbose)
096        {
097            _verbose = verbose;
098        }
099    }