Constructors Summary |
---|
public GenericStatsImpl(String statsInterfaceName, Object statsProvider)
this(statsInterfaceName, GenericStatsImpl.class.getClassLoader(), statsProvider);
|
public GenericStatsImpl(String statsInterfaceName, ClassLoader loader, Object statsProvider)
this(Class.forName(statsInterfaceName, true, loader), statsProvider);
|
public GenericStatsImpl(Class statsInterface, Object statsProvider)Constructs a new instance of this class for a given interface and its implementation.
It is mandatory that following contract is satisfied to call this satisfactorily:
- None of the parameters are null.
- Given statsProvider implements the given statsInterface.
- Given statsInterface has to extend the @{link Stats} interface.
Note that it is expected (though not mandatory) to have a getXXX method that
does not return an instance of {@link Statistic} interface.
if (! implementsInterface(statsInterface, statsProvider) ||
! extendsStatsInterface(statsInterface)) {
throw new IllegalArgumentException("Contract violation: invalid interface-implementation pair");
}
this.statsProvider = statsProvider;
this.statsInterface = statsInterface;
this.getters = new HashMap();
populateGetterMap();
|
Methods Summary |
---|
private boolean | extendsStatsInterface(java.lang.Class i)
final Class statsInterface = javax.management.j2ee.statistics.Stats.class;
return ( statsInterface.isAssignableFrom(i) );
|
private java.lang.reflect.Method[] | filterStatsMethods(java.lang.reflect.Method[] m)
ArrayList methodList = new ArrayList();
for(int i = 0; i < m.length; i++) {
if(! isStatsInterfaceMethod(m[i].getName()))
methodList.add(m[i]);
}
final Method[] methods = new Method[methodList.size()];
return (Method[])methodList.toArray(methods);
|
private java.lang.reflect.Method[] | getGetters(java.lang.reflect.Method[] all)
final ArrayList l = new ArrayList();
for (int i = 0 ; i < all.length ; i++) {
final Method am = all[i];
if (isValidGetter(am)) {
l.add(am);
}
}
final Method[] m = new Method[l.size()];
return ( (Method[])l.toArray(m) );
|
public javax.management.j2ee.statistics.Statistic | getStatistic(java.lang.String statisticName)
final Method getter = (Method) getters.get(statisticName);
assert (getter != null) : ("Getter not initialized properly: " + statisticName);
Object result = null;
try {
result = getter.invoke(statsProvider);
}
catch(Exception e) {
final RuntimeException oe = new IllegalStateException();
oe.initCause(e);
throw oe;
}
return ( (Statistic)result );
|
public java.lang.String[] | getStatisticNames()
/* The return array is fixed at the construction time */
final String[] names = new String[getters.size()];
return ( (String[])getters.keySet().toArray(names) ); //TODOOOOOOO
|
public javax.management.j2ee.statistics.Statistic[] | getStatistics()
return ( getStatisticsOneByOne() ); //invokes sequentially
|
private javax.management.j2ee.statistics.Statistic[] | getStatisticsOneByOne()
final Iterator iter = getters.keySet().iterator();
final Statistic[] stats = new Statistic[getters.keySet().size()];
int i = 0;
while (iter.hasNext()) {
final String sn = (String) iter.next();
stats[i++] = this.getStatistic(sn);
}
assert (stats.length == i);
return ( stats );
|
private boolean | implementsInterface(java.lang.Class c, java.lang.Object o)
boolean impls = false;
final Class[] interfaces = o.getClass().getInterfaces();
for (int i = 0 ; i < interfaces.length ; i++) {
if (interfaces[i].equals(c)){
impls = true;
break;
}
}
return ( impls );
|
private boolean | isStatsInterfaceMethod(java.lang.String name)
final Method[] methods = javax.management.j2ee.statistics.Stats.class.getMethods();
boolean isInterfaceMethod = false;
for (int i = 0 ; i < methods.length ; i++) {
if (methods[i].getName().equals(name)) {
isInterfaceMethod = true;
break;
}
}
return ( isInterfaceMethod );
|
private boolean | isValidGetter(java.lang.reflect.Method m)
final boolean startsWithGet = m.getName().startsWith("get");
final boolean hasNoParams = m.getParameterTypes().length == 0;
final boolean returnsStatistic = Statistic.class.isAssignableFrom(m.getReturnType());
return ( startsWithGet && hasNoParams && returnsStatistic );
|
private java.lang.String[] | methods2Statistics(java.lang.reflect.Method[] methods)
final String[] names = new String[methods.length];
for (int i = 0 ; i < methods.length ; i++) {
final String m = methods[i].getName();
final int s = "get".length();
names[i] = m.substring(s);
}
return ( names );
|
private void | populateGetterMap()
// Fix for Bugs 5045435, 6172088
//final Method[] apis = statsInterface.getDeclaredMethods(); //all of these should be PUBLIC.
final Method[] m = statsInterface.getMethods();
// exclude methods that belong to the javax.management.j2ee.Stats
final Method[] apis = filterStatsMethods(m);
final Method[] methods = getGetters(apis);
final String[] names = methods2Statistics(methods);
assert (names.length == methods.length) : ("Statistic names array is not having same length as that of array of getters");
int i;
for (i = 0 ; i < names.length ; i++) {
getters.put(names[i], methods[i]);
}
assert (getters.size() == i) : ("Getters map is incorrect, names.length = " + names.length + " methods.length = " + methods.length);
|