1 23 24 28 29 34 35 package com.sun.enterprise.admin.monitor.stats; 36 37 import java.lang.reflect.Method ; 38 import java.util.Iterator ; 39 import java.util.Map ; 40 import java.util.ArrayList ; 41 import java.util.HashMap ; 42 import javax.management.j2ee.statistics.Stats ; 43 import javax.management.j2ee.statistics.Statistic ; 44 45 57 public class GenericStatsImpl implements Stats { 58 59 private final Class statsInterface; 60 private final Object statsProvider; 61 62 private final Map getters; 63 64 66 public GenericStatsImpl(String statsInterfaceName, Object statsProvider) 67 throws ClassNotFoundException { 68 this(statsInterfaceName, GenericStatsImpl.class.getClassLoader(), statsProvider); 69 } 70 71 73 public GenericStatsImpl(String statsInterfaceName, ClassLoader loader, 74 Object statsProvider) throws ClassNotFoundException { 75 this(Class.forName(statsInterfaceName, true, loader), statsProvider); 76 } 77 78 90 public GenericStatsImpl(Class statsInterface, Object statsProvider) { 91 if (! implementsInterface(statsInterface, statsProvider) || 92 ! extendsStatsInterface(statsInterface)) { 93 throw new IllegalArgumentException ("Contract violation: invalid interface-implementation pair"); 94 } 95 this.statsProvider = statsProvider; 96 this.statsInterface = statsInterface; 97 this.getters = new HashMap (); 98 populateGetterMap(); 99 } 100 101 public Statistic getStatistic(String statisticName) { 102 final Method getter = (Method ) getters.get(statisticName); 103 assert (getter != null) : ("Getter not initialized properly: " + statisticName); 104 Object result = null; 105 try { 106 result = getter.invoke(statsProvider); 107 } 108 catch(Exception e) { 109 final RuntimeException oe = new IllegalStateException (); 110 oe.initCause(e); 111 throw oe; 112 } 113 return ( (Statistic )result ); 114 } 115 116 public String [] getStatisticNames() { 117 118 final String [] names = new String [getters.size()]; 119 return ( (String [])getters.keySet().toArray(names) ); } 121 122 public Statistic [] getStatistics() { 123 return ( getStatisticsOneByOne() ); } 125 126 private Statistic [] getStatisticsOneByOne() { 127 final Iterator iter = getters.keySet().iterator(); 128 final Statistic [] stats = new Statistic [getters.keySet().size()]; 129 int i = 0; 130 while (iter.hasNext()) { 131 final String sn = (String ) iter.next(); 132 stats[i++] = this.getStatistic(sn); 133 } 134 assert (stats.length == i); 135 return ( stats ); 136 } 137 138 private boolean implementsInterface(Class c, Object o) { 139 boolean impls = false; 140 final Class [] interfaces = o.getClass().getInterfaces(); 141 for (int i = 0 ; i < interfaces.length ; i++) { 142 if (interfaces[i].equals(c)){ 143 impls = true; 144 break; 145 } 146 } 147 return ( impls ); 148 } 149 150 private boolean extendsStatsInterface(Class i) { 151 final Class statsInterface = javax.management.j2ee.statistics.Stats .class; 152 return ( statsInterface.isAssignableFrom(i) ); 153 } 154 155 private void populateGetterMap() { 156 final Method [] m = statsInterface.getMethods(); 159 final Method [] apis = filterStatsMethods(m); 161 final Method [] methods = getGetters(apis); 162 final String [] names = methods2Statistics(methods); 163 assert (names.length == methods.length) : ("Statistic names array is not having same length as that of array of getters"); 164 int i; 165 for (i = 0 ; i < names.length ; i++) { 166 getters.put(names[i], methods[i]); 167 } 168 assert (getters.size() == i) : ("Getters map is incorrect, names.length = " + names.length + " methods.length = " + methods.length); 169 } 170 171 private Method [] getGetters(Method [] all) { 172 final ArrayList l = new ArrayList (); 173 for (int i = 0 ; i < all.length ; i++) { 174 final Method am = all[i]; 175 if (isValidGetter(am)) { 176 l.add(am); 177 } 178 } 179 final Method [] m = new Method [l.size()]; 180 return ( (Method [])l.toArray(m) ); 181 } 182 183 private boolean isValidGetter(Method m) { 184 final boolean startsWithGet = m.getName().startsWith("get"); 185 final boolean hasNoParams = m.getParameterTypes().length == 0; 186 final boolean returnsStatistic = Statistic .class.isAssignableFrom(m.getReturnType()); 187 188 return ( startsWithGet && hasNoParams && returnsStatistic ); 189 } 190 191 private String [] methods2Statistics(Method [] methods) { 192 final String [] names = new String [methods.length]; 193 for (int i = 0 ; i < methods.length ; i++) { 194 final String m = methods[i].getName(); 195 final int s = "get".length(); 196 names[i] = m.substring(s); 197 } 198 return ( names ); 199 } 200 201 private boolean isStatsInterfaceMethod(String name) { 202 final Method [] methods = javax.management.j2ee.statistics.Stats .class.getMethods(); 203 boolean isInterfaceMethod = false; 204 for (int i = 0 ; i < methods.length ; i++) { 205 if (methods[i].getName().equals(name)) { 206 isInterfaceMethod = true; 207 break; 208 } 209 } 210 return ( isInterfaceMethod ); 211 } 212 213 private Method [] filterStatsMethods(Method [] m) { 214 ArrayList methodList = new ArrayList (); 215 for(int i = 0; i < m.length; i++) { 216 if(! isStatsInterfaceMethod(m[i].getName())) 217 methodList.add(m[i]); 218 } 219 final Method [] methods = new Method [methodList.size()]; 220 return (Method [])methodList.toArray(methods); 221 } 222 } 223 | Popular Tags |