1 23 package com.sun.appserv.management.util.j2ee.stringifier; 24 25 import java.util.Arrays ; 26 import java.util.SortedMap ; 27 import java.util.TreeMap ; 28 import java.util.Iterator ; 29 30 import javax.management.j2ee.statistics.Statistic ; 31 32 import com.sun.appserv.management.util.stringifier.Stringifier; 33 import com.sun.appserv.management.util.stringifier.SmartStringifier; 34 import com.sun.appserv.management.util.misc.StringUtil; 35 36 import com.sun.appserv.management.util.j2ee.J2EEUtil; 37 38 41 public class StatisticStringifier implements Stringifier 42 { 43 public static final StatisticStringifier DEFAULT = new StatisticStringifier(); 44 45 public 46 StatisticStringifier( ) 47 { 48 } 49 50 private final static String DELIM = ", "; 51 private final static String GET = "get"; 52 53 54 private static final String [] ORDERED_VALUES = 55 new String [] 56 { 57 "Name", 58 "Description", 59 "Unit", 60 "StartTime", 61 "LastSampleTime", 62 "Count", 63 "Low", "Current", "High", 64 "LowerBound", "UpperBound", 65 "Current", 66 }; 67 68 private static String 69 createNameValuePair( final String name, final Object value ) 70 { 71 final String valueString = value instanceof String ? 72 StringUtil.quote( (String )value ) : SmartStringifier.toString( value ); 73 74 return name + "=" + valueString; 75 } 76 77 81 public String 82 stringify( Object o ) 83 { 84 final Statistic statistic = (Statistic )o; 85 86 final StringBuffer buf = new StringBuffer (); 87 88 buf.append( statistic.getName() + ": " ); 89 90 final SortedMap <String ,Object > pairs = 91 new TreeMap <String ,Object >( J2EEUtil.statisticToMap( statistic ) ); 92 93 for( int i = 0; i < ORDERED_VALUES.length; ++i ) 95 { 96 final String name = ORDERED_VALUES[ i ]; 97 if ( pairs.containsKey( name ) ) 98 { 99 final Object value = pairs.get( name ); 100 buf.append( createNameValuePair( name, value ) ); 101 buf.append( DELIM ); 102 pairs.remove( name ); 103 } 104 } 105 106 107 final Iterator iter = pairs.keySet().iterator(); 108 while ( iter.hasNext() ) 109 { 110 final String name = (String )iter.next(); 111 final Object value = pairs.get( name ); 112 113 buf.append( createNameValuePair( name, value ) ); 114 buf.append( DELIM ); 115 116 } 117 118 String result = buf.toString(); 119 if ( result.endsWith( DELIM ) ) 120 { 121 result = result.substring( 0, result.length() - DELIM.length() ); 122 } 123 124 125 return( result ); 126 } 127 } 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | Popular Tags |