KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 JavaDoc;
27 import java.awt.image.BufferedImage JavaDoc;
28 import java.io.BufferedInputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.UnsupportedEncodingException JavaDoc;
32 import java.util.Map JavaDoc;
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 /**
43  *
44  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
45  * @version CVS $Revision: 1.9 $ $Date: 2004/03/06 14:01:28 $
46  * @since 4.1
47  */

48 public class SampleChartHandler
49     extends AbstractHTTPURLHandler
50 {
51     /** The instrument manager */
52     private DefaultInstrumentManager m_manager;
53     
54     /** Default width of the image. */
55     private int m_width;
56     
57     /** Default height of the image. */
58     private int m_height;
59     
60     /** Default antialias flag. */
61     private boolean m_antialias;
62     
63     /*---------------------------------------------------------------
64      * Constructors
65      *-------------------------------------------------------------*/

66     /**
67      * Creates a new SampleChartHandler.
68      *
69      * @param manager Reference to the instrument manager interface.
70      * @param width Default image width.
71      * @param height Default image height.
72      * @param antialias True if the default antialias parameter should be true.
73      */

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     /*---------------------------------------------------------------
89      * AbstractHandler Methods
90      *-------------------------------------------------------------*/

91     /**
92      * Handles the specified request.
93      *
94      * @param The full path being handled.
95      * @param parameters A Map of the parameters in the request.
96      * @param os The OutputStream to write the result to.
97      */

98     public void doGet( String JavaDoc path, Map JavaDoc parameters, OutputStream JavaDoc os )
99         throws IOException JavaDoc
100     {
101         String JavaDoc name = getParameter( parameters, "name" );
102         InstrumentSampleDescriptor desc;
103         try
104         {
105             desc = m_manager.locateInstrumentSampleDescriptor( name );
106         }
107         catch ( NoSuchInstrumentSampleException e )
108         {
109             // Sample no longer exists, go back to the parent instrument.
110
int pos = name.lastIndexOf( '.' );
111             if ( pos >= 0 )
112             {
113                 // Starting with Java 1.4, encode takes an encoding, but this needs to
114
// work with 1.3. Use our own version.
115
String JavaDoc 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         // Decide on a line interval based on the interval of the sample.
136
long interval = snapshot.getInterval();
137         int hInterval;
138         String JavaDoc format;
139         String JavaDoc detailFormat;
140         if( interval < 1000 )
141         {
142             // Once per 10 seconds.
143
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             // Once per minute.
150
hInterval = (int)( 60000 / interval );
151             format = "{3}:{4}:{5}";
152             detailFormat = "{1}/{2} {3}:{4}:{5}";
153         }
154         else if( interval < 600000 )
155         {
156             // Once per 10 minutes
157
hInterval = (int)( 600000 / interval );
158             format = "{1}/{2} {3}:{4}";
159             detailFormat = "{1}/{2} {3}:{4}";
160         }
161         else if( interval < 3600000 )
162         {
163             // Once per hour.
164
hInterval = (int)( 3600000 / interval );
165             format = "{1}/{2} {3}:{4}";
166             detailFormat = "{1}/{2} {3}:{4}";
167         }
168         else if( interval < 86400000 )
169         {
170             // Once per day.
171
hInterval = (int)( 86400000 / interval );
172             format = "{1}/{2}";
173             detailFormat = "{1}/{2} {3}:{4}";
174         }
175         else if( interval < 604800000 )
176         {
177             // Once per week.
178
hInterval = (int)( 604800000 / interval );
179             format = "{0}/{1}/{2}";
180             detailFormat = "{0}/{1}/{2}";
181         }
182         else
183         {
184             // Default to every 10 points.
185
hInterval = 10;
186             format = "{0}/{1}/{2}";
187             detailFormat = "{0}/{1}/{2}";
188         }
189             
190         // Actually create the chart and add it to the content pane
191
LineChart chart = new LineChart( hInterval, interval, format, detailFormat, 20, antialias );
192         chart.setValues( snapshot.getSamples(), snapshot.getTime() );
193         
194         byte[] imageData = null;
195         
196         // Create a new BufferedImage onto which the plant will be painted.
197
BufferedImage JavaDoc bi = new BufferedImage JavaDoc( width, height, BufferedImage.TYPE_INT_RGB );
198         
199         // Paint the chart onto the Graphics object of the BufferedImage.
200
chart.setSize( bi.getWidth(), bi.getHeight() );
201         
202         Graphics JavaDoc g;
203         try
204         {
205             g = bi.createGraphics();
206         }
207         catch ( Throwable JavaDoc t )
208         {
209             // Linux throws NoClassDefFoundError.
210
// Solaris throws InternalError
211

212             // On Headless UNIX machines this error will be thrown when attempting to
213
// create an graphic. The AWT libraries require a native library that
214
// only exists on UNIX system which have X-Windows installed. This is
215
// never a problem on Windows systems.
216

217             // Rather than giving the user nothing, send them a preprepared jpeg file
218
// that notifies them of the problem.
219
String JavaDoc imageResource = "noawtlibs.jpg";
220             BufferedInputStream JavaDoc is =
221                 new BufferedInputStream JavaDoc( 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             // Now write the error image out to the client.
230
os.write( noAWTLibs );
231             return;
232         }
233         
234         chart.paintComponent( g );
235
236         // Encode the BufferedImage as a JPEG image and write it to the output stream.
237
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