1 23 package com.sun.appserv.management.j2ee.statistics; 24 25 import java.util.Map ; 26 import java.lang.reflect.Proxy ; 27 28 import javax.management.openmbean.CompositeData ; 29 import javax.management.openmbean.CompositeType ; 30 31 import javax.management.j2ee.statistics.*; 32 33 import com.sun.appserv.management.util.jmx.OpenMBeanUtil; 34 import com.sun.appserv.management.util.j2ee.J2EEUtil; 35 import com.sun.appserv.management.util.misc.TypeCast; 36 import com.sun.appserv.management.util.misc.ClassUtil; 37 38 42 public final class StatsFactory 43 { 44 private StatsFactory() {} 45 46 47 52 public static Stats 53 create( Class <? extends Stats> theInterface, final CompositeData data ) 54 { 55 final Map <String ,Statistic> statistics = TypeCast.asMap( compositeDataToMap( data ) ); 56 57 return( createStats( theInterface, statistics ) ); 58 } 59 60 public static Map <String ,Statistic> 61 compositeDataToMap( final CompositeData data ) 62 { 63 return( TypeCast.asMap( OpenMBeanUtil.compositeDataToMap( data ) ) ); 64 } 65 66 67 71 public static Stats 72 createStats( final CompositeData data ) 73 { 74 final String typeName = data.getCompositeType().getTypeName(); 75 Class <? extends Stats> theInterface = null; 76 77 try 78 { 79 theInterface = TypeCast.asClass( ClassUtil.classForName( typeName ) ); 80 } 81 catch( Exception e ) 82 { 83 theInterface = Stats.class; 84 } 85 86 return( create( theInterface, data ) ); 87 } 88 89 90 99 public static <T extends Stats> T 100 createStats( Class <T> theInterface, final Map <String ,Statistic> statistics ) 101 { 102 if ( ! Stats.class.isAssignableFrom( theInterface ) ) 103 { 104 throw new IllegalArgumentException ( theInterface.getName() ); 105 } 106 107 final MapGetterInvocationHandler handler = 109 new MapGetterInvocationHandler<Statistic>( statistics ); 110 final ClassLoader classLoader = theInterface.getClassLoader(); 111 112 final Class <T>[] interfaces = TypeCast.asArray( new Class [] { theInterface } ); 113 114 final Object proxy = Proxy.newProxyInstance( classLoader, interfaces, handler); 115 116 return theInterface.cast( proxy ); 117 } 118 119 private void test() 120 { 121 createStats( EJBStats.class, new java.util.HashMap <String ,Statistic>() ); 122 } 123 124 127 public static <T extends Stats> T 128 createStats( Class <T> theInterface, final Statistic[] statistics ) 129 { 130 final Map <String ,Statistic> statisticsMap = J2EEUtil.statisticsToMap( statistics ); 131 132 return( createStats( theInterface, statisticsMap ) ); 133 } 134 135 public static EJBStats 136 createEJBStats( final Map <String ,Statistic> statistics ) 137 { 138 return( createStats( EJBStats.class, statistics ) ); 139 } 140 141 public static URLStats 142 createURLStats( final Map <String ,Statistic> statistics ) 143 { 144 return( createStats( URLStats.class, statistics ) ); 145 } 146 147 public static EntityBeanStats 148 createEntityBeanStats( final Map <String ,Statistic> statistics ) 149 { 150 return( createStats( EntityBeanStats.class, statistics ) ); 151 } 152 153 public static JCAConnectionPoolStats 154 createJCAConnectionPoolStats( final Map <String ,Statistic> statistics ) 155 { 156 return( createStats( JCAConnectionPoolStats.class, statistics ) ); 157 } 158 159 public static JCAConnectionStats 160 createJCAConnectionStats( final Map <String ,Statistic> statistics ) 161 { 162 return( createStats( JCAConnectionStats.class, statistics ) ); 163 } 164 165 public static JCAStats 166 createJCAStats( final Map <String ,Statistic> statistics ) 167 { 168 return( createStats( JCAStats.class, statistics ) ); 169 } 170 171 public static JDBCConnectionPoolStats 172 createJDBCConnectionPoolStats( final Map <String ,Statistic> statistics ) 173 { 174 return( createStats( JDBCConnectionPoolStats.class, statistics ) ); 175 } 176 177 public static JDBCConnectionStats 178 createJDBCConnectionStats( final Map <String ,Statistic> statistics ) 179 { 180 return( createStats( JDBCConnectionStats.class, statistics ) ); 181 } 182 183 public static JDBCStats 184 createJDBCStats( final Map <String ,Statistic> statistics ) 185 { 186 return( createStats( JDBCStats.class, statistics ) ); 187 } 188 189 public static JMSConnectionStats 190 createJMSConnectionStats( final Map <String ,Statistic> statistics ) 191 { 192 return( createStats( JMSConnectionStats.class, statistics ) ); 193 } 194 195 public static JMSConsumerStats 196 createJMSConsumerStats( final Map <String ,Statistic> statistics ) 197 { 198 return( createStats( JMSConsumerStats.class, statistics ) ); 199 } 200 201 public static JMSEndpointStats 202 createJMSEndpointStats( final Map <String ,Statistic> statistics ) 203 { 204 return( createStats( JMSEndpointStats.class, statistics ) ); 205 } 206 207 public static JMSProducerStats 208 createJMSProducerStats( final Map <String ,Statistic> statistics ) 209 { 210 return( createStats( JMSProducerStats.class, statistics ) ); 211 } 212 213 public static JMSSessionStats 214 createJMSSessionStats( final Map <String ,Statistic> statistics ) 215 { 216 return( createStats( JMSSessionStats.class, statistics ) ); 217 } 218 219 public static JMSStats 220 createJMSStats( final Map <String ,Statistic> statistics ) 221 { 222 return( createStats( JMSStats.class, statistics ) ); 223 } 224 225 public static JTAStats 226 createJTAStats( final Map <String ,Statistic> statistics ) 227 { 228 return( createStats( JTAStats.class, statistics ) ); 229 } 230 231 public static JVMStats 232 createJVMStats( final Map <String ,Statistic> statistics ) 233 { 234 return( createStats( JVMStats.class, statistics ) ); 235 } 236 237 public static MessageDrivenBeanStats 238 createMessageDrivenBeanStats( final Map <String ,Statistic> statistics ) 239 { 240 return( createStats( MessageDrivenBeanStats.class, statistics ) ); 241 } 242 243 public static ServletStats 244 createServletStats( final Map <String ,Statistic> statistics ) 245 { 246 return( createStats( ServletStats.class, statistics ) ); 247 } 248 249 public static SessionBeanStats 250 createSessionBeanStats( final Map <String ,Statistic> statistics ) 251 { 252 return( createStats( SessionBeanStats.class, statistics ) ); 253 } 254 255 public static StatefulSessionBeanStats 256 createStatefulSessionBeanStats( final Map <String ,Statistic> statistics ) 257 { 258 return( createStats( StatefulSessionBeanStats.class, statistics ) ); 259 } 260 261 public static StatelessSessionBeanStats 262 createStatelessSessionBeanStats( final Map <String ,Statistic> statistics ) 263 { 264 return( createStats( StatelessSessionBeanStats.class, statistics ) ); 265 } 266 267 public static JavaMailStats 268 createJavaMailStats( final Map <String ,Statistic> statistics ) 269 { 270 final Class <JavaMailStats> theInterface = JavaMailStats.class; 272 JavaMailStats result = null; 273 274 final MapGetterInvocationHandler handler = 276 new MapGetterInvocationHandler<Statistic>( statistics ); 277 final ClassLoader classLoader = theInterface.getClassLoader(); 278 279 final Class <JavaMailStats>[] interfaces = TypeCast.asArray( new Class [] { theInterface } ); 280 281 result = (JavaMailStats)Proxy.newProxyInstance( classLoader, interfaces, handler); 282 283 return result; 284 } 285 286 } 287 288 289 290 291 292 | Popular Tags |