1 25 26 package org.objectweb.jonas.webapp.jonasadmin.monitoring; 27 28 import java.awt.Color ; 30 import java.awt.Graphics ; 31 import java.awt.image.BufferedImage ; 32 import java.io.IOException ; 33 import java.io.OutputStream ; 34 35 import javax.servlet.ServletException ; 36 import javax.servlet.http.HttpServlet ; 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 import javax.servlet.http.HttpSession ; 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 51 public class MemoryGraphServlet extends HttpServlet { 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; private static final int GAUGE_WIDTH = 10; 62 63 private static final Color BG_COLOR = new Color (0x006699); 64 private static final Color FG_COLOR = Color.white; 65 private static final Color GRAPHICS_BG_COLOR = Color.lightGray; 66 private static final Color GRAPHICS_FG_COLOR = Color.darkGray; 67 private static final Color AXIS_COLOR = Color.white; 68 private static final Color TEXT_COLOR = Color.white; 69 70 80 public void doGet(HttpServletRequest req, HttpServletResponse res) 81 throws IOException , ServletException { 82 83 HttpSession oSession = req.getSession(); 84 WhereAreYou oWhere = (WhereAreYou) oSession.getAttribute(WhereAreYou.SESSION_NAME); 85 86 res.setContentType("image/gif"); 87 OutputStream out = res.getOutputStream(); 88 89 Long [] data = (Long []) 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 img = new BufferedImage (width, HEIGHT, BufferedImage.TYPE_INT_RGB); 96 Graphics g = img.getGraphics(); 97 98 g.setColor(BG_COLOR); 100 g.fillRect(0, 0, width, HEIGHT); 101 102 104 g.setColor(GRAPHICS_BG_COLOR); 106 g.fillRect(cartesianXToGraphicsX(0), cartesianYToGraphicsY(Y_LENGTH), xLength - 1, Y_LENGTH); 107 108 long yMax = data[0].longValue(); 110 for (int i = 1; i < data.length; i++) { 111 yMax = Math.max(yMax, data[i].longValue()); 112 113 } 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 int pow = 1; 125 for (int i = 0; i < n; i++) { 126 pow = pow * 10; 127 128 } 130 for (int i = 1; i <= k; i++) { 131 int y = (new Long (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 g.setColor(GRAPHICS_FG_COLOR); 142 for (int i = 0; i < data.length; i++) { 143 int y = (new Long (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 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 ) JonasManagementRepr.getAttribute(J2eeObjectName.J2EEServer(oWhere. 169 getCurrentDomainName(), oWhere.getCurrentJonasServerName()) 170 , "currentUsedMemory")).longValue(); 171 long totalMemory = ((Long ) JonasManagementRepr.getAttribute(J2eeObjectName.J2EEServer( 172 oWhere.getCurrentDomainName(), oWhere.getCurrentJonasServerName()) 173 , "currentTotalMemory")).longValue(); 174 175 g.setColor(GRAPHICS_FG_COLOR); 176 int y = (new Long (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 203 public static int getWidth(int length) { 204 return LEFT_MARGIN + 2 * length + HORIZONTAL_SPACE + GAUGE_WIDTH + RIGHT_MARGIN; 205 } 206 207 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 |