001    // Copyright 2008, 2009 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.tapestry5.corelib.components;
016    
017    import org.apache.tapestry5.RenderSupport;
018    import org.apache.tapestry5.annotations.Environmental;
019    import org.apache.tapestry5.annotations.IncludeJavaScriptLibrary;
020    import org.apache.tapestry5.annotations.Parameter;
021    import org.apache.tapestry5.annotations.Property;
022    import org.apache.tapestry5.internal.InternalConstants;
023    import org.apache.tapestry5.ioc.annotations.Inject;
024    import org.apache.tapestry5.ioc.annotations.Primary;
025    import org.apache.tapestry5.ioc.annotations.Symbol;
026    import org.apache.tapestry5.ioc.services.ExceptionAnalysis;
027    import org.apache.tapestry5.ioc.services.ExceptionAnalyzer;
028    import org.apache.tapestry5.ioc.services.ExceptionInfo;
029    import org.apache.tapestry5.services.StackTraceElementAnalyzer;
030    import org.apache.tapestry5.services.StackTraceElementClassConstants;
031    
032    import java.util.List;
033    
034    /**
035     * Integral part of the default {@link org.apache.tapestry5.corelib.pages.ExceptionReport} page used to break apart and
036     * display the properties of the exception.
037     *
038     * @see org.apache.tapestry5.ioc.services.ExceptionAnalyzer
039     */
040    @IncludeJavaScriptLibrary("exceptiondisplay.js")
041    public class ExceptionDisplay
042    {
043        /**
044         * Exception to report.
045         */
046        @Parameter(required = true, allowNull = false)
047        private Throwable exception;
048    
049        @Inject
050        private ExceptionAnalyzer analyzer;
051    
052        @Inject
053        @Symbol(InternalConstants.TAPESTRY_APP_PACKAGE_PARAM)
054        private String appPackage;
055    
056        @Property
057        private ExceptionInfo info;
058    
059        @Property
060        private String propertyName;
061    
062        @Property
063        private StackTraceElement frame;
064    
065        @Property
066        private List<ExceptionInfo> stack;
067    
068        @Environmental
069        private RenderSupport renderSupport;
070    
071        @Property
072        private String toggleId;
073    
074        private boolean sawDoFilter;
075    
076        @Inject
077        @Primary
078        private StackTraceElementAnalyzer frameAnalyzer;
079    
080        void setupRender()
081        {
082            ExceptionAnalysis analysis = analyzer.analyze(exception);
083    
084            stack = analysis.getExceptionInfos();
085    
086            toggleId = renderSupport.allocateClientId("toggleStack");
087        }
088    
089        public boolean getShowPropertyList()
090        {
091            // True if either is non-empty
092    
093            return !(info.getPropertyNames().isEmpty() && info.getStackTrace().isEmpty());
094        }
095    
096        public Object getPropertyValue()
097        {
098            return info.getProperty(propertyName);
099        }
100    
101        public String getFrameClass()
102        {
103            if (sawDoFilter) return StackTraceElementClassConstants.OMITTED;
104    
105            String result = frameAnalyzer.classForFrame(frame);
106    
107            sawDoFilter |= frame.getMethodName().equals("doFilter");
108    
109            return result;
110        }
111    
112        void afterRender()
113        {
114            renderSupport.addScript("Tapestry.stackFrameToggle('%s');", toggleId);
115        }
116    }