1 23 package com.sun.appserv.management.j2ee.statistics; 24 25 import java.util.Map ; 26 27 import javax.management.openmbean.CompositeData ; 28 import javax.management.openmbean.CompositeType ; 29 30 import javax.management.j2ee.statistics.Statistic ; 31 import javax.management.j2ee.statistics.CountStatistic ; 32 import javax.management.j2ee.statistics.RangeStatistic ; 33 import javax.management.j2ee.statistics.BoundaryStatistic ; 34 import javax.management.j2ee.statistics.BoundedRangeStatistic ; 35 import javax.management.j2ee.statistics.TimeStatistic ; 36 37 import com.sun.appserv.management.util.jmx.OpenMBeanUtil; 38 import com.sun.appserv.management.util.misc.ClassUtil; 39 import com.sun.appserv.management.util.misc.TypeCast; 40 41 44 public final class StatisticFactory 45 { 46 private StatisticFactory() {} 47 48 49 54 public static Statistic 55 create( Class <? extends Statistic > theInterface, final CompositeData data ) 56 { 57 return( create( theInterface, OpenMBeanUtil.compositeDataToMap( data ) ) ); 58 } 59 60 private static final String COUNT_STATISTIC = CountStatistic .class.getName(); 61 private static final String TIME_STATISTIC = TimeStatistic .class.getName(); 62 private static final String RANGE_STATISTIC = RangeStatistic .class.getName(); 63 private static final String BOUNDARY_STATISTIC = BoundaryStatistic .class.getName(); 64 private static final String BOUNDED_RANGE_STATISTIC = BoundedRangeStatistic .class.getName(); 65 private static final String STRING_STATISTIC = StringStatistic.class.getName(); 66 private static final String MAP_STATISTIC = MapStatistic.class.getName(); 67 private static final String NUMBER_STATISTIC = NumberStatistic.class.getName(); 68 69 70 71 75 public static Statistic 76 create( final CompositeData data ) 77 { 78 final String typeName = data.getCompositeType().getTypeName(); 79 Class <? extends Statistic > theInterface = null; 80 if ( typeName.equals( COUNT_STATISTIC ) ) 81 { 82 theInterface = CountStatistic .class; 83 } 84 else if ( typeName.equals( TIME_STATISTIC ) ) 85 { 86 theInterface = TimeStatistic .class; 87 } 88 else if ( typeName.equals( RANGE_STATISTIC ) ) 89 { 90 theInterface = RangeStatistic .class; 91 } 92 else if ( typeName.equals( BOUNDARY_STATISTIC ) ) 93 { 94 theInterface = BoundaryStatistic .class; 95 } 96 else if ( typeName.equals( BOUNDED_RANGE_STATISTIC ) ) 97 { 98 theInterface = BoundedRangeStatistic .class; 99 } 100 else if ( typeName.equals( STRING_STATISTIC ) ) 101 { 102 theInterface = StringStatistic.class; 103 } 104 else if ( typeName.equals( NUMBER_STATISTIC ) ) 105 { 106 theInterface = NumberStatistic.class; 107 } 108 else if ( typeName.equals( MAP_STATISTIC ) ) 109 { 110 theInterface = MapStatistic.class; 111 } 112 else 113 { 114 try 115 { 116 theInterface = TypeCast.asClass( ClassUtil.classForName( typeName ) ); 117 } 118 catch( Exception e ) 119 { 120 theInterface = Statistic .class; 121 } 122 } 123 124 return( create( theInterface, data ) ); 125 } 126 127 128 137 public static Statistic 138 create( Class <? extends Statistic > theInterface, final Map <String ,?> mappings ) 139 { 140 Statistic result = null; 141 142 if ( theInterface == CountStatistic .class ) 144 { 145 result = new CountStatisticImpl( mappings ); 146 } 147 else if ( theInterface == RangeStatistic .class ) 148 { 149 result = new RangeStatisticImpl( mappings ); 150 } 151 else if ( theInterface == BoundaryStatistic .class ) 152 { 153 result = new BoundaryStatisticImpl( mappings ); 154 } 155 else if ( theInterface == BoundedRangeStatistic .class ) 156 { 157 result = new BoundedRangeStatisticImpl( mappings ); 158 } 159 else if ( theInterface == TimeStatistic .class ) 160 { 161 result = new TimeStatisticImpl( mappings ); 162 } 163 else if ( theInterface == StringStatistic.class ) 164 { 165 result = new StringStatisticImpl( mappings ); 166 } 167 else if ( theInterface == NumberStatistic.class ) 168 { 169 result = new NumberStatisticImpl( mappings ); 170 } 171 else if ( theInterface == MapStatistic.class ) 172 { 173 result = new MapStatisticImpl( mappings ); 174 } 175 else 176 { 177 throw new IllegalArgumentException ( 178 "Unsupported Statistic interface: " + theInterface.getName() ); 179 } 180 181 return( result ); 182 } 183 184 185 186 private static final Class <? extends Statistic >[] KNOWN_STATISTICS = TypeCast.asArray( new Class [] 187 { 188 CountStatistic .class, 189 TimeStatistic .class, 190 BoundedRangeStatistic .class, RangeStatistic .class, 192 BoundaryStatistic .class, 193 StringStatistic.class, 194 NumberStatistic.class, 195 } ); 196 197 private static final String INTERNAL_STRING_STATISTIC_CLASSNAME = 198 "com.sun.enterprise.admin.monitor.stats.StringStatisticImpl"; 199 200 public static Class <? extends Statistic > 201 getInterface( final Statistic s ) 202 { 203 final Class <? extends Statistic > implClass = s.getClass(); 204 205 Class <? extends Statistic > theInterface = MapStatistic.class; 206 207 for( int i = 0; i < KNOWN_STATISTICS.length; ++i ) 208 { 209 final Class <? extends Statistic > candidateInterface = KNOWN_STATISTICS[ i ]; 210 211 if ( candidateInterface.isAssignableFrom( implClass ) ) 212 { 213 theInterface = candidateInterface; 214 break; 215 } 216 } 217 218 if ( theInterface == MapStatistic.class && ! (s instanceof MapStatisticImpl) ) 219 { 220 if ( s.getClass().getName().equals( INTERNAL_STRING_STATISTIC_CLASSNAME ) ) 221 { 222 theInterface = StringStatistic.class; 223 } 224 else 225 { 226 throw new IllegalArgumentException ( "Unknown statistic class: " + s.getClass().getName() ); 227 } 228 } 229 230 return( theInterface ); 231 } 232 233 234 235 } 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | Popular Tags |