1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.geronimo.console.jsr77;
18
19 import java.io.Serializable;
20 import java.text.NumberFormat;
21
22 /**
23 * @version $Rev: 392639 $ $Date: 2006-04-08 17:22:16 -0700 (Sat, 08 Apr 2006) $
24 */
25 public class DynamicServerInfo implements Serializable {
26 private final static long BYTES_MAX = 2048;
27 private final static long KB_MAX = BYTES_MAX * 1024l;
28 private final static long MB_MAX = KB_MAX * 1024l;
29 private final static long GB_MAX = MB_MAX * 1024l;
30 private final static long TB_MAX = GB_MAX * 1024l;
31 private final static double KB_DIV = 1024;
32 private final static double MB_DIV = KB_DIV*1024d;
33 private final static double GB_DIV = MB_DIV*1024d;
34 private final static double TB_DIV = GB_DIV*1024d;
35 private NumberFormat dec2Format;
36 private String memoryCurrent;
37 private String memoryMost;
38 private String memoryAllocated;
39 private String upTime;
40 private long bytesCurrent, bytesMost, bytesAllocated;
41
42 public DynamicServerInfo(long upTime) {
43 this.upTime = calculateTime(upTime);
44 memoryAllocated = memoryCurrent = memoryMost = "Unknown";
45 }
46
47 public DynamicServerInfo(long memoryCurrent, long memoryMost, long memoryAllocated, long upTime) {
48 dec2Format = NumberFormat.getNumberInstance();
49 dec2Format.setMaximumFractionDigits(2);
50 bytesCurrent = memoryCurrent;
51 bytesMost = memoryMost;
52 bytesAllocated = memoryAllocated;
53 this.memoryCurrent = calculateMemory(memoryCurrent);
54 this.memoryMost = calculateMemory(memoryMost);
55 this.memoryAllocated = calculateMemory(memoryAllocated);
56 this.upTime = calculateTime(upTime);
57 }
58
59 private String calculateMemory(long bytes) {
60 if(bytes < BYTES_MAX) {
61 return bytes+" B";
62 } else if(bytes < KB_MAX) {
63 return dec2Format.format((double)bytes/KB_DIV)+" kB";
64 } else if(bytes < MB_MAX) {
65 return dec2Format.format((double)bytes/MB_DIV)+" MB";
66 } else if(bytes < GB_MAX) {
67 return dec2Format.format((double)bytes/GB_DIV)+" GB";
68 } else if(bytes < TB_MAX) {
69 return dec2Format.format((double)bytes/TB_DIV)+" TB";
70 } else {
71 return "Out of range";
72 }
73 }
74
75 private String calculateTime(long millis) {
76 int secs = (int)(millis/1000L);
77 int days = secs/86400;
78 secs = secs % 86400;
79 int hours = secs/3600;
80 secs = secs % 3600;
81 int minutes = secs / 60;
82 secs = secs % 60;
83 StringBuffer buf = new StringBuffer();
84 if(days > 1) {
85 buf.append(' ').append(days).append(" days");
86 } else if(days > 0) {
87 buf.append(' ').append(days).append(" day");
88 }
89 if(hours > 1) {
90 buf.append(' ').append(hours).append(" hours");
91 } else if(hours > 0) {
92 buf.append(' ').append(hours).append(" hour");
93 }
94 if(minutes > 1) {
95 buf.append(' ').append(minutes).append(" minutes");
96 } else if(minutes > 0) {
97 buf.append(' ').append(minutes).append(" minute");
98 }
99 if(secs > 1) {
100 buf.append(' ').append(secs).append(" seconds");
101 } else if(secs > 0) {
102 buf.append(' ').append(secs).append(" second");
103 }
104 buf.delete(0,1);
105 return buf.toString();
106 }
107
108 public String getMemoryCurrent() {
109 return memoryCurrent;
110 }
111
112 public String getMemoryMost() {
113 return memoryMost;
114 }
115
116 public String getMemoryAllocated() {
117 return memoryAllocated;
118 }
119
120 public String getUpTime() {
121 return upTime;
122 }
123
124 public long getBytesCurrent() {
125 return bytesCurrent;
126 }
127
128 public long getBytesMost() {
129 return bytesMost;
130 }
131
132 public long getBytesAllocated() {
133 return bytesAllocated;
134 }
135 }