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 */
017 package org.apache.geronimo.console.jsr77;
018
019 import java.io.Serializable;
020 import java.text.NumberFormat;
021
022 import org.directwebremoting.annotations.DataTransferObject;
023 import org.directwebremoting.annotations.RemoteProperty;
024
025 /**
026 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
027 */
028 @DataTransferObject
029 public class DynamicServerInfo implements Serializable {
030 private final static long BYTES_MAX = 2048;
031 private final static long KB_MAX = BYTES_MAX * 1024l;
032 private final static long MB_MAX = KB_MAX * 1024l;
033 private final static long GB_MAX = MB_MAX * 1024l;
034 private final static long TB_MAX = GB_MAX * 1024l;
035 private final static double KB_DIV = 1024;
036 private final static double MB_DIV = KB_DIV*1024d;
037 private final static double GB_DIV = MB_DIV*1024d;
038 private final static double TB_DIV = GB_DIV*1024d;
039 private NumberFormat dec2Format;
040 private String memoryCurrent;
041 private String memoryMost;
042 private String memoryAllocated;
043 private String upTime;
044 private long bytesCurrent, bytesMost, bytesAllocated;
045
046 public DynamicServerInfo(long upTime) {
047 this.upTime = calculateTime(upTime);
048 memoryAllocated = memoryCurrent = memoryMost = "Unknown";
049 }
050
051 public DynamicServerInfo(long memoryCurrent, long memoryMost, 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 }