1 19 20 package org.apache.excalibur.instrument.manager.http; 21 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.io.UnsupportedEncodingException ; 25 import java.util.Date ; 26 27 import org.apache.excalibur.instrument.manager.http.server.URLCoder; 28 import org.apache.excalibur.instrument.manager.InstrumentableDescriptor; 29 import org.apache.excalibur.instrument.manager.InstrumentDescriptor; 30 import org.apache.excalibur.instrument.manager.InstrumentSampleDescriptor; 31 import org.apache.excalibur.instrument.manager.DefaultInstrumentManager; 32 33 39 public abstract class AbstractHTMLHandler 40 extends AbstractHandler 41 { 42 43 private static final String m_packageVersion = 44 AbstractHTMLHandler.class.getPackage().getImplementationVersion(); 45 46 49 56 public AbstractHTMLHandler( String path, 57 DefaultInstrumentManager manager, 58 InstrumentManagerHTTPConnector connector ) 59 { 60 super( path, CONTENT_TYPE_TEXT_HTML, manager, connector ); 61 } 62 63 66 protected String urlEncode( String str ) 67 { 68 try 69 { 70 return URLCoder.encode( str, InstrumentManagerHTTPConnector.ENCODING ); 73 } 74 catch ( UnsupportedEncodingException e ) 75 { 76 throw new IllegalStateException ( "Unknown encoding: " + e.toString() ); 78 } 79 } 80 81 protected void breadCrumbs( PrintWriter out, boolean link ) 82 { 83 String rootURL = getConnector().getRootBreadCrumbURL(); 84 if ( rootURL != null ) 85 { 86 String rootLabel = getConnector().getRootBreadCrumbLabel(); 88 out.println( "<a HREF='" + rootURL + "'>" + rootLabel + "</a>" ); 89 out.print( " <b>></b> " ); 90 } 91 92 if ( link ) 93 { 94 out.println( "<a HREF='instrument-manager.html'>" 95 + getInstrumentManager().getDescription() + "</a>" ); 96 } 97 else 98 { 99 out.println( getInstrumentManager().getDescription() ); 100 } 101 } 102 103 protected void breadCrumbs( PrintWriter out, InstrumentableDescriptor desc, boolean link ) 104 { 105 InstrumentableDescriptor parent = desc.getParentInstrumentableDescriptor(); 106 if ( parent == null ) 107 { 108 breadCrumbs( out, true ); 109 } 110 else 111 { 112 breadCrumbs( out, parent, true ); 113 } 114 out.print( " <b>></b> " ); 115 if ( link ) 116 { 117 out.println( "<a HREF='instrumentable.html?name=" + urlEncode( desc.getName() ) + "'>" 118 + desc.getDescription() + "</a>" ); 119 } 120 else 121 { 122 out.println( desc.getDescription() ); 123 } 124 } 125 126 protected void breadCrumbs( PrintWriter out, InstrumentDescriptor desc, boolean link ) 127 { 128 breadCrumbs( out, desc.getInstrumentableDescriptor(), true ); 129 out.print( " <b>></b> " ); 130 if ( link ) 131 { 132 out.println( "<a HREF='instrument.html?name=" + urlEncode( desc.getName() ) + "'>" 133 + desc.getDescription() + "</a>" ); 134 } 135 else 136 { 137 out.println( desc.getDescription() ); 138 } 139 } 140 141 protected void breadCrumbs( PrintWriter out, InstrumentSampleDescriptor desc, boolean link ) 142 { 143 breadCrumbs( out, desc.getInstrumentDescriptor(), true ); 144 out.print( " <b>></b> " ); 145 if ( link ) 146 { 147 out.println( "<a HREF='sample.html?name=" + urlEncode( desc.getName() ) + "'>" 148 + desc.getDescription() + "</a>" ); 149 } 150 else 151 { 152 out.println( desc.getDescription() ); 153 } 154 } 155 156 protected void startTable( PrintWriter out ) 157 throws IOException 158 { 159 out.println( "<table cellpadding='1' cellspacing='0'><tr><td bgcolor='#bbbbbb'><table cellpadding='2' cellspacing='1'>" ); 160 } 161 protected void endTable( PrintWriter out ) 162 throws IOException 163 { 164 out.println( "</table></td></tr></table>" ); 165 } 166 167 protected void startTableHeaderRow( PrintWriter out ) 168 throws IOException 169 { 170 out.println( "<tr>" ); 171 } 172 173 protected void endTableHeaderRow( PrintWriter out ) 174 throws IOException 175 { 176 out.println( "</tr>" ); 177 } 178 179 protected void tableHeaderCell( PrintWriter out, String value ) 180 throws IOException 181 { 182 out.print( "<td bgcolor='#dddddd' nowrap><b>" + value + "</b></td>" ); 183 } 184 185 protected void startTableRow( PrintWriter out, int row ) 186 throws IOException 187 { 188 String color; 189 if ( row % 2 == 0 ) 190 { 191 color = "#eeeeee"; 192 } 193 else 194 { 195 color = "#e4e4e4"; 196 } 197 out.println( "<tr bgcolor='" + color + "'>" ); 198 } 199 200 protected void endTableRow( PrintWriter out ) 201 throws IOException 202 { 203 out.println( "</tr>" ); 204 } 205 206 protected void tableCell( PrintWriter out, String value ) 207 throws IOException 208 { 209 out.print( "<td nowrap>" + value + "</td>" ); 210 } 211 212 protected void tableCellRight( PrintWriter out, String value ) 213 throws IOException 214 { 215 out.print( "<td align='right' nowrap>" + value + "</td>" ); 216 } 217 218 protected void tableRow( PrintWriter out, int row, String label, String value ) 219 throws IOException 220 { 221 startTableRow( out, row ); 222 tableHeaderCell( out, label ); 223 tableCell( out, value ); 224 endTableRow( out ); 225 } 226 227 protected void footer( PrintWriter out ) 228 { 229 out.println( "<br>" ); 230 out.print( "<font size='-1' color='#888888'>" ); 231 out.print( "<center>" ); 232 out.print( "<a HREF='http://excalibur.apache.org/instrument/html-client.html'>" ); 233 out.print( "Excalibur Instrument HTTP Client" ); 234 out.print( "</a> Version " ); 235 out.print( m_packageVersion ); 236 out.print( "<br>" ); 237 out.print( "Copyright© 2002-2004 The Apache Software Foundation. All rights reserved." ); 238 out.print( "</center>" ); 239 out.println( "</font>" ); 240 } 241 242 protected void outputInstrumentables( PrintWriter out, InstrumentableDescriptor[] descs ) 243 throws IOException 244 { 245 startTable( out ); 246 startTableHeaderRow( out ); 247 tableHeaderCell( out, "Name" ); 248 endTableHeaderRow( out ); 249 250 for ( int i = 0; i < descs.length; i++ ) 251 { 252 InstrumentableDescriptor desc = descs[i]; 253 254 startTableRow( out, i ); 255 tableCell( out, 256 "<a HREF='instrumentable.html?name=" + urlEncode( desc.getName() ) + "'>" 257 + desc.getDescription() + "</a>" ); 258 endTableRow( out ); 259 } 260 261 endTable( out ); 262 } 263 264 protected void outputInstruments( PrintWriter out, InstrumentDescriptor[] descs ) 265 throws IOException 266 { 267 startTable( out ); 268 startTableHeaderRow( out ); 269 tableHeaderCell( out, "Name" ); 270 endTableHeaderRow( out ); 271 272 for ( int i = 0; i < descs.length; i++ ) 273 { 274 InstrumentDescriptor desc = descs[i]; 275 276 startTableRow( out, i ); 277 tableCell( out, "<a HREF='instrument.html?name=" + urlEncode( desc.getName() ) + "'>" 278 + desc.getDescription() + "</a>" ); 279 endTableRow( out ); 280 } 281 282 endTable( out ); 283 } 284 285 protected void outputInstrumentSamples( PrintWriter out, 286 InstrumentSampleDescriptor[] descs, 287 boolean readOnly ) 288 throws IOException 289 { 290 startTable( out ); 291 startTableHeaderRow( out ); 292 tableHeaderCell( out, "Name" ); 293 tableHeaderCell( out, "Last Sample" ); 294 tableHeaderCell( out, "Last Sample Period" ); 295 tableHeaderCell( out, "Interval" ); 296 tableHeaderCell( out, "Size" ); 297 tableHeaderCell( out, "Expiration Time" ); 298 endTableHeaderRow( out ); 299 300 for ( int i = 0; i < descs.length; i++ ) 301 { 302 InstrumentSampleDescriptor desc = descs[i]; 303 304 startTableRow( out, i ); 305 tableCell( out, "<a HREF='sample.html?name=" + urlEncode( desc.getName() ) + "'>" 306 + desc.getDescription() + "</a> (<a HREF='sample.html?name=" 307 + urlEncode( desc.getName() ) + "&chart=true'>Chart</a>)" ); 308 tableCellRight( out, Integer.toString( desc.getValue() ) ); 309 tableCell( out, new Date ( desc.getTime() ).toString() ); 310 tableCellRight( out, Long.toString( desc.getInterval() ) ); 311 tableCellRight( out, Integer.toString( desc.getSize() ) ); 312 String value; 313 if ( desc.getLeaseExpirationTime() == 0 ) 314 { 315 value = "<i>Permanent</i>"; 316 } 317 else 318 { 319 String renewUrl = 320 "sample-lease.html?name=" + urlEncode( desc.getName() ) + "&instrument=true&lease="; 321 322 value = new Date ( desc.getLeaseExpirationTime() ).toString(); 323 if ( !readOnly ) 324 { 325 value = value 326 + " (Renew <a HREF='" + renewUrl + "600000'>10min</a>, " 327 + "<a HREF='" + renewUrl + "3600000'>1hr</a>, " 328 + "<a HREF='" + renewUrl + "86400000'>1day</a>)"; 329 } 330 331 if ( desc.getLeaseExpirationTime() - System.currentTimeMillis() < 300000 ) 333 { 334 value = "<font color='ff0000'>" + value + "</font>"; 335 } 336 } 337 tableCell( out, value ); 338 endTableRow( out ); 339 } 340 341 endTable( out ); 342 } 343 } 344 345 | Popular Tags |