KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > instrument > manager > http > HTMLSampleHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.excalibur.instrument.manager.http;
21
22 import java.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.excalibur.instrument.manager.http.server.HTTPRedirect;
28 import org.apache.excalibur.instrument.manager.DefaultInstrumentManager;
29 import org.apache.excalibur.instrument.manager.InstrumentSampleDescriptor;
30 import org.apache.excalibur.instrument.manager.InstrumentSampleSnapshot;
31 import org.apache.excalibur.instrument.manager.NoSuchInstrumentSampleException;
32
33 /**
34  *
35  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
36  * @version CVS $Revision: 1.9 $ $Date: 2004/03/09 14:33:06 $
37  * @since 4.1
38  */

39 public class HTMLSampleHandler
40     extends AbstractHTMLHandler
41 {
42     /*---------------------------------------------------------------
43      * Constructors
44      *-------------------------------------------------------------*/

45     /**
46      * Creates a new HTMLSampleHandler.
47      *
48      * @param manager Reference to the DefaultInstrumentManager.
49      * @param connector The InstrumentManagerHTTPConnector.
50      */

51     public HTMLSampleHandler( DefaultInstrumentManager manager,
52                               InstrumentManagerHTTPConnector connector )
53     {
54         super( "/sample.html", manager, connector );
55     }
56     
57     /*---------------------------------------------------------------
58      * AbstractHTTPURLHandler Methods
59      *-------------------------------------------------------------*/

60     /**
61      * Handles the specified request.
62      *
63      * @param The full path being handled.
64      * @param parameters A Map of the parameters in the request.
65      * @param os The PrintWriter to write the result to.
66      */

67     public void doGet( String JavaDoc path, Map JavaDoc parameters, PrintWriter JavaDoc out )
68         throws IOException JavaDoc
69     {
70         String JavaDoc name = getParameter( parameters, "name" );
71         InstrumentSampleDescriptor desc;
72         try
73         {
74             desc = getInstrumentManager().locateInstrumentSampleDescriptor( name );
75         }
76         catch ( NoSuchInstrumentSampleException e )
77         {
78             // Sample no longer exists, go back to the parent instrument.
79
int pos = name.lastIndexOf( '.' );
80             if ( pos >= 0 )
81             {
82                 throw new HTTPRedirect(
83                     "instrument.html?name=" + urlEncode( name.substring( 0, pos ) ) );
84             }
85             else
86             {
87                 throw new HTTPRedirect( "instrument-manager.html" );
88             }
89         }
90         String JavaDoc chart = getParameter( parameters, "chart", null );
91         
92         InstrumentSampleSnapshot snapshot = desc.getSnapshot();
93         
94         String JavaDoc type;
95         switch ( desc.getType() )
96         {
97         case DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_COUNTER:
98             type = "Counter";
99             break;
100             
101         case DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MAXIMUM:
102             type = "Max Value";
103             break;
104             
105         case DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MINIMUM:
106             type = "Min Value";
107             break;
108             
109         case DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MEAN:
110             type = "Mean Value";
111             break;
112             
113         default:
114             type = "Unknown";
115             break;
116         }
117         
118         out.println( "<html>" );
119         out.println( "<head><title>" + desc.getDescription() + "</title></head>" );
120         out.println( "<body>" );
121         
122         breadCrumbs( out, desc, false );
123         
124         out.println( "<h2>Instrument Sample</h2>" );
125         startTable( out );
126         tableRow( out, 0, "Name", desc.getName() );
127         tableRow( out, 0, "Description", desc.getDescription() );
128         tableRow( out, 0, "Type", type );
129         tableRow( out, 0, "Interval", desc.getInterval() + "ms." );
130         tableRow( out, 0, "Size", Integer.toString( desc.getSize() ) );
131         if ( desc.getLeaseExpirationTime() > 0 )
132         {
133             String JavaDoc renewUrl = "sample-lease.html?name=" + urlEncode( desc.getName() )
134                 + ( chart == null ? "" : "&chart=true" ) + "&lease=";
135             
136             String JavaDoc value = new Date JavaDoc( desc.getLeaseExpirationTime() ).toString();
137             
138             if ( !getConnector().isReadOnly() )
139             {
140                 value = value + " (Renew <a HREF='" + renewUrl + "600000'>10min</a>, "
141                     + "<a HREF='" + renewUrl + "3600000'>1hr</a>, "
142                     + "<a HREF='" + renewUrl + "86400000'>1day</a>)";
143             }
144             
145             // Make the text red if it is about to expire.
146
if ( desc.getLeaseExpirationTime() - System.currentTimeMillis() < 300000 )
147             {
148                 value = "<font color='ff0000'>" + value + "</font>";
149             }
150
151             tableRow( out, 0, "Expiration", value );
152         }
153         else
154         {
155             tableRow( out, 0, "Expiration", "Permanent" );
156         }
157         endTable( out );
158         
159         if ( chart == null )
160         {
161             out.println( "<h2>Data Samples (<a HREF='sample.html?name="
162                 + urlEncode( desc.getName() ) + "&chart=true'>Chart</a>)</h2>" );
163             
164             startTable( out );
165             startTableHeaderRow( out );
166             tableHeaderCell( out, "Period" );
167             tableHeaderCell( out, "Value" );
168             endTableHeaderRow( out );
169             long time = snapshot.getTime();
170             int[] samples = snapshot.getSamples();
171             for ( int i = 0; i < samples.length; i++ )
172             {
173                 startTableRow( out, i );
174                 tableCell( out, new Date JavaDoc( time ).toString() );
175                 tableCellRight( out, Integer.toString( samples[samples.length - i - 1] ) );
176                 endTableRow( out );
177                 
178                 time -= snapshot.getInterval();
179             }
180             endTable( out );
181         }
182         else
183         {
184             out.println( "<h2>Data Samples (<a HREF='sample.html?name="
185                 + urlEncode( desc.getName() ) + "'>Plain</a>)</h2>" );
186             
187             // Originally, the JavaScript timer in the page made use of the setInterval
188
// function to build a simple timer. This worked fine under normal
189
// operation. But if a browser was suspended for 1 hour with a timer
190
// running at 1 second intervals, the timer would try to catch up when
191
// the machine was resumed. This would result in the timer firing 3600
192
// times over the course of a few seconds. The sudden burst of requests
193
// was swamping the server.
194
// The current scripts below now always reset the timer each time it is
195
// fired. If the timer ever falls behind it will recover smoothly
196
// without trying to catch up. While not quite as accurate it is
197
// sufficient for our purposes.
198

199             out.println( "<SCRIPT LANGUAGE=\"JavaScript\">" );
200             out.println( "var timerId = 0;" );
201             out.println( "var timerInterval = 5000;" );
202             out.println( "function refreshChart() {" );
203             //out.println( " alert(\"in refreshChart()\");" );
204
out.println( " document.chart.src=\"sample-chart.jpg?name=" + urlEncode( desc.getName() ) + "&time=\" + new Date().getTime();" );
205             out.println( "}" );
206             out.println( "function timerFired() {" );
207             //out.println( " alert(\"in timerFired()\");" );
208
out.println( " if (timerId) {" );
209             out.println( " clearTimeout(timerId);" );
210             out.println( " }" );
211             out.println( " timerId = setTimeout(\"timerFired()\", timerInterval)" );
212             out.println( " refreshChart();" );
213             out.println( "}" );
214             out.println( "function setRefresh(refresh) {" );
215             //out.println( " alert(\"in setRefresh(\" + refresh + \")\");" );
216
out.println( " timerInterval = refresh;" );
217             out.println( " timerFired();" );
218             out.println( "}" );
219             out.println( "function chartError() {" );
220             //out.println( " alert(\"in chartError()\");" );
221
out.println( " clearTimeout(timerId);" );
222             out.println( " document.location=\"instrument.html?name=" + urlEncode( desc.getInstrumentDescriptor().getName() ) + "\";" );
223             out.println( "}" );
224             out.println( "</SCRIPT>" );
225             
226             out.println( "<form>" );
227             startTable( out );
228             // Add a time to the chart as is done in the Javascript. Some browsers ignore the
229
// do not cache headers in the image and display a cached version of the image
230
// anyway.
231
tableCell( out, "<img name='chart' SRC='sample-chart.jpg?name=" + urlEncode( desc.getName() )
232                 + "&time=" + System.currentTimeMillis() + "' onError='javascript:chartError()'>" );
233             endTable( out );
234             out.println( "Refresh rate:" );
235             out.println( "<input type='button' value='No Refresh' onClick='javascript:clearTimeout(timerId)'>" );
236             out.println( "<input type='button' value='1 Second' onClick='javascript:setRefresh(1000)'>" );
237             out.println( "<input type='button' value='5 Seconds' onClick='javascript:setRefresh(5000)'>" );
238             out.println( "<input type='button' value='10 Seconds' onClick='javascript:setRefresh(10000)'>" );
239             out.println( "<input type='button' value='1 Minute' onClick='javascript:setRefresh(60000)'>" );
240             out.println( "<input type='button' value='Refresh Now' onClick='javascript:refreshChart()'>" );
241             out.println( "</form>" );
242         }
243         
244         footer( out );
245         
246         out.println( "</body>" );
247         out.println( "</html>" );
248     }
249             
250     /*---------------------------------------------------------------
251      * Methods
252      *-------------------------------------------------------------*/

253 }
254
255
Popular Tags