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.html;
016    
017    import java.util.Collections;
018    import java.util.Iterator;
019    import java.util.List;
020    import java.util.Properties;
021    import java.util.StringTokenizer;
022    
023    import org.apache.tapestry.BaseComponent;
024    import org.apache.tapestry.IMarkupWriter;
025    import org.apache.tapestry.IRender;
026    import org.apache.tapestry.IRequestCycle;
027    import org.apache.tapestry.describe.ReportStatusHub;
028    import org.apache.tapestry.web.WebUtils;
029    
030    /**
031     * Supports the {@link org.apache.tapestry.pages.Exception} page by displaying the request,
032     * session, servlet context and servlet object for the current request.
033     * 
034     * @author Howard M. Lewis Ship
035     * @since 4.0
036     */
037    public abstract class RequestDisplay extends BaseComponent
038    {
039        private boolean _even;
040    
041        // Injected
042    
043        public abstract ReportStatusHub getReportStatusHub();
044    
045        public void renderSystemProperties(IMarkupWriter writer)
046        {
047            _even = true;
048    
049            Properties p = System.getProperties();
050    
051            String pathSeparator = p.getProperty("path.separator");
052    
053            writer.begin("div");
054            writer.attribute("class", "described-object-title");
055            writer.print("JVM System Properties");
056            writer.end();
057            writer.println();
058    
059            writer.begin("table");
060            writer.attribute("class", "described-object");
061    
062            Iterator i = WebUtils.toSortedList(p.keys()).iterator();
063    
064            while (i.hasNext())
065            {
066                String key = (String) i.next();
067                String value = p.getProperty(key);
068    
069                renderKeyAndValue(writer, key, value, pathSeparator);
070            }
071    
072            writer.end();
073        }
074    
075        private void renderKeyAndValue(IMarkupWriter writer, String key, String value,
076                String pathSeparator)
077        {
078            String[] values = split(key, value, pathSeparator);
079    
080            for (int i = 0; i < values.length; i++)
081            {
082                writer.begin("tr");
083    
084                writer.attribute("class", _even ? "even" : "odd");
085    
086                _even = !_even;
087    
088                writer.begin("th");
089    
090                if (i == 0)
091                    writer.print(key);
092    
093                writer.end();
094                writer.begin("td");
095                writer.print(values[i]);
096                writer.end("tr");
097                writer.println();
098            }
099        }
100    
101        private String[] split(String key, String value, String pathSeparator)
102        {
103            if (!key.endsWith(".path"))
104                return new String[]
105                { value };
106    
107            StringTokenizer tokenizer = new StringTokenizer(value, pathSeparator);
108            List values = Collections.list(tokenizer);
109    
110            return (String[]) values.toArray(new String[values.size()]);
111        }
112    
113        public IRender getSystemPropertiesRenderer()
114        {
115            return new IRender()
116            {
117                public void render(IMarkupWriter writer, IRequestCycle cycle)
118                {
119                    renderSystemProperties(writer);
120                }
121            };
122        }
123    
124        public IRender getReportStatusRenderer()
125        {
126            return new IRender()
127            {
128                public void render(IMarkupWriter writer, IRequestCycle cycle)
129                {
130                    getReportStatusHub().fireReportStatus(writer);
131                }
132            };
133        }
134    }