KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wingset > MemUsageExample


1 /*
2  * $Id: MemUsageExample.java,v 1.3 2004/12/01 07:54:05 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package wingset;
15
16 import org.wings.*;
17 import org.wings.event.SRenderEvent;
18 import org.wings.event.SRenderListener;
19 import org.wings.session.WingsStatistics;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.text.DecimalFormat JavaDoc;
24
25 /**
26  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
27  * @version $Revision: 1.3 $
28  */

29 public class MemUsageExample extends WingSetPane {
30
31     private static final long SECOND = 1000;
32     private static final long MINUTE = 60 * SECOND;
33     private static final long HOUR = 60 * MINUTE;
34     private static final long DAY = 24 * HOUR;
35
36     private static final DecimalFormat JavaDoc megaByteFormatter = new DecimalFormat JavaDoc("#.###");
37
38     private static final String JavaDoc getByteString(long bytes) {
39         if (bytes < 1024) {
40             return Long.toString(bytes);
41         } // end of if ()
42

43         if (bytes < 1024 * 1024) {
44             return Long.toString(bytes / (1024)) + "kB";
45         } // end of if ()
46

47         return megaByteFormatter.format(new Double JavaDoc(((double) bytes) / (1024 * 1024))) + "MB";
48
49     }
50
51     private static final String JavaDoc getUptimeString(long uptime) {
52         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
53
54         boolean doAppend = false;
55
56         if (uptime / DAY > 0) {
57             result.append(uptime / DAY).append("d ");
58             uptime %= DAY;
59             doAppend = true;
60         }
61
62         if (uptime / HOUR > 0 || doAppend) {
63             result.append(uptime / HOUR).append("h ");
64             uptime %= HOUR;
65             doAppend = true;
66         }
67
68         if (uptime / MINUTE > 0 || doAppend) {
69             result.append(uptime / MINUTE).append("m ");
70             uptime %= MINUTE;
71             doAppend = true;
72         }
73
74         if (uptime / SECOND > 0 || doAppend) {
75             result.append(uptime / SECOND).append("s ");
76             uptime %= SECOND;
77         }
78
79         result.append(uptime).append("ms");
80
81         return result.toString();
82     }
83
84     public SComponent createExample() {
85         final SButton gc = new SButton("gc");
86
87         final SProgressBar infoBar = new SProgressBar();
88         final SLabel totalMemory = new SLabel();
89         final SLabel freeMemory = new SLabel();
90         final SLabel usedMemory = new SLabel();
91         final SLabel overallSessions = new SLabel();
92         final SLabel activeSessions = new SLabel();
93         final SLabel uptime = new SLabel();
94         final SLabel requestCount = new SLabel();
95         final SPanel panel = new SPanel();
96
97         gc.addActionListener(new ActionListener JavaDoc() {
98             public void actionPerformed(ActionEvent JavaDoc e) {
99                 System.gc();
100             }
101         });
102
103         SRenderListener renderListener = new SRenderListener() {
104             public void startRendering(SRenderEvent e) {
105                 long free = Runtime.getRuntime().freeMemory();
106                 long total = Runtime.getRuntime().totalMemory();
107
108                 infoBar.setMaximum((int) total);
109                 infoBar.setValue((int) (total - free));
110                 infoBar.setString(getByteString(total - free));
111
112                 totalMemory.setText(getByteString(total));
113                 freeMemory.setText(getByteString(free));
114                 usedMemory.setText(getByteString(total - free));
115                 overallSessions.setText(Integer.toString(WingsStatistics.getStatistics().getOverallSessionCount()));
116                 activeSessions.setText(Integer.toString(WingsStatistics.getStatistics().getActiveSessionCount()));
117                 requestCount.setText(Integer.toString(WingsStatistics.getStatistics().getRequestCount()));
118                 uptime.setText(getUptimeString(WingsStatistics.getStatistics().getUptime()));
119             }
120
121             public void doneRendering(SRenderEvent e) {}
122         };
123         panel.addRenderListener(renderListener);
124
125
126         infoBar.setUnfilledColor(java.awt.Color.gray);
127         infoBar.setFilledColor(java.awt.Color.red);
128         infoBar.setForeground(java.awt.Color.red);
129         infoBar.setBorderColor(java.awt.Color.black);
130         infoBar.setStringPainted(true);
131         infoBar.setPreferredSize(new SDimension(200, 5));
132
133
134         try {
135             java.net.URL JavaDoc templateURL =
136                     getSession().getServletContext().getResource("/templates/MemUsageExample.thtml");
137             STemplateLayout layout = new STemplateLayout(templateURL);
138             panel.setLayout(layout);
139         } catch (Exception JavaDoc ex) {
140             ex.printStackTrace();
141         }
142
143         panel.add(gc, "GC");
144         panel.add(infoBar, "InfoBar");
145         panel.add(totalMemory, "TotalMemory");
146         panel.add(freeMemory, "FreeMemory");
147         panel.add(usedMemory, "UsedMemory");
148         panel.add(activeSessions, "ActiveSessions");
149         panel.add(overallSessions, "OverallSessions");
150         panel.add(requestCount, "RequestCount");
151         panel.add(uptime, "Uptime");
152
153
154         return panel;
155     }
156
157
158 }
159
160
161
Popular Tags