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.unableToProcessClientRequest(cause), cause);
056    
057                // Also, write the exception thrown when redendering the exception
058                // page, so that can get fixed as well.
059    
060                _requestExceptionReporter.reportRequestException(ErrorMessages.unableToPresentExceptionPage(ex), ex);
061    
062                // And throw the exception.
063    
064                throw new ApplicationRuntimeException(ex.getMessage(), ex);
065            }
066    
067            if (_verbose)
068                _requestExceptionReporter.reportRequestException(ErrorMessages.unableToProcessClientRequest(cause), cause);
069        }
070    
071        public void setExceptionPageName(String exceptionPageName)
072        {
073            _exceptionPageName = exceptionPageName;
074        }
075    
076        public void setRequestExceptionReporter(
077                RequestExceptionReporter requestExceptionReporter)
078        {
079            _requestExceptionReporter = requestExceptionReporter;
080        }
081    
082        public void setResponseRenderer(ResponseRenderer responseRenderer)
083        {
084            _responseRenderer = responseRenderer;
085        }
086    
087        public boolean isVerbose()
088        {
089            return _verbose;
090        }
091    
092        public void setVerbose(boolean verbose)
093        {
094            _verbose = verbose;
095        }
096    }