MemoryGraphServlet.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * --------------------------------------------------------------------------
00022  * $Id: MemoryGraphServlet.java,v 1.9 2004/03/19 14:31:52 sauthieg Exp $
00023  * --------------------------------------------------------------------------
00024  */
00025 
00026 package org.objectweb.jonas.webapp.jonasadmin.monitoring;
00027 
00028 // servlet imports
00029 import java.awt.Color;
00030 import java.awt.Graphics;
00031 import java.awt.image.BufferedImage;
00032 import java.io.IOException;
00033 import java.io.OutputStream;
00034 
00035 import javax.servlet.ServletException;
00036 import javax.servlet.http.HttpServlet;
00037 import javax.servlet.http.HttpServletRequest;
00038 import javax.servlet.http.HttpServletResponse;
00039 import javax.servlet.http.HttpSession;
00040 
00041 import org.objectweb.jonas.jmx.J2eeObjectName;
00042 import org.objectweb.jonas.jmx.JonasManagementRepr;
00043 import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;
00044 
00045 import Acme.JPM.Encoders.GifEncoder;
00046 
00051 public class MemoryGraphServlet extends HttpServlet {
00052 
00053     private static final int HEIGHT = 180;
00054     private static final int TOP_MARGIN = 35;
00055     private static final int RIGHT_MARGIN = 80;
00056     private static final int BOTTOM_MARGIN = 10;
00057     private static final int LEFT_MARGIN = 70;
00058     private static final int HORIZONTAL_SPACE = 30;
00059 
00060     private static final int Y_LENGTH = HEIGHT - TOP_MARGIN - BOTTOM_MARGIN; // the graph height
00061     private static final int GAUGE_WIDTH = 10;
00062 
00063     private static final Color BG_COLOR = new Color(0x006699);
00064     private static final Color FG_COLOR = Color.white;
00065     private static final Color GRAPHICS_BG_COLOR = Color.lightGray;
00066     private static final Color GRAPHICS_FG_COLOR = Color.darkGray;
00067     private static final Color AXIS_COLOR = Color.white;
00068     private static final Color TEXT_COLOR = Color.white;
00069 
00080     public void doGet(HttpServletRequest req, HttpServletResponse res)
00081         throws IOException, ServletException {
00082 
00083         HttpSession oSession = req.getSession();
00084         WhereAreYou oWhere = (WhereAreYou) oSession.getAttribute(WhereAreYou.SESSION_NAME);
00085 
00086         res.setContentType("image/gif");
00087         OutputStream out = res.getOutputStream();
00088 
00089         // get the data to draw:
00090         Long[] data = (Long[]) JonasManagementRepr.getAttribute(J2eeObjectName.J2EEServer(oWhere.
00091             getCurrentDomainName(), oWhere.getCurrentJonasServerName()), "tableMeasures");
00092         int xLength = 2 * data.length;
00093         int width = LEFT_MARGIN + xLength + HORIZONTAL_SPACE + GAUGE_WIDTH + RIGHT_MARGIN;
00094 
00095         BufferedImage img = new BufferedImage(width, HEIGHT, BufferedImage.TYPE_INT_RGB);
00096         Graphics g = img.getGraphics();
00097 
00098         // fill the image background:
00099         g.setColor(BG_COLOR);
00100         g.fillRect(0, 0, width, HEIGHT);
00101 
00102         // firstly, the memory used history:
00103 
00104         // draw the chart area:
00105         g.setColor(GRAPHICS_BG_COLOR);
00106         g.fillRect(cartesianXToGraphicsX(0), cartesianYToGraphicsY(Y_LENGTH), xLength - 1, Y_LENGTH);
00107 
00108         // search the max value in the data array:
00109         long yMax = data[0].longValue();
00110         for (int i = 1; i < data.length; i++) {
00111             yMax = Math.max(yMax, data[i].longValue());
00112 
00113             // looking for k and n where: k.10^n <= yMax < (k+1).10^n
00114         }
00115         long tmp = yMax;
00116         int n = 0;
00117         while ((tmp / 10) >= 1) {
00118             tmp = tmp / 10;
00119             n++;
00120         }
00121         long k = tmp % 10;
00122 
00123         // pow = 10^n
00124         int pow = 1;
00125         for (int i = 0; i < n; i++) {
00126             pow = pow * 10;
00127 
00128             // draw the range on the y axis:
00129         }
00130         for (int i = 1; i <= k; i++) {
00131             int y = (new Long(i * Y_LENGTH / (k + 1))).intValue();
00132             g.setColor(FG_COLOR);
00133             g.drawLine(cartesianXToGraphicsX( -3), cartesianYToGraphicsY(y)
00134                 , cartesianXToGraphicsX(xLength), cartesianYToGraphicsY(y));
00135             g.setColor(TEXT_COLOR);
00136             g.drawString("" + (i * pow), cartesianXToGraphicsX( -18 - n * 6)
00137                 , cartesianYToGraphicsY(y - 5));
00138         }
00139 
00140         // draw the values:
00141         g.setColor(GRAPHICS_FG_COLOR);
00142         for (int i = 0; i < data.length; i++) {
00143             int y = (new Long(data[i].longValue() * Y_LENGTH / ((k + 1) * pow))).intValue();
00144             g.drawLine(cartesianXToGraphicsX(2 * i), cartesianYToGraphicsY(0)
00145                 , cartesianXToGraphicsX(2 * i), cartesianYToGraphicsY(y));
00146         }
00147 
00148         g.setColor(TEXT_COLOR);
00149         g.drawString("History of memory used", cartesianXToGraphicsX(0)
00150             , cartesianYToGraphicsY(Y_LENGTH + 20));
00151         g.drawString("(Kbytes)", cartesianXToGraphicsX(0), cartesianYToGraphicsY(Y_LENGTH + 10));
00152         g.drawString("0", cartesianXToGraphicsX( -12), cartesianYToGraphicsY( -5));
00153         g.setColor(AXIS_COLOR);
00154         g.drawLine(cartesianXToGraphicsX( -3), cartesianYToGraphicsY(0)
00155             , cartesianXToGraphicsX(xLength), cartesianYToGraphicsY(0));
00156         g.drawLine(cartesianXToGraphicsX( -1), cartesianYToGraphicsY(0), cartesianXToGraphicsX( -1)
00157             , cartesianYToGraphicsY(Y_LENGTH));
00158         g.drawLine(cartesianXToGraphicsX(xLength - 2), cartesianYToGraphicsY(0)
00159             , cartesianXToGraphicsX(xLength - 2), cartesianYToGraphicsY(Y_LENGTH));
00160 
00161         // secondly, the current memory used:
00162 
00163         int xGauge = LEFT_MARGIN + xLength + HORIZONTAL_SPACE;
00164 
00165         g.setColor(GRAPHICS_BG_COLOR);
00166         g.fillRect(xGauge, cartesianYToGraphicsY(Y_LENGTH), GAUGE_WIDTH, Y_LENGTH);
00167 
00168         long usedMemory = ((Long) JonasManagementRepr.getAttribute(J2eeObjectName.J2EEServer(oWhere.
00169             getCurrentDomainName(), oWhere.getCurrentJonasServerName())
00170             , "currentUsedMemory")).longValue();
00171         long totalMemory = ((Long) JonasManagementRepr.getAttribute(J2eeObjectName.J2EEServer(
00172             oWhere.getCurrentDomainName(), oWhere.getCurrentJonasServerName())
00173             , "currentTotalMemory")).longValue();
00174 
00175         g.setColor(GRAPHICS_FG_COLOR);
00176         int y = (new Long(usedMemory * Y_LENGTH / totalMemory)).intValue();
00177         g.fillRect(xGauge, cartesianYToGraphicsY(y), GAUGE_WIDTH, y);
00178 
00179         g.setColor(AXIS_COLOR);
00180         g.drawRect(xGauge, cartesianYToGraphicsY(Y_LENGTH), GAUGE_WIDTH, Y_LENGTH);
00181 
00182         g.setColor(TEXT_COLOR);
00183         g.drawString("0", xGauge + GAUGE_WIDTH + 3, cartesianYToGraphicsY( -5));
00184         g.drawString("" + usedMemory, xGauge + GAUGE_WIDTH + 3
00185             , cartesianYToGraphicsY(Math.min(y - 5, Y_LENGTH - 25)));
00186         g.drawString("(current)", xGauge + GAUGE_WIDTH + 3, cartesianYToGraphicsY(Math.min(y - 15
00187             , Y_LENGTH - 35)));
00188         g.drawString("" + totalMemory, xGauge + GAUGE_WIDTH + 3, cartesianYToGraphicsY(Y_LENGTH - 5));
00189         g.drawString("(total)", xGauge + GAUGE_WIDTH + 3, cartesianYToGraphicsY(Y_LENGTH - 15));
00190         g.drawString("Memory used", xGauge, cartesianYToGraphicsY(Y_LENGTH + 20));
00191         g.drawString("(Kbytes)", xGauge, cartesianYToGraphicsY(Y_LENGTH + 10));
00192 
00193         GifEncoder ge = new GifEncoder(img, out);
00194         ge.setDimensions(width, HEIGHT);
00195         ge.encode();
00196         out.close();
00197     }
00198 
00203     public static int getWidth(int length) {
00204         return LEFT_MARGIN + 2 * length + HORIZONTAL_SPACE + GAUGE_WIDTH + RIGHT_MARGIN;
00205     }
00206 
00210     public static int getHeight() {
00211         return HEIGHT;
00212     }
00213 
00214     private int cartesianXToGraphicsX(int x) {
00215         return x + LEFT_MARGIN;
00216     }
00217 
00218     private int cartesianYToGraphicsY(int y) {
00219         return HEIGHT - 1 - y - BOTTOM_MARGIN;
00220     }
00221 }

Generated on Tue Feb 15 15:05:29 2005 for JOnAS by  doxygen 1.3.9.1