1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jelly.tags.memory;
18
19 import org.apache.commons.jelly.JellyTagException;
20 import org.apache.commons.jelly.TagSupport;
21 import org.apache.commons.jelly.XMLOutput;
22
23 /***
24 * Tag supporting displaying free memory.
25 *
26 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
27 */
28 public class FreeMemoryTag extends TagSupport {
29
30 private static final int MB = 1024 * 1024;
31 private static final int KB = 1024;
32
33 private String style = "mb";
34
35 public void setStyle(String style) {
36 if (style == null) {
37 style = "mb";
38 }
39 this.style = style.toLowerCase();
40 }
41
42 public String getStyle() {
43 return this.style;
44 }
45
46
47
48 public void doTag(XMLOutput output) throws JellyTagException {
49
50 Runtime r = Runtime.getRuntime();
51
52 try {
53 long total = r.totalMemory();
54 long free = total - r.freeMemory();
55
56 if (style.equals("kb")) {
57 free /= KB;
58 total /= KB;
59 }
60 else if (style.equals("mb")) {
61 free /= MB;
62 total /= MB;
63 }
64
65 output.write( free + style + "/" + total + style );
66 }
67 catch ( Exception e ) {
68 throw new JellyTagException( "Error writing to output", e );
69 }
70 }
71
72 }