1 19 20 package org.apache.excalibur.instrument.manager.http; 21 22 import com.sun.image.codec.jpeg.JPEGCodec; 23 import com.sun.image.codec.jpeg.JPEGEncodeParam; 24 import com.sun.image.codec.jpeg.JPEGImageEncoder; 25 26 import java.awt.Graphics ; 27 import java.awt.image.BufferedImage ; 28 import java.io.BufferedInputStream ; 29 import java.io.IOException ; 30 import java.io.OutputStream ; 31 import java.io.UnsupportedEncodingException ; 32 import java.util.Map ; 33 34 import org.apache.excalibur.instrument.manager.http.server.AbstractHTTPURLHandler; 35 import org.apache.excalibur.instrument.manager.http.server.HTTPRedirect; 36 import org.apache.excalibur.instrument.manager.http.server.URLCoder; 37 import org.apache.excalibur.instrument.manager.DefaultInstrumentManager; 38 import org.apache.excalibur.instrument.manager.InstrumentSampleDescriptor; 39 import org.apache.excalibur.instrument.manager.InstrumentSampleSnapshot; 40 import org.apache.excalibur.instrument.manager.NoSuchInstrumentSampleException; 41 42 48 public class SampleChartHandler 49 extends AbstractHTTPURLHandler 50 { 51 52 private DefaultInstrumentManager m_manager; 53 54 55 private int m_width; 56 57 58 private int m_height; 59 60 61 private boolean m_antialias; 62 63 66 74 public SampleChartHandler( DefaultInstrumentManager manager, 75 int width, 76 int height, 77 boolean antialias ) 78 { 79 super( "/sample-chart.jpg", CONTENT_TYPE_IMAGE_JPEG, 80 InstrumentManagerHTTPConnector.ENCODING ); 81 82 m_manager = manager; 83 m_width = width; 84 m_height = height; 85 m_antialias = antialias; 86 } 87 88 91 98 public void doGet( String path, Map parameters, OutputStream os ) 99 throws IOException 100 { 101 String name = getParameter( parameters, "name" ); 102 InstrumentSampleDescriptor desc; 103 try 104 { 105 desc = m_manager.locateInstrumentSampleDescriptor( name ); 106 } 107 catch ( NoSuchInstrumentSampleException e ) 108 { 109 int pos = name.lastIndexOf( '.' ); 111 if ( pos >= 0 ) 112 { 113 String iName = URLCoder.encode( name.substring( 0, pos ), 116 InstrumentManagerHTTPConnector.ENCODING ); 117 118 throw new HTTPRedirect( "instrument.html?name=" + iName ); 119 } 120 else 121 { 122 throw new HTTPRedirect( "instrumentable.html" ); 123 } 124 } 125 126 int width = getIntegerParameter( parameters, "width", m_width ); 127 width = Math.max( 1, Math.min( 2048, width ) ); 128 int height = getIntegerParameter( parameters, "height", m_height ); 129 height = Math.max( 1, Math.min( 1024, height ) ); 130 131 boolean antialias = getBooleanParameter( parameters, "antialias", m_antialias ); 132 133 InstrumentSampleSnapshot snapshot = desc.getSnapshot(); 134 135 long interval = snapshot.getInterval(); 137 int hInterval; 138 String format; 139 String detailFormat; 140 if( interval < 1000 ) 141 { 142 hInterval = (int)( 10000 / interval ); 144 format = "{3}:{4}:{5}"; 145 detailFormat = "{1}/{2} {3}:{4}:{5}.{6}"; 146 } 147 else if( interval < 60000 ) 148 { 149 hInterval = (int)( 60000 / interval ); 151 format = "{3}:{4}:{5}"; 152 detailFormat = "{1}/{2} {3}:{4}:{5}"; 153 } 154 else if( interval < 600000 ) 155 { 156 hInterval = (int)( 600000 / interval ); 158 format = "{1}/{2} {3}:{4}"; 159 detailFormat = "{1}/{2} {3}:{4}"; 160 } 161 else if( interval < 3600000 ) 162 { 163 hInterval = (int)( 3600000 / interval ); 165 format = "{1}/{2} {3}:{4}"; 166 detailFormat = "{1}/{2} {3}:{4}"; 167 } 168 else if( interval < 86400000 ) 169 { 170 hInterval = (int)( 86400000 / interval ); 172 format = "{1}/{2}"; 173 detailFormat = "{1}/{2} {3}:{4}"; 174 } 175 else if( interval < 604800000 ) 176 { 177 hInterval = (int)( 604800000 / interval ); 179 format = "{0}/{1}/{2}"; 180 detailFormat = "{0}/{1}/{2}"; 181 } 182 else 183 { 184 hInterval = 10; 186 format = "{0}/{1}/{2}"; 187 detailFormat = "{0}/{1}/{2}"; 188 } 189 190 LineChart chart = new LineChart( hInterval, interval, format, detailFormat, 20, antialias ); 192 chart.setValues( snapshot.getSamples(), snapshot.getTime() ); 193 194 byte[] imageData = null; 195 196 BufferedImage bi = new BufferedImage ( width, height, BufferedImage.TYPE_INT_RGB ); 198 199 chart.setSize( bi.getWidth(), bi.getHeight() ); 201 202 Graphics g; 203 try 204 { 205 g = bi.createGraphics(); 206 } 207 catch ( Throwable t ) 208 { 209 212 217 String imageResource = "noawtlibs.jpg"; 220 BufferedInputStream is = 221 new BufferedInputStream ( getClass().getResourceAsStream( imageResource ) ); 222 byte[] noAWTLibs; 223 try { 224 noAWTLibs = new byte[is.available()]; 225 is.read( noAWTLibs, 0, noAWTLibs.length ); 226 } finally { 227 is.close(); 228 } 229 os.write( noAWTLibs ); 231 return; 232 } 233 234 chart.paintComponent( g ); 235 236 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( os ); 238 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam( bi ); 239 param.setQuality( 0.90f, true ); 240 encoder.encode( bi, param ); 241 } 242 } 243 244 | Popular Tags |