001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: MemoryGraphServlet.java 9680 2006-10-06 12:08:33Z danesa $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.webapp.jonasadmin.monitoring;
025:
026: // servlet imports
027: import java.awt.Color;
028: import java.awt.Graphics;
029: import java.awt.image.BufferedImage;
030: import java.io.IOException;
031: import java.io.OutputStream;
032:
033: import javax.imageio.IIOException;
034: import javax.imageio.ImageIO;
035: import javax.servlet.ServletException;
036: import javax.servlet.http.HttpServlet;
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletResponse;
039: import javax.servlet.http.HttpSession;
040:
041: import org.objectweb.jonas.common.Log;
042: import org.objectweb.jonas.jmx.J2eeObjectName;
043: import org.objectweb.jonas.jmx.JonasManagementRepr;
044: import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;
045: import org.objectweb.util.monolog.api.BasicLevel;
046: import org.objectweb.util.monolog.api.Logger;
047:
048: /**
049: * @author Adriana Danes
050: * <br>Contributor Michel-Ange Anton
051: */
052: public class MemoryGraphServlet extends HttpServlet {
053:
054: /**
055: * Logger.
056: */
057: private static Logger logger = Log
058: .getLogger(Log.JONAS_ADMIN_PREFIX);
059:
060: private static final int HEIGHT = 180;
061: private static final int TOP_MARGIN = 35;
062: private static final int RIGHT_MARGIN = 80;
063: private static final int BOTTOM_MARGIN = 10;
064: private static final int LEFT_MARGIN = 70;
065: private static final int HORIZONTAL_SPACE = 30;
066:
067: private static final int Y_LENGTH = HEIGHT - TOP_MARGIN
068: - BOTTOM_MARGIN; // the graph height
069: private static final int GAUGE_WIDTH = 10;
070:
071: private static final Color BG_COLOR = new Color(0x006699);
072: private static final Color FG_COLOR = Color.white;
073: private static final Color GRAPHICS_BG_COLOR = Color.lightGray;
074: private static final Color GRAPHICS_FG_COLOR = Color.darkGray;
075: private static final Color AXIS_COLOR = Color.white;
076: private static final Color TEXT_COLOR = Color.white;
077:
078: /**
079: * Respond to a GET request for the content produced by
080: * this servlet.
081: *
082: * @param req The servlet request we are processing
083: * @param res The servlet response we are producing
084: *
085: * @exception IOException if an input/output error occurs
086: * @exception ServletException if a servlet error occurs
087: */
088: public void doGet(HttpServletRequest req, HttpServletResponse res)
089: throws IOException, ServletException {
090:
091: HttpSession oSession = req.getSession();
092: WhereAreYou oWhere = (WhereAreYou) oSession
093: .getAttribute(WhereAreYou.SESSION_NAME);
094: String serverName = oWhere.getCurrentJonasServerName();
095: String domainName = oWhere.getCurrentDomainName();
096:
097: res.setContentType("image/png");
098: OutputStream out = res.getOutputStream();
099:
100: // get the data to draw:
101: Long[] data = (Long[]) JonasManagementRepr.getAttribute(
102: J2eeObjectName.J2EEServer(domainName, serverName),
103: "tableMeasures", serverName);
104: int xLength = 2 * data.length;
105: int width = LEFT_MARGIN + xLength + HORIZONTAL_SPACE
106: + GAUGE_WIDTH + RIGHT_MARGIN;
107:
108: BufferedImage img = new BufferedImage(width, HEIGHT,
109: BufferedImage.TYPE_INT_RGB);
110: Graphics g = img.getGraphics();
111:
112: // fill the image background:
113: g.setColor(BG_COLOR);
114: g.fillRect(0, 0, width, HEIGHT);
115:
116: // firstly, the memory used history:
117:
118: // draw the chart area:
119: g.setColor(GRAPHICS_BG_COLOR);
120: g.fillRect(cartesianXToGraphicsX(0),
121: cartesianYToGraphicsY(Y_LENGTH), xLength - 1, Y_LENGTH);
122:
123: // search the max value in the data array:
124: long yMax = data[0].longValue();
125: for (int i = 1; i < data.length; i++) {
126: yMax = Math.max(yMax, data[i].longValue());
127:
128: // looking for k and n where: k.10^n <= yMax < (k+1).10^n
129: }
130: long tmp = yMax;
131: int n = 0;
132: while ((tmp / 10) >= 1) {
133: tmp = tmp / 10;
134: n++;
135: }
136: long k = tmp % 10;
137:
138: // pow = 10^n
139: int pow = 1;
140: for (int i = 0; i < n; i++) {
141: pow = pow * 10;
142:
143: // draw the range on the y axis:
144: }
145: for (int i = 1; i <= k; i++) {
146: int y = (new Long(i * Y_LENGTH / (k + 1))).intValue();
147: g.setColor(FG_COLOR);
148: g.drawLine(cartesianXToGraphicsX(-3),
149: cartesianYToGraphicsY(y),
150: cartesianXToGraphicsX(xLength),
151: cartesianYToGraphicsY(y));
152: g.setColor(TEXT_COLOR);
153: g.drawString("" + (i * pow), cartesianXToGraphicsX(-18 - n
154: * 6), cartesianYToGraphicsY(y - 5));
155: }
156:
157: // draw the values:
158: g.setColor(GRAPHICS_FG_COLOR);
159: for (int i = 0; i < data.length; i++) {
160: int y = (new Long(data[i].longValue() * Y_LENGTH
161: / ((k + 1) * pow))).intValue();
162: g.drawLine(cartesianXToGraphicsX(2 * i),
163: cartesianYToGraphicsY(0),
164: cartesianXToGraphicsX(2 * i),
165: cartesianYToGraphicsY(y));
166: }
167:
168: g.setColor(TEXT_COLOR);
169: g.drawString("History of memory used",
170: cartesianXToGraphicsX(0),
171: cartesianYToGraphicsY(Y_LENGTH + 20));
172: g.drawString("(Kbytes)", cartesianXToGraphicsX(0),
173: cartesianYToGraphicsY(Y_LENGTH + 10));
174: g.drawString("0", cartesianXToGraphicsX(-12),
175: cartesianYToGraphicsY(-5));
176: g.setColor(AXIS_COLOR);
177: g.drawLine(cartesianXToGraphicsX(-3), cartesianYToGraphicsY(0),
178: cartesianXToGraphicsX(xLength),
179: cartesianYToGraphicsY(0));
180: g.drawLine(cartesianXToGraphicsX(-1), cartesianYToGraphicsY(0),
181: cartesianXToGraphicsX(-1),
182: cartesianYToGraphicsY(Y_LENGTH));
183: g.drawLine(cartesianXToGraphicsX(xLength - 2),
184: cartesianYToGraphicsY(0),
185: cartesianXToGraphicsX(xLength - 2),
186: cartesianYToGraphicsY(Y_LENGTH));
187:
188: // secondly, the current memory used:
189:
190: int xGauge = LEFT_MARGIN + xLength + HORIZONTAL_SPACE;
191:
192: g.setColor(GRAPHICS_BG_COLOR);
193: g.fillRect(xGauge, cartesianYToGraphicsY(Y_LENGTH),
194: GAUGE_WIDTH, Y_LENGTH);
195:
196: long usedMemory = ((Long) JonasManagementRepr.getAttribute(
197: J2eeObjectName.J2EEServer(domainName, serverName),
198: "currentUsedMemory", serverName)).longValue();
199: long totalMemory = ((Long) JonasManagementRepr.getAttribute(
200: J2eeObjectName.J2EEServer(domainName, serverName),
201: "currentTotalMemory", serverName)).longValue();
202:
203: g.setColor(GRAPHICS_FG_COLOR);
204: int y = (new Long(usedMemory * Y_LENGTH / totalMemory))
205: .intValue();
206: g.fillRect(xGauge, cartesianYToGraphicsY(y), GAUGE_WIDTH, y);
207:
208: g.setColor(AXIS_COLOR);
209: g.drawRect(xGauge, cartesianYToGraphicsY(Y_LENGTH),
210: GAUGE_WIDTH, Y_LENGTH);
211:
212: g.setColor(TEXT_COLOR);
213: g.drawString("0", xGauge + GAUGE_WIDTH + 3,
214: cartesianYToGraphicsY(-5));
215: g.drawString("" + usedMemory, xGauge + GAUGE_WIDTH + 3,
216: cartesianYToGraphicsY(Math.min(y - 5, Y_LENGTH - 25)));
217: g.drawString("(current)", xGauge + GAUGE_WIDTH + 3,
218: cartesianYToGraphicsY(Math.min(y - 15, Y_LENGTH - 35)));
219: g.drawString("" + totalMemory, xGauge + GAUGE_WIDTH + 3,
220: cartesianYToGraphicsY(Y_LENGTH - 5));
221: g.drawString("(total)", xGauge + GAUGE_WIDTH + 3,
222: cartesianYToGraphicsY(Y_LENGTH - 15));
223: g.drawString("Memory used", xGauge,
224: cartesianYToGraphicsY(Y_LENGTH + 20));
225: g.drawString("(Kbytes)", xGauge,
226: cartesianYToGraphicsY(Y_LENGTH + 10));
227:
228: try {
229: ImageIO.write(img, "png", out);
230: } catch (IIOException iioe) {
231: logger.log(BasicLevel.DEBUG, "Flushing problem", iioe);
232: }
233: out.close();
234: }
235:
236: /**
237: * @param length the data array size (data.length)
238: * @return the picture width when displaying an array of the specified size
239: */
240: public static int getWidth(int length) {
241: return LEFT_MARGIN + 2 * length + HORIZONTAL_SPACE
242: + GAUGE_WIDTH + RIGHT_MARGIN;
243: }
244:
245: /**
246: * @return the picture height
247: */
248: public static int getHeight() {
249: return HEIGHT;
250: }
251:
252: private int cartesianXToGraphicsX(int x) {
253: return x + LEFT_MARGIN;
254: }
255:
256: private int cartesianYToGraphicsY(int y) {
257: return HEIGHT - 1 - y - BOTTOM_MARGIN;
258: }
259: }
|