001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.console.jsr77;
017:
018: import java.io.Serializable;
019: import java.text.NumberFormat;
020:
021: import org.directwebremoting.annotations.DataTransferObject;
022: import org.directwebremoting.annotations.RemoteProperty;
023:
024: /**
025: * @version $Rev: 556507 $ $Date: 2007-07-15 22:37:07 -0700 (Sun, 15 Jul 2007) $
026: */
027: @DataTransferObject
028: public class DynamicServerInfo implements Serializable {
029: private final static long BYTES_MAX = 2048;
030: private final static long KB_MAX = BYTES_MAX * 1024l;
031: private final static long MB_MAX = KB_MAX * 1024l;
032: private final static long GB_MAX = MB_MAX * 1024l;
033: private final static long TB_MAX = GB_MAX * 1024l;
034: private final static double KB_DIV = 1024;
035: private final static double MB_DIV = KB_DIV * 1024d;
036: private final static double GB_DIV = MB_DIV * 1024d;
037: private final static double TB_DIV = GB_DIV * 1024d;
038: private NumberFormat dec2Format;
039: private String memoryCurrent;
040: private String memoryMost;
041: private String memoryAllocated;
042: private String upTime;
043: private long bytesCurrent, bytesMost, bytesAllocated;
044:
045: public DynamicServerInfo(long upTime) {
046: this .upTime = calculateTime(upTime);
047: memoryAllocated = memoryCurrent = memoryMost = "Unknown";
048: }
049:
050: public DynamicServerInfo(long memoryCurrent, long memoryMost,
051: long memoryAllocated, long upTime) {
052: dec2Format = NumberFormat.getNumberInstance();
053: dec2Format.setMaximumFractionDigits(2);
054: bytesCurrent = memoryCurrent;
055: bytesMost = memoryMost;
056: bytesAllocated = memoryAllocated;
057: this .memoryCurrent = calculateMemory(memoryCurrent);
058: this .memoryMost = calculateMemory(memoryMost);
059: this .memoryAllocated = calculateMemory(memoryAllocated);
060: this .upTime = calculateTime(upTime);
061: }
062:
063: private String calculateMemory(long bytes) {
064: if (bytes < BYTES_MAX) {
065: return bytes + " B";
066: } else if (bytes < KB_MAX) {
067: return dec2Format.format((double) bytes / KB_DIV) + " kB";
068: } else if (bytes < MB_MAX) {
069: return dec2Format.format((double) bytes / MB_DIV) + " MB";
070: } else if (bytes < GB_MAX) {
071: return dec2Format.format((double) bytes / GB_DIV) + " GB";
072: } else if (bytes < TB_MAX) {
073: return dec2Format.format((double) bytes / TB_DIV) + " TB";
074: } else {
075: return "Out of range";
076: }
077: }
078:
079: private String calculateTime(long millis) {
080: int secs = (int) (millis / 1000L);
081: int days = secs / 86400;
082: secs = secs % 86400;
083: int hours = secs / 3600;
084: secs = secs % 3600;
085: int minutes = secs / 60;
086: secs = secs % 60;
087: StringBuffer buf = new StringBuffer();
088: if (days > 1) {
089: buf.append(' ').append(days).append(" days");
090: } else if (days > 0) {
091: buf.append(' ').append(days).append(" day");
092: }
093: if (hours > 1) {
094: buf.append(' ').append(hours).append(" hours");
095: } else if (hours > 0) {
096: buf.append(' ').append(hours).append(" hour");
097: }
098: if (minutes > 1) {
099: buf.append(' ').append(minutes).append(" minutes");
100: } else if (minutes > 0) {
101: buf.append(' ').append(minutes).append(" minute");
102: }
103: if (secs > 1) {
104: buf.append(' ').append(secs).append(" seconds");
105: } else if (secs > 0) {
106: buf.append(' ').append(secs).append(" second");
107: }
108: buf.delete(0, 1);
109: return buf.toString();
110: }
111:
112: @RemoteProperty
113: public String getMemoryCurrent() {
114: return memoryCurrent;
115: }
116:
117: @RemoteProperty
118: public String getMemoryMost() {
119: return memoryMost;
120: }
121:
122: @RemoteProperty
123: public String getMemoryAllocated() {
124: return memoryAllocated;
125: }
126:
127: @RemoteProperty
128: public String getUpTime() {
129: return upTime;
130: }
131:
132: @RemoteProperty
133: public long getBytesCurrent() {
134: return bytesCurrent;
135: }
136:
137: @RemoteProperty
138: public long getBytesMost() {
139: return bytesMost;
140: }
141:
142: @RemoteProperty
143: public long getBytesAllocated() {
144: return bytesAllocated;
145: }
146: }
|