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