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 /** 023 * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $ 024 */ 025 public class DynamicServerInfo implements Serializable { 026 private final static long BYTES_MAX = 2048; 027 private final static long KB_MAX = BYTES_MAX * 1024l; 028 private final static long MB_MAX = KB_MAX * 1024l; 029 private final static long GB_MAX = MB_MAX * 1024l; 030 private final static long TB_MAX = GB_MAX * 1024l; 031 private final static double KB_DIV = 1024; 032 private final static double MB_DIV = KB_DIV*1024d; 033 private final static double GB_DIV = MB_DIV*1024d; 034 private final static double TB_DIV = GB_DIV*1024d; 035 private NumberFormat dec2Format; 036 private String memoryCurrent; 037 private String memoryMost; 038 private String memoryAllocated; 039 private String upTime; 040 private long bytesCurrent, bytesMost, bytesAllocated; 041 042 public DynamicServerInfo(long upTime) { 043 this.upTime = calculateTime(upTime); 044 memoryAllocated = memoryCurrent = memoryMost = "Unknown"; 045 } 046 047 public DynamicServerInfo(long memoryCurrent, long memoryMost, long memoryAllocated, long upTime) { 048 dec2Format = NumberFormat.getNumberInstance(); 049 dec2Format.setMaximumFractionDigits(2); 050 bytesCurrent = memoryCurrent; 051 bytesMost = memoryMost; 052 bytesAllocated = memoryAllocated; 053 this.memoryCurrent = calculateMemory(memoryCurrent); 054 this.memoryMost = calculateMemory(memoryMost); 055 this.memoryAllocated = calculateMemory(memoryAllocated); 056 this.upTime = calculateTime(upTime); 057 } 058 059 private String calculateMemory(long bytes) { 060 if(bytes < BYTES_MAX) { 061 return bytes+" B"; 062 } else if(bytes < KB_MAX) { 063 return dec2Format.format((double)bytes/KB_DIV)+" kB"; 064 } else if(bytes < MB_MAX) { 065 return dec2Format.format((double)bytes/MB_DIV)+" MB"; 066 } else if(bytes < GB_MAX) { 067 return dec2Format.format((double)bytes/GB_DIV)+" GB"; 068 } else if(bytes < TB_MAX) { 069 return dec2Format.format((double)bytes/TB_DIV)+" TB"; 070 } else { 071 return "Out of range"; 072 } 073 } 074 075 private String calculateTime(long millis) { 076 int secs = (int)(millis/1000L); 077 int days = secs/86400; 078 secs = secs % 86400; 079 int hours = secs/3600; 080 secs = secs % 3600; 081 int minutes = secs / 60; 082 secs = secs % 60; 083 StringBuffer buf = new StringBuffer(); 084 if(days > 1) { 085 buf.append(' ').append(days).append(" days"); 086 } else if(days > 0) { 087 buf.append(' ').append(days).append(" day"); 088 } 089 if(hours > 1) { 090 buf.append(' ').append(hours).append(" hours"); 091 } else if(hours > 0) { 092 buf.append(' ').append(hours).append(" hour"); 093 } 094 if(minutes > 1) { 095 buf.append(' ').append(minutes).append(" minutes"); 096 } else if(minutes > 0) { 097 buf.append(' ').append(minutes).append(" minute"); 098 } 099 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 }