1 23 24 38 39 package com.sun.enterprise.admin.monitor.registry.spi; 40 41 import java.lang.reflect.Method ; 42 import java.util.Map ; 43 import java.util.Iterator ; 44 import java.util.HashMap ; 45 import java.util.logging.Logger ; 46 import java.util.ArrayList ; 47 import javax.management.MBeanAttributeInfo ; 48 49 import javax.management.j2ee.statistics.Stats ; 50 import javax.management.j2ee.statistics.Statistic ; 51 import javax.management.j2ee.statistics.BoundedRangeStatistic ; 52 import javax.management.j2ee.statistics.CountStatistic ; 53 import javax.management.j2ee.statistics.RangeStatistic ; 54 import javax.management.j2ee.statistics.TimeStatistic ; 55 56 import com.sun.enterprise.admin.monitor.registry.StatsHolder; 57 import com.sun.enterprise.admin.common.constant.AdminConstants; 58 import com.sun.enterprise.util.i18n.StringManager; 59 60 65 class StatsMediatorImpl implements StatsMediator { 66 67 private final Stats delegate; 68 private final Class metaData; 69 private final Map methodMap; 70 private final Map firstParts; 71 private final Map secondParts; 72 private final static Logger logger = Logger.getLogger(AdminConstants.kLoggerName); 73 private final static StringManager sm = StringManager.getManager(StatsMediatorImpl.class); 74 private final String DELIMITER = "-"; 75 private final String OLD_DELIMITER = "_"; 76 private Map opsMap; 78 private final StatsDescriptionHelper helper = new StatsDescriptionHelper(); 79 private final String DESCRIPTION_GETTER = "getDescription"; 80 private static final String DOTTED_NAME = "dotted-name"; 81 82 public StatsMediatorImpl(Stats delegate, Class metaData) { 83 86 this.delegate = delegate; 87 this.metaData = metaData; this.methodMap = new HashMap (); 89 this.firstParts = new HashMap (); 90 this.secondParts = new HashMap (); 91 reflectedAttributes(); 92 if (isJtaMetaData()) 94 reflectJTAOps(); 95 } 96 97 public Object getAttribute(String name) { 98 if (! methodMap.containsKey(name)) { 99 logger.finer("The name supplied may be an old-styled name, making one more attempt: " + name); 100 name = getHyphenedName(name); if (! methodMap.containsKey(name)) { 102 final String msg = sm.getString("smi.no_such_attribute", name); 103 throw new IllegalArgumentException (msg); 104 } 105 } 106 return ( invokeGetter(name) ); 107 } 108 109 public MBeanAttributeInfo [] getAttributeInfos() { 110 return ( attributes2Info() ); 111 112 } 113 114 public Object invoke(String method, Object [] params, String [] sign) { 115 logger.fine("Invoking Method: "+method); 119 Object result = null; 120 if(! opsMap.containsKey(method)) { 121 final String msg = sm.getString("smi.no_such_method", method); 122 throw new IllegalArgumentException (msg); 123 } 124 else 125 { 126 Method m = (Method )opsMap.get(method); 127 try { 128 result = m.invoke(this.delegate, params); 129 } catch(Exception e) { 130 logger.info(e.getMessage()); 131 } 132 } 133 return result; 134 } 135 136 private void reflectedAttributes() { 137 if (metaData == null) 138 return; 142 final Method [] methods = metaData.getMethods(); 143 for (int i = 0; i < methods.length ; i++) { 144 final String method = methods[i].getName(); 145 if (isStatsInterfaceMethod(method)) 146 continue; final int index = method.indexOf("get"); 148 if(index != -1) { 151 final String baseAttrName = method.substring(index + 3); 152 final String [] attrSubs = getAttributeSubs(methods[i]); 153 final Method actualMethod = getMethodFromDelegate(method); 154 addMapping(baseAttrName, attrSubs, actualMethod); 155 } 156 } 157 } 158 159 private void reflectJTAOps() { 160 opsMap = new HashMap (); 161 final Method [] methods = metaData.getMethods(); 162 for(int i = 0; i < methods.length ; i++) { 163 String methodName = methods[i].getName(); 164 if((StatsHolderMBeanImpl.JTA_FREEZE.equals(methodName)) || 165 (StatsHolderMBeanImpl.JTA_UNFREEZE.equals(methodName)) || 166 (StatsHolderMBeanImpl.JTA_ACTIVE_TRANSACTIONS.equals(methodName)) || 167 (StatsHolderMBeanImpl.JTA_ROLLBACK.equals(methodName))) { 168 169 opsMap.put(methodName, methods[i]); 170 } 171 } 172 } 173 174 178 private boolean isJtaMetaData() { 179 boolean jta = false; 180 if (metaData != null) { 181 if (com.sun.enterprise.admin.monitor.stats.JTAStats.class.getName().equals(metaData.getName())) 182 jta = true; 183 } 184 return ( jta ); 185 } 186 private boolean isStatsInterfaceMethod(String name) { 187 final Method [] methods = javax.management.j2ee.statistics.Stats .class.getMethods(); 188 boolean isInterfaceMethod = false; 189 for (int i = 0 ; i < methods.length ; i++) { 190 if (methods[i].getName().equals(name)) { 191 isInterfaceMethod = true; 192 break; 193 } 194 } 195 return ( isInterfaceMethod ); 196 } 197 private Method getMethodFromDelegate(String methodName) { 198 final Method [] instanceMethods = delegate.getClass().getMethods(); 199 Method m = null; 200 boolean matched = false; 201 for (int i = 0 ; i < instanceMethods.length ; i++) { 202 m = instanceMethods[i]; 203 if (methodName.equals(m.getName())) { 204 matched = true; 205 break; 206 } 207 } 208 assert (matched != false) : "The Stats object: " + delegate.getClass().getName() + " does not implement declared method: " + methodName; 209 return ( m ); 210 } 211 private String [] getAttributeSubs(Method m) { 212 final Class c = m.getReturnType(); 213 assert (javax.management.j2ee.statistics.Statistic .class.isAssignableFrom(c)) : "The method does not return a Statistic: " + m.getName(); 214 final Method [] rets = c.getMethods(); 216 final String [] subs = new String [rets.length]; 217 for (int i = 0 ; i < rets.length ; i++) { 218 final Method am = rets[i]; 219 final String name = am.getName(); 220 if (name.startsWith("get")) { 221 subs[i] = name.substring(3); logger.fine("return type = " + subs[i]); 223 } 224 } 225 return ( subs ); 226 } 227 private void addMapping(String first, String [] lasts, Method getter) { 228 for (int i = 0 ; i < lasts.length ; i++) { 229 String lc1 = null; 230 if (first != null) { 231 lc1 = first.toLowerCase(); 232 } 233 if ( lasts[i] != null) { 234 final String lc2 = lasts[i].toLowerCase(); 235 final String full = new StringBuffer (lc1).append(DELIMITER).append(lc2).toString(); 236 methodMap.put(full, getter); 237 firstParts.put(lc1, first); 238 secondParts.put(lc2, lasts[i]); 239 logger.finer("Method: " + getter.getName() + " added for full attribute: " + full); 240 } 241 } 242 } 243 private Object invokeGetter(String ab) { 244 String first = ab.substring(0, ab.indexOf(DELIMITER)); 246 first = (String )firstParts.get(first); 247 final String fName = "get" + first; 248 String last = ab.substring(ab.indexOf(DELIMITER) + 1); 249 last = (String )secondParts.get(last); 250 final String lName = "get" + last; 251 if(lName.equalsIgnoreCase(DESCRIPTION_GETTER)) 252 return ((Object )getDescription(first)); 253 254 Method lastMethod = null; 255 try { 256 final Method firstMethod = (Method )methodMap.get(ab); 257 final Object firstResult = firstMethod.invoke(delegate); 258 lastMethod = firstResult.getClass().getMethod(lName); 259 final Object value = lastMethod.invoke(firstResult); 260 logger.finer("Got value for: " + ab + " as: " + value + " class = " + value.getClass().getName()); 261 return ( value ); 262 } 263 catch(Exception e) { 264 logger.throwing(StatsMediatorImpl.class.getName(), "invokeGetter", e); 265 throw new RuntimeException (e); 266 } 267 } 268 269 private MBeanAttributeInfo [] attributes2Info() { 270 final Iterator it = methodMap.keySet().iterator(); 272 final ArrayList attrInfo = new ArrayList (); 273 274 int i = 0; 275 while (it.hasNext()) { 276 final String name = (String ) it.next(); 277 final String type = getType(name); 278 final String desc = getDescription(name); 279 final boolean isReadable = getReadable(name); 280 final boolean isWritable = false; final boolean isIs = false; 282 attrInfo.add(new MBeanAttributeInfo (name, type, desc, isReadable, isWritable, isIs)); 283 logger.finer("Added the attribute to MBeanAttributeInfo: " + name); 284 } 285 MBeanAttributeInfo dottedNameInfo = new MBeanAttributeInfo (StatsHolderMBeanImpl.DOTTED_NAME, 287 getType(DOTTED_NAME), 288 getDescription(DOTTED_NAME), 289 true, 290 false, 291 false); 292 attrInfo.add(dottedNameInfo); 293 final MBeanAttributeInfo [] ais = new MBeanAttributeInfo [attrInfo.size()]; 294 logger.finer("No of attrs = " + attrInfo.size()); 295 return (MBeanAttributeInfo [])attrInfo.toArray(ais); 296 } 297 298 private String getDescription(String name) { 299 return helper.getDescription(name); 300 } 301 302 private String getType(String name) { 303 return ( "java.lang.String" ); } 305 306 private boolean getReadable(String name) { 307 return ( true ); } 309 310 317 private String getHyphenedName(final String name) { 318 return ( name.toLowerCase().replace(OLD_DELIMITER.charAt(0), DELIMITER.charAt(0)) ); 319 } 320 } 321 | Popular Tags |