1 19 20 package org.apache.excalibur.instrument.manager.http; 21 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.util.Date ; 25 import java.util.Map ; 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 39 public class HTMLSampleHandler 40 extends AbstractHTMLHandler 41 { 42 45 51 public HTMLSampleHandler( DefaultInstrumentManager manager, 52 InstrumentManagerHTTPConnector connector ) 53 { 54 super( "/sample.html", manager, connector ); 55 } 56 57 60 67 public void doGet( String path, Map parameters, PrintWriter out ) 68 throws IOException 69 { 70 String name = getParameter( parameters, "name" ); 71 InstrumentSampleDescriptor desc; 72 try 73 { 74 desc = getInstrumentManager().locateInstrumentSampleDescriptor( name ); 75 } 76 catch ( NoSuchInstrumentSampleException e ) 77 { 78 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 chart = getParameter( parameters, "chart", null ); 91 92 InstrumentSampleSnapshot snapshot = desc.getSnapshot(); 93 94 String 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 renewUrl = "sample-lease.html?name=" + urlEncode( desc.getName() ) 134 + ( chart == null ? "" : "&chart=true" ) + "&lease="; 135 136 String value = new Date ( 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 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 ( 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 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( " 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( " 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( " timerInterval = refresh;" ); 217 out.println( " timerFired();" ); 218 out.println( "}" ); 219 out.println( "function chartError() {" ); 220 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 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 253 } 254 255 | Popular Tags |