001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package wingset;
014:
015: import org.wings.*;
016: import org.wings.event.SRenderEvent;
017: import org.wings.event.SRenderListener;
018: import org.wings.session.WingsStatistics;
019: import org.wings.util.SStringBuilder;
020:
021: import java.awt.event.ActionEvent;
022: import java.text.DecimalFormat;
023:
024: /**
025: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
026: */
027: public class MemUsageExample extends WingSetPane {
028: private static final DecimalFormat megaByteFormatter = new DecimalFormat(
029: "#.###");
030:
031: protected SComponent createControls() {
032: return null;
033: }
034:
035: public SComponent createExample() {
036: final SButton gc = new SButton("gc"); // label overwritten via attribute in template
037: final SButton refresh = new SButton("Refresh");
038: final SButton exit = new SButton("Exit session");
039: final SProgressBar progressBar = new SProgressBar();
040: final SLabel totalMemory = new SLabel();
041: final SLabel freeMemory = new SLabel();
042: final SLabel usedMemory = new SLabel();
043: final SLabel overallSessions = new SLabel();
044: final SLabel activeSessions = new SLabel();
045: final SLabel uptime = new SLabel();
046: final SLabel requestCount = new SLabel();
047: final SPanel panel = new SPanel();
048:
049: // Action listener for GC button trigger
050: gc.addActionListener(new java.awt.event.ActionListener() {
051: public void actionPerformed(ActionEvent e) {
052: System.gc();
053: panel.reload();
054: }
055: });
056: // Action listener for refresh button trigger
057: refresh.addActionListener(new java.awt.event.ActionListener() {
058: public void actionPerformed(ActionEvent e) {
059: panel.reload();
060: }
061: });
062: // Action listener for exit button trigger
063: exit.addActionListener(new java.awt.event.ActionListener() {
064: public void actionPerformed(ActionEvent e) {
065: getSession().exit();
066: }
067: });
068:
069: // This render listenre updates all lables when the page starts to render.
070: SRenderListener renderListener = new SRenderListener() {
071: public void startRendering(SRenderEvent e) {
072: long free = Runtime.getRuntime().freeMemory();
073: long total = Runtime.getRuntime().totalMemory();
074:
075: progressBar.setMaximum((int) total);
076: progressBar.setValue((int) (total - free));
077: progressBar.setString(getByteString(total - free));
078:
079: totalMemory.setText(getByteString(total));
080: freeMemory.setText(getByteString(free));
081: usedMemory.setText(getByteString(total - free));
082: overallSessions.setText(Integer
083: .toString(WingsStatistics.getStatistics()
084: .getOverallSessionCount()));
085: activeSessions.setText(Integer.toString(WingsStatistics
086: .getStatistics().getActiveSessionCount()));
087: requestCount.setText(Integer.toString(WingsStatistics
088: .getStatistics().getRequestCount()));
089: uptime.setText(getUptimeString(WingsStatistics
090: .getStatistics().getUptime()));
091: }
092:
093: public void doneRendering(SRenderEvent e) {
094: }
095: };
096: panel.addRenderListener(renderListener);
097:
098: // style progress bar
099: progressBar.setUnfilledColor(java.awt.Color.lightGray);
100: progressBar.setFilledColor(java.awt.Color.red);
101: progressBar.setForeground(java.awt.Color.red);
102: progressBar.setStringPainted(true);
103: progressBar.setProgressBarDimension(new SDimension(200, 5));
104:
105: try {
106: java.net.URL templateURL = getSession().getServletContext()
107: .getResource("/templates/MemUsageExample.thtml");
108: STemplateLayout layout = new STemplateLayout(templateURL);
109: panel.setLayout(layout);
110: } catch (Exception ex) {
111: ex.printStackTrace();
112: }
113:
114: panel.add(gc, "GC");
115: panel.add(refresh, "refresh");
116: panel.add(exit, "exit");
117: panel.add(progressBar, "InfoBar");
118: panel.add(totalMemory, "TotalMemory");
119: panel.add(freeMemory, "FreeMemory");
120: panel.add(usedMemory, "UsedMemory");
121: panel.add(activeSessions, "ActiveSessions");
122: panel.add(overallSessions, "OverallSessions");
123: panel.add(requestCount, "RequestCount");
124: panel.add(uptime, "Uptime");
125:
126: return panel;
127: }
128:
129: private static final String getByteString(long bytes) {
130: if (bytes < 1024) {
131: return Long.toString(bytes);
132: }
133: if (bytes < 1024 * 1024) {
134: return Long.toString(bytes / (1024)) + "kB";
135: }
136: return megaByteFormatter.format(new Double(((double) bytes)
137: / (1024 * 1024)))
138: + "MB";
139:
140: }
141:
142: private static final String getUptimeString(long uptime) {
143: final long SECOND = 1000;
144: final long MINUTE = 60 * SECOND;
145: final long HOUR = 60 * MINUTE;
146: final long DAY = 24 * HOUR;
147: final SStringBuilder result = new SStringBuilder();
148: boolean doAppend = false;
149:
150: if (uptime / DAY > 0) {
151: result.append(uptime / DAY).append("d ");
152: uptime %= DAY;
153: doAppend = true;
154: }
155: if (uptime / HOUR > 0 || doAppend) {
156: result.append(uptime / HOUR).append("h ");
157: uptime %= HOUR;
158: doAppend = true;
159: }
160: if (uptime / MINUTE > 0 || doAppend) {
161: result.append(uptime / MINUTE).append("m ");
162: uptime %= MINUTE;
163: doAppend = true;
164: }
165: if (uptime / SECOND > 0 || doAppend) {
166: result.append(uptime / SECOND).append("s ");
167: uptime %= SECOND;
168: }
169: result.append(uptime).append("ms");
170:
171: return result.toString();
172: }
173: }
|