KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > jonasadmin > monitoring > MemoryGraphServlet


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: MemoryGraphServlet.java,v 1.9 2004/03/19 14:31:52 sauthieg Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.webapp.jonasadmin.monitoring;
27
28 // servlet imports
29
import java.awt.Color JavaDoc;
30 import java.awt.Graphics JavaDoc;
31 import java.awt.image.BufferedImage JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.OutputStream JavaDoc;
34
35 import javax.servlet.ServletException JavaDoc;
36 import javax.servlet.http.HttpServlet JavaDoc;
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39 import javax.servlet.http.HttpSession JavaDoc;
40
41 import org.objectweb.jonas.jmx.J2eeObjectName;
42 import org.objectweb.jonas.jmx.JonasManagementRepr;
43 import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;
44
45 import Acme.JPM.Encoders.GifEncoder;
46
47 /**
48  * @author Adriana Danes
49  * <br>Contributor Michel-Ange Anton
50  */

51 public class MemoryGraphServlet extends HttpServlet JavaDoc {
52
53     private static final int HEIGHT = 180;
54     private static final int TOP_MARGIN = 35;
55     private static final int RIGHT_MARGIN = 80;
56     private static final int BOTTOM_MARGIN = 10;
57     private static final int LEFT_MARGIN = 70;
58     private static final int HORIZONTAL_SPACE = 30;
59
60     private static final int Y_LENGTH = HEIGHT - TOP_MARGIN - BOTTOM_MARGIN; // the graph height
61
private static final int GAUGE_WIDTH = 10;
62
63     private static final Color JavaDoc BG_COLOR = new Color JavaDoc(0x006699);
64     private static final Color JavaDoc FG_COLOR = Color.white;
65     private static final Color JavaDoc GRAPHICS_BG_COLOR = Color.lightGray;
66     private static final Color JavaDoc GRAPHICS_FG_COLOR = Color.darkGray;
67     private static final Color JavaDoc AXIS_COLOR = Color.white;
68     private static final Color JavaDoc TEXT_COLOR = Color.white;
69
70     /**
71      * Respond to a GET request for the content produced by
72      * this servlet.
73      *
74      * @param req The servlet request we are processing
75      * @param res The servlet response we are producing
76      *
77      * @exception IOException if an input/output error occurs
78      * @exception ServletException if a servlet error occurs
79      */

80     public void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
81         throws IOException JavaDoc, ServletException JavaDoc {
82
83         HttpSession JavaDoc oSession = req.getSession();
84         WhereAreYou oWhere = (WhereAreYou) oSession.getAttribute(WhereAreYou.SESSION_NAME);
85
86         res.setContentType("image/gif");
87         OutputStream JavaDoc out = res.getOutputStream();
88
89         // get the data to draw:
90
Long JavaDoc[] data = (Long JavaDoc[]) JonasManagementRepr.getAttribute(J2eeObjectName.J2EEServer(oWhere.
91             getCurrentDomainName(), oWhere.getCurrentJonasServerName()), "tableMeasures");
92         int xLength = 2 * data.length;
93         int width = LEFT_MARGIN + xLength + HORIZONTAL_SPACE + GAUGE_WIDTH + RIGHT_MARGIN;
94
95         BufferedImage JavaDoc img = new BufferedImage JavaDoc(width, HEIGHT, BufferedImage.TYPE_INT_RGB);
96         Graphics JavaDoc g = img.getGraphics();
97
98         // fill the image background:
99
g.setColor(BG_COLOR);
100         g.fillRect(0, 0, width, HEIGHT);
101
102         // firstly, the memory used history:
103

104         // draw the chart area:
105
g.setColor(GRAPHICS_BG_COLOR);
106         g.fillRect(cartesianXToGraphicsX(0), cartesianYToGraphicsY(Y_LENGTH), xLength - 1, Y_LENGTH);
107
108         // search the max value in the data array:
109
long yMax = data[0].longValue();
110         for (int i = 1; i < data.length; i++) {
111             yMax = Math.max(yMax, data[i].longValue());
112
113             // looking for k and n where: k.10^n <= yMax < (k+1).10^n
114
}
115         long tmp = yMax;
116         int n = 0;
117         while ((tmp / 10) >= 1) {
118             tmp = tmp / 10;
119             n++;
120         }
121         long k = tmp % 10;
122
123         // pow = 10^n
124
int pow = 1;
125         for (int i = 0; i < n; i++) {
126             pow = pow * 10;
127
128             // draw the range on the y axis:
129
}
130         for (int i = 1; i <= k; i++) {
131             int y = (new Long JavaDoc(i * Y_LENGTH / (k + 1))).intValue();
132             g.setColor(FG_COLOR);
133             g.drawLine(cartesianXToGraphicsX( -3), cartesianYToGraphicsY(y)
134                 , cartesianXToGraphicsX(xLength), cartesianYToGraphicsY(y));
135             g.setColor(TEXT_COLOR);
136             g.drawString("" + (i * pow), cartesianXToGraphicsX( -18 - n * 6)
137                 , cartesianYToGraphicsY(y - 5));
138         }
139
140         // draw the values:
141
g.setColor(GRAPHICS_FG_COLOR);
142         for (int i = 0; i < data.length; i++) {
143             int y = (new Long JavaDoc(data[i].longValue() * Y_LENGTH / ((k + 1) * pow))).intValue();
144             g.drawLine(cartesianXToGraphicsX(2 * i), cartesianYToGraphicsY(0)
145                 , cartesianXToGraphicsX(2 * i), cartesianYToGraphicsY(y));
146         }
147
148         g.setColor(TEXT_COLOR);
149         g.drawString("History of memory used", cartesianXToGraphicsX(0)
150             , cartesianYToGraphicsY(Y_LENGTH + 20));
151         g.drawString("(Kbytes)", cartesianXToGraphicsX(0), cartesianYToGraphicsY(Y_LENGTH + 10));
152         g.drawString("0", cartesianXToGraphicsX( -12), cartesianYToGraphicsY( -5));
153         g.setColor(AXIS_COLOR);
154         g.drawLine(cartesianXToGraphicsX( -3), cartesianYToGraphicsY(0)
155             , cartesianXToGraphicsX(xLength), cartesianYToGraphicsY(0));
156         g.drawLine(cartesianXToGraphicsX( -1), cartesianYToGraphicsY(0), cartesianXToGraphicsX( -1)
157             , cartesianYToGraphicsY(Y_LENGTH));
158         g.drawLine(cartesianXToGraphicsX(xLength - 2), cartesianYToGraphicsY(0)
159             , cartesianXToGraphicsX(xLength - 2), cartesianYToGraphicsY(Y_LENGTH));
160
161         // secondly, the current memory used:
162

163         int xGauge = LEFT_MARGIN + xLength + HORIZONTAL_SPACE;
164
165         g.setColor(GRAPHICS_BG_COLOR);
166         g.fillRect(xGauge, cartesianYToGraphicsY(Y_LENGTH), GAUGE_WIDTH, Y_LENGTH);
167
168         long usedMemory = ((Long JavaDoc) JonasManagementRepr.getAttribute(J2eeObjectName.J2EEServer(oWhere.
169             getCurrentDomainName(), oWhere.getCurrentJonasServerName())
170             , "currentUsedMemory")).longValue();
171         long totalMemory = ((Long JavaDoc) JonasManagementRepr.getAttribute(J2eeObjectName.J2EEServer(
172             oWhere.getCurrentDomainName(), oWhere.getCurrentJonasServerName())
173             , "currentTotalMemory")).longValue();
174
175         g.setColor(GRAPHICS_FG_COLOR);
176         int y = (new Long JavaDoc(usedMemory * Y_LENGTH / totalMemory)).intValue();
177         g.fillRect(xGauge, cartesianYToGraphicsY(y), GAUGE_WIDTH, y);
178
179         g.setColor(AXIS_COLOR);
180         g.drawRect(xGauge, cartesianYToGraphicsY(Y_LENGTH), GAUGE_WIDTH, Y_LENGTH);
181
182         g.setColor(TEXT_COLOR);
183         g.drawString("0", xGauge + GAUGE_WIDTH + 3, cartesianYToGraphicsY( -5));
184         g.drawString("" + usedMemory, xGauge + GAUGE_WIDTH + 3
185             , cartesianYToGraphicsY(Math.min(y - 5, Y_LENGTH - 25)));
186         g.drawString("(current)", xGauge + GAUGE_WIDTH + 3, cartesianYToGraphicsY(Math.min(y - 15
187             , Y_LENGTH - 35)));
188         g.drawString("" + totalMemory, xGauge + GAUGE_WIDTH + 3, cartesianYToGraphicsY(Y_LENGTH - 5));
189         g.drawString("(total)", xGauge + GAUGE_WIDTH + 3, cartesianYToGraphicsY(Y_LENGTH - 15));
190         g.drawString("Memory used", xGauge, cartesianYToGraphicsY(Y_LENGTH + 20));
191         g.drawString("(Kbytes)", xGauge, cartesianYToGraphicsY(Y_LENGTH + 10));
192
193         GifEncoder ge = new GifEncoder(img, out);
194         ge.setDimensions(width, HEIGHT);
195         ge.encode();
196         out.close();
197     }
198
199     /**
200      * @param length the data array size (data.length)
201      * @return the picture width when displaying an array of the specified size
202      */

203     public static int getWidth(int length) {
204         return LEFT_MARGIN + 2 * length + HORIZONTAL_SPACE + GAUGE_WIDTH + RIGHT_MARGIN;
205     }
206
207     /**
208      * @return the picture height
209      */

210     public static int getHeight() {
211         return HEIGHT;
212     }
213
214     private int cartesianXToGraphicsX(int x) {
215         return x + LEFT_MARGIN;
216     }
217
218     private int cartesianYToGraphicsY(int y) {
219         return HEIGHT - 1 - y - BOTTOM_MARGIN;
220     }
221 }
222
Popular Tags